Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_link_manager.inc
1 <?php
17 require_once SQ_PACKAGES_PATH.'/cms/page_templates/page_asset_listing/page_asset_listing.inc';
18 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
19 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
20 
21 define('SQ_PAGE_LINK_MGR_PRESET_LINKED', 'Linked');
22 
35 {
36 
37  var $bodycopies = Array(
38  'logged_in' => 'Logged In Layout',
39  'not_logged_in' => 'Not Logged In Layout',
40  );
41 
42  var $errors;
43 
44  var $messages;
45 
46 
53  function __construct($assetid=0)
54  {
55  $this->_ser_attrs = TRUE;
56  parent::__construct($assetid);
57 
58  }//end constructor
59 
60 
69  function _createBodycopies()
70  {
71  if (!parent::_createBodycopies()) return FALSE;
72 
73  // add a new 'not_logged_in' bodycopy
74  $asset = new Bodycopy();
75  $copy_link = Array(
76  'asset' => &$this,
77  'value' => 'not_logged_in',
78  'link_type' => SQ_LINK_TYPE_2,
79  'is_dependant' => 1,
80  'is_exclusive' => 1,
81  );
82  $args = Array('content' => $this->_getDefaultBodycopyContent('not_logged_in'));
83  $asset->setAttrValue('name', 'Page Contents (Not Logged In)');
84  return $asset->create($copy_link, $args);
85 
86  }//end _createBodycopies()
87 
88 
97  function _getDefaultBodycopyContent($bodycopy_code)
98  {
99  switch ($bodycopy_code) {
100  case 'not_logged_in':
101  return '<p>'.translate('cms_link_manager_not_logged_in_msg').'</p>';
102  case 'page_contents':
103  return '<h2>'.translate('cms_link_manager_managing_links_for', '%managed_asset_asset_name%').'</h2>%asset_listing%<p>%submit_button%</p>';
104  case 'default_format':
105  return '%Linked_asset_selection% %asset_name%';
106  }
107  return parent::_getDefaultBodycopyContent($bodycopy_code);
108 
109  }//end _getDefaultBodycopyContent()
110 
111 
119  function _getAllowedLinks()
120  {
121  $page_links = parent::_getAllowedLinks();
122  $page_links[SQ_LINK_TYPE_2]['bodycopy'] = Array('card' => 3, 'exclusive' => FALSE);
123  return $page_links;
124 
125  }//end _getAllowedLinks()
126 
127 
138  protected function _getName($short_name=FALSE, $contextid=NULL)
139  {
140  // No context specified, using the current context
141  if ($contextid === NULL) {
142  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
143  }//end if
144 
145  // Obtain the attribute value for Name from the specified Context
146  $attr = ($short_name) ? 'short_name' : 'name';
147  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName($attr, $this->type(), Array($this->id), $contextid);
148  if (empty($values) === TRUE) {
149  return parent::_getName($short_name, $contextid);
150  } else {
151  return $values[$this->id];
152  }
153 
154  }//end _getName()
155 
156 
157 //-- Linking Functions --//
158 
159 
171  function &_getAssetLinks(&$asset, $no_cache=FALSE)
172  {
173  if (is_null($asset)) {
174  $this->_tmp['linked_selections'] = Array();
175  }
176 
177  if (!isset($this->_tmp['linked_selections'])) {
178 
179  // get the asset links
180  $asset_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, $this->attr('link_type'), '', FALSE, 'minor', NULL, NULL, NULL, NULL, SQ_PERMISSION_WRITE);
181 
182  // gather the root nodes
183  $root_asset_ids = $this->getRootNodes();
184  $valid_nodes = Array();
185  foreach ($root_asset_ids as $root_asset_id) {
186  $new_valid_nodes = array_keys($GLOBALS['SQ_SYSTEM']->am->getChildren($root_asset_id, array_keys($this->attr('types')), FALSE));
187  $valid_nodes = array_merge($valid_nodes, $new_valid_nodes);
188  }
189 
190  $current_links = Array();
191  foreach ($asset_links as $id => $link) {
192  if (in_array($link['majorid'], $valid_nodes)) {
193  $current_links[$link['majorid']] = $link['majorid'];
194  }
195  }
196 
197  // if no_cache is turned on, clear the cache
198  if ($no_cache) {
199  unset($this->_tmp['linked_selections']);
200  return $current_links;
201  }
202 
203  $this->_tmp['linked_selections'] = $current_links;
204  }
205 
206  return $this->_tmp['linked_selections'];
207 
208  }//end _getAssetLinks()
209 
210 
217  function &_getManagedAsset()
218  {
219  $return = NULL;
220 
221  // check that we don't have a root
222  $parameter_map = $this->getAttribute('parameter_map');
223  $tmp_managed_assetid = $parameter_map->getParameterValue('managed_asset');
224  if (!empty($tmp_managed_assetid)) {
225  $return = $GLOBALS['SQ_SYSTEM']->am->getAsset($tmp_managed_assetid);
226  }
227 
228  // if nothing, try the attribute
229  if (is_null($return) && $this->attr('managed_asset')) {
230  $return = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->attr('managed_asset'));
231  }
232 
233  if (is_null($return)) {
234  $return = $GLOBALS['SQ_SYSTEM']->user;
235  }
236 
237  return $return;
238 
239  }//end _getManagedAsset()
240 
241 
249  {
250  $asset = $this->_getManagedAsset();
251  return $asset->name;
252 
253  }//end _getManagedAssetTypeName()
254 
255 
256 //-- Selections --//
257 
258 
269  function getAssetSelectionValue($selection_name, $assetid)
270  {
271  // if the selection name matches the hard-set selection
272  if ($selection_name == SQ_PAGE_LINK_MGR_PRESET_LINKED) {
273 
274  $asset_links = $this->_getAssetLinks($this->_getManagedAsset());
275 
276  // check whether this assetid is linked
277  if (!empty($asset_links[$assetid])) {
278  // if radio button, return the linked assetid
279  // otherwise, return 1 for every time a linked asset is found
280  $selections = $this->attr('asset_selections');
281  if (isset($selections[SQ_PAGE_LINK_MGR_PRESET_LINKED]['type'])
282  && $selections[SQ_PAGE_LINK_MGR_PRESET_LINKED]['type'] == 'radio'
283  && empty($selection[SQ_PAGE_LINK_MGR_PRESET_LINKED]['options']['allow_multiple'])) {
284  return $assetid;
285  } else {
286  return 1;
287  }
288  }
289  }
290 
291  return 0;
292 
293  }//end getAssetSelectionValue()
294 
295 
302  function printBody()
303  {
304  // Tie the process and following paint together using db2 in order to
305  // avoid possible replication slowdown on db1
306  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
307  $this->processAssetSelections();
308  parent::printBody();
309 
310  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
311 
312  }//end printBody()
313 
314 
321  function printContents()
322  {
323  // print the 'not logged in' bodycopy if the user hasn't logged in
324  $public_user = $GLOBALS['SQ_SYSTEM']->getPublicUser();
325  if ($GLOBALS['SQ_SYSTEM']->currentUser($public_user)) {
326 
327  $bodycopy = $this->getBodycopy('not_logged_in');
328  if (is_null($bodycopy)) return FALSE;
329 
330  require_once SQ_FUDGE_PATH.'/general/text.inc';
331  $keywords = $bodycopy->getKeywords();
332  $replacements = Array();
333  foreach ($keywords as $keyword) {
334  $replacements[$keyword] = $this->getKeywordReplacement($keyword);
335  }
336  $bodycopy->setKeywordReplacements($replacements);
337  $bodycopy->printBody();
338  return;
339 
340  } else {
341  return parent::printContents();
342  }
343 
344  }//end printContents()
345 
346 
355  {
356  $managed_asset = $this->_getManagedAsset();
357 
358  // get the assets the managed asset is linked to, and make sure the list isn't cached
359  $asset_links = $this->_getAssetLinks($managed_asset, TRUE);
360 
361  $asset_selection_linked = array_get_index($this->attr('asset_selections'), SQ_PAGE_LINK_MGR_PRESET_LINKED, Array());
362 
363  // is the asset selection a checkbox?
364  $is_checkbox = array_get_index($asset_selection_linked, 'type', 'radio')
365  && array_get_index(array_get_index($asset_selection_linked, 'options', Array()), 'allow_multiple', 1);
366 
367  // take effect if the selection is present
368  $request_index = $this->_getSelectionFieldNamePrefix(SQ_PAGE_LINK_MGR_PRESET_LINKED);
369  if ((!$is_checkbox && isset($_REQUEST[$request_index])) || ($is_checkbox && isset($_REQUEST['selection_flag_'.$request_index]))) {
370  $new_links = array_get_index($_REQUEST, $request_index, Array());
371 
372  // sanitise the new links for later
373  if (!is_array($new_links)) {
374  $new_links = Array(
375  $new_links => $new_links,
376  );
377  } else {
378  foreach ($new_links as $index => $ticked) {
379  $new_links[$index] = $index;
380  }
381  }
382 
383  // if the listing is paginated, make sure we're only modifying the displayed assets
384  // this way is terribly inefficient, but I'm not sure how to do this without massive cut-pastes
385  $num_per_page = $this->attr('num_per_page');
386  if ($num_per_page > 0) {
387  $children = $this->getAssetList();
388  $this->filterAssetList($children);
389 
390  // the list is sorted and positions set, now we can split to groups
391  if ($this->attr('group_by') == 'grouped') {
392  $this->groupAssetsRecursively($this->attr('asset_grouping'), $children, $children);
393  $this->sortGroups($this->attr('asset_grouping'), $children, $children);
394  }
395 
396  $asset_result_page_var = 'result_'.$this->id.'_result_page';
397  $generic_result_page_var = 'result_page';
398 
399  // have [assetid]_result_page take precedence over result_page
400  $result_page = 1;
401  if (isset($_REQUEST[$generic_result_page_var])) {
402  $result_page = (int)$_REQUEST[$generic_result_page_var];
403  }
404  if (isset($_REQUEST[$asset_result_page_var])) {
405  $result_page = (int)$_REQUEST[$asset_result_page_var];
406  }
407 
408  $num_per_page = $this->attr('num_per_page');
409  if ($result_page <= 0) $result_page = 1;
410 
411  $replacements = Array();
412 
413  $chunk =& $this->getChunk($children, $replacements, Array(), $result_page, $num_per_page);
414  $flattened_chunk = dal_array_flatten($chunk, 'type_code');
415 
416  // remove any assets from the asset_links array that isn't visible on the page
417  foreach ($asset_links as $assetid => $value) {
418  if (!isset($flattened_chunk[$assetid])) {
419  unset($asset_links[$assetid]);
420  }
421  }
422  }//end if
423 
424  // keep forced, but make sure we have link integrity turned on
425  $run_level = SQ_RUN_LEVEL_FORCED | SQ_SECURITY_LINK_INTEGRITY;
426  $GLOBALS['SQ_SYSTEM']->setRunLevel($run_level);
427  $done = $this->processMinorLinks($managed_asset->id, $asset_links, $new_links, $this->attr('link_type'));
428  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
429 
430  // Clear the user groups cache so that any new user groups are picked
431  // up (assuming we are working on a user here - otherwise this will
432  // have no effect, but no harm done either)
433  unset($_SESSION['sq_user_groups']);
434 
435  $this->generateMessages($done);
436  }//end if
437 
438  // update the lookups of the managed asset
439  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
440  $vars = Array('assetids' => Array($managed_asset->id));
441  $hh->freestyleHipo('hipo_job_update_lookups', $vars);
442 
443  return TRUE;
444 
445  }//end processAssetSelections()
446 
447 
461  function processMinorLinks($minor_id, $old_links=Array(), $new_links=Array(), $link_type)
462  {
463  $cannot_create = Array();
464  $cannot_delete = Array();
465  $to_delete = array_diff($old_links, $new_links);
466  $to_create = array_diff($new_links, $old_links);
467  $minor = $GLOBALS['SQ_SYSTEM']->am->getAsset($minor_id);
468 
469  $done = Array();
470 
471  // loop through and link our minor under all majors
472  foreach ($to_create as $majorid) {
473  $major = $GLOBALS['SQ_SYSTEM']->am->getAsset($majorid);
474  if (!$GLOBALS['SQ_SYSTEM']->am->createAssetLink($major, $minor, $link_type)) {
475  $done['add_error'][] = $major->id;
476  } else {
477  $done['add_success'][] = $major->id;
478  }
479 
480  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($major);
481  }
482  // handle links marked to be removed
483  foreach ($to_delete as $majorid) {
484  $curr_link = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($majorid, $minor_id, $link_type);
485  $link_id = $curr_link['linkid'];
486 
487  if (!$GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($link_id)) {
488  $done['delete_error'][] = $majorid;
489  } else {
490  $done['delete_success'][] = $majorid;
491  }
492  }
493 
494  // cleanup
495  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($minor);
496 
497  return $done;
498 
499  }//end processMinorLinks()
500 
501 
511  function generateMessages($fail_success)
512  {
513  foreach ($fail_success as $message_type => $assetids) {
514  switch ($message_type) {
515  case 'add_success':
516  case 'delete_success':
517  $this->messages[] = translate('cms_link_manager_message_'.$message_type, $this->_getManagedAssetTypeName(), $this->_getCommaSeparatedNamesByAssetid($assetids));
518  break;
519  case 'add_error':
520  case 'delete_error':
521  $this->errors[] = translate('cms_link_manager_message_'.$message_type, $this->_getManagedAssetTypeName(), $this->_getCommaSeparatedNamesByAssetid($assetids));
522  break;
523  }
524  }
525 
526  }//end generateMessages()
527 
528 
541  function &getBodycopyContents($name, $replacements=Array())
542  {
543  $bodycopy = $this->getBodycopy($name);
544  if (is_null($bodycopy)) return '';
545 
546  $bodycopy->setKeywordReplacements($replacements);
547 
548  ob_start();
549  $bodycopy->printBody();
550  $html = ob_get_contents();
551  ob_end_clean();
552 
553  return $html;
554 
555  }//end getBodycopyContents()
556 
557 
568  function &getBodycopy($name)
569  {
570  if (array_key_exists($name, $this->bodycopies)) {
571  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_TYPE_2, 'bodycopy', TRUE, $name, 'major', '1');
572  $bodycopy = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
573  return $bodycopy;
574  } else {
575  return NULL;
576  }
577 
578  }//end getBodycopy()
579 
580 
589  {
590  $public_user = $GLOBALS['SQ_SYSTEM']->getPublicUser();
591  if ($GLOBALS['SQ_SYSTEM']->currentUser($public_user)) {
592  $bodycopy = $this->getBodycopy('not_logged_in');
593  } else {
594  $bodycopy = $this->getBodycopy('logged_in');
595  }
596 
597  $keywords = $bodycopy->getKeywords();
598  $replacements = Array();
599  foreach ($keywords as $name) {
600  $replacements[$name] = $this->getKeywordReplacement($name);
601  }
602  $bodycopy->setKeywordReplacements($replacements);
603 
604  ob_start();
605  $bodycopy->printBody();
606  $html = ob_get_contents();
607  ob_end_clean();
608 
609  return $html;
610 
611  }//end printBodycopyContents()
612 
613 
623  {
624  $string = '';
625  $names = Array();
626  foreach ($assetids as $assetid) {
627  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
628  $names[] = $asset->name;
629  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
630  }
631  if (!empty($names)) {
632  // split out the names into something that looks like: "Foo", "Bar"
633  $string .= '"'.implode('", "', $names).'"';
634  }
635 
636  return $string;
637 
638  }//end _getCommaSeparatedNamesByAssetid()
639 
640 
641 //-- Keywords --//
642 
643 
658  {
659  $keywords = parent::_getContentsKeywords();
660 
661  // messages
662  $keywords += Array(
663  'error_messages_list' => translate('cms_link_manager_error_messages_list'),
664  'success_messages_list' => translate('cms_link_manager_success_messages_list'),
665  );
666 
667  // add replacements for the managed assets
668  $managed_asset = $this->_getManagedAsset();
669  $managed_keywords = $managed_asset->getAvailableKeywords();
670  foreach ($managed_keywords as $keyword => $description) {
671  // upper and lowercase
672  $keywords['managed_asset_'.$keyword] = str_replace('Asset', 'Managed Asset', str_replace('asset', 'managed asset', $description));
673  }
674  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($managed_asset);
675 
676  return $keywords;
677 
678  }//end _getContentsKeywords()
679 
680 
695  function getKeywordReplacement($keyword)
696  {
697  // Handle keywords for the managed asset
698  if (strpos($keyword, 'managed_asset_') === 0) {
699  $managed_asset = $this->_getManagedAsset();
700  if (isset($managed_asset)) {
701  return $managed_asset->getKeywordReplacement(substr($keyword, strlen('managed_asset_')));
702  }
703  }
704 
705  return parent::getKeywordReplacement($keyword);
706 
707  }//end getKeywordReplacement()
708 
709 
717  {
718  return $this->_generateMessageList($this->errors, '_errors');
719 
720  }//end getErrorMessagesListKeywordReplacement()
721 
722 
730  {
731  return $this->_generateMessageList($this->messages, '_success');
732 
733  }//end getSuccessMessagesListKeywordReplacement()
734 
735 
745  function _generateMessageList($messages, $suffix)
746  {
747  $string = '';
748  if (!empty($messages)) {
749  $string .= '<ul>';
750  foreach ($messages as $message) {
751  $string .= '<li class="'.$this->getPrefix().'_'.$suffix.'">'.$message.'</li>';
752  }
753  $string .= '</ul>';
754  }
755  return $string;
756 
757  }//end _generateMessageList()
758 
759 
768  function _getSelectionFieldNamePrefix($selection_name)
769  {
770  return $this->getPrefix().'_'.parent::_getSelectionFieldNamePrefix($selection_name);
771 
772  }//end _getSelectionFieldNamePrefix()
773 
774 
775 }//end class
776 
777 ?>