Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design_area_access_history.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/designs/design_area/design_area.inc';
19 define('SQ_DESIGN_AREA_HISTORY_STATUS_IGNORE', '0');
20 define('SQ_DESIGN_AREA_HISTORY_STATUS_PRESENT', '1');
21 define('SQ_DESIGN_AREA_HISTORY_STATUS_ABSENT', '2');
22 
35 {
36 
41  public $request_types = Array(
42  SQ_DESIGN_AREA_HISTORY_STATUS_IGNORE => 'Ignore',
43  SQ_DESIGN_AREA_HISTORY_STATUS_PRESENT => 'Must exist',
44  SQ_DESIGN_AREA_HISTORY_STATUS_ABSENT => 'Must NOT exist',
45  );
46 
47 
54  function __construct($assetid=0)
55  {
56  parent::__construct($assetid);
57 
58  }//end constructor
59 
60 
72  public function printVar($var, Array $args, $value_prefix='', $value_suffix='')
73  {
74  switch ($var) {
75  case 'assetid' :
76  case 'asset_link' :
77  case 'asset_short_name' :
78  case 'asset_name' :
79  case 'visit_count' :
80  echo '<?php echo htmlspecialchars(array_get_index($asset_info,"'.$var.'")); ?>';
81  break;
82  case 'access_time' :
83  case 'last_visit_time' :
84  case 'first_visit_time' :
85  echo '<?php echo date("'.$this->attr('time_format').'", htmlspecialchars(array_get_index($asset_info,"'.$var.'"))); ?>';
86  break;
87 
88  case 'has_update' :
89  echo '<?php
90  // has_update
91  $assetid = array_get_index($asset_info, "assetid");
92  if (!empty($assetid)) {
93  $last_visit_time = array_get_index($asset_info, "last_visit_time");
94  // get asset updated time
95  $upasset = $GLOBALS["SQ_SYSTEM"]->am->getAsset($assetid);
96  if (!is_null($upasset)) {
97  $updated = $upasset->updated;
98  if ($last_visit_time < $updated) {
99  ';
100  if (isset($args['label'])) {
101  echo 'echo "'.$args['label'].'";';
102  }
103  echo '
104  }
105  }
106  }
107  ?>';
108  break;
109 
110  case 'query_var' :
111  echo '<?php
112  $url = array_get_index($asset_info,\'asset_link\');
113  $position = strpos($url,"?");
114  if ($position !== false) {
115  $query_string = substr($url, $position+1);
116  $matches = Array();
117  preg_match_all(\'(([^=^&]+)=([^&]+))is\', $query_string, $matches);
118  foreach ($matches[1] as $key => $value) {
119  if ($value == \''.$args['name'].'\') {
120  echo htmlspecialchars(urldecode($matches[2][$key]));
121  break;
122  }
123  }
124  }
125  ?>';
126  break;
127 
128 
129  case 'history_count' :
130  echo '<?php echo $history_count; ?>';
131  break;
132 
133  case 'add_to_history_button' :
134  $area_id = $this->attr('id_name');
135  $exclude_assets = $this->attr('exclude_assets');
136  echo '<?php
137  $show_button = FALSE;
138  $exclude_assets = '.var_export($exclude_assets,TRUE).';
139  $excluded = in_array($ASSET->id, $exclude_assets);
140  if (!$excluded) {
141  $show_button_for_asset_types = '.var_export($this->attr('add_history_asset_types'), TRUE).';
142  if (isset($show_button_for_asset_types[$ASSET->type()])) {
143  $show_button = TRUE;
144  } else {
145  foreach ($show_button_for_asset_types as $type => $inherit) {
146  if ($GLOBALS["SQ_SYSTEM"]->am->isTypeDecendant($ASSET->type(), $type)) {
147  $show_button = TRUE;
148  break;
149  }
150  }
151  }
152  }
153  ?>';
154  echo '<?php if ($show_button) { ?>';
155  echo '<form action="<?php echo $url; ?>" method="post" id="'.$area_id.'_add_form">';
156  echo '<div><input type="hidden" value="add" name="'.$area_id.'_add_to_history" /></div>';
157  $submit_str = 'document.getElementById(\''.$area_id.'_add_form\').submit();';
158  if (!$this->attr('add_history_button_image')) {
159  normal_button($area_id.'_add_to_history_button', $this->attr('add_history_button_label'), $submit_str);
160  } else {
161  echo '<?php $img_url = ""; ?>';
162  $image_id = $this->attr('add_history_image');
163  if (!empty($image_id)) {
164  echo '<?php
165  $img_url = $GLOBALS["SQ_SYSTEM"]->am->getAssetURL('.$image_id.');
166  ?>';
167  }
168  echo '<img alt="" id="'.$area_id.'_add_to_history_button" src="<?php echo $img_url ?>" onclick="'.$submit_str.'" onmouseover="this.style.cursor=\'pointer\'" />';
169  }
170  echo '</form>';
171  echo '<?php } ?>';
172  break;
173 
174  case 'remove_from_history_checkbox' :
175  echo '<?php check_box("'.$this->attr('id_name').'_rm_from_history[]", array_get_index($asset_info,\'asset_link\')); ?>';
176  break;
177 
178  case 'remove_from_history_image' :
179  $area_id = $this->attr('id_name');
180  echo '<?php $img_url = ""; ?>';
181  $image_id = $this->attr('remove_history_image_small');
182  if (!empty($image_id)) {
183  echo '<?php
184  $img_url = $GLOBALS["SQ_SYSTEM"]->am->getAssetURL('.$image_id.');
185  ?>';
186  }
187  echo '<img alt="" id="'.$area_id.'_rm_from_history_<?php echo array_get_index($asset_info,\'assetid\'); ?>" src="<?php echo $img_url ?>" onclick="document.getElementById(\''.$area_id.'_form_remove_asset\').value=\'<?php echo array_get_index($asset_info,\'asset_link\'); ?>\'; document.getElementById(\''.$area_id.'_form\').submit();" onmouseover="this.style.cursor=\'pointer\'" />';
188  break;
189 
190  case 'remove_from_history_button' :
191  $area_id = $this->attr('id_name');
192  if (!$this->attr('remove_history_button_image')) {
193  normal_button($area_id.'_remove_from_history_button', $this->attr('remove_history_button_label'), 'document.getElementById(\''.$area_id.'_form\').submit();');
194  } else {
195  echo '<?php $img_url = ""; ?>';
196  $image_id = $this->attr('remove_history_image');
197  if (!empty($image_id)) {
198  echo '<?php
199  $img_url = $GLOBALS["SQ_SYSTEM"]->am->getAssetURL('.$image_id.');
200  ?>';
201  }
202  echo '<img name="'.$area_id.'_remove_from_history_button" src="<?php echo $img_url ?>" onclick="document.getElementById(\''.$area_id.'_form\').submit();" onmouseover="this.style.cursor=\'pointer\'" />';
203  }
204  break;
205 
206  //looking for asset keyword
207  case (preg_match('/keyword_/',$var, $matches)? $var : !$var) :
208  $keyword = substr($var, 8);
209  // Get keyword replacement only if the asset still exists
210  echo '<?php
211  $assetid = array_get_index($asset_info, "assetid");
212  if (!empty($assetid)) {
213  $asset = $GLOBALS["SQ_SYSTEM"]->am->getAsset($assetid, "", TRUE);
214  if (empty($asset) === FALSE) {
215  $replacement = $asset->getKeywordReplacement("'.$keyword.'");
216  } else {
217  $replacement = "";
218  }
219  echo $replacement;
220  }
221  ?>';
222  break;
223 
224  default :
225  parent::printVar($var, $args);
226 
227  }//end switch
228 
229  }//end printVar()
230 
231 
240  protected function _printContentItemCode(Array $element)
241  {
242  $area_id = $this->attr('id_name');
243 
244  switch (array_get_index($element, 'operation')) {
245  case 'asset' :
246  echo '<?php if (!isset($url)) {
247  $url = current_url();
248  }
249  include_once(SQ_SYSTEM_ROOT."/core/lib/html_form/html_form.inc");
250  ?>';
251  echo '
252  <form action="<?php echo $url; ?>" method="post" id="'.$area_id.'_form">
253  <div><input type="hidden" id="'.$area_id.'_form_remove_asset" name="'.$area_id.'_form_remove_asset" value="" /></div>
254 
255  <?php
256  foreach($history as $position => $asset_info) {
257  ?>
258  ';
259  if ($asset_contents = $this->attr('asset_contents')) {
260  foreach ($asset_contents as $asset_content) {
261  $this->_printContentItemCode($asset_content);
262  }
263  }
264  echo '
265  <?php
266  }//end foreach
267  ?>
268  </form>
269  ';
270  return TRUE;
271  default :
272  return parent::_printContentItemCode($element);
273  }//end switch
274 
275  }//end _printContentItemCode()
276 
277 
286  protected function _canInit()
287  {
288  return count($this->attr('asset_types')) && count($this->attr('asset_count'));
289 
290  }//end _canInit()
291 
292 
299  protected function _printInitCode()
300  {
301  $area_id = $this->attr('id_name');
302  $exclude_assets = $this->attr('exclude_assets');
303  $this_types = $this->attr('asset_types');
304  $max_count = $this->attr('asset_count');
305  $time_format = $this->attr('time_format');
306  $auto_access_history = $this->attr('auto_access_history');
307  $store_permanent = $this->attr('store_permanent');
308 
309  echo '
310  <?php
311  $exclude_assets = '.var_export($exclude_assets,TRUE).';
312  $allowed_asset_types = '.var_export($this_types,TRUE).';
313  $max_asset_count = '.$max_count.';
314  $current_user = $GLOBALS["SQ_SYSTEM"]->user;
316 
317  ';
318 
319  // if it is stored permanently, we will try to fetch it from db for the first access
320  if ($store_permanent) {
321  echo '
322  // if current user is logged in and the access history has not been fetched yet
323  if(!isset($_SESSION["design_areas"]["'.$area_id.'"]["fetched"]) && $current_user && !($current_user instanceof Public_User)) {
324  $_SESSION["design_areas"]["'.$area_id.'"] = _getPermanentAccessHistory("'.$area_id.'");
325  }
326  ';
327  }
328  echo '
329 
330  // initialise the history data
331  if (!isset($_SESSION["design_areas"]["'.$area_id.'"])) {
332  $_SESSION["design_areas"]["'.$area_id.'"] = Array();
333  }
334  $saved_data = $_SESSION["design_areas"]["'.$area_id.'"];
335  $history = array_get_index($saved_data, "history", Array());
336  $history_hash = array_get_index($saved_data, "history_hash", Array());
337 
338  // check if this asset type is being tracked
339  $require_query_vars = array_get_index($allowed_asset_types, $ASSET->type());
340 
341  $excluded = in_array($ASSET->id, $exclude_assets);
342  if (!is_null($require_query_vars) && (!$excluded)) {
343  // check how existence of query vars influences history recording
344  $url = null;
345  $query_var = array_get_index($_SERVER, "QUERY_STRING");
346  if (!empty($query_var)) {
347  $query = TRUE;
348  $query_var = "?".$query_var;
349  } else {
350  $query = false;
351  }
352  $current_url = current_url();
353 
354  if ((($require_query_vars == '.SQ_DESIGN_AREA_HISTORY_STATUS_PRESENT.') && $query) || ($require_query_vars == '.SQ_DESIGN_AREA_HISTORY_STATUS_IGNORE.')) {
355  $url = $current_url.$query_var;
356  } else if (($require_query_vars == '.SQ_DESIGN_AREA_HISTORY_STATUS_ABSENT.') && !$query) {
357  $url = $current_url;
358  }
359 
360  // now update history if necessary
361  if (!is_null($url)){
362  ';
363  if (!$auto_access_history) {
364  echo 'if (isset($_REQUEST["'.$area_id.'_add_to_history"])) {';
365  }
366  echo '
367  // if this url already exists in history, remove it
368  $history_index = array_get_index($history_hash, $url);
369 
370  $visit_count = 1;
371  $first_access_time = time();
372  if (!is_null($history_index)) {
373  $visit_count = array_get_index($history[$history_index], "visit_count", 0);
374  $first_access_time = array_get_index($history[$history_index], "first_visit_time", $first_access_time);
375  unset($history[$history_index]);
376  }
377 
378  if (count($history) > $max_asset_count) {
379  array_pop($history);
380  }
381 
382  // create a new history entry
383  $new_entry["assetid"] = $ASSET->id;
384  $new_entry["asset_link"] = $url;
385  $new_entry["asset_name"] = $ASSET->name;
386  $new_entry["asset_short_name"] = $ASSET->short_name;
387  $new_entry["access_time"] = time();
388  $new_entry["visit_count"] = $visit_count;
389  $new_entry["first_visit_time"] = $first_access_time;
390  $new_entry["last_visit_time"] = time();
391 
392  array_unshift($history, $new_entry);
393  ';
394  if (!$auto_access_history) echo '}';
395 
396  echo '
397  }//end if (!is_null(url))
398  }//end if (!is_null(require_query_vars))
399 
400 
401  // if current page is in history update its details like visit count, access time etc
402  $history_index = array_get_index($history_hash, current_url(), NULL);
403 
404  if (!is_null($history_index)) {
405  $history[$history_index]["visit_count"] = array_get_index($history[$history_index], "visit_count", 0) + 1;
406  $history[$history_index]["last_visit_time"] = time();
407  }
408 
409  // remove selected assets from history
410  if (isset($_REQUEST["'.$area_id.'_rm_from_history"]) && !empty($_REQUEST["'.$area_id.'_rm_from_history"])) {
411 
412  foreach ($_REQUEST["'.$area_id.'_rm_from_history"] as $remove_url) {
413  $history_index = array_get_index($history_hash, urldecode($remove_url));
414 
415  if (!is_null($history_index)) {
416  unset($history[$history_index]);
417  }
418  }
419  }
420 
421  if (isset($_REQUEST["'.$area_id.'_form_remove_asset"]) && !empty($_REQUEST["'.$area_id.'_form_remove_asset"])) {
422  $remove_url = $_REQUEST["'.$area_id.'_form_remove_asset"];
423  $history_index = array_get_index($history_hash, urldecode($remove_url));
424  if (!is_null($history_index)) {
425  unset($history[$history_index]);
426  }
427  }
428 
429  // update history hash
430  $new_history_hash = Array();
431  foreach ($history as $index => $contents) {
432  if (isset($contents["asset_link"])) {
433  $new_history_hash[$contents["asset_link"]] = $index;
434  }
435  }
436 
437 
438  $_SESSION["design_areas"]["'.$area_id.'"]["history"] = $history;
439  $_SESSION["design_areas"]["'.$area_id.'"]["history_hash"] = $new_history_hash;
440 
441  ';
442  // if store permanently, we have to save the access history data to db
443  if ($store_permanent) {
444  echo '
445  // if current user logged in
446  if ($current_user && !($current_user instanceof Public_User)) {
447  _savePermanentAccessHistory("'.$area_id.'",$_SESSION["design_areas"]["'.$area_id.'"]);
448  }
449  ';
450  }
451 
452  if ($auto_access_history) {
453  echo '
454  // now that the session is saved, we want to remove the first item because
455  // we do not want to list the current page
456  if (!$excluded) array_shift($history);
457  ';
458  }
459  echo '
460  $history = array_slice($history, 0, $max_asset_count);
461  $history_count = count($history);
462 
463 
472  function _getPermanentAccessHistory ($area_id){
473 
474  $user_id = $GLOBALS["SQ_SYSTEM"]->user->id;
475  $db = MatrixDAL::getDb();
476  $sql = "SELECT history, history_hash FROM sq_design_area_access_history where userid = :userid AND areaid = :areaid";
477 
478  try {
479  $query = MatrixDAL::preparePdoQuery($sql);
480  MatrixDAL::bindValueToPdo($query, "userid", $user_id, PDO::PARAM_STR);
481  MatrixDAL::bindValueToPdo($query, "areaid", $area_id, PDO::PARAM_STR);
482  $results = MatrixDAL::executePdoAll($query);
483  } catch (Exception $e) {
484  throw new Exception("Unable to retrieve access history data from database, due to database error: ".$e->getMessage());
485  }
486  if (!isset($results[0])) return Array();
487 
488  $history = unserialize($results[0]["history"]);
489  $history_hash = unserialize($results[0]["history_hash"]);
490  $access_data = Array (
491  "history" => $history,
492  "history_hash" => $history_hash,
493  "fetched" => TRUE,
494  );
495 
496  return ($access_data);
497 
498  }
499 
500 
510  function _savePermanentAccessHistory ($area_id, $access_data){
511 
512 
513  $user_id = $GLOBALS["SQ_SYSTEM"]->user->id;
514  $history = serialize($access_data["history"]);
515  $history_hash = serialize($access_data["history_hash"]);
516 
517 
518  $db = MatrixDAL::getDb();
519 
520 
521  if(!isset($access_data["fetched"])){
522  $db_action = "insert";
523  }
524  else {
525  $db_action = "update";
526  }
527 
528  $GLOBALS["SQ_SYSTEM"]->changeDatabaseConnection("db2");
529  $GLOBALS["SQ_SYSTEM"]->doTransaction("BEGIN");
530 
531  switch ($db_action) {
532  case "insert":
533  $sql = "INSERT INTO
534  sq_design_area_access_history
535  (
536  userid,
537  areaid,
538  history,
539  history_hash
540  )
541  VALUES
542  (
543  :userid,
544  :areaid,
545  :history,
546  :history_hash
547  )";
548 
549  break;
550  case "update":
551  $sql = "UPDATE
552  sq_design_area_access_history
553  SET
554  history = :history,
555  history_hash = :history_hash
556  WHERE
557  userid = :userid
558  AND areaid = :areaid";
559 
560  break;
561  }//end switch
562 
563  try {
564  $query = MatrixDAL::preparePdoQuery($sql);
565  MatrixDAL::bindValueToPdo($query, "userid", $user_id);
566  MatrixDAL::bindValueToPdo($query, "areaid", $area_id);
567  MatrixDAL::bindValueToPdo($query, "history", $history);
568  MatrixDAL::bindValueToPdo($query, "history_hash", $history_hash);
569  $result = MatrixDAL::execPdoQuery($query);
570  } catch (Exception $e) {
571  throw new Exception("Unable to save access history to database, due to the following database error:".$e->getMessage());
572  }//end try catch
573 
574  $GLOBALS["SQ_SYSTEM"]->doTransaction("COMMIT");
575  $GLOBALS["SQ_SYSTEM"]->restoreDatabaseConnection();
576 
577  }
578  ?>
579  ';
580 
581  }//end _printInitCode()
582 
583 
593  public function getProtectedAttrs()
594  {
595  $res = parent::getProtectedAttrs();
596  $res[] = 'asset_contents';
597  return $res;
598 
599  }//end getProtectedAttrs()
600 
601 
602 
603 
604 
618  public function setAttrValue($name, $value)
619  {
620 
621  //parse attr such as asset_types, add_history_types
622  // For asset_types, e.g folder[1] means asset type is foler, parameters must exists.
623  //The digit means the parameters requirements , 0=> ignore, 1=> must exists,2=>must not exists
624  //For add_history_asset_types , e.g trigger_action_add_url[0] means Add URL, not inherit.
625  //Digit means inherit or not
626 
627  if (($name == 'asset_types' || $name == 'add_history_asset_types') && is_scalar($value)) {
628  $value = preg_split('/[ ,]+/', (string)$value);
629  $processed_value = Array();
630 
631  foreach ($value as $type_content) {
632  preg_match('/(?<type_code>\w+)(\[(?<parameters>[012])\])?/', $type_content, $matches);
633 
634  if (!$GLOBALS['SQ_SYSTEM']->am->installed($matches['type_code'])) {
635  trigger_localised_error('CORE0145', E_USER_WARNING, $matches['type_code']);
636  return FALSE;
637  }
638  if (empty($matches['parameters'])){
639  $matches['parameters'] = 0;
640  }
641  $processed_value[$matches['type_code']] = $matches['parameters'];
642  }
643  $value = $processed_value;
644  }
645 
646  else if ($name == 'exclude_assets' && is_scalar($value)) {
647  $value = preg_split('/[ ,]+/', (string)$value);
648  }
649  return parent::setAttrValue($name, $value);
650 
651  }//end setAttrValue()
652 
653 }//end class
654 ?>