function stats()
 {
     $this->output->admin();
     $f = $this->input->f;
     $tpl = new XTemplate("templates/pub_user.html");
     if (!$f[page]) {
         $f[page] = 1;
     }
     if (!$f[date]) {
         $f[date] = "thismonth";
     }
     if (!$f[sort]) {
         $f[sort] = "date DESC,ip";
     }
     list($startdate, $enddate) = lib_date_range($f['date']);
     // Grab the data for that page
     $limit = 100;
     $offset = ($f[page] - 1) * $limit;
     // Count records in set first
     $sdate = strtotime($startdate);
     $edate = strtotime($enddate) + 86400;
     $recs = $this->db->getsql("SELECT count(*) as num, sum(amount) as amount FROM adrev_aff_traffic \r\n\t\t\t\t\t\t\t\t\tWHERE affid=? AND date BETWEEN ? AND ?", array($f['id'], $sdate, $edate));
     $z = $recs[0][num];
     $amount = $recs[0][amount];
     $pages = ceil($z / $limit);
     $prevpage = $f[page] - 1;
     $nextpage = $f[page] + 1;
     if ($f[page] > 1) {
         $pager .= "<a href=?section=pubadmin&action=stats&f[id]={$f['id']}&f[page]={$prevpage}&f[date]={$f['date']}&f[sort]={$f['sort']}&f[ip]={$f['ip']}>&laquo;Previous</a>&nbsp;";
     }
     $pager .= "<b>" . number_format($z) . "</b> records. Page <font color=red>{$f['page']}</font> of <b>{$pages}</b> pages. ";
     if ($pages > $f[page]) {
         $pager .= "&nbsp;<a href=?section=pubadmin&action=stats&f[id]={$f['id']}&f[page]={$nextpage}&f[date]={$f['date']}&f[sort]={$f['sort']}&f[ip]={$f['ip']}>Next&raquo;</a>";
     }
     // Show up to 20 page selector
     $pagelist = "";
     for ($x = 1; $x <= $pages; $x++) {
         if ($x == $f[page]) {
             $pagelist .= "&nbsp;<b>{$x}</b>";
         } else {
             $pagelist .= "&nbsp;<a href=?section=pubadmin&action=stats&f[id]={$f['id']}&f[page]={$x}&f[date]={$f['date']}&f[sort]={$f['sort']}&f[ip]={$f['ip']}>{$x}</a>";
         }
         if ($x >= 20) {
             break;
         }
     }
     $pager .= "&nbsp;&nbsp;&nbsp;{$pagelist}";
     $tpl->assign("PAGER", $pager);
     if ($z > 0) {
         if ($f[ip]) {
             $extra = "AND ip='{$f['ip']}'";
         }
         $recs = $this->db->getsql("SELECT * FROM adrev_aff_traffic \r\n\t\t\t\t\t\t\t\t\tWHERE affid=? AND (date BETWEEN ? AND ?) {$extra}\r\n\t\t\t\t\t\t\t\t\tORDER BY ? LIMIT {$limit} OFFSET {$offset}", array($f['id'], $sdate, $edate, $f['sort']));
         $gen = new formgen();
         $gen->startrow("#CCCCCC");
         $gen->column("<b>" . lib_lang("Date") . "</b>");
         $gen->column("<b>" . lib_lang("Type") . "</b>");
         $gen->column("<b>" . lib_lang("IP") . "</b>");
         $gen->column("<b>" . lib_lang("Amount") . "</b>", "", "", "", "right");
         $gen->column("<b>" . lib_lang("Page") . "</b>");
         $gen->endrow();
         foreach ($recs as $rec) {
             $ref = stripslashes($rec[referer]);
             if (strlen($ref) > 60) {
                 $ref = substr($ref, 0, 30) . "..." . substr($ref, -30);
             }
             $bgcolor = $bgcolor == "#FFFFFF" ? "#FFFFEE" : "#FFFFFF";
             $gen->startrow($bgcolor);
             $gen->column(date("m-d-Y h:i:sa", $rec[date]));
             $gen->column($rec[adtype]);
             $gen->column($rec[ip]);
             $gen->column(number_format($rec[amount], 3), "", "", "", "right");
             $gen->column("<A href=\"{$rec['referer']}\" title=\"Open in new window\" target=\"_new\">{$ref}</a>");
             $gen->endrow();
         }
         $tpl->assign("TABLE", $gen->gentable("100%", 0, 1, 3, "#FFFFFF"));
     }
     $dates = array("today" => lib_lang('Today'), "yesterday" => lib_lang('Yesterday'), "thisweek" => lib_lang('This Week'), "lastweek" => lib_lang('Last Week'), "thismonth" => lib_lang('This Month'), "lastmonth" => lib_lang('Last Month'), all => lib_lang('All Time'));
     $tpl->assign("DATELIST", lib_htlist_array($dates, $f[date]));
     $tpl->assign("SORTLIST", lib_htlist_array(array('ip,date DESC' => 'IP Address', 'date DESC,ip' => 'Date'), $f[sort]));
     $tpl->assign("ID", $f[id]);
     $tpl->assign("IP", $f[ip]);
     $tpl->assign("BALANCE", number_format($amount, 3));
     $tpl->parse("stats");
     $this->title = lib_lang("Publisher Statistics");
     $this->content = $tpl->text("stats");
     $this->display();
     $this->printpage();
     exit;
 }
 function stats()
 {
     $this->output->secure();
     $f = $this->input->f;
     $uid = $_SESSION['user']['id'];
     $tpl = new XTemplate("templates/ads_keywords.html");
     // Grab the ad info
     $ad = $this->db->getsql("SELECT * FROM adrev_ads WHERE zid=?", array($f['id']));
     $id = $ad['0']['id'];
     $zoneid = $ad['0']['zone'];
     // Grab the zone information
     $zone = $this->db->getsql("SELECT * FROM adrev_zones WHERE id=?", array($zoneid));
     // Setup some defaults
     list($startdate, $enddate) = lib_date_range($f['period']);
     $dates = array("today" => lib_lang('Today'), "yesterday" => lib_lang('Yesterday'), "thisweek" => lib_lang('This Week'), "lastweek" => lib_lang('Last Week'), "thismonth" => lib_lang('This Month'), "lastmonth" => lib_lang('Last Month'), all => lib_lang('All Time'));
     // Show the stats for this word (up to 100 days worth)
     if (!$f['kid']) {
         $f['kid'] = 0;
     }
     $uid = $_SESSION['user']['id'];
     $stats = $this->db->getsql("SELECT * FROM adrev_traffic \r\n\t\t\t\t\t\t\t\t\t\tWHERE adid=? AND keywordid=? \r\n\t\t\t\t\t\t\t\t\t\tAND date BETWEEN ? AND ?\r\n\t\t\t\t\t\t\t\t\t\tORDER BY date DESC\r\n\t\t\t\t\t\t\t\t\t\tLIMIT 100", array($id, $f['kid'], $startdate, $enddate));
     if (count($stats) > 0) {
         foreach ($stats as $rec) {
             $bgcolor = iif($bgcolor == "#FFFFFF", "#FFFFEE", "#FFFFFF");
             $tpl->assign("BGCOLOR", $bgcolor);
             $tpl->assign("DATE", date("M d Y", strtotime($rec['date'])));
             $tpl->assign("CLICKS", number_format($rec['clicks']));
             $tpl->assign("VIEWS", number_format($rec['impressions']));
             $tpl->assign("ORDERS", number_format($rec['orders']));
             $tpl->assign("SPEND", number_format($rec['amount'], 2));
             $tpl->assign("CTR", number_format($rec['clicks'] * 100 / iif(!$rec['impressions'], 1, $rec['impressions']), 2));
             $tpl->parse("main.list");
             $t_impressions += $rec['impressions'];
             $t_clicks += $rec['clicks'];
             $t_orders += $rec['orders'];
             $t_spend += $rec['amount'];
         }
         $tpl->assign("TVIEWS", number_format($t_impressions));
         $tpl->assign("TCLICKS", number_format($t_clicks));
         $tpl->assign("TCTR", number_format($t_clicks * 100 / iif(!$t_impressions, 1, $t_impressions), 2));
         $tpl->assign("TORDERS", number_format($t_orders));
         $tpl->assign("TSPEND", number_format($t_spend, 2));
         $tpl->parse("main.totals");
     }
     // Show the keyword list
     if ($f['kid']) {
         $keywords = $this->db->getsql("SELECT b.id,b.keyword FROM adrev_keyword_map a, adrev_keywords b\r\n\t\t\t\t\t\t\t\t\t\t\tWHERE a.keywordid=b.id AND a.zoneid=? AND a.adid=?\r\n\t\t\t\t\t\t\t\t\t\t\tORDER BY b.keyword", array($zoneid, $id));
         $klist = array();
         if (count($keywords) > 0) {
             foreach ($keywords as $rec) {
                 $klist[$rec['id']] = $rec['keyword'];
             }
             $tpl->assign("KEYWORDLIST", lib_htlist_array($klist, $f['kid']));
         }
     }
     $tpl->assign("PERIOD", date("D M d, Y", strtotime($startdate)) . " - " . date("D M d, Y", strtotime($enddate)));
     $tpl->assign("ID", $f['id']);
     $tpl->assign("KID", $f['kid']);
     $tpl->assign("PERIODLIST", lib_htlist_array($dates, $f['period']));
     $tpl->assign("ADS_MENU", $this->menu($_REQUEST['f']['id']));
     $tpl->parse("main");
     $this->title = lib_lang("View Ad Stats");
     $this->content = $tpl->text("main");
     $this->display();
     $this->printpage();
     exit;
 }
 function zone_list()
 {
     $dates = $_REQUEST['dates'] ? $_REQUEST['dates'] : "all";
     list($sd, $ed) = lib_date_range($dates);
     $this->output->admin();
     $zones = $this->db->getsql("SELECT a.*, b.name as style FROM adrev_zones a, adrev_ad_types b\r\n\t\t\t\t\t\t\t\t\t\tWHERE a.style=b.id\r\n\t\t\t\t\t\t\t\t\t\tORDER BY a.name");
     $tpl = new XTemplate("templates/zone_list.html");
     if (count($zones) > 0) {
         foreach ($zones as $rec) {
             // Count number of total ads
             $a = $this->db->getsql("SELECT count(*) as num FROM adrev_ads WHERE zone=?", array($rec['id']));
             $tpl->assign("ADS", $a[0][num]);
             // Grab stats for this ad
             $stats = $this->db->getsql("SELECT sum(a.clicks) as clicks, sum(a.impressions) as impressions, \r\n\t\t\t\t\t\t\t\t\t\t\tsum(a.amount) as amount, sum(a.orders) as orders \r\n\t\t\t\t\t\t\t\t\t\t\tFROM adrev_traffic a, adrev_ads b\r\n\t\t\t\t\t\t\t\t\t\t\tWHERE a.adid=b.id AND b.zone=? \r\n\t\t\t\t\t\t\t\t\t\t\tAND a.date BETWEEN ? AND ?", array($rec['id'], $sd, $ed));
             // Add the stats columns
             $tpl->assign("CLICKS", $stats[0]['clicks'] > 0 ? $stats[0]['clicks'] : 0);
             $tpl->assign("IMPRESSIONS", $stats[0]['impressions'] > 0 ? $stats[0]['impressions'] : 0);
             $tpl->assign("ORDERS", $stats[0]['orders'] > 0 ? $stats[0]['orders'] : 0);
             $tpl->assign("AMOUNT", $stats[0]['amount'] > 0 ? $stats[0]['amount'] : 0);
             // Build totals
             $total_clicks += $stats[0]['clicks'] > 0 ? $stats[0]['clicks'] : 0;
             $total_impressions += $stats[0]['impressions'] > 0 ? $stats[0]['impressions'] : 0;
             $total_orders += $stats[0]['orders'] > 0 ? $stats[0]['orders'] : 0;
             $total_amount += $stats[0]['amount'] > 0 ? $stats[0]['amount'] : 0;
             $total_ads += $a[0]['num'] > 0 ? $a[0]['num'] : 0;
             // And number of pending ads
             $a = $this->db->getsql("SELECT count(*) as num FROM adrev_ads WHERE zone=? AND status='2'", array($rec['id']));
             $tpl->assign("PENDING_ADS", $a[0][num]);
             $bgcolor = iif($bgcolor == "#FFFFFF", "#FFFFEE", "#FFFFFF");
             $tpl->assign("ID", $rec[id]);
             $tpl->assign("BGCOLOR", $bgcolor);
             $tpl->assign("NAME", stripslashes($rec[name]));
             $tpl->assign("STYLE", stripslashes($rec[style]));
             if ($rec[status] == 1) {
                 $tpl->assign("STATUS", lib_lang("Active"));
             } elseif ($rec[status] == 2) {
                 $tpl->assign("STATUS", lib_lang("Admin&nbsp;only"));
             } else {
                 $tpl->assign("STATUS", lib_lang("Inactive"));
             }
             $tpl->assign("APPROVE", $rec[auto_approve] ? lib_lang("Automatic") : lib_lang("Manual"));
             $tpl->assign("RATE_TYPE", $rec[rate_type]);
             $tpl->assign("RATE", number_format($rec[rate], 2));
             if ($rec[ad_sort] == "bid") {
                 $sorter = "Bidded";
             } elseif ($rec[ad_sort] == "rand") {
                 $sorter = "Random";
             } elseif ($rec[ad_sort] == "asc") {
                 $sorter = "FIFO";
             } elseif ($rec[ad_sort] == "desc") {
                 $sorter = "LIFO";
             }
             $tpl->assign("SORT", $sorter);
             $tpl->assign("PUB", $rec[aff_percent]);
             $tpl->parse("main.list");
         }
     }
     $tpl->assign("ZONE_STYLE", $this->db->htlist("adrev_ad_types", "id", "name"));
     $tpl->assign("TOTAL_CLICKS", number_format($total_clicks));
     $tpl->assign("TOTAL_IMPRESSIONS", number_format($total_impressions));
     $tpl->assign("TOTAL_ORDERS", number_format($total_orders));
     $tpl->assign("TOTAL_AMOUNT", number_format($total_amount, 2));
     $tpl->assign("TOTAL_ADS", number_format($total_ads));
     if ($total_clicks > 0 || $total_impressions > 0) {
         $tpl->parse("main.totals");
     }
     $mdates = array("today" => lib_lang('Today'), "yesterday" => lib_lang('Yesterday'), "thisweek" => lib_lang('This Week'), "lastweek" => lib_lang('Last Week'), "thismonth" => lib_lang('This Month'), "lastmonth" => lib_lang('Last Month'), 'all' => lib_lang('All Time'));
     $tpl->assign("MDATES", lib_htlist_array($mdates, $dates));
     $tpl->parse("main");
     $this->title = lib_lang("Manage Zones");
     $this->content = $tpl->text("main");
     $this->display();
     $this->printpage();
     exit;
 }