XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
counter.model.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
3 
9 class counterModel extends counter
10 {
16  function init()
17  {
18  }
19 
26  function isLogged($site_srl = 0)
27  {
28  $args = new stdClass();
29  $args->regdate = date('Ymd');
30  $args->ipaddress = $_SERVER['REMOTE_ADDR'];
31  $args->site_srl = $site_srl;
32 
33  $iplogged = false;
34  $oCacheHandler = CacheHandler::getInstance('object');
35  if($oCacheHandler->isSupport())
36  {
37  $object_key = 'counter:' . $site_srl . '_' . str_replace(array('.', ':'), '-', $args->ipaddress);
38  $cache_key = $oCacheHandler->getGroupKey('counterIpLogged_' . $args->regdate, $object_key);
39  $iplogged = $oCacheHandler->get($cache_key);
40  }
41 
42  if($iplogged === false)
43  {
44  $output = executeQuery('counter.getCounterLog', $args);
45  if($output->data->count) $iplogged = TRUE;
46  }
47 
48  if($iplogged && $oCacheHandler->isSupport())
49  {
50  $oCacheHandler->put($cache_key, $iplogged);
51  }
52 
53  return $iplogged;
54  }
55 
62  function isInsertedTodayStatus($site_srl = 0)
63  {
64  $args = new stdClass;
65  $args->regdate = date('Ymd');
66 
67  $insertedTodayStatus = false;
68  $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE);
69  if($oCacheHandler->isSupport())
70  {
71  $cache_key = 'counter:insertedTodayStatus:' . $site_srl . '_' . $args->regdate;
72  $insertedTodayStatus = $oCacheHandler->get($cache_key);
73  }
74 
75  if($insertedTodayStatus === false)
76  {
77  if($site_srl)
78  {
79  $args->site_srl = $site_srl;
80  $output = executeQuery('counter.getSiteTodayStatus', $args);
81  }
82  else
83  {
84  $output = executeQuery('counter.getTodayStatus', $args);
85  }
86 
87  $insertedTodayStatus = !!$output->data->count;
88 
89  if($insertedTodayStatus && $oCacheHandler->isSupport())
90  {
91  $oCacheHandler->put($cache_key, TRUE);
92  $_old_date = date('Ymd', strtotime('-1 day'));
93  $oCacheHandler->delete('counter:insertedTodayStatus:' . $site_srl . '_' . $_old_date);
94  }
95  }
96 
97  return $insertedTodayStatus;
98  }
99 
107  function getStatus($selected_date, $site_srl = 0)
108  {
109  // If more than one date logs are selected
110  $args = new stdClass();
111  $args->regdate = is_array($selected_date) ? join(',', $selected_date) : $selected_date;
112  if($site_srl)
113  {
114  $args->site_srl = $site_srl;
115  $output = executeQuery('counter.getSiteCounterStatusDays', $args);
116  }
117  else
118  {
119  $output = executeQuery('counter.getCounterStatusDays', $args);
120  }
121  $status = $output->data;
122 
123  if(!is_array($selected_date))
124  {
125  return $status;
126  }
127 
128  if(!is_array($status)) $status = array($status);
129  $output = array();
130  foreach($status as $val)
131  {
132  $output[substr($val->regdate, 0, 8)] = $val;
133  }
134 
135  return $output;
136  }
137 
146  function getHourlyStatus($type = 'hour', $selected_date, $site_srl = 0, $isPageView = false)
147  {
148  $max = 0;
149  $sum = 0;
150 
151  switch($type)
152  {
153  case 'year' :
154  // Get a date to start counting
155  if($site_srl)
156  {
157  $args = new stdClass();
158  $args->site_srl = $site_srl;
159  $output = executeQuery('counter.getSiteStartLogDate', $args);
160  }
161  else
162  {
163  $output = executeQuery('counter.getStartLogDate');
164  }
165 
166  if(!($start_year = substr($output->data->regdate, 0, 4)))
167  {
168  $start_year = date("Y");
169  }
170 
171  for($i = $start_year, $y = date("Y"); $i <= $y; $i++)
172  {
173  $args = new stdClass();
174  $args->start_date = sprintf('%04d0000', $i);
175  $args->end_date = sprintf('%04d1231', $i);
176 
177  if($site_srl)
178  {
179  $args->site_srl = $site_srl;
180  $output = executeQuery('counter.getSiteCounterStatus', $args);
181  }
182  else
183  {
184  $output = executeQuery('counter.getCounterStatus', $args);
185  }
186 
187  $count = (int)($isPageView ? $output->data->pageview : $output->data->unique_visitor);
188  $status->list[$i] = $count;
189 
190  if($count > $max) $max = $count;
191 
192  $sum += $count;
193  }
194  break;
195 
196  case 'week' :
197  $time = strtotime($selected_date);
198  $w = date("D");
199 
200  while(date("D", $time) != "Sun")
201  {
202  $time += 60 * 60 * 24;
203  }
204 
205  $time -= 60 * 60 * 24;
206 
207  while(date("D", $time) != "Sun")
208  {
209  $thisWeek[] = date("Ymd", $time);
210  $time -= 60 * 60 * 24;
211  }
212 
213  $thisWeek[] = date("Ymd", $time);
214 
215  asort($thisWeek);
216 
217  foreach($thisWeek as $day)
218  {
219  $args = new stdClass();
220  $args->start_date = $day;
221  $args->end_date = $day;
222 
223  if($site_srl)
224  {
225  $args->site_srl = $site_srl;
226  $output = executeQuery('counter.getSiteCounterStatus', $args);
227  }
228  else
229  {
230  $output = executeQuery('counter.getCounterStatus', $args);
231  }
232 
233  $count = (int)($isPageView ? $output->data->pageview : $output->data->unique_visitor);
234  $status->list[$day] = $count;
235 
236  if($count > $max) $max = $count;
237 
238  $sum += $count;
239  }
240  break;
241 
242  case 'month' :
243  $year = substr($selected_date, 0, 4);
244  for($i = 1; $i <= 12; $i++)
245  {
246  $args = new stdClass();
247  $args->start_date = sprintf('%04d%02d00', $year, $i);
248  $args->end_date = sprintf('%04d%02d31', $year, $i);
249 
250  if($site_srl)
251  {
252  $args->site_srl = $site_srl;
253  $output = executeQuery('counter.getSiteCounterStatus', $args);
254  }
255  else
256  {
257  $output = executeQuery('counter.getCounterStatus', $args);
258  }
259 
260  $count = (int)($isPageView ? $output->data->pageview : $output->data->unique_visitor);
261  $status->list[$i] = $count;
262 
263  if($count > $max) $max = $count;
264 
265  $sum += $count;
266  }
267  break;
268 
269  case 'hour' :
270  for($i = 0; $i < 24; $i++)
271  {
272  $args = new stdClass();
273  $args->start_date = sprintf('%08d%02d0000', $selected_date, $i);
274  $args->end_date = sprintf('%08d%02d5959', $selected_date, $i);
275 
276  if($site_srl)
277  {
278  $args->site_srl = $site_srl;
279  $output = executeQuery('counter.getSiteCounterLogStatus', $args);
280  }
281  else
282  {
283  $args->site_srl = 0;
284  $output = executeQuery('counter.getCounterLogStatus', $args);
285  }
286 
287  $count = (int) $output->data->count;
288  $status->list[$i] = $count;
289 
290  if($count > $max) $max = $count;
291 
292  $sum += $count;
293  }
294  break;
295 
296  default :
297  $year = substr($selected_date, 0, 4);
298  $month = substr($selected_date, 4, 2);
299  $end_day = date('t', mktime(0, 0, 0, $month, 1, $year));
300 
301  for($i = 1; $i <= $end_day; $i++)
302  {
303  $args = new stdClass();
304  $args->start_date = sprintf('%04d%02d%02d', $year, $month, $i);
305  $args->end_date = sprintf('%04d%02d%02d', $year, $month, $i);
306 
307  if($site_srl)
308  {
309  $args->site_srl = $site_srl;
310  $output = executeQuery('counter.getSiteCounterStatus', $args);
311  }
312  else
313  {
314  $output = executeQuery('counter.getCounterStatus', $args);
315  }
316 
317  $count = (int)($isPageView ? $output->data->pageview : $output->data->unique_visitor);
318  $status->list[$i] = $count;
319 
320  if($count > $max) $max = $count;
321 
322  $sum += $count;
323  }
324  break;
325  }
326 
327  $status->max = $max;
328  $status->sum = $sum;
329 
330  return $status;
331  }
332 
333  public function getWeeklyUniqueVisitor()
334  {
335  //for last week
336  $date1 = date('Ymd', strtotime('-1 week'));
337  $output1 = $this->getHourlyStatus('week', $date1);
338 
339  $tmp = array();
340  foreach($output1->list as $key => $value)
341  {
342  $tmp["'" . $key . "'"] = $value;
343  }
344  $output1->list = $tmp;
345 
346  //for this week
347  $date2 = date('Ymd');
348  $output2 = $this->getHourlyStatus('week', $date2);
349 
350  $tmp = array();
351  foreach($output2->list as $key => $value)
352  {
353  $tmp["'" . $key . "'"] = $value;
354  }
355  $output2->list = $tmp;
356 
357  $this->add('last_week', $output1);
358  $this->add('this_week', $output2);
359  }
360 
361  public function getWeeklyPageView()
362  {
363  //for last week
364  $date1 = date('Ymd', strtotime('-1 week'));
365  $output1 = $this->getHourlyStatus('week', $date1, 0, TRUE);
366 
367  $tmp = array();
368  foreach($output1->list as $key => $value)
369  {
370  $tmp["'" . $key . "'"] = $value;
371  }
372  $output1->list = $tmp;
373 
374  //for this week
375  $date2 = date('Ymd');
376  $output2 = $this->getHourlyStatus('week', $date2, 0, TRUE);
377 
378  $tmp = array();
379  foreach($output2->list as $key => $value)
380  {
381  $tmp["'" . $key . "'"] = $value;
382  }
383  $output2->list = $tmp;
384 
385  $this->add('last_week', $output1);
386  $this->add('this_week', $output2);
387  }
388 
389 }
390 /* End of file counter.model.php */
391 /* Location: ./modules/counter/counter.model.php */
$output
Definition: ko.install.php:193
add($key, $val)
& getInstance($target= 'object', $info=null, $always_use_file=false)
isInsertedTodayStatus($site_srl=0)
getHourlyStatus($type= 'hour', $selected_date, $site_srl=0, $isPageView=false)
$args
Definition: ko.install.php:185
isLogged($site_srl=0)
getStatus($selected_date, $site_srl=0)
executeQuery($query_id, $args=NULL, $arg_columns=NULL)
Definition: func.inc.php:203