1. <?php
  2.  
  3. // ================================================================================
  4. // ******************************* DEFINE CONSTANTS *******************************
  5. // ================================================================================
  6.  
  7. require("includes/constants.php");
  8. include(INCLUDE_DIR . "/db_connect.php");
  9.  
  10. //Required for display.
  11. require('smarty/Smarty.class.php');
  12. $smarty = new Smarty;
  13. $smarty->template_dir = 'smarty/templates';
  14. $smarty->compile_dir = 'smarty/templates_c';
  15. $smarty->cache_dir = 'smarty/cache';
  16. $smarty->config_dir = 'smarty/configs';
  17.  
  18. //Change the TZ to Los Angeles
  19. date_default_timezone_set('America/Los_Angeles');
  20.  
  21. $currentDate = strtotime(date("m/d/y"));
  22.  
  23. if($_GET['date'] == 'yesterday'){
  24.     $yesterdayTimestamp = strtotime("-1 day", $currentDate);
  25.    
  26.     $query = "SELECT SensorName, SUM(CountA) AS Traffic, StartDate, EndDate FROM counterdata
  27.                 WHERE StartDate >= '{$yesterdayTimestamp}'
  28.                 GROUP BY StartDate, SensorName";
  29.     $result = mysql_query($query);
  30.    
  31.     $i = 0;
  32.     $noDataError = false;
  33.     while($row = mysql_fetch_assoc($result)){
  34.         $row['StartDate2'] = date('m/d/y', $row['StartDate']);
  35.         $row['EndDate2'] = date('m/d/y', $row['EndDate']);
  36.        
  37.         if($i == 0){
  38.             $tmpRows['date'] = $row['StartDate2'];
  39.         }
  40.            
  41.         if($i < 6){
  42.             $tmpRows[] = $row;
  43.             $i++;
  44.         }
  45.         else {
  46.             $rows[] = $tmpRows;
  47.             $tmpRows = '';
  48.             $i = 0;
  49.            
  50.             $tmpRows['date'] = $row['StartDate2'];
  51.             $tmpRows[] = $row;
  52.             $i++;
  53.         }
  54.     }
  55.    
  56.     if($rows == ''){
  57.         $noDataError = true;
  58.     }
  59.    
  60.     $smarty->assign('noDataError', $noDataError);
  61.     $smarty->assign('dataArray', $rows);
  62.     $smarty->assign('date', 'yesterday');
  63.     $smarty->assign('listStartDate', date('m/d/y', $yesterdayTimestamp));
  64.     $smarty->display('report.tpl');
  65.     die;
  66. }
  67. elseif($_GET['date'] == 'week'){
  68.     $weekAgoTimestamp = strtotime("-7 day", $currentDate);
  69.    
  70.     $query = "SELECT SensorName, SUM(CountA) AS Traffic, StartDate, EndDate FROM counterdata
  71.                 WHERE StartDate >= '{$weekAgoTimestamp}' AND EndDate <= '{$currentDate}'
  72.                 GROUP BY StartDate, SensorName";
  73.     $result = mysql_query($query);
  74.    
  75.     $i = 0;
  76.     $noDataError = false;
  77.     while($row = mysql_fetch_assoc($result)){
  78.         $row['StartDate2'] = date('m/d/y', $row['StartDate']);
  79.         $row['EndDate2'] = date('m/d/y', $row['EndDate']);
  80.        
  81.         if($i == 0){
  82.             $tmpRows['date'] = $row['StartDate2'];
  83.         }
  84.            
  85.         if($i < 6){
  86.             $tmpRows[] = $row;
  87.             $i++;
  88.         }
  89.         else {
  90.             $rows[] = $tmpRows;
  91.             $tmpRows = '';
  92.             $i = 0;
  93.            
  94.             $tmpRows['date'] = $row['StartDate2'];
  95.             $tmpRows[] = $row;
  96.             $i++;
  97.         }
  98.     }
  99.    
  100.     if($rows == ''){
  101.         $noDataError = true;
  102.     }
  103.    
  104.     $smarty->assign('noDataError', $noDataError);
  105.     $smarty->assign('dataArray', $rows);
  106.     $smarty->assign('date', 'week');
  107.     $smarty->assign('listStartDate', date('m/d/y', $weekAgoTimestamp));
  108.     $smarty->assign('listEndDate', date('m/d/y', $currentDate));
  109.     $smarty->display('report.tpl');
  110.     die;
  111. }
  112. elseif($_GET['date'] == 'month'){
  113.     $monthAgoTimestamp = strtotime("-31 days", $currentDate);
  114.    
  115. //  $query = "SELECT SensorName, StartDate, TIME_FORMAT(StartTime, '%H:%i') as StartTime,
  116. //              EndDate, TIME_FORMAT(EndTime, '%H:%i') as EndTime, CountA
  117. //              FROM counterdata WHERE StartDate >= '{$monthAgoTimestamp}' AND EndDate <= '{$currentDate}'";
  118.  
  119.     $query = "SELECT SensorName, SUM(CountA) AS Traffic, StartDate, EndDate FROM counterdata
  120.                 WHERE StartDate >= '{$monthAgoTimestamp}' AND EndDate <= '{$currentDate}'
  121.                 GROUP BY StartDate, SensorName";
  122.     $result = mysql_query($query);
  123.    
  124.     $i = 0;
  125.     while($row = mysql_fetch_assoc($result)){
  126.         $row['StartDate2'] = date('m/d/y', $row['StartDate']);
  127.         $row['EndDate2'] = date('m/d/y', $row['EndDate']);
  128.        
  129.         if($i == 0)
  130.             $tmpRows['date'] = $row['StartDate2'];
  131.            
  132.         if($i < 6){
  133.  
  134.             $tmpRows[] = $row;
  135.             $i++;
  136.         }
  137.         else {
  138.             $rows[] = $tmpRows;
  139.             $tmpRows = '';
  140.             $i = 0;
  141.            
  142.             $tmpRows['date'] = $row['StartDate2'];
  143.             $tmpRows[] = $row;
  144.             $i++;
  145.         }
  146.     }
  147.    
  148.     $smarty->assign('dataArray', $rows);
  149.     $smarty->assign('date', 'month');
  150.     $smarty->assign('listStartDate', date('m/d/y', $monthAgoTimestamp));
  151.     $smarty->assign('listEndDate', date('m/d/y', $currentDate));
  152.     $smarty->display('report.tpl');
  153.     die;
  154. }
  155. elseif($_GET['date'] == 'semester'){
  156.     $smarty->assign('date', 'semester');
  157.     $smarty->display('report.tpl');
  158.     die;
  159. }
  160.  
  161. ?>