<?php
include("../config.php3");
include($BIB."bib_generic.php3");
?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Stats Campagne Tracking</div>
<br>
<?php
if($submit=="ShowEmail")
{
	$VIEWID = $Id;
}


?>
<table border=1>
	<tr class=trTitle>
		<td>Id</td>
		<td>Code</td>
		<td>Libellé</td>		
		<td>Total clicks</td>		
		<td>Total Affichés</td>		
		<td>Total emails</td>
		<td>Transformation</td>
		<td>Détails</td>
	</tr>
<?php


$rows = main_db_query("SELECT Tracking.*,Tracking_Type.CodeType FROM Tracking LEFT JOIN Tracking_Type ON Tracking.TypeId = Tracking_Type.Id ORDER BY TypeId,Id ASC");

while($row = db_fetch_array($rows))
{
	$count = db_fetch_assoc(main_db_query("SELECT COUNT(*) AS NbEmail FROM Tracking_Email WHERE TrackId=".$row['Id']));
	
	echo "<tr><td><a name=".$row['Id']."><a href='?Id=".$row['Id']."&submit=Infos'>".$row['Id']."</a></td>";
	echo "<td>".$row['CodeType']."_".$row['Id']."</td>";
	echo "<td>".$row['Lib']."</td>";
	echo "<td align=right>".$row['Nb']."</td>";
	echo "<td align=right>".$row['Total']."</td>";
	echo "<td align=right>".$count['NbEmail']."</td>";
	if ($count['NbEmail'] > 1 && $row['Total'])
		echo "<td align=right>".intval($count['NbEmail'] * 100 / $row['Total'])."%</td>";
	else if ($row['Nb'] > 0 && $row['Total'] > 0)
		echo "<td align=right>".intval($row['Nb'] * 100 / $row['Total'])."%</td>";
	else 
		echo "<td align=right>-</td>";
		
	
	if($VIEWID == $row['Id'])
	{
		echo "<td align=center><a href='?Id=0&submit=0#".$row['Id']."' style='color:#FF5555'>Réduire</a></td>";
	}
	else
	{
		echo "<td align=center><a href='?Id=".$row['Id']."&submit=ShowEmail#".$row['Id']."'  style='color:#55FF55'>Voir</a></td>";
	}
	echo "</tr>";
	
	//DETAILS DES @EMAILS
	if($VIEWID == $row['Id'])
	{
		echo "<tr>";
		echo "<td colspan=4 align=center valign=top>";
			//ZONE D'EXTRACTION EMAIL
			if($action == "Extract")
			{
				$NewFile = "email_id_".$row['Id']."-".date("Y-m-d_his").".csv";
				$extract_rows = main_db_query("SELECT Email  FROM Tracking_Email WHERE TrackId=".$row['Id']);
				
				$fd = fopen( $ARCHIVES_DIR.$NewFile, "w");
				while($extract_row = db_fetch_array($extract_rows ))
				{
					fwrite($fd, trim($extract_row["Email"]).";\n");
				}
				
				echo "<br>Cliquer avec le bouton DROIT de la souris sur le lien ci-dessous.<br>Sélectionner \"Enregistrer la cible sous...\"<br><br>";
				echo "<a href=\"".$SYNCHRO_URL."Adm/exploitation/".$NewFile."\" style='color:#55FF55'><b>Cliquer ici (avec le bouton DROIT) pour récupérer le fichier</b></a>";
			}
			else
			{
				echo ">> <a href='?Id=".$row['Id']."&submit=ShowEmail&action=Extract#".$row['Id']."' style='color:#55FF55'>Cliquez ici pour extraire les emails</a> <<";
			}
		echo "</td>";
		
		echo "<td colspan=4 align=right>";
			//ZONE AFFICHAGE EMAIL
			echo "<table border=1 width=100%><tr class=trTitle><td>Email</td><td>Nb Clics</td></tr>";
			
			$email_rows =main_db_query("SELECT *  FROM Tracking_Email WHERE TrackId=".$row['Id']);
			
			while($email_row = db_fetch_assoc($email_rows))
			{
				echo "<tr><td>".$email_row['Email']."</td><td>".$email_row['Nb']."</td></tr>";
			}
			echo "</table>";
			
		echo "</td></tr>";
	}
}

?>
</table>

<a class=under href="<?php echo $PHP_SELF ?>">Rafraichir la page</a>