<?php
include("../config.php3");
include($BIB."bib_generic.php3");

if(!isset($tableEmail))
	$tableEmail = "Email";

if(!isset($chpEmail))
	$chpEmail = "Email";
?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Stats Domaines Tracking</div>
<br>
<form>
1. Choisir le TAG tracking : 
<?php
$query =  "SELECT Tracking.*,Tracking_Type.CodeType FROM Tracking LEFT JOIN Tracking_Type ON Tracking.TypeId = Tracking_Type.Id ORDER BY TypeId,Id ASC";
$rows = main_db_query($query);


echo "<select name=TrackId>";
while($row = db_fetch_assoc($rows))
{
	$SELECTED = "";
	if($TrackId == $row['Id'])
		$SELECTED = "SELECTED";
		
	echo "<option value='".$row['Id']."' ".$SELECTED.">".$row['CodeType']."_".$row['Id']."-".$row['Lib']."</option>";
}
echo "</select>";
?>
<br>2. Choisir la table contenant les emails : <input name="tableEmail" value="<?php echo $tableEmail;?>"> (Email ou Members)
<br>3. Choisir le nom du champ email : <input name="chpEmail" value="<?php echo $chpEmail;?>">
<br><input type="submit" name="afficher" value="afficher">
<form>

<hr>


<?php
if($afficher)
{
	echo "<table><tr><td valign=top>";
//--------------STATS DU TRACKING-------------------	
		$nbdomaine = 0;
		$totalemail = 0;
		$TABDOMAINE = array();
		$TABEMAIL = array();
		$rows = main_db_query("SELECT Email,TrackId FROM Tracking_Email WHERE TrackId=".$TrackId." ORDER BY TrackId ASC");
		while($row = db_fetch_array($rows))
		{
			$parser = explode("@",$row["Email"]);
			$domaine = strtolower(trim($parser[1]));
			
			if(strlen($domaine)>0)
			{
				if( $TABDOMAINE[$domaine] <= 0)		
				{	
					$TABDOMAINE[$domaine] = 1;	
					$nbdomaine++;
				}
				else
				{
					$TABDOMAINE[$domaine]++;
				}
				$totalemail++;
			}
		}
		echo "<br><b>Pour ce tracking :</b> ".$nbdomaine." domaines trouvés<br><br>";
		
		arsort($TABDOMAINE);
		?>

		<table border=1>
			<tr class=trTitle>
				<td>Domaine</td>
				<td>Nb</td>
				<td>%</td>
			</tr>
		<?php
		foreach($TABDOMAINE AS $domaine =>$total)
		{
			echo "<tr><td>".$domaine."</td><td> ".$total."</td><td> ".intval($total/$totalemail*100)." %</td></tr>";
		}
		echo "</table>";


	echo "</td><td width=50px></td><td valign=top>";
	
//--------------STATS DE LA TABLE DES EMAIL-------------------
		$nbdomaine = 0;
		$totalemail = 0;
		$TABDOMAINE = array();
		$TABEMAIL = array();
		$rows = main_db_query("SELECT ".$chpEmail." AS Email FROM ".$tableEmail);
		while($row = db_fetch_array($rows))
		{
			$parser = explode("@",$row["Email"]);
			$domaine = strtolower(trim($parser[1]));
			
			if(strlen($domaine)>0)
			{
				if( $TABDOMAINE[$domaine] <= 0)		
				{	
					$TABDOMAINE[$domaine] = 1;	
					$nbdomaine++;
				}
				else
				{
					$TABDOMAINE[$domaine]++;
				}
				$totalemail++;
			}
		}
		
		echo "<br><b>Sur la base d'emails :</b> ".$nbdomaine." domaines trouvés<br><br>";

		arsort($TABDOMAINE);

		?>

		<table border=1>
			<tr class=trTitle>
				<td>Domaine</td>
				<td>Nb</td>
				<td>%</td>
			</tr>
		<?php
		foreach($TABDOMAINE AS $domaine =>$total)
		{
			echo "<tr><td>".$domaine."</td><td> ".$total."</td><td> ".intval($total/$totalemail*100)." %</td></tr>";
		}
		echo "</table>";



	echo "<table><tr><td>";

}
?>

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