<?php
// ================================================================================
// ******************************* DEFINE CONSTANTS *******************************
// ================================================================================
require("includes/constants.php");
include(INCLUDE_DIR . "/db_connect.php");
//Required for display.
require('smarty/Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = 'smarty/templates';
$smarty->compile_dir = 'smarty/templates_c';
$smarty->cache_dir = 'smarty/cache';
$smarty->config_dir = 'smarty/configs';
//Change the TZ to Los Angeles
date_default_timezone_set('America/Los_Angeles');
$currentDate = strtotime(date("m/d/y"));
if($_GET['date'] == 'yesterday'){
$yesterdayTimestamp = strtotime("-1 day", $currentDate);
$query = "SELECT SensorName, SUM(CountA) AS Traffic, StartDate, EndDate FROM counterdata
WHERE StartDate >= '{$yesterdayTimestamp}'
GROUP BY StartDate, SensorName";
$result = mysql_query($query);
$i = 0;
$noDataError = false;
while($row = mysql_fetch_assoc($result)){
$row['StartDate2'] = date('m/d/y', $row['StartDate']);
$row['EndDate2'] = date('m/d/y', $row['EndDate']);
if($i == 0){
$tmpRows['date'] = $row['StartDate2'];
}
if($i < 6){
$tmpRows[] = $row;
$i++;
}
else {
$rows[] = $tmpRows;
$tmpRows = '';
$i = 0;
$tmpRows['date'] = $row['StartDate2'];
$tmpRows[] = $row;
$i++;
}
}
if($rows == ''){
$noDataError = true;
}
$smarty->assign('noDataError', $noDataError);
$smarty->assign('dataArray', $rows);
$smarty->assign('date', 'yesterday');
$smarty->assign('listStartDate', date('m/d/y', $yesterdayTimestamp));
$smarty->display('report.tpl');
die;
}
elseif($_GET['date'] == 'week'){
$weekAgoTimestamp = strtotime("-7 day", $currentDate);
$query = "SELECT SensorName, SUM(CountA) AS Traffic, StartDate, EndDate FROM counterdata
WHERE StartDate >= '{$weekAgoTimestamp}' AND EndDate <= '{$currentDate}'
GROUP BY StartDate, SensorName";
$result = mysql_query($query);
$i = 0;
$noDataError = false;
while($row = mysql_fetch_assoc($result)){
$row['StartDate2'] = date('m/d/y', $row['StartDate']);
$row['EndDate2'] = date('m/d/y', $row['EndDate']);
if($i == 0){
$tmpRows['date'] = $row['StartDate2'];
}
if($i < 6){
$tmpRows[] = $row;
$i++;
}
else {
$rows[] = $tmpRows;
$tmpRows = '';
$i = 0;
$tmpRows['date'] = $row['StartDate2'];
$tmpRows[] = $row;
$i++;
}
}
if($rows == ''){
$noDataError = true;
}
$smarty->assign('noDataError', $noDataError);
$smarty->assign('dataArray', $rows);
$smarty->assign('date', 'week');
$smarty->assign('listStartDate', date('m/d/y', $weekAgoTimestamp));
$smarty->assign('listEndDate', date('m/d/y', $currentDate));
$smarty->display('report.tpl');
die;
}
elseif($_GET['date'] == 'month'){
$monthAgoTimestamp = strtotime("-31 days", $currentDate);
// $query = "SELECT SensorName, StartDate, TIME_FORMAT(StartTime, '%H:%i') as StartTime,
// EndDate, TIME_FORMAT(EndTime, '%H:%i') as EndTime, CountA
// FROM counterdata WHERE StartDate >= '{$monthAgoTimestamp}' AND EndDate <= '{$currentDate}'";
$query = "SELECT SensorName, SUM(CountA) AS Traffic, StartDate, EndDate FROM counterdata
WHERE StartDate >= '{$monthAgoTimestamp}' AND EndDate <= '{$currentDate}'
GROUP BY StartDate, SensorName";
$result = mysql_query($query);
$i = 0;
while($row = mysql_fetch_assoc($result)){
$row['StartDate2'] = date('m/d/y', $row['StartDate']);
$row['EndDate2'] = date('m/d/y', $row['EndDate']);
if($i == 0)
$tmpRows['date'] = $row['StartDate2'];
if($i < 6){
$tmpRows[] = $row;
$i++;
}
else {
$rows[] = $tmpRows;
$tmpRows = '';
$i = 0;
$tmpRows['date'] = $row['StartDate2'];
$tmpRows[] = $row;
$i++;
}
}
$smarty->assign('dataArray', $rows);
$smarty->assign('date', 'month');
$smarty->assign('listStartDate', date('m/d/y', $monthAgoTimestamp));
$smarty->assign('listEndDate', date('m/d/y', $currentDate));
$smarty->display('report.tpl');
die;
}
elseif($_GET['date'] == 'semester'){
$smarty->assign('date', 'semester');
$smarty->display('report.tpl');
die;
}
?>