issue 2119. supporting php 5.4. counter module.

git-svn-id: http://xe-core.googlecode.com/svn/branches/maserati@12723 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
flyskyko 2013-02-07 08:09:15 +00:00
parent 687fe8683b
commit 2e8b49acce
4 changed files with 227 additions and 86 deletions

View file

@ -1,4 +1,5 @@
<?php
/**
* Admin view class of counter module
*
@ -6,6 +7,7 @@
*/
class counterAdminView extends counter
{
/**
* Initialization
*
@ -14,7 +16,7 @@ class counterAdminView extends counter
function init()
{
// set the template path
$this->setTemplatePath($this->module_path.'tpl');
$this->setTemplatePath($this->module_path . 'tpl');
}
/**
@ -26,28 +28,40 @@ class counterAdminView extends counter
{
// set today's if no date is given
$selected_date = Context::get('selected_date');
if(!$selected_date) $selected_date = date("Ymd");
if(!$selected_date)
{
$selected_date = date("Ymd");
}
Context::set('selected_date', $selected_date);
// create the counter model object
$oCounterModel = &getModel('counter');
$oCounterModel = getModel('counter');
// get a total count and daily count
$site_module_info = Context::get('site_module_info');
$status = $oCounterModel->getStatus(array(0,$selected_date),$site_module_info->site_srl);
$status = $oCounterModel->getStatus(array(0, $selected_date), $site_module_info->site_srl);
Context::set('total_counter', $status[0]);
Context::set('selected_day_counter', $status[$selected_date]);
// get data by time, day, month, and year
$type = Context::get('type');
if(!$type)
{
$type = 'day';
Context::set('type',$type);
Context::set('type', $type);
}
$detail_status = $oCounterModel->getHourlyStatus($type, $selected_date, $site_module_info->site_srl);
Context::set('detail_status', $detail_status);
// display
$this->setTemplateFile('index');
}
}
/* End of file counter.admin.view.php */
/* Location: ./modules/counter/counter.admin.view.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* High class of counter module
*
@ -6,15 +7,18 @@
*/
class counter extends ModuleObject
{
/**
* Implement if additional tasks are necessary when installing
* @return Object
*/
function moduleInstall()
{
$oCounterController = &getController('counter');
$oCounterController = getController('counter');
// add a row for the total visit history
//$oCounterController->insertTotalStatus();
// add a row for today's status
//$oCounterController->insertTodayStatus();
@ -29,11 +33,18 @@ class counter extends ModuleObject
function checkUpdate()
{
// Add site_srl to the counter
$oDB = &DB::getInstance();
if(!$oDB->isColumnExists('counter_log', 'site_srl')) return true;
if(!$oDB->isIndexExists('counter_log','idx_site_counter_log')) return true;
$oDB = DB::getInstance();
if(!$oDB->isColumnExists('counter_log', 'site_srl'))
{
return TRUE;
}
return false;
if(!$oDB->isIndexExists('counter_log', 'idx_site_counter_log'))
{
return TRUE;
}
return FALSE;
}
/**
@ -44,9 +55,17 @@ class counter extends ModuleObject
function moduleUpdate()
{
// Add site_srl to the counter
$oDB = &DB::getInstance();
if(!$oDB->isColumnExists('counter_log', 'site_srl')) $oDB->addColumn('counter_log','site_srl','number',11,0,true);
if(!$oDB->isIndexExists('counter_log','idx_site_counter_log')) $oDB->addIndex('counter_log','idx_site_counter_log',array('site_srl','ipaddress'),false);
$oDB = DB::getInstance();
if(!$oDB->isColumnExists('counter_log', 'site_srl'))
{
$oDB->addColumn('counter_log', 'site_srl', 'number', 11, 0, TRUE);
}
if(!$oDB->isIndexExists('counter_log', 'idx_site_counter_log'))
{
$oDB->addIndex('counter_log', 'idx_site_counter_log', array('site_srl', 'ipaddress'), FALSE);
}
return new Object(0, 'success_updated');
}
@ -58,7 +77,9 @@ class counter extends ModuleObject
*/
function recompileCache()
{
}
}
/* End of file counter.class.php */
/* Location: ./modules/counter/counter.class.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* Counter module's controller class
*
@ -6,6 +7,7 @@
*/
class counterController extends counter
{
/**
* Initialization
*
@ -13,6 +15,7 @@ class counterController extends counter
*/
function init()
{
}
/**
@ -23,6 +26,7 @@ class counterController extends counter
*/
function procCounterExecute()
{
}
/**
@ -32,17 +36,19 @@ class counterController extends counter
*/
function counterExecute()
{
$oDB = &DB::getInstance();
$oDB = DB::getInstance();
$oDB->begin();
$site_module_info = Context::get('site_module_info');
$site_srl = (int)$site_module_info->site_srl;
$site_srl = (int) $site_module_info->site_srl;
// Check the logs
$oCounterModel = &getModel('counter');
$oCounterModel = getModel('counter');
// Register today's row if not exist
if(!$oCounterModel->isInsertedTodayStatus($site_srl))
{
$this->insertTodayStatus(0,$site_srl);
$this->insertTodayStatus(0, $site_srl);
// check user if the previous row exists
}
else
@ -71,11 +77,13 @@ class counterController extends counter
* @param integer $site_srl
* @return Object result of count query
*/
function insertLog($site_srl=0)
function insertLog($site_srl = 0)
{
$args = new stdClass();
$args->regdate = date("YmdHis");
$args->user_agent = substr ($_SERVER['HTTP_USER_AGENT'], 0, 250);
$args->user_agent = substr($_SERVER['HTTP_USER_AGENT'], 0, 250);
$args->site_srl = $site_srl;
return executeQuery('counter.insertCounterLog', $args);
}
@ -85,8 +93,10 @@ class counterController extends counter
* @param integer $site_srl
* @return void
*/
function insertUniqueVisitor($site_srl=0)
function insertUniqueVisitor($site_srl = 0)
{
$args = new stdClass();
if($site_srl)
{
$args->regdate = '0' . date('Ymd');
@ -106,7 +116,7 @@ class counterController extends counter
* @param integer $site_srl
* @return void
*/
function insertPageView($site_srl=0)
function insertPageView($site_srl = 0)
{
$args = new stdClass;
$args->regdate = '0,' . date('Ymd');
@ -128,10 +138,11 @@ class counterController extends counter
* @param integer $site_srl
* @return void
*/
function insertTotalStatus($site_srl=0)
function insertTotalStatus($site_srl = 0)
{
$args = new stdClass;
$args = new stdClass();
$args->regdate = 0;
if($site_srl)
{
$args->site_srl = $site_srl;
@ -150,11 +161,18 @@ class counterController extends counter
* @param integer $site_srl
* @return void
*/
function insertTodayStatus($regdate = 0, $site_srl=0)
function insertTodayStatus($regdate = 0, $site_srl = 0)
{
$args = new stdClass;
if($regdate) $args->regdate = $regdate;
else $args->regdate = date("Ymd");
$args = new stdClass();
if($regdate)
{
$args->regdate = $regdate;
}
else
{
$args->regdate = date("Ymd");
}
if($site_srl)
{
$args->site_srl = $site_srl;
@ -168,9 +186,12 @@ class counterController extends counter
$query_id = 'counter.insertTodayStatus';
executeQuery($query_id); // /< when inserting a daily row, attempt to inser total rows(where regdate=0) together
}
$output = executeQuery($query_id, $args);
// Leave logs
$this->insertLog($site_srl);
// Register unique and pageview
$this->insertUniqueVisitor($site_srl);
}
@ -183,10 +204,12 @@ class counterController extends counter
*/
function deleteSiteCounterLogs($site_srl)
{
$args = new stdClass();
$args->site_srl = $site_srl;
executeQuery('counter.deleteSiteCounter',$args);
executeQuery('counter.deleteSiteCounterLog',$args);
executeQuery('counter.deleteSiteCounter', $args);
executeQuery('counter.deleteSiteCounterLog', $args);
}
}
/* End of file counter.controller.php */
/* Location: ./modules/counter/counter.controller.php */

View file

@ -1,4 +1,5 @@
<?php
/**
* Model class of counter module
*
@ -6,6 +7,7 @@
*/
class counterModel extends counter
{
/**
* Initialization
*
@ -13,6 +15,7 @@ class counterModel extends counter
*/
function init()
{
}
/**
@ -21,25 +24,31 @@ class counterModel extends counter
* @param integer $site_srl Site_srl
* @return bool
*/
function isLogged($site_srl=0)
function isLogged($site_srl = 0)
{
$args = new stdClass;
$args = new stdClass();
$args->regdate = date('Ymd');
$args->ipaddress = $_SERVER['REMOTE_ADDR'];
$args->site_srl = $site_srl;
$oCacheHandler = &CacheHandler::getInstance('object');
$oCacheHandler = CacheHandler::getInstance('object');
if($oCacheHandler->isSupport())
{
$object_key = 'object:' . $site_srl . '_' . str_replace('.', '-', $args->ipaddress);
$cache_key = $oCacheHandler->getGroupKey('counterIpLogged_' . $args->regdate, $object_key);
if($oCacheHandler->isValid($cache_key)) return $oCacheHandler->get($cache_key);
if($oCacheHandler->isValid($cache_key))
{
return $oCacheHandler->get($cache_key);
}
}
$output = executeQuery('counter.getCounterLog', $args);
$result = $output->data->count ? TRUE : FALSE;
if($result && $oCacheHandler->isSupport()) $oCacheHandler->put($cache_key, $result);
if($result && $oCacheHandler->isSupport())
{
$oCacheHandler->put($cache_key, $result);
}
return $result;
}
@ -50,16 +59,19 @@ class counterModel extends counter
* @param integer $site_srl Site_srl
* @return bool
*/
function isInsertedTodayStatus($site_srl=0)
function isInsertedTodayStatus($site_srl = 0)
{
$args = new stdClass;
$args->regdate = date('Ymd');
$oCacheHandler = &CacheHandler::getInstance('object', NULL, TRUE);
$oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE);
if($oCacheHandler->isSupport())
{
$cache_key = 'object:insertedTodayStatus:' . $site_srl . '_' . $args->regdate;
if($oCacheHandler->isValid($cache_key)) return $oCacheHandler->get($cache_key);
if($oCacheHandler->isValid($cache_key))
{
return $oCacheHandler->get($cache_key);
}
}
if($site_srl)
@ -67,8 +79,11 @@ class counterModel extends counter
$args->site_srl = $site_srl;
$output = executeQuery('counter.getSiteTodayStatus', $args);
}
else $output = executeQuery('counter.getTodayStatus', $args);
else
{
$output = executeQuery('counter.getTodayStatus', $args);
}
$result = $output->data->count ? TRUE : FALSE;
if($result && $oCacheHandler->isSupport())
{
@ -94,12 +109,15 @@ class counterModel extends counter
{
$date_count = count($selected_date);
$args = new stdClass();
$args->regdate = implode(',',$selected_date);
$args->regdate = implode(',', $selected_date);
// If a single date log is selected
}
else
{
if(strlen($selected_date) == 8) $selected_date = $selected_date;
if(strlen($selected_date) == 8)
{
$selected_date = $selected_date;
}
$args->regdate = $selected_date;
}
@ -112,17 +130,26 @@ class counterModel extends counter
{
$output = executeQuery('counter.getCounterStatusDays', $args);
}
$status = $output->data;
if(!is_array($selected_date)) return $status;
if(!is_array($selected_date))
{
return $status;
}
if(!is_array($status))
{
$status = array($status);
}
if(!is_array($status)) $status = array($status);
unset($output);
foreach($status as $key => $val)
{
$output[substr($val->regdate,0,8)] = $val;
$output[substr($val->regdate, 0, 8)] = $val;
}
return $output;
}
@ -134,16 +161,18 @@ class counterModel extends counter
* @param integer $site_srl Site_srl
* @return Object
*/
function getHourlyStatus($type='hour', $selected_date, $site_srl=0, $isPageView=false)
function getHourlyStatus($type = 'hour', $selected_date, $site_srl = 0, $isPageView = false)
{
$max = 0;
$sum = 0;
switch($type)
{
case 'year' :
// Get a date to start counting
if($site_srl)
{
$args = new stdClass();
$args->site_srl = $site_srl;
$output = executeQuery('counter.getSiteStartLogDate', $args);
}
@ -151,13 +180,20 @@ class counterModel extends counter
{
$output = executeQuery('counter.getStartLogDate');
}
$start_year = substr($output->data->regdate,0,4);
if(!$start_year) $start_year = date("Y");
for($i=$start_year;$i<=date("Y");$i++)
$start_year = substr($output->data->regdate, 0, 4);
if(!$start_year)
{
$start_year = date("Y");
}
for($i = $start_year; $i <= date("Y"); $i++)
{
$args = new stdClass();
$args->start_date = sprintf('%04d0000', $i);
$args->end_date = sprintf('%04d1231', $i);
if($site_srl)
{
$args->site_srl = $site_srl;
@ -170,37 +206,51 @@ class counterModel extends counter
if(!$isPageView)
{
$count = (int)$output->data->unique_visitor;
$count = (int) $output->data->unique_visitor;
}
else
{
$count = (int)$output->data->pageview;
$count = (int) $output->data->pageview;
}
$status->list[$i] = $count;
if($count>$max) $max = $count;
if($count > $max)
{
$max = $count;
}
$sum += $count;
}
break;
case 'week' :
$time = strtotime($selected_date);
$w = date("D");
while(date("D",$time) != "Sun")
while(date("D", $time) != "Sun")
{
$time += 60*60*24;
$time += 60 * 60 * 24;
}
$time -= 60*60*24;
while(date("D",$time)!="Sun")
$time -= 60 * 60 * 24;
while(date("D", $time) != "Sun")
{
$thisWeek[] = date("Ymd",$time);
$time -= 60*60*24;
$thisWeek[] = date("Ymd", $time);
$time -= 60 * 60 * 24;
}
$thisWeek[] = date("Ymd",$time);
$thisWeek[] = date("Ymd", $time);
asort($thisWeek);
foreach($thisWeek as $day)
{
$args = new stdClass();
$args->start_date = $day;
$args->end_date = $day;
if($site_srl)
{
$args->site_srl = $site_srl;
@ -213,24 +263,32 @@ class counterModel extends counter
if(!$isPageView)
{
$count = (int)$output->data->unique_visitor;
$count = (int) $output->data->unique_visitor;
}
else
{
$count = (int)$output->data->pageview;
$count = (int) $output->data->pageview;
}
$status->list[$day] = (int)$count;
if($count>$max) $max = $count;
$status->list[$day] = (int) $count;
if($count > $max)
{
$max = $count;
}
$sum += $count;
}
break;
case 'month' :
$year = substr($selected_date, 0, 4);
for($i=1;$i<=12;$i++)
for($i = 1; $i <= 12; $i++)
{
$args = new stdClass();
$args->start_date = sprintf('%04d%02d00', $year, $i);
$args->end_date = sprintf('%04d%02d31', $year, $i);
if($site_srl)
{
$args->site_srl = $site_srl;
@ -243,23 +301,31 @@ class counterModel extends counter
if(!$isPageView)
{
$count = (int)$output->data->unique_visitor;
$count = (int) $output->data->unique_visitor;
}
else
{
$count = (int)$output->data->pageview;
$count = (int) $output->data->pageview;
}
$status->list[$i] = (int)$count;
if($count>$max) $max = $count;
$status->list[$i] = (int) $count;
if($count > $max)
{
$max = $count;
}
$sum += $count;
}
break;
case 'hour' :
for($i=0;$i<24;$i++)
for($i = 0; $i < 24; $i++)
{
$args = new stdClass();
$args->start_date = sprintf('%08d%02d0000', $selected_date, $i);
$args->end_date = sprintf('%08d%02d5959', $selected_date, $i);
if($site_srl)
{
$args->site_srl = $site_srl;
@ -270,21 +336,30 @@ class counterModel extends counter
$args->site_srl = 0;
$output = executeQuery('counter.getCounterLogStatus', $args);
}
$count = (int)$output->data->count;
$count = (int) $output->data->count;
$status->list[$i] = $count;
if($count>$max) $max = $count;
if($count > $max)
{
$max = $count;
}
$sum += $count;
}
break;
default :
default :
$year = substr($selected_date, 0, 4);
$month = substr($selected_date, 4, 2);
$end_day = date('t', mktime(0,0,0,$month,1,$year));
for($i=1;$i<=$end_day;$i++)
$end_day = date('t', mktime(0, 0, 0, $month, 1, $year));
for($i = 1; $i <= $end_day; $i++)
{
$args = new stdClass();
$args->start_date = sprintf('%04d%02d%02d', $year, $month, $i);
$args->end_date = sprintf('%04d%02d%02d', $year, $month, $i);
if($site_srl)
{
$args->site_srl = $site_srl;
@ -297,14 +372,20 @@ class counterModel extends counter
if(!$isPageView)
{
$count = (int)$output->data->unique_visitor;
$count = (int) $output->data->unique_visitor;
}
else
{
$count = (int)$output->data->pageview;
$count = (int) $output->data->pageview;
}
$status->list[$i] = $count;
if($count>$max) $max = $count;
if($count > $max)
{
$max = $count;
}
$sum += $count;
}
break;
@ -312,6 +393,7 @@ class counterModel extends counter
$status->max = $max;
$status->sum = $sum;
return $status;
}
@ -322,9 +404,9 @@ class counterModel extends counter
$output1 = $this->getHourlyStatus('week', $date1);
$tmp = array();
foreach($output1->list AS $key=>$value)
foreach($output1->list AS $key => $value)
{
$tmp["'".$key."'"] = $value;
$tmp["'" . $key . "'"] = $value;
}
$output1->list = $tmp;
@ -333,9 +415,9 @@ class counterModel extends counter
$output2 = $this->getHourlyStatus('week', $date2);
$tmp = array();
foreach($output2->list AS $key=>$value)
foreach($output2->list AS $key => $value)
{
$tmp["'".$key."'"] = $value;
$tmp["'" . $key . "'"] = $value;
}
$output2->list = $tmp;
@ -347,29 +429,30 @@ class counterModel extends counter
{
//for last week
$date1 = date('Ymd', strtotime('-1 week'));
$output1 = $this->getHourlyStatus('week', $date1, 0, true);
$output1 = $this->getHourlyStatus('week', $date1, 0, TRUE);
$tmp = array();
foreach($output1->list AS $key=>$value)
foreach($output1->list AS $key => $value)
{
$tmp["'".$key."'"] = $value;
$tmp["'" . $key . "'"] = $value;
}
$output1->list = $tmp;
//for this week
$date2 = date('Ymd');
$output2 = $this->getHourlyStatus('week', $date2, 0, true);
$output2 = $this->getHourlyStatus('week', $date2, 0, TRUE);
$tmp = array();
foreach($output2->list AS $key=>$value)
foreach($output2->list AS $key => $value)
{
$tmp["'".$key."'"] = $value;
$tmp["'" . $key . "'"] = $value;
}
$output2->list = $tmp;
$this->add('last_week', $output1);
$this->add('this_week', $output2);
}
}
/* End of file counter.model.php */
/* Location: ./modules/counter/counter.model.php */