Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
VisitedHistory.php
1 <?php
2 /**
3  * Class for handling users last visited pages
4  *
5  * @packaged default
6  * @author Me
7  **/
9 {
10  public static function saveUrlToHistory($event)
11  {
12  self::addItem(Yii::app()->request->getUrl());
13  }
14 
15  public static function getCacheKey($collection_id){
16 
17  return md5($collection_id ."_history_url");
18  }
19 
20  public static function addItem($url, $title = null, $page_id = null, $collection_id = null)
21  {
22  if ($collection_id === null) {
23  $collection_id = self::getCurrentCollectionId();
24  }
25  $key = self::getCacheKey($collection_id);
26  $userHistory = Yii::app()->cache->get($key);
27  if($userHistory === false){
28  $userHistory = array();
29  }
30  $itemExists = false;
31  foreach($userHistory as $k => $uh){
32  if ($uh['url'] == $url){
33  $userHistory[$k]['timestamp'] = time();
34  $itemExists = true;
35  }
36  }
37  if(!$itemExists){
38  $userHistory[] = array('url' => $url, 'title' => $title, 'page_id' => $page_id, 'collection_id' => $collection_id, 'timestamp' => time());
39  }
40  Yii::app()->cache->set($key, $userHistory, Yii::app()->getModule('history')->cacheTime);
41  }
42 
43  public static function getItems($collection_id = null, $count = null)
44  {
45  if ($collection_id === null) {
46  $collection_id = self::getCurrentCollectionId();
47  }
48  $key = md5($collection_id ."_history_url");
49  $userHistory = Yii::app()->cache->get($key);
50  if($userHistory === false){
51  return array();
52  }
53 
54  foreach ($userHistory as $key => $val) {
55  $time[$key] = $val['timestamp'];
56  }
57  array_multisort($time, SORT_ASC, $userHistory);
58 
59  if($count === null) {
60  $count = count($userHistory);
61  }
62  return array_slice(array_reverse($userHistory),0,$count);
63  }
64 
65 
66  /**
67  * Flush user history
68  *
69  * @return void
70  * @author Me
71  **/
72  public static function flush($collection_id = null)
73  {
74  if ($collection_id === null) {
75  $collection_id = self::getCurrentCollectionId();
76  }
77  $key = md5($collection_id ."_history_url");
78  $userHistory = Yii::app()->cache->delete($key);
79  }
80 
81  /**
82  * Get current user id
83  *
84  * @return string
85  **/
86  public static function getCurrentCollectionId()
87  {
88  return Yii::app()->user->isGuest ? Yii::app()->session->getSessionId() : Yii::app()->user->id;
89  }
90 }