Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
MostVisitedPages.php
1 <?php
2 
3 /**
4  * Gentics Portal.Node PHP
5  * Author & Copyright (c) by Gentics Software GmbH
6  * sales@gentics.com
7  * http://www.gentics.com
8  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
9  * You must not use this software without a valid license agreement.
10  *
11  * Last visited pages
12  *
13  **/
14 class MostVisitedPages extends CJuiWidget
15 {
16 
17  /**
18  * Piwik-Token to identify
19  *
20  * @var string
21  **/
22  public $token;
23 
24  /**
25  * url to the Piwik-Service
26  *
27  * @var string
28  **/
29  public $piwikServerURL;
30 
31  /**
32  * commaseperated list of sites OR "all"
33  *
34  * @var string
35  **/
36  public $sites;
37 
38  /**
39  * Amount of displaying items
40  *
41  * @var string
42  **/
43  public $count;
44 
45  /**
46  * How long the Pages-Array will be cached
47  *
48  * @var string
49  **/
50  public $cacheTime;
51 
52  /**
53  * Find pages, display
54  *
55  **/
56  public function run()
57  {
58  if (empty($this->count))
59  $this->count = Yii::app()->getModule('piwik')->count;
60  if (empty($this->token))
61  $this->token = Yii::app()->getModule('piwik')->token;
62  if (empty($this->piwikServerURL))
63  $this->piwikServerURL = Yii::app()->getModule('piwik')->piwikServerURL;
64  if (empty($this->sites))
65  $this->sites = Yii::app()->getModule('piwik')->sites;
66  if (empty($this->cacheTime))
67  $this->cacheTime = Yii::app()->getModule('piwik')->cacheTime;
68 
69  $mostvisited = array();
70 
71  $check = Yii::app()->cache->get(md5($this->sites ."_popular"));
72 
73  if (!empty($check)){
74  $mostvisited = $check;
75  } else {
76 
77  $url = $this->piwikServerURL;
78  $url .= "/?module=API&method=Actions.getPageUrls";
79  $url .= "&idSite=".$this->sites."&period=month&date=yesterday";
80  $url .= "&format=PHP&filter_limit=".$this->count;
81  $url .= "&filter_sort_column=nb_hits&filter_sort_order=desc";
82  $url .= "&token_auth=".$this->token;
83 
84  $fetched = file_get_contents($url);
85  $content = unserialize($fetched);
86 
87  if ($this->sites == "all" || strstr($this->sites,',')){
88  foreach($content as $mysiteid => $mysite){
89  foreach($mysite as $myline){
90  $mostvisited[] = $this->calcresult($myline);
91  }
92  $key = md5($mysiteid ."_popular");
93  Yii::app()->cache->set($key, $mostvisited, $this->cacheTime);
94  }
95  $key = md5($this->sites ."_popular");
96  Yii::app()->cache->set($key, $mostvisited, $this->cacheTime);
97 
98  } else {
99  foreach($content as $myline){
100  $mostvisited[] = $this->calcresult($myline);
101  }
102  $key = md5($this->sites ."_popular");
103  Yii::app()->cache->set($key, $mostvisited, $this->cacheTime);
104 
105  }
106  }
107  $this->render($mostvisited);
108  }
109 
110  private function calcresult($line){
111  return array("url" => $line['url'], "name" => $line['label']);
112  }
113 
114  public function render($mostvisited){
115  foreach($mostvisited as $h){
116  $name = (!empty($h['name']) ? $h['name'] : $h['url']);
117  $cmsObject = Yii::app()->getComponent("repositoryApi")->getCmsObject($h['name'], array("name"));
118  if (!empty($cmsObject)){
119  $name = $cmsObject["attributes"]["name"];
120  }
121  echo "<li>".CHtml::link($name, $h['url'])."</li>";
122  }
123  }
124 }