Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Googlesitemaps.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  * Class for working with Gentics Content Connector
11  */
12 class Googlesitemaps extends CComponent
13 {
14 
15  /**
16  * @var string $rootfolderId contains the ID of the rootfolder.
17  */
18  public $rootfolderId;
19 
20  /**
21  * @var string $hostParam can be set to build absolute Urls (eg 'http://www.gentics.com')
22  */
23  public $hostParam;
24 
25  /**
26  * @var array $contentTypes contains the Type Id's which content to show (10007,10008,ect)
27  */
28  public $contentTypes;
29 
30  /**
31  * @var string $hideParam contains the Name of the object Parameter which is can be set to not show content
32  */
33  public $hideParam;
34 
35  /**
36  * @var string $containerParam contains the Name of the object Parameter which will be checked against the value to show the content
37  */
38  public $containerParam;
39 
40  /**
41  * @var string $containerParamValue contains the value the object Parameter need to have to show the content
42  */
43  public $containerParamValue;
44 
45  /**
46  * @var string $outputFormatValue sets the choosen Output Format in the View
47  */
48  public $outputFormatValue;
49 
50  /**
51  * @var int the number of seconds in which the cached value will expire. 0 means never expire.
52  */
53  public $cacheTime;
54 
55  /**
56  * @var string $gccRequestParams contains parameters that should be requested fron GCC
57  */
58  public $gccRequestParams;
59 
60  /**
61  * @var string $lang contains current languagecode
62  */
63  public $lang;
64 
65  /**
66  * array that contains categories tree
67  * @var $data
68  */
69 
70  public $data = array();
71 
72  /**
73  * @var string $sorting contains sorting expression
74  */
75  public $sorting;
76 
77 
78  /**
79  * Construction that will load nodes tree for given $folderIds
80  *
81  * @param array $folderIds contains folder IDs of content to show
82  *
83  * @return \personalizedContent
84  */
85  public function __construct($rootfolderId = "", $hostParam = null, $contentTypes = null, $hideParam = null, $containerParam = null, $containerParamValue = null, $outputFormatValue = "", $cacheTime = null, $gccRequestParams = null, $sorting = null)
86  {
87 
88  if ( $rootfolderId != "" ) {
89 
90  $this->lang = substr(Yii::app()->language, 0, 2);
91  $this->hostParam = isset($hostParam)? $hostParam : Yii::app()->getModule('googlesitemaps')->hostParam;
92  $this->contentTypes = isset($contentTypes)? $contentTypes : Yii::app()->getModule('googlesitemaps')->contentTypes;
93  $this->hideParam = isset($hideParam)? $hideParam : Yii::app()->getModule('googlesitemaps')->hideParam;
94  $this->containerParam = isset($containerParam)? $containerParam : Yii::app()->getModule('googlesitemaps')->containerParam;
95  $this->containerParamValue = isset($containerParamValue)? $containerParamValue : Yii::app()->getModule('googlesitemaps')->containerParamValue;
96  $this->cacheTime = isset($cacheTime)? $cacheTime : Yii::app()->getModule('googlesitemaps')->cacheTime;
97  $this->gccRequestParams = isset($gccRequestParams)? $gccRequestParams : Yii::app()->getModule('googlesitemaps')->gccRequestParams;
98  $this->sorting = isset($sorting)? $sorting : Yii::app()->getModule('googlesitemaps')->sorting;
99  $this->outputFormatValue = $outputFormatValue;
100  $this->hideParam = $hideParam;
101  $this->rootfolderId = $rootfolderId;
102  $this->_loadData();
103  //$this->_cleanUp(Yii::app()->user->id);
104 
105  } else {
106  return false;
107  }
108  }
109 
110  /**
111  * Filter tree by some schema
112  *
113  * @param integer $user_id Id of the user to personalize tree for
114  *
115  * @return array
116  */
117  private function _cleanUp($user_id)
118  {
119  $maxTimestamp = $this->loadMaxUpdatetimestamp();
120  $cacheId = 'googlesitemaps_PERS_'.md5($this->rootfolderId.implode(',', $this->contentTypes).$this->hideParam.$this->containerParam.$this->containerParamValue.$this->lang.$this->gccRequestParams.$this->sorting.$maxTimestamp);
121 
122  if (($data = Yii::app()->cache->get($cacheId)) === false) {
123  //get personalisation fields from settings
124  $filters = $this->getFiltersList();
125  $data = array();
126 
127  // apply filter here
128  if ($filters && $this->data) {
129  foreach ($this->data as $id=>$contentPage) {
130  if (!is_null($this->_unsetNodes($filters, $user_id, $contentPage))) {
131  $data[$id] = $contentPage;
132  }
133  }
134  }
135 
136  $this->data = $data;
137 
138  Yii::app()->cache->set($cacheId, $this->data);
139  } else {
140  $this->data = $data;
141  }
142  }
143 
144  /**
145  * Private method that will unset not allowed data
146  *
147  * @param array $filters contains keys that will be used as filters
148  * @param array $user_id contains user permissions
149  * @param array $data points to the array with nodes tree
150  *
151  * @return array|null
152  */
153  private function _unsetNodes($filters, $user_id, $data)
154  {
155  /* check permissions for current node */
156  $persAttributes = array();
157  foreach ($filters as $filter) {
158  if (!empty($data['attributes'][$filter])) {
159  $persAttributes = array_merge($persAttributes, $data['attributes'][$filter]);
160  }
161  }
162 
163  //check if page has allowed startpageurl and exlude the branch if it does not.
164  if ($data['contentid'] != in_array($data['contentid'], $this->folderIds)) {
165  $excludeURLs = Yii::app()->getModule('googlesitemaps')->excludeURLs;
166  if ((isset($data['attributes']['url']) && in_array($data['attributes']['url'], $excludeURLs)) || !isset($data['attributes']['url'])) {
167  return null;
168  }
169  }
170 
171  return $data;
172  }
173 
174  /**
175  * Filter tree by some schema
176  *
177  * @return array|bool
178  */
179  public function getFiltersList()
180  {
181  if (Yii::app()->getModule('contentSource')->contentSource->usePersonalisation) {
182  $filters = Yii::app()->getModule('contentSource')->contentSource->personalisationFields;
183  return $filters;
184  }
185 
186  return false;
187  }
188 
189  /**
190  * Load required tree data from cache. If not exists request Gentics Content Connector API for a new tree.
191  *
192  * @return array
193  */
194  private function _loadData()
195  {
196  $data = array();
197  //$filtersList = '';
198  $filter = '';
199 
200  $this->gccRequestParams = rtrim($this->gccRequestParams, ',');
201  $requertFields = explode(',', $this->gccRequestParams);
202 
203  $requertFields[] = 'updatetimestamp';
204  $requertFields[] = 'name';
205  $requertFields[] = 'url';
206 
207  $requertFields = array_unique($requertFields);
208 
209  // get max timestamp for given folderIds
210  //$maxTimestamp = $this->loadMaxUpdatetimestamp();
211  $maxTimestamp = $this->_getYoungestTimestamp($this->rootfolderId, 'googlesitemaps_UTS_'.md5($this->rootfolderId.implode(',', $this->contentTypes).$this->hideParam.$this->containerParam.$this->containerParamValue.$this->cacheTime.$this->lang.$this->gccRequestParams));
212 
213  //create cache key that includes folderIds+language+gccRequestString+timestamp of last updated folder;
214  $cacheId = 'googlesitemaps_'.md5($this->rootfolderId.implode(',', $this->contentTypes).$this->hideParam.$this->containerParam.$this->containerParamValue.$this->cacheTime.$this->lang.$this->gccRequestParams.$maxTimestamp);
215 
216  //Load data
217  if (($data = Yii::app()->cache->get($cacheId)) === false) {
218  // Build additional Filter
219  $filter = '(object.obj_type=="10002" OR object.obj_type=="'.implode('" OR object.obj_type=="', $this->contentTypes).'")';
220  if ($this->hideParam != "") {
221  $filter .= ' AND (object.'.$this->hideParam.' != "1")';
222  }
223  if ($this->containerParam != "" && $this->containerParamValue != "") {
224  $filter .= ' AND (object.'.$this->containerParam.' == "'.$this->containerParamValue.'")';
225  }
226  $params = array(
227  'rootfilter' => 'object.contentid=="'.$this->rootfolderId.'"',
228  'childfilter' => $filter,
229  'sorting' => $this->sorting,
230  'navigation' => 'true',
231  'type' => 'php',
232  'filter' => ''
233  );
234 
235  $params = Yii::app()->getComponent('repositoryApi')->addAdditionalParameters($params, 'contentRepositorySearch');
236 
237  $data = Yii::app()->repositoryApi->requestNavigation(
238  $params,
239  CMap::mergeArray($this->getFiltersList(), $requertFields),
240  "notimeout"
241  );
242  $data = unserialize($data);
243 
244  if (isset($data['status']) && $data['status'] == 'ok') {
245  unset($data['status']);
246  } else {
247  $data = array();
248  }
249 
250  Yii::app()->cache->set($cacheId, $data);
251  }
252  $this->data = $data;
253  }
254 
255  // Exchange getMaxUpdateTimestamp with call to API for "youngest"
256  private function _getYoungestTimestamp($objectId, $objectCacheId)
257  {
258  $cacheId = $objectCacheId;
259  $filter = '(object.obj_type=="10002" OR object.obj_type=="'.implode('" OR object.obj_type=="', $this->contentTypes).'")';
260  if ($this->hideParam != "") {
261  $filter .= ' AND (object.'.$this->hideParam.' != "1")';
262  }
263  if ($this->containerParam != "" && $this->containerParamValue != "") {
264  $filter .= ' AND (object.'.$this->containerParam.' == "'.$this->containerParamValue.'")';
265  }
266  if (($updateTimestamp = Yii::app()->cache->get($cacheId)) === false) {
267  $updateTimestamp = Yii::app()->repositoryApi->requestNavigation(
268  array(
269  "rootfilter" => 'object.contentid=="' . $objectId . '"',
270  "type" => "youngest",
271  "sorting" => $this->sorting,
272  "childfilter" => $filter
273  ),
274  array()
275  );
276  Yii::app()->cache->set($cacheId, $updateTimestamp, $this->getLatestUpdateTimestampCacheTime());
277  }
278  return $updateTimestamp;
279  }
280 
281  public function getLatestUpdateTimestampCacheTime()
282  {
283  return Yii::app()->getModule('navigation')->timestampCacheTime;
284  }
285 }