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

$ID_TYPE_TRACKING_NUAGES = 2;




function write_nuage_var($rows_nuage)
{
	global $ARCHIVES_DIR,$CHOWN_CMD,$CHMOD_CMD;
	
	
	
	$file_text = "<?php\n";
	
	$avancement = 0;
	while($row = db_fetch_assoc($rows_nuage))
	{
		
		$file_text .= "$"."TABNUAGE[".$avancement."]=array(lib=>'".$row[Lib]."',url=>'http://".$row[Url]."',code=>'".$row['CodeType']."_".$row['Id']."',clic=>".$row[Nb].");\n";

		$avancement++;
		
	}
	
	$file_text .= "?> ";
	
	$filename = "nuage_var.inc";
	$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 web:web ".$ARCHIVES_DIR);
	//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, "");
}






?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Nouvelle Campagne Nuage</div>
<br>

<table border=1>
	<tr class=trTitle>
		<td>Id</td>
		<td>Code</td>
		<td>Libellé</td>
		<td>URL</td>		
		<td>Total clicks</td>
	</tr>
<?php
switch($submit)
{
	case "Valider":
	
		if ($Id==0)
		{
			# on insere une nvo
			main_db_query("INSERT INTO Tracking (Id,TypeId, Lib) VALUES ('".$Id."','".$TypeId."', '".$Lib."')");
			$TrackingId = db_insert_id();
			main_db_query("INSERT INTO Tracking_Nuages (Id,TrackingId , Url) VALUES ('".$Id."','".$TrackingId."', '".$Url."')");
		}
		else
		{
			# on update si c'est une modif
			main_db_query("UPDATE Tracking SET TypeId='".$TypeId."' ,Lib='".$Lib."', Total = '".$Total."' WHERE Id='".$Id."'");
			main_db_query("UPDATE Tracking_Nuages SET TrackingId='".$Id."' ,Url='".$Url."' WHERE Id='".$Tracking_Nuages_Id."'");
		}
		$Id=0;
		
		break;
		
	case "Supprimer":
		main_db_query("DELETE FROM Tracking WHERE Id='".$Id."'");
		main_db_query("DELETE FROM Tracking_Nuages WHERE Id='".$Tracking_Nuages_Id."'");
		$Id=0;
		$TypeId  = "";
		$Tracking_Nuages_Id = "";
		$Nb  = "";
		$Lib  = "";
		$Total  = "";
		
		break;
	
	case "Infos":
		$info = db_fetch_array(main_db_query("SELECT Tracking_Nuages.Id AS Tracking_Nuages_Id, Tracking_Nuages.Url, Tracking.*,Tracking_Type.CodeType FROM Tracking_Nuages LEFT JOIN Tracking ON Tracking_Nuages.TrackingId = Tracking.Id LEFT JOIN Tracking_Type ON Tracking.TypeId = Tracking_Type.Id WHERE Tracking.Id='".$Id."'"));
		$Id = $info[Id];
		$TypeId = $info[TypeId];
		$Tracking_Nuages_Id = $info[Tracking_Nuages_Id];
		$Url = $info[Url];
		$Nb = $info[Nb];
		$Lib = $info[Lib];
		$Total = $info[Total];

		break;
	
	
	case "Regenerer":
		$rows_nuage = main_db_query("SELECT Tracking_Nuages.Id AS Tracking_Nuages_Id, Tracking_Nuages.Url, Tracking.*,Tracking_Type.CodeType FROM Tracking_Nuages LEFT JOIN Tracking ON Tracking_Nuages.TrackingId = Tracking.Id LEFT JOIN Tracking_Type ON Tracking.TypeId = Tracking_Type.Id WHERE Tracking_Type.Id=".$ID_TYPE_TRACKING_NUAGES." ORDER BY CodeType ASC");
		write_nuage_var($rows_nuage);

		break;
	
}




$rows = main_db_query("SELECT Tracking_Nuages.Id AS Tracking_Nuages_Id, Tracking_Nuages.Url, Tracking.*,Tracking_Type.CodeType FROM Tracking_Nuages LEFT JOIN Tracking ON Tracking_Nuages.TrackingId = Tracking.Id LEFT JOIN Tracking_Type ON Tracking.TypeId = Tracking_Type.Id WHERE Tracking_Type.Id=".$ID_TYPE_TRACKING_NUAGES." ORDER BY CodeType ASC");

while($row = db_fetch_array($rows))
{
	echo "<tr><td><a href='?Id=".$row['Id']."&submit=Infos'>".$row['Id']."</a></td>";
	echo "<td><a href='?Id=".$row['Id']."&submit=Infos'>".$row['CodeType']."_".$row['Id']."</a></td>";
	echo "<td>".$row['Lib']."</td>";
	echo "<td>".$row['Url']."</td>";
	echo "<td align=right>".$row['Nb']."</td>";
	

}

?>
</table>
<?php
if(isset($Tag))
{
	echo "<br><b><u>Voici le lien de la promo ".$PromoTag." :</u></b><br>";
	echo $Tag;
}
?>
<form>
<table>
	<tr>
		<td>
			Id (vide ou egal 0 pour ajout):
		</td>
		<td>
			<input type="text" size=10 name=Id value="<?php echo $Id ?>">
			<input type="hidden" name="TypeId" value="<?php echo $ID_TYPE_TRACKING_NUAGES ?>">		
			<input type="hidden" name="Tracking_Nuages_Id" value="<?php echo $Tracking_Nuages_Id ?>">						
		</td>
	</tr>
	<tr>
		<td>
			Libellé :
		</td>
		<td>
			<input type="text" size=30 name=Lib value="<?php echo $Lib ?>">
		</td>
	</tr>
	<tr>
		<td>
			URL de promo :
		</td>
		<td>
			http://<input type="text" size=30 name=Url value="<?php echo $Url ?>">
		</td>
	</tr>
</table>
<br>
    <input type=submit name=submit value="Infos">&nbsp;&nbsp;&nbsp;
    <input type=submit name=submit value="Valider">&nbsp;&nbsp;&nbsp;
	<input type=submit name=submit value="Supprimer"><br>
	</form>
<a class=under href="<?php echo $PHP_SELF ?>">Rafraichir la page</a>


<hr>
<form>
<input type=submit name=submit value="Regenerer"><br>
</form>


