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');
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',
56 parent::__construct($assetid);
72 public function printVar($var, Array $args, $value_prefix=
'', $value_suffix=
'')
77 case 'asset_short_name' :
80 echo
'<?php echo htmlspecialchars(array_get_index($asset_info,"'.$var.
'")); ?>';
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.
'"))); ?>';
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) {
100 if (isset($args[
'label'])) {
101 echo
'echo "'.$args[
'label'].
'";';
112 $url = array_get_index($asset_info,\'asset_link\');
113 $position = strpos($url,"?");
114 if ($position !== false) {
115 $query_string = substr($url, $position+1);
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]));
129 case 'history_count
' :
130 echo '<?php echo $history_count; ?>
';
133 case 'add_to_history_button
' :
134 $area_id = $this->attr('id_name
');
135 $exclude_assets = $this->attr('exclude_assets
');
137 $show_button = FALSE;
138 $exclude_assets =
'.var_export($exclude_assets,TRUE).';
139 $excluded = in_array($ASSET->id, $exclude_assets);
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()])) {
145 foreach ($show_button_for_asset_types as $type => $inherit) {
146 if ($GLOBALS[
"SQ_SYSTEM"]->am->isTypeDecendant($ASSET->type(), $type)) {
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);
161 echo
'<?php $img_url = ""; ?>';
162 $image_id = $this->
attr(
'add_history_image');
163 if (!empty($image_id)) {
165 $img_url = $GLOBALS["SQ_SYSTEM"]->am->getAssetURL('.$image_id.
');
168 echo
'<img alt="" id="'.$area_id.
'_add_to_history_button" src="<?php echo $img_url ?>" onclick="'.$submit_str.
'" onmouseover="this.style.cursor=\'pointer\'" />';
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\')); ?>';
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)) {
184 $img_url = $GLOBALS["SQ_SYSTEM"]->am->getAssetURL('.$image_id.
');
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\'" />';
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();');
195 echo
'<?php $img_url = ""; ?>';
196 $image_id = $this->
attr(
'remove_history_image');
197 if (!empty($image_id)) {
199 $img_url = $GLOBALS["SQ_SYSTEM"]->am->getAssetURL('.$image_id.
');
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\'" />';
207 case (preg_match(
'/keyword_/',$var, $matches)? $var : !$var) :
208 $keyword = substr($var, 8);
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.
'");
225 parent::printVar($var, $args);
240 protected function _printContentItemCode(Array $element)
242 $area_id = $this->attr(
'id_name');
244 switch (array_get_index($element,
'operation')) {
246 echo
'<?php if (!isset($url)) {
247 $url = current_url();
249 include_once(SQ_SYSTEM_ROOT."/core/lib/html_form/html_form.inc");
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>
256 foreach($history as $position => $asset_info) {
259 if ($asset_contents = $this->attr(
'asset_contents')) {
260 foreach ($asset_contents as $asset_content) {
261 $this->_printContentItemCode($asset_content);
272 return parent::_printContentItemCode($element);
286 protected function _canInit()
288 return count($this->attr(
'asset_types')) && count($this->attr(
'asset_count'));
299 protected function _printInitCode()
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');
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;
320 if ($store_permanent) {
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.
'");
330 // initialise the history data
331 if (!isset($_SESSION["design_areas"]["'.$area_id.
'"])) {
332 $_SESSION["design_areas"]["'.$area_id.
'"] = Array();
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());
338 // check if this asset type is being tracked
339 $require_query_vars = array_get_index($allowed_asset_types, $ASSET->type());
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
345 $query_var = array_get_index($_SERVER, "QUERY_STRING");
346 if (!empty($query_var)) {
348 $query_var = "?".$query_var;
352 $current_url = current_url();
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) {
360 // now update history if necessary
363 if (!$auto_access_history) {
364 echo
'if (isset($_REQUEST["'.$area_id.
'_add_to_history"])) {';
367 // if this url already exists in history, remove it
368 $history_index = array_get_index($history_hash, $url);
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]);
378 if (count($history) > $max_asset_count) {
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();
392 array_unshift($history, $new_entry);
394 if (!$auto_access_history) echo
'}';
397 }//end if (!is_null(url))
398 }//end if (!is_null(require_query_vars))
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);
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();
409 // remove selected assets from history
410 if (isset($_REQUEST["'.$area_id.
'_rm_from_history"]) && !empty($_REQUEST["'.$area_id.
'_rm_from_history"])) {
412 foreach ($_REQUEST["'.$area_id.
'_rm_from_history"] as $remove_url) {
413 $history_index = array_get_index($history_hash, urldecode($remove_url));
415 if (!is_null($history_index)) {
416 unset($history[$history_index]);
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]);
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;
438 $_SESSION["design_areas"]["'.$area_id.
'"]["history"] = $history;
439 $_SESSION["design_areas"]["'.$area_id.
'"]["history_hash"] = $new_history_hash;
443 if ($store_permanent) {
445 // if current user logged in
446 if ($current_user && !($current_user instanceof Public_User)) {
447 _savePermanentAccessHistory("'.$area_id.
'",$_SESSION["design_areas"]["'.$area_id.
'"]);
452 if ($auto_access_history) {
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);
460 $history = array_slice($history, 0, $max_asset_count);
461 $history_count = count($history);
472 function _getPermanentAccessHistory ($area_id){
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";
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());
486 if (!isset($results[0])) return Array();
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,
496 return ($access_data);
510 function _savePermanentAccessHistory ($area_id, $access_data){
513 $user_id = $GLOBALS["SQ_SYSTEM"]->user->id;
514 $history = serialize($access_data["history"]);
515 $history_hash = serialize($access_data["history_hash"]);
518 $db = MatrixDAL::getDb();
521 if(!isset($access_data["fetched"])){
522 $db_action = "insert";
525 $db_action = "update";
528 $GLOBALS["SQ_SYSTEM"]->changeDatabaseConnection("db2");
529 $GLOBALS["SQ_SYSTEM"]->doTransaction("BEGIN");
531 switch ($db_action) {
534 sq_design_area_access_history
552 sq_design_area_access_history
555 history_hash = :history_hash
558 AND areaid = :areaid";
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());
574 $GLOBALS["SQ_SYSTEM"]->doTransaction("COMMIT");
575 $GLOBALS["SQ_SYSTEM"]->restoreDatabaseConnection();
593 public function getProtectedAttrs()
595 $res = parent::getProtectedAttrs();
596 $res[] =
'asset_contents';
618 public function setAttrValue($name, $value)
627 if (($name ==
'asset_types' || $name ==
'add_history_asset_types') && is_scalar($value)) {
628 $value = preg_split(
'/[ ,]+/', (
string)$value);
629 $processed_value = Array();
631 foreach ($value as $type_content) {
632 preg_match(
'/(?<type_code>\w+)(\[(?<parameters>[012])\])?/', $type_content, $matches);
634 if (!$GLOBALS[
'SQ_SYSTEM']->am->installed($matches[
'type_code'])) {
635 trigger_localised_error(
'CORE0145', E_USER_WARNING, $matches[
'type_code']);
638 if (empty($matches[
'parameters'])){
639 $matches[
'parameters'] = 0;
641 $processed_value[$matches[
'type_code']] = $matches[
'parameters'];
643 $value = $processed_value;
646 else if ($name ==
'exclude_assets' && is_scalar($value)) {
647 $value = preg_split(
'/[ ,]+/', (
string)$value);
649 return parent::setAttrValue($name, $value);