<?php
###########################
# met à jour un fichier dans le répertoire d'archive
# IN : un nom de fichier ($filename)
###########################

include("../config.php3");
include($BIB."bib_generic.php3");
include($ROOT_DIR."VIRALGAMES_Adm/Bib/bib_synchro.php");

$NB_TAG = 10;


function get_file_Id($filename)
{
	$query_select = "SELECT * FROM Affiliation_files WHERE File = '".$filename."'";
	$row_file = db_fetch_assoc(main_db_query($query_select));
	return $row_file["Id"];
}

function get_file($fileId)
{
	$query_select = "SELECT * FROM Affiliation_files WHERE Id = '".$fileId."'";
	$row_file = db_fetch_assoc(main_db_query($query_select));
	return $row_file["File"];
}


function get_file_tags($filename)
{
	$query_get_tag = "SELECT Affiliation_tags.* FROM Affiliation_tags LEFT JOIN Affiliation_files ON Affiliation_tags.FileId = Affiliation_files.Id WHERE File='".$filename."' ORDER BY Ponderation DESC";	
	$rows_file = main_db_query($query_get_tag);	
	return $rows_file;
}

function get_file_total_ponderation($filename)
{
	$query_get_tag = "SELECT SUM(Affiliation_tags.Ponderation) AS Total FROM Affiliation_tags LEFT JOIN Affiliation_files ON Affiliation_tags.FileId = Affiliation_files.Id WHERE File='".$filename."'";	
	$row_ponderation = db_fetch_assoc(main_db_query($query_get_tag));	
	return $row_ponderation["Total"];
}


function add_new_file($filename)
{
	$query_insert = "INSERT INTO Affiliation_files(File) VALUES ('".$filename."')";			
	main_db_query($query_insert);
	//On retourne son Id
	return get_file_Id($filename);
}

function del_file_tags($fileId)
{
	$query_delete_tag = "DELETE FROM Affiliation_tags WHERE FileId=".$fileId;			
	main_db_query($query_delete_tag);	
	return 1;
}

function add_file_tags($fileId,$Titre,$Tag,$Ponderation)
{
	$query_insert_tag = "INSERT INTO Affiliation_tags(FileId,Titre,Tag,Ponderation) VALUES ('".$fileId."','".$Titre."','".$Tag."','".$Ponderation."')";		
	$row_file = main_db_query($query_insert_tag,false,"",false);	
	return 1;
}


function write_file_tags($fileId)
{
	global $ARCHIVES_DIR,$CHMOD_CMD,$CHOWN_CMD;
	
	$filename = get_file($fileId);
	$mes_tags = get_file_tags($filename);
	$TotalPonderation = get_file_total_ponderation($filename);
	
	if($TotalPonderation < 1)
		$TotalPonderation = 1;
	
	$file_text = "<?php\n".
				"srand ((double) microtime() * 1000000);\n".
				"$"."randval = rand(1,".$TotalPonderation.");\n\n";
	
	$avancement = 1;
	while($mon_tag = db_fetch_assoc($mes_tags))
	{
		if($avancement == 1)
			$file_text .= "if ($"."randval >= ".$avancement." &&  $"."randval < ".($avancement+$mon_tag["Ponderation"]).")\n{\n?>\n";
		else
			$file_text .= "elseif ($"."randval >= ".$avancement." &&  $"."randval < ".($avancement+$mon_tag["Ponderation"]).")\n{\n?>\n";
		
		$file_text .= $mon_tag["Tag"]."\n";
		
		$file_text .= "<?php \n}\n";
		
		$avancement +=$mon_tag["Ponderation"];
	}
	
	$file_text .= "?> ";
	
	
	//on remet le droit d'ecriture sur le fichier
	cmd($CHMOD_CMD." 777 ".$ARCHIVES_DIR.$filename);	
	
	$fd = fopen($ARCHIVES_DIR.$filename,'w');
	$file_text = stripslashes($file_text);			
	fwrite($fd,$file_text);
	fclose($fd);

	# on change le propriétaire du dossier, il devient web pour pouvoir être répliqué sans pbs
	cmd($CHOWN_CMD." -R webb:webb ".$ARCHIVES_DIR.$filename);
	//on remet le droit d'ecriture sur le fichier
	cmd($CHMOD_CMD." 777 ".$ARCHIVES_DIR.$filename);	
	# le nouveau dossier généré doit être répliqué
	shareFileToAll($ARCHIVES_DIR,"" );
}




###########################
# CONSTANTS
###########################
$aFile = $ARCHIVES_DIR.$filename;

//echo $aFile
?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Mettre à jour <?php echo $filename ?></div>
<br><br>
<font color=red><?php echo $filename ?></font>
<?php

switch($submit)
{
	case "Valider":
		
		$fileId = get_file_Id($filename);
		
		//Si il n'existe pas encore en base
		if($fileId <= 0)
		{				
			$fileId = add_new_file($filename);			
		}

		del_file_tags($fileId);
			
		for($i=0;$i < $NB_TAG;$i++)
		{
			
			$Ponderation = "Ponderation_".$i;
			$Tag = "Tag_".$i;
			$Titre = "Titre".$i;
			//si on a pas de ponderation on la met à zéro
			if(!$$Ponderation)
			{
				$$Ponderation = 0;
			}
			if($$Tag)
				add_file_tags($fileId,$$Titre,$$Tag,$$Ponderation);
			
		}
		
		write_file_tags($fileId);
		
		echo "<br><br><font color=red><b>Le fichier ".$filename." a été généré</b></font>";
		break;
	
	case "Supprimer":
		if (file_exists($aFile))
		{
			unlink($aFile);
			echo "<font color=red><b>Le fichier ".$filename." a été supprimé</b></font>";
		}
}
?>
<form method="post" ENCTYPE="multipart/form-data">
<table border=0>
<?php
$rows_file_tag = get_file_tags($filename);
for($i=0;$i < $NB_TAG;$i++)
{
	$file_tag = db_fetch_assoc($rows_file_tag);
	
	
	echo "<tr>\n";
	echo "<td><b>Titre ".($i+1).":</b></td>\n";
	echo "<td rowspan=6 valign=top>Prévisualisation:<br>".$file_tag["Tag"]."</td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "<td valign=top align=left><input type=\"text\" name=\"Titre".$i."\" size=\"50\" value=\"".$file_tag["Titre"]."\"></td>\n";
	
	echo "</tr>\n";
	
	echo "<tr>\n";
	echo "<td><b>Ponderation ".($i+1).":</b></td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "<td valign=top align=left><input type=\"text\" name=\"Ponderation_".$i."\" size=\"10\" value=\"".$file_tag["Ponderation"]."\"></td>\n";
	echo "</tr>\n";
	
	echo "<tr>\n";
	echo "<td><b>TAG ".($i+1).":</b></td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "<td valign=top><textarea name=\"Tag_".$i."\" cols=\"60\" rows=15>".$file_tag["Tag"]."</textarea></td>\n";
	echo "</tr>\n";



	
	echo "<td colspan=2><br><hr><br></td>\n";
	echo "</tr>\n";

}
?>
</table>
<input type=submit name="submit" value="Valider">
&nbsp;&nbsp;&nbsp;<input type=submit name="submit" value="Supprimer">

</form>


<div align=center class=trTitle>Aperçu</div>
<?php 
if(file_exists($aFile))
	include($aFile);
else
	echo "<b>Fichier non existant</b>";
?>

</body>
</html>
