--- ad_report-o.module	2008-04-16 06:14:57.000000000 +0530
+++ ad_report.module	2008-05-08 15:21:34.000000000 +0530
@@ -1,5 +1,5 @@
 <?php
-// $Id: ad_report.module,v 1.1.2.3.2.7 2008/04/16 00:44:57 jeremy Exp $
+// $Id: ad_report.module,v 1.1.2.3.2.4 2007/07/30 21:01:39 jeremy Exp $
 
 /**
  * @file
@@ -12,63 +12,47 @@
 /**
  * Implementation of hook_menu().
  */
-function ad_report_menu($may_cache) {
+function ad_report_menu() {
   $items = array();
-
-  if (!$may_cache) {
-    if (arg(0) == 'node' && is_numeric(arg(1)) && ad_adaccess(arg(1), 'access statistics')) {
-      $node = node_load(array('nid' => arg(1)));
-      if ($node->adtype) {
-        $items[] = array(
-          'path' => "node/$node->nid/report",
-          'title' => t('Reports'),
-          'callback' => 'ad_report_bargraph',
-          'callback arguments' => array($node, arg(3), 'node'),
-          'type' => MENU_LOCAL_TASK,
-          'access' => ad_adaccess($node->nid, 'access statistics'));
-        $items[] = array(
-          'path' => "node/$node->nid/report/hourly",
-          'access' => ad_adaccess($node->nid, 'access statistics'),
-          'title' => t('Hourly'),
-          'type' => MENU_LOCAL_TASK,
-          'weight' => 0);
-        $items[] = array(
-          'path' => "node/$node->nid/report/daily",
-          'access' => ad_adaccess($node->nid, 'access statistics'),
-          'title' => t('Daily'),
-          'type' => MENU_DEFAULT_LOCAL_TASK,
-          'weight' => 1);
-        $items[] = array(
-          'path' => "node/$node->nid/report/weekly",
-          'access' => ad_adaccess($node->nid, 'access statistics'),
-          'title' => t('Weekly'),
-          'type' => MENU_LOCAL_TASK,
-          'weight' => 2);
-        $items[] = array(
-          'path' => "node/$node->nid/report/monthly",
-          'access' => ad_adaccess($node->nid, 'access statistics'),
-          'title' => t('Monthly'),
-          'type' => MENU_LOCAL_TASK,
-          'weight' => 3);
-      }
-    }
-    else if (arg(0) == 'ad_report' && is_numeric(arg(1))) {
-      if (arg(3) == 'node') {
-        $access = ad_adaccess(arg(1), 'access statistics');
-      }
-      else {
-        // TODO: Need to implement proper permissions here.
-        $access = TRUE;
-      }
-      $items[] = array(
-        'path' => 'ad_report/'. arg(1). '/bargraph/' . arg(3),
-        'title' => 'Bar graph',
-        'callback' => 'ad_report_generate_bargraph',
-        'callback arguments' => array(arg(1), arg(3)),
-        'type' => MENU_CALLBACK,
-        'access' => $access);
+  if (arg(0) == 'node' && is_numeric(arg(1)) && ad_adaccess(arg(1), 'access statistics')) {
+    $node = node_load(array('nid' => arg(1)));
+    if ($node->adtype) {
+      $items["node/$node->nid/report"] = array(
+        'title' => t('Reports'),
+        'page callback' => 'ad_report_bargraph',
+        'page arguments' => array($node, arg(3)),
+        'type' => MENU_LOCAL_TASK,
+        'access arguments' => ad_adaccess($node->nid, 'access statistics'));
+      $items["node/$node->nid/report/hourly"] = array(
+        'access arguments' => ad_adaccess($node->nid, 'access statistics'),
+        'title' => t('Hourly'),
+        'type' => MENU_LOCAL_TASK,
+        'weight' => 0);
+      $items["node/$node->nid/report/daily"] = array(
+        'access arguments' => ad_adaccess($node->nid, 'access statistics'),
+        'title' => t('Daily'),
+        'type' => MENU_DEFAULT_LOCAL_TASK,
+        'weight' => 1);
+      $items["node/$node->nid/report/weekly"] = array(
+        'access arguments' => ad_adaccess($node->nid, 'access statistics'),
+        'title' => t('Weekly'),
+        'type' => MENU_LOCAL_TASK,
+        'weight' => 2);
+      $items["node/$node->nid/report/monthly"] = array(
+        'access arguments' => ad_adaccess($node->nid, 'access statistics'),
+        'title' => t('Monthly'),
+        'type' => MENU_LOCAL_TASK,
+        'weight' => 3);
+     }
+   }
+   else if (arg(0) == 'ad_report' && is_numeric(arg(1))) {
+     $items['ad_report/'. arg(1). '/bargraph'] = array(
+       'title' => 'Bar graph',
+       'page callback' => 'ad_report_generate_bargraph',
+       'page arguments' => array(arg(1), arg(3)),
+       'type' => MENU_CALLBACK,
+       'access' => ad_adaccess(arg(1), 'access statistics'));
     }
-  }
 
   return $items;
 }
@@ -76,14 +60,13 @@ function ad_report_menu($may_cache) {
 /**
  * Page to display ad with bargraph.
  */
-function ad_report_bargraph($data, $granularity, $type = 'node') {
+function ad_report_bargraph($node, $granularity) {
   switch ($granularity) {
     case 'hourly':
       drupal_set_title(t('past twelve hours'));
       break;
     case 'daily':
     default:
-      $granularity = 'daily';
       drupal_set_title(t('past twelve days'));
       break;
     case 'weekly':
@@ -93,28 +76,21 @@ function ad_report_bargraph($data, $gran
       drupal_set_title(t('past twelve months'));
       break;
   }
-
-  switch ($type) {
-    case 'node':
-      $ad = db_fetch_object(db_query('SELECT aid, redirect, adtype FROM {ads} WHERE aid = %d', $data->nid));
-      if ($ad->aid) {
-        $output = '<img src="'. url("ad_report/$data->nid/bargraph/$granularity/node") .'" />';
-        $output .= theme('box', $data->title, module_invoke("ad_$data->adtype", 'display_ad', $ad));
-      }
-      break;
-    default:
-      $output = '<img src="'. url("ad_report/$data->uid/bargraph/$granularity/$type") .'" />';
-      break;
+  $ad = db_fetch_object(db_query('SELECT aid, redirect, adtype FROM {ads} WHERE aid = %d', $node->nid));
+  if ($ad->aid) {
+    $output = '<img src="'. url("ad_report/$node->nid/bargraph/$granularity") .'" />';
+    $output .= theme('box', $node->title, module_invoke("ad_$node->adtype", 'display_ad', $ad));
   }
   return $output;
 }
 
 /**
- * Page that utilizes gd to generate a bargraph.
- *
- * TODO: Make this more dynamic, allowing to move through time, etc.
+ * Page that utilizes gd to generate a bargraph.  It's currently hard-coded
+ * to only graph the past 7 days, but the idea is to support past 24 hours,
+ * past 7 days, past 4 weeks, past 12 months, etc, and to be able to navigate
+ * through time as well.
  */
-function ad_report_generate_bargraph($id, $granularity, $type) {
+function ad_report_generate_bargraph($aid, $granularity) {
   header("Content-type: image/png");
 
   // Preperation.
@@ -182,24 +158,7 @@ function ad_report_generate_bargraph($id
   for ($i = $time - $start_time; $i <= $time; $i = $i + $increment_time) {
     $day_start = date($format_start, $i);
     $day_end = date($format_end, $i + $end_add). $format_end_append;
-    if ($type == 'node') {
-      $view = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $id, $day_start, $day_end));
-      $click = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d AND date <= %d", $id, $day_start, $day_end));
-    }
-    else if ($type == 'user') {
-      $view = (int)db_result(db_query("SELECT SUM(a.count) FROM {ad_statistics} a LEFT JOIN {node} n ON a.aid = n.nid WHERE uid = %d AND type = 'ad' AND (action = 'view' OR action = 'count') AND date >= %d AND date <= %d", $id, $day_start, $day_end));
-      $click = (int)db_result(db_query("SELECT SUM(a.count) FROM {ad_statistics} a LEFT JOIN {node} n ON a.aid = n.nid WHERE uid = %d AND type = 'ad' AND action = 'click' AND date >= %d AND date <= %d", $id, $day_start, $day_end));
-    }
-    else {
-      $function = "ad_report_views_$type";
-      if (function_exists("$function")) {
-        $view = $function($id, $day_start, $day_end);
-      }
-      $function = "ad_report_clicks_$type";
-      if (function_exists("$function")) {
-        $click = $function($id, $day_start, $day_end);
-      }
-    }
+    $view = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $aid, $day_start, $day_end));
     if ($view > $max_views) {
       $max_views = $view;
     }
@@ -207,7 +166,7 @@ function ad_report_generate_bargraph($id
       'upper' => date($format_upper, $i),
       'lower' => date($format_lower, $i),
       'views' => $view,
-      'clicks' => $click);
+      'clicks' => (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d AND date <= %d", $aid, $day_start, $day_end)));
   }
 
   // Build graph image.
@@ -284,3 +243,4 @@ function ad_report_generate_bargraph($id
   
 }
 
+?>
