<?php 
require_once 'conf/link.php';
$lst_assoc = 'Lst_prueba_01';
$player_name = 'Player_01';

$media = get_plan_json($lst_assoc, $player_name, $conexion);
gen_xml($media, $player_name, $lst_assoc);
	
function get_plan_json($lst_assoc, $player_name, $conexion)
{
	
	$almacen = "almacen/";
	
	
	// Buscamos los videos que tiene la lista asociada	
	$sql_lista = 'SELECT videos_id FROM listas WHERE nombre_lista = "'.$lst_assoc.'"';
	$result_lista = mysqli_query($conexion, $sql_lista);
	
	while($row_lista = mysqli_fetch_assoc($result_lista))
	{
		$media_aux = $row_lista['videos_id'];
	}
	//echo "<br><br>Media Aux : ";
	//print_r($media_aux);

	$media_aux_02 = json_decode($media_aux,true);
	//echo "<br><br>Media Aux_02 : ";
	//print_r($media_aux_02);
	
	//echo "<br><br>Media Aux_02 []: ";
	//echo "<br>".$media_aux_02[0]['filename'];
	//echo "<br>".$media_aux_02[1]['filename'];
	//echo "<br>".$media_aux_02[2]['filename'];
	//echo $str_aux;
	//$row_lista = mysqli_fetch_assoc($result_lista);
	//echo "<br>";
	//print_r($row_lista['videos_id']);
	//$media_aux = json_decode($row_lista['videos_id']);
	
	//echo "<br>";
	//$media = json_decode($str_aux, true);
	//print_r($media_aux);
	//return $media; // JSON con la lista de archivos de la playlist

	// $media : contiene los nombres de archivo que hay que copiar en los players
	/////////////////////////////////
	//echo "<br>xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
	//echo "<br>".$player_name;
	//echo "<br>";
	$sql_player = 'SELECT * FROM players WHERE player_name = "'.$player_name.'"';
	$result_player = mysqli_query($conexion, $sql_player);
	//while($row_player = mysqli_fetch_array($result_player))
	$row_player = mysqli_fetch_assoc($result_player);
	//echo "<br><br>row_player";
	$player_info = $row_player;
	

	/*
	echo "<br><br>";
	foreach($row_player as $id=>$key)
	{
		//echo "<br>--".$id."--".$key;
		$player_info[$id] = $key;
	}
	//echo $str_aux;
	//echo "<br>";
	//$media = json_decode($str_aux, true);
	*/
	//echo "<br><br>player_info : ";
	
	//print_r($player_info);

	//return $media; // JSON con la lista de archivos de la playlist

	// $media : contiene los nombres de archivo que hay que copiar en los players
	/////////////////////////////////
	//Fecha, player_name, lst_assoc, res_x, res_y, x0, xy, 
	//hour_start, min_start, hour_end, min_end, content
	//echo "<br><br>xxxxxxxxxxxxxxxxxxxxx";
	//echo "XML GEN<br>";
	$array_data_player['date'] = date('Y-m-d H:i:s');
	$array_data_player['player_name'] =  $player_info['player_name'];
	$array_data_player['lst_assoc'] = $player_info['lst_assoc'];
	$array_data_player['res_x'] =  $player_info['res_x'];
	$array_data_player['res_y'] =  $player_info['res_y'];
	$array_data_player['x0'] =  $player_info['x0'];
	$array_data_player['y0'] =  $player_info['y0'];
	$array_data_player['hour_start'] =  $player_info['hour_start'];
	$array_data_player['min_start'] =  $player_info['min_start'];
	$array_data_player['hour_end'] =  $player_info['hour_end'];
	$array_data_player['min_end'] =  $player_info['min_end'];
	
	$item = 0;
	foreach($media_aux_02 as $key=>$value)
	{
		$array_data_player['content']['filename']['item'.$item.''] =  $value['filename'];
		$item++;
	}
	//echo "<br><br>";
	//print_r($array_data_player);
	//echo "<br><br>data player : ";
	$data_player = json_encode($array_data_player);
	//echo $data_player;
	return $array_data_player;
	
	
	

}
/*
function get_playlist_json($lst_assoc, $conexion)
{
	
	$almacen = "almacen/";
	
	
	// Buscamos los videos que tiene la lista asociada	
	$sql_lista = 'SELECT videos_id FROM listas WHERE nombre_lista = "'.$lst_assoc.'"';
	$result_lista = mysqli_query($conexion, $sql_lista);
	while($row_lista = mysqli_fetch_array($result_lista))
	{
		$str_aux = $row_lista['videos_id'];
	}
	//echo $str_aux;
	//echo "<br>";
	$media = json_decode($str_aux, true);
	//print_r($media);
	return $media; // JSON con la lista de archivos de la playlist

	// $media : contiene los nombres de archivo que hay que copiar en los players
	/////////////////////////////////

}
*/
//function defination to convert array to xml
function array_to_xml($array, &$xml_user_info)
{

    foreach($array as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml_user_info->addChild("$key");
                array_to_xml($value, $subnode);
            }else{
                $subnode = $xml_user_info->addChild("item$key");
                array_to_xml($value, $subnode);
            }
        }else {
            $xml_user_info->addChild("$key",htmlspecialchars("$value"));
        }
    }
}

function gen_xml($media, $player_name, $lst_assoc)
{

	//creating object of SimpleXMLElement
	$xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><list_info_".$lst_assoc."></list_info_".$lst_assoc.">");

	//function call to convert array to xml
	array_to_xml($media,$xml_user_info);

	//saving generated xml file
	$xml_file = $xml_user_info->asXML('Players/'.$player_name.'/Content/plan.xml');

	//success and error message based on xml creation
	if($xml_file){
		return true;//echo 'XML file have been generated successfully.';
	}else{
		return false;//echo 'XML file generation error.';
	}
}


function otra()
{	
	
	// Buscamos los players que usan la lista asociada
	$sql_players = 'SELECT player_name FROM players WHERE lst_assoc = "'.$lst_assoc.'"';
	
    if ($result_players = mysqli_query($conexion, $sql_players))
	{
		while($row = mysqli_fetch_array($result_players))
		{
			$player_name = $row['player_name'];
			$path = "Players/".$player_name."/";
			echo "<br>".$path;//$path = "Players/".$player_name."/";
			
			//Listamos y borramos archivos antiguos
			$files = glob('Players/'.$player_name.'/*');//obtenemos todos los nombres de los ficheros
			/*
			echo "<br>Files<br>";
			print_r($files);
			echo "<br>Files<br>";
			*/
			foreach($files as $file)
			{
				if(is_file($file))
				unlink($file); //elimino el fichero
			}
			
			$error_copy = false;
			foreach($media as $value)
			{
				echo "<br>".$value['filename'];
				if(copy($almacen.$value['filename'], $path.$value['filename']))
				{
					echo "<br>".$almacen.$value['filename']."==>".$path.$value['filename']." ==>Copiado";
				}
				else
				{
					$error_copy = true;
					echo "<br>".$almacen.$value['filename']."==>".$path.$value['filename']." ==>Error, no se ppudo copiar.";
				}
			}
		}
    }
	else
	{
        echo 'Error al consultar lista de Players : SQL';
		
    }
}
