Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
PersonalizedContentWidget.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  * Widget for viewing navigation tree
11  */
12 
14 {
15  /**
16  * @var $folderIds array contains folder IDs of content to show
17  */
18  public $folderIds = array();
19 
20  /**
21  * @var $tplString string template string for repeating content
22  */
23  public $tplString = '';
24 
25  /**
26  * language
27  * @var lang
28  */
29  public $lang = '';
30 
31  /**
32  * How many results should be skipped. Used fot pagination
33  * @var string
34  */
35  public $offset = 0;
36 
37  /**
38  * total matches found, exlc. status
39  * @var int
40  */
41  public $totalQty = 0;
42 
43  /**
44  * how many results show per page
45  * @var int
46  */
47  public $perPage = null;
48 
49  /**
50  * Base href for current page
51  * @var int
52  */
53  public $baseHref = '';
54 
55  /**
56  * @var bool $usePersonalisation defines if personalisation should be performed
57  */
58  public $usePersonalisation = true;
59  /**
60  * @var string GCC request params. Comma separated.
61  */
62  public $gccRequestParams;
63 
64  /**
65  * @var string $sorting contains sorting expression
66  */
67  public $sorting;
68  /**
69  * Initializes the widget.
70  *
71  * @return void
72  */
73  public function init()
74  {
75  //die('this is init');
76  }
77 
78  /**
79  * renders view
80  *
81  * @throws Exception
82  * @return void
83  */
84  public function run()
85  {
86  $tag = '';
87  if (!empty($_GET['folderIds']) && !empty($_GET['tag'])) {
88  preg_match_all('/([0-9]+)/', $_GET['folderIds'], $folderIds);
89 
90  array_walk(
91  $folderIds[0],
92  function(&$item)
93  {
94  $item = '10002.'.$item;
95  }
96  );
97 
98  $this->folderIds = $folderIds[0];
99  $tag = $_GET['tag'];
100  }
101  /*
102  * Get values from widget option for PersonalizedContent
103  * if it is set otherwise it will be taken from module
104  */
105  $gccRequestParams = null;
106  if(isset($this->gccRequestParams)){
107  $gccRequestParams = $this->gccRequestParams;
108  }
109  $sorting = null;
110  if(isset($this->sorting)){
111  $sorting = $this->sorting;
112  }
113 
114  if (!empty($this->folderIds) && !empty($this->tplString)) {
115  $content = new PersonalizedContent($this->folderIds, $this->usePersonalisation, $tag, $gccRequestParams , $sorting);
116  } else {
117  throw new Exception('PersonalizedContentWidget error: folderIds and tplString are required parameters and can`t be blank ');
118  }
119 
120  $this->loadBaseHref();
121  $this->offset = (int)Yii::app()->request->getQuery('offset', 0);
122 
123  if (!empty($content->data)) {
124  $this->totalQty = count($content->data);
125  $this->perPage = (isset($this->perPage) && $this->perPage > 0)? $this->perPage : Yii::app()->getModule('personalizedContent')->perPage;
126 
127  /* check if offet is in range of results quantity */
128  if ($this->offset>=$this->totalQty || $this->offset<0) {
129  $this->offset = 0;
130  }
131 
132  /* calculating pagination */
133  $content->data = array_slice($content->data, $this->offset, $this->perPage);
134 
135  $this->lang = substr(Yii::app()->language, 0, 2);
136  $this->render('PersonalizedContentWidget', array('contentList'=>$content->data));
137  }
138  }
139 
140  /**
141  * Finds out current base href
142  *
143  * @return void
144  */
145  public function loadBaseHref()
146  {
147  $this->baseHref = Yii::app()->request->pathInfo;
148 
149  if (!empty(Yii::app()->request->queryString)) {
150  $getParams = explode('&', Yii::app()->request->queryString);
151 
152  $params = array();
153  foreach ($getParams as $param) {
154  $param = explode('=', $param);
155  if ($param[0]=='offset') {
156  continue;
157  }
158  $params[] = $param[0].'='.$param[1];
159  }
160  if (!empty($params)) {
161  $this->baseHref .= '?'.implode('&', $params).'&';
162  } else {
163  $this->baseHref .= '?';
164  }
165  } else {
166  $this->baseHref .= '?';
167  }
168 
169  $this->baseHref = Yii::app()->CreateUrl($this->baseHref);
170  }
171 }