<?php
	if (isset($_GET['id'])) header("Location: view_campains.ag.php#".$_GET['id']);
include("inc.top.php");
$q=new Cdb;
$q2=new Cdb;

	$t->set_file("content", "view_campains.ag.html");
	$t->set_file("campaign_row_file", "view_campains_row.html");
	$t->set_file("stats_row", "statistics_row.html");
	
	get_logged_info();
	$member_id=$q->f("id");
	$order_sql = " ORDER BY id DESC";
	if ($_GET['name'] == "up"){
		$order_sql = " ORDER BY name DESC";
	}
	
	if ($_GET['name'] == "down"){
		$order_sql = " ORDER BY name ASC";
	}
	
	if ($_GET['n_friends'] == "down"){
		$order_sql = " ORDER BY number_of_friends ASC";
	}
	
	if ($_GET['n_friends'] == "up"){
		$order_sql = " ORDER BY number_of_friends DESC";
	}
	
	$q->query("SELECT * FROM campaigns WHERE member_id='".$member_id."'".$order_sql);

	while ($q->next_record()){
		$t->set_var("id",$q->f("id"));
		$t->set_var("name_campaign",stripslashes($q->f("name")));
		$t->set_var("nr_friends",$q->f("number_of_friends"));
		$t->set_var("nr_friends_oblig",$q->f("mandatory_number_of_friends"));
		$t->set_var("down_url","download_taf.php?id=".$q->f("id"));
		$t->set_var("details","camp_".$q->f("id"));
		$t->set_var("color_bg_main",$q->f("color_bg_main") ? $q->f("color_bg_main") : "FFFFFF");
		$t->set_var("color_bg_table",$q->f("color_bg_table") ? $q->f("color_bg_table") : "FFFFFF");
		$t->set_var("color_border_table",$q->f("color_border_table") ? $q->f("color_border_table") : "FFFFFF");
		$t->set_var("color_font",$q->f("font_color") ? $q->f("font_color") : "000000");
		$font_select = '';
		$fonts_arr = array('Times New Roman','Arial','Verdana','Tahoma','Courier New','Georgia');		
		foreach ($fonts_arr as $key=>$value){
			if ($q->f("font") == $value){
				$font_select = $value;
				break;
			}
		}

		$t->set_var($font_select ? $font_select : 'Arial',"selected");
		$t->set_var("size_".($q->f("size") ? $q->f("size") : '10'),"selected");
		$t->set_var("style_".($q->f("style") ? $q->f("style") : '0'),"selected");
		$t->set_var("border_".($q->f("border_table") ? $q->f("border_table") : '0'),"selected");
		
		$t->set_var("stats",($q->f('enable_link_tracking') != 0 ? '' : '<tr><td colspan="8" align="center"><b  style="font-size:14px; color:#FF0000"><br>Stats turned off<br><br></b></td></tr>'));
		
		$total_mails = 0;
		$total_joined = 0;
		$total_unsub = 0;
		$total_click = 0;

		$stats = '';
		$q2->query("SELECT COUNT(a.id) as total, SUM(a.mails_sent) AS total_mails, SUM(a.nr_joined) AS total_joined, a.camp_id, SUM(IF(a.date_click='0000-00-00 00:00:00',0,1)) AS total_click, SUM(a.not_mail) AS total_unsub FROM track a, track c WHERE a.camp_id='".$q->f("id")."'  AND (a.aff=c.id OR a.aff=0 )AND a.id!=1 GROUP BY a.camp_id");
		if (!$q2->num_rows() && $q->f('enable_link_tracking')!='0')
				$t->set_var("stats",'<tr><td colspan="8" align="center"><b  style="font-size:14px; color:#FF0000"><br>No tracking Available Yet<br><br></b></td></tr>');
		$q2->next_record();
/*		while ($q2->next_record()){

			if ($q2->f("mails_sent"))
				$total_mails += $q2->f("mails_sent");
			if ($q2->f("nr_joined"))
				$total_joined += $q2->f("nr_joined");
			if ($q2->f("not_mail"))	
				$total_unsub++;
			if ($q2->f("date_click") != "0000-00-00 00:00:00")	
				$total_click++;
			$t->set_var("name",$q2->f("name"));
			$t->set_var("email",$q2->f("email"));
			$t->set_var("mails_sent",$q2->f("mails_sent"));
			$t->set_var("nr_joined",$q2->f("nr_joined"));
			$t->set_var("date_click",($q2->f("date_click") == "0000-00-00 00:00:00" ? "Not Clicked" : $q2->f("date_click")));
			$t->set_var("ip",$q2->f("ip"));
			$t->set_var("not_mail",($q2->f("not_mail") ? "Yes" : "No"));
			$t->set_var("aff",($q2->f("aff_name") != '' ? $q2->f("aff_name") : "None"));
			$stats .= $t->parse("statistics", "stats_row", true);
		}
*/		$t->set_var("url","'".get_setting('site_full_url')."'");
		$t->set_var("total",$q2->f('total'));
		$t->set_var("total_mails",$q2->f("total_mails"));
		$t->set_var("total_joined",$q2->f("total_joined"));
		$t->set_var("total_unsub",$q2->f("total_unsub"));
		$t->set_var("total_click",$q2->f("total_click"));
//		$t->set_var("statistics", $stats);
		$t->parse("fields_row", "campaign_row_file", true);
		$t->unset_var("border_".($q->f("border_table") ? $q->f("border_table") : '0'));
		$t->unset_var("size_".($q->f("size") ? $q->f("size") : '0'));
		$t->unset_var("style_".($q->f("style") ? $q->f("style") : '0'));
		$t->unset_var($font_select ? $font_select : 'Arial');
	
	}
			$t->set_file("menu_html", "member.area.menu.html");
			
			$t->parse("menu", "menu_html");

	$t->pparse("out", "content");	
	
//include("inc.bottom.php");
?>