Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_multiple_page.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
19 require_once SQ_FUDGE_PATH.'/general/text.inc';
20 
32 class Page_Multiple_Page extends Page
33 {
34 
35 
42  function __construct($assetid=0)
43  {
44  $this->_ser_attrs = TRUE;
45  parent::__construct($assetid);
46 
47  }//end constructor
48 
49 
57  function _getAllowedLinks()
58  {
59  $page_links = parent::_getAllowedLinks();
60  $page_links[SQ_LINK_TYPE_2]['page_multiple_page_page'] = Array('card' => 'M', 'exclusive' => TRUE);
61  return $page_links;
62 
63  }//end _getAllowedLinks()
64 
65 
86  function prepareLink(&$asset, $side_of_link, &$link_type, &$value, &$sort_order, &$dependant, &$exclusive)
87  {
88  if (($side_of_link == 'major') && ($asset instanceof Page_Multiple_Page_Page)) {
89  if (($link_type != SQ_LINK_TYPE_2) || ($exclusive != TRUE)) {
90  $link_type = SQ_LINK_TYPE_2;
91  $exclusive = TRUE;
92  return TRUE;
93  }
94  }
95  return FALSE;
96 
97  }//end prepareLink()
98 
99 
106  function printBody()
107  {
108  // This should never be called, unless there are no pages linked underneath this.
109  // printFrontend will divert the attention of the output to a child asset before this is hit
110 
111  echo translate('currently_no_pages_to_display');
112 
113  }//end printBody()
114 
115 
124  function printPage(&$asset)
125  {
126  $this->_tmp['current_page_asset'] = &$asset;
127 
128  $keywords = $asset->getKeywords();
129 
130  $replacements = Array();
131 
132  foreach ($keywords as $value) {
133  $replacements[$value] = $this->getKeywordReplacement($value);
134  }
135 
136  $asset->setKeywordReplacements($replacements);
137 
138  $content = $asset->getContent();
139  echo $content;
140 
141  }//end printPage()
142 
143 
150  function getContent()
151  {
152  $contents = translate('cms_page_multiple_page_get_content_msg');
153  return $contents;
154 
155  }//end getContent()
156 
157 
164  function lockTypes()
165  {
166  $lock_types = parent::lockTypes();
167  $lock_types['content'] = ($lock_types['attributes'] | $lock_types['links']);
168  return $lock_types;
169 
170  }//end lockTypes()
171 
172 
181  function getEditingLocks($keywords)
182  {
183  $locks = Array();
184  if (!is_array($keywords) || empty($keywords)) {
185  return Array();
186  }
187  if (!in_array('contents', $keywords)) return Array();
188  return Array('content');
189 
190  }//end getEditingLocks()
191 
192 
200  function getNumberofPages()
201  {
202  return count($this->getPageLinks());
203 
204  }//end getNumberofPages()
205 
206 
214  function getPageLinks()
215  {
216  $am = $GLOBALS['SQ_SYSTEM']->am;
217  $page_links = $am->getLinks($this->id, SQ_LINK_TYPE_2|SQ_LINK_TYPE_1, 'page_multiple_page_page', TRUE);
218  return $page_links;
219 
220  }//end getPageLinks()
221 
222 
231  function &getPageAsset($page_number)
232  {
233  $page_asset = NULL;
234  $links = $this->getPageLinks();
235  foreach ($links as $link_data) {
236  if ($link_data['sort_order'] == strval(intval($page_number) - 1)) {
237  $page_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($link_data['minorid']);
238  return $page_asset;
239  }
240  }
241  return $page_asset;
242 
243  }//end getPageAsset()
244 
245 
252  function getPageIds()
253  {
254  $page_links = $this->getPageLinks();
255  $ids = Array();
256  $cmp = create_function('$a,$b', 'return $a[\'sort_order\'] < $b[\'sort_order\'] ? -1 : 1;');
257 
258  // Sort the links, so that the pages appear in the backend, in the same order as they appear in the aset map
259  usort($page_links, $cmp);
260  foreach ($page_links as $id => $data) {
261  $ids[] = $data['minorid'];
262  }
263  return $ids;
264 
265  }//end getPageIds()
266 
267 
274  function linksUpdated()
275  {
276  // This is bugging out so we can delay the updating of our web paths
277  if (!empty($this->_tmp['deleting_links'])) {
278  return TRUE;
279  }
280  $current_children = array_flip($this->attr('current_page_ids'));
281 
282  $am = $GLOBALS['SQ_SYSTEM']->am;
283 
284  // Work out new ordering and web paths
285  $new_page_children = Array();
286  $page_links = $this->getPageLinks();
287  $new_paths = Array();
288  foreach ($page_links as $data) {
289  $new_page_children[] = $data['minorid'];
290  $page = $am->getAsset($data['minorid']);
291  unset($current_children[$data['minorid']]);
292  $new_paths[$data['minorid']] = $this->getPageNewWebpaths($page, strval(intval($data['sort_order']) + 1));
293  if (!empty($this->_tmp['no_sort_order_paths'])) {
294  $page->saveWebPaths(Array());
295  }
296  $am->forgetAsset($page);
297  }
298 
299  // whatever is left in current_children now are the deleted children
300  foreach (array_keys($current_children) as $id) {
301  $asset_to_delete_paths_from = $am->getAsset($id);
302  if (!is_null($asset_to_delete_paths_from)) {
303  $asset_to_delete_paths_from->saveWebPaths(Array());
304  $am->forgetAsset($asset_to_delete_paths_from);
305  }
306  }
307 
308  parent::linksUpdated();
309 
310  if($this->getURL() == '') {
311  // Bug fix #3659 'Multiple Page' cloning loses 'Multiple Page Page' web paths
312  // force assign the url to the newly cloned asset as next block will try to
313  // assign webpath to child pages, but if there is no URL on the parent asset
314  // then child assets wont have current URL.
315  $valid_paths = make_valid_web_paths(Array(strtolower($this->name)));
316  $good_paths = $GLOBALS['SQ_SYSTEM']->am->webPathsInUse($this, $valid_paths, $this->id, TRUE);
317  $this->saveWebPaths($good_paths, FALSE);
318  }
319 
320  // when trying to rearrange we specifically dont want to run the following code because of
321  // bug #4845 'Multiple Page' asset throws MySource Warning when trying to re-arrange the pages
322  // after re-arranging the pages, adding or deleting new pages linksUpdate() is called again
323  // which is when the following block of code should be executed
324  if ((isset($this->_tmp['no_sort_order_paths']) && $this->_tmp['no_sort_order_paths'] ) && (isset($new_paths) && !empty($new_paths))) {
325  foreach ($new_paths as $pageid => $new_webpaths) {
326  $page = $am->getAsset($pageid, '', TRUE);
327  if (!empty($page)) {
328  $page->saveWebPaths($new_webpaths);
329  $am->forgetAsset($page);
330  }
331  }
332  }
333 
334  // This will store all current pages,
335  $GLOBALS['SQ_SYSTEM']->setRunLevel($GLOBALS['SQ_SYSTEM']->getRunLevel() - SQ_SECURITY_PERMISSIONS - SQ_SECURITY_LOCKING);
336  $this->setAttrValue('current_page_ids', $new_page_children);
337  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
338 
339  }//end linksUpdated()
340 
341 
352  function getPageNewWebpaths(&$page, $order)
353  {
354  $webpaths = $page->getWebPaths();
355 
356  foreach ($webpaths as $idx => $webpath) {
357  if (is_numeric($webpath)) unset($webpaths[$idx]);
358  }
359 
360  if (empty($webpaths)) $webpaths = Array($order);
361 
362  return $webpaths;
363 
364  }//end getPageNewWebpaths()
365 
366 
374  {
375  $keywords = parent::getAvailableKeywords();
376 
377  $keywords['previous_page_href'] = translate('cms_page_multiple_page_previous_page_href');
378  $keywords['next_page_href'] = translate('cms_page_multiple_page_next_page_href');
379  $keywords['previous_page_name'] = translate('cms_page_multiple_page_previous_page_name');
380  $keywords['next_page_name'] = translate('cms_page_multiple_page_next_page_name');
381  $keywords['previous_page_link'] = translate('cms_page_multiple_page_previous_page_link');
382  $keywords['next_page_link'] = translate('cms_page_multiple_page_next_page_link');
383  $keywords['total_page_count'] = translate('cms_page_multiple_page_total_page_count');
384  $keywords['page_name_x'] = translate('cms_page_multiple_page_page_name_x');
385  $keywords['page_href_x'] = translate('cms_page_multiple_page_page_href_x');
386  $keywords['page_link_x'] = translate('cms_page_multiple_page_page_link_x');
387  $keywords['page_assetid_x'] = translate('cms_page_multiple_page_page_assetid_x');
388  $keywords['current_page_name'] = translate('cms_page_multiple_page_current_page_name');
389  $keywords['current_page_attribute_name'] = translate('cms_page_multiple_page_current_page_attribute_name');
390  $keywords['current_page_number'] = translate('cms_page_multiple_page_current_page_number');
391  $keywords['current_page_assetid'] = translate('cms_page_multiple_page_current_page_assetid');
392  $keywords['toc_unordered'] = translate('cms_page_multiple_page_toc_unordered');
393  $keywords['toc_ordered'] = translate('cms_page_multiple_page_toc_ordered');
394  $keywords['navigation_menu_selection_list'] = translate('cms_page_multiple_page_navigation_menu_selection_list');
395  $keywords['multiple_page_assetid'] = 'Multiple Page Asset ID';
396 
397  return $keywords;
398 
399  }//end getAvailableKeywords()
400 
401 
411  function createChildPage($name='page', $link_type=SQ_LINK_TYPE_2)
412  {
413  $import_link = Array('asset' => &$this, 'link_type' => $link_type, 'value' => '', 'sort_order' => $this->getNumberofPages(), 'is_dependant' => 1, 'is_exclusive' => 1);
414  $GLOBALS['SQ_SYSTEM']->am->includeAsset('page_multiple_page_page');
415  $page = new Page_Multiple_Page_Page();
416  $page->setAttrValue('name', $name);
417  $page->create($import_link);
418 
419  }//end createChildPage()
420 
421 
434  function getCurrentPageId()
435  {
436  if (!isset($this->_tmp['current_page_id'])) {
437  $this->_tmp['current_page_id'] = 0;
438  $asset = $GLOBALS['SQ_SYSTEM']->am->getAssetFromURL(NULL, NULL, TRUE, TRUE);
439  if (!empty($asset)) {
440  if ($asset->id == $this->id) {
441  // this is the Multiple Page Asset itself, so we need the first page
442  $this->_tmp['current_page_id'] = 1;
443  } else {
444  $links = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($this->id, $asset->id, SQ_LINK_TYPE_2|SQ_LINK_TYPE_1, NULL, 'major', TRUE);
445  if (!empty($links) && $links[0]['minorid']) {
446  $this->_tmp['current_page_id'] = $links[0]['sort_order'] + 1;
447  }
448  }
449  }
450  }
451 
452  return $this->_tmp['current_page_id'];
453 
454  }//end getCurrentPageId()
455 
456 
465  function &getCurrentPage()
466  {
467  return $this->getPageAsset($this->getCurrentPageId());
468 
469  }//end getCurrentPage()
470 
471 
481  function getKeywordReplacement($value)
482  {
483  // Try this so we only have to initialise these values once for all keywords
484  if (!isset($this->_tmp['current_page_id']) || empty($this->_tmp['current_page_id'])) {
485  $this->_tmp['current_page_id'] = $this->getCurrentPageId();
486  }
487 
488  if ($value == 'current_page_number') {
489  return $this->_tmp['current_page_id'];
490  }
491 
492  if (!isset($this->_tmp['current_page_asset']) || empty($this->_tmp['current_page_asset'])) {
493  $this->_tmp['current_page_asset'] = $this->getCurrentPage();
494  }
495  if (!isset($this->_tmp['current_page_links']) || empty($this->_tmp['current_page_links'])) {
496  $this->_tmp['current_page_links'] = $this->getPageLinks();
497  }
498 
499  // Navigation menus
500  if ($value == 'navigation_menu_selection_list') {
501  return $this->paintNavSelectionList();
502  }
503 
504  // Paint TOC here, so we dont have to go through the drama below
505  if (strpos($value, 'toc_') !== FALSE) {
506  $ordered = TRUE;
507  if (strpos($value, '_unordered') !== FALSE) {
508  $ordered = FALSE;
509  }
510  return $this->paintTOC($ordered);
511  }
512 
513  $am = $GLOBALS['SQ_SYSTEM']->am;
514 
515  $replacement = '';
516 
517  // Get the ids of our previous and next pages
518  $next_page_id = '';
519  if (strpos($value, 'next_page_') !== FALSE) {
520  $found_one = FALSE;
521  foreach ($this->_tmp['current_page_links'] as $id => $data) {
522  if ($found_one == TRUE) {
523  $next_page_id = $data['minorid'];
524  break;
525  }
526  if ($data['sort_order'] == strval(intval($this->_tmp['current_page_id']) - 1)) {
527  $found_one = TRUE;
528  }
529  }
530  }
531  $prev_page_id = '';
532  if (strpos($value, 'previous_page_') !== FALSE) {
533  $found_one = FALSE;
534  $prev_page = '';
535  foreach ($this->_tmp['current_page_links'] as $id => $data) {
536  if ($data['sort_order'] == strval(intval($this->_tmp['current_page_id']) - 1)) {
537  $found_one = TRUE;
538  }
539  if ($found_one == TRUE) {
540  $prev_page_id = $prev_page;
541  break;
542  }
543  $prev_page = $data['minorid'];
544  }
545  }
546 
547  // We need to handle the page_bla_X stuff first, as a switch wont be suitable with the variable page numbers
548  $page_number = '';
549  $page_number_asset = NULL;
550  if (strpos($value, 'page_link_') !== FALSE) {
551  $page_number = str_replace('page_link_', '', $value);
552  $value = 'page_link_';
553  }
554  if (strpos($value, 'page_href_') !== FALSE) {
555  $page_number = str_replace('page_href_', '', $value);
556  $value = 'page_href_';
557  }
558  if (strpos($value, 'page_name_') !== FALSE) {
559  $page_number = str_replace('page_name_', '', $value);
560  $value = 'page_name_';
561  }
562  if (strpos($value, 'page_assetid_') !== FALSE) {
563  $page_number = str_replace('page_assetid_', '', $value);
564  $value = 'page_assetid_';
565  }
566  if ($page_number != '') {
567  $page_number_asset = $this->getPageAsset($page_number);
568  }
569 
570  // Evaluate the values we need for keyword replacement
571  switch ($value) {
572 
573  // Next Page Values
574  case 'next_page_href':
575  if (!empty($next_page_id)) {
576  $replacement = current($am->getAssetUrl(Array($next_page_id)));
577  }
578  break;
579  case 'next_page_name':
580  if (!empty($next_page_id)) {
581  $next_page_asset = $am->getAsset($next_page_id);
582  if (!is_null($next_page_asset)) {
583  $replacement = $next_page_asset->_getName();
584  }
585  }
586  break;
587  case 'next_page_link':
588  if (!empty($next_page_id)) {
589  $next_page_asset = $am->getAsset($next_page_id);
590  if (!is_null($next_page_asset)) {
591  $href = $next_page_asset->getURL();
592  if (!empty($href)) {
593  $replacement = '<a href="'.$href.'">'.$next_page_asset->_getName().'</a>';
594  }
595  }
596  }
597  break;
598 
599  // Previous Page Values
600  case 'previous_page_href':
601  if (!empty($prev_page_id)) {
602  $replacement = current($am->getAssetUrl(Array($prev_page_id)));
603  }
604  break;
605  case 'previous_page_name':
606  if (!empty($prev_page_id)) {
607  $prev_page_asset = $am->getAsset($prev_page_id);
608  if (!is_null($prev_page_asset)) {
609  $replacement = $prev_page_asset->_getName();
610  }
611  }
612  break;
613  case 'previous_page_link':
614  if (!empty($prev_page_id)) {
615  $prev_page_asset = $am->getAsset($prev_page_id);
616  if (!is_null($prev_page_asset)) {
617  $href = $prev_page_asset->getURL();
618  if (!empty($href)) {
619  $replacement = '<a href="'.$href.'">'.$prev_page_asset->_getName().'</a>';
620  }
621  }
622  }
623  break;
624 
625  // Some additional keywords for the overall page set
626  case 'total_page_count':
627  $replacement = $this->getNumberOfPages();
628  break;
629 
630  // Provides individual links for
631  case 'page_link_':
632  if (!is_null($page_number_asset)) {
633  $href = $page_number_asset->getURL();
634  if (!empty($href)) {
635  $replacement = '<a href="'.$href.'">'.$page_number_asset->_getName().'</a>';
636  }
637  }
638  break;
639  case 'page_href_':
640  if (!is_null($page_number_asset)) {
641  $replacement = $page_number_asset->getHref();
642  }
643  break;
644  case 'page_name_':
645  if (!is_null($page_number_asset)) {
646  $replacement = $page_number_asset->_getName();
647  }
648  break;
649  case 'page_assetid_':
650  if (!is_null($page_number_asset)) {
651  $replacement = $page_number_asset->id;
652  }
653  break;
654 
655  // current page details
656  case 'current_page_name':
657  case 'current_page_attribute_name':
658  if (!is_null($this->_tmp['current_page_asset'])) {
659  $replacement = $this->_tmp['current_page_asset']->_getName();
660  }
661  break;
662 
663  case 'current_page_assetid':
664  if (!is_null($this->_tmp['current_page_asset'])) {
665  $replacement = $this->_tmp['current_page_asset']->id;
666  }
667  break;
668 
669  // parent page of all subpages, which is this asset
670  case 'multiple_page_assetid':
671  $replacement = $this->id;
672  break;
673 
674  // Defaults, probably a standard keyword
675  default:
676  $replacement = parent::getKeywordReplacement($value);
677  }//end switch keyword
678 
679  return $replacement;
680 
681  }//end getKeywordReplacement()
682 
683 
690  function &getFirstPageAsset()
691  {
692  $page_one = $this->getPageAsset('1');
693  return $page_one;
694 
695  }//end getFirstPageAsset()
696 
697 
705  function printFrontend()
706  {
707  $page_one = $this->getFirstPageAsset();
708  if (is_null($page_one)) $page_one = $this;
709  $this->printFrontendAsset($page_one);
710 
711  }//end printFrontend()
712 
713 
723  function paintTOC($ordered=FALSE, $link_current=FALSE)
724  {
725  $am = $GLOBALS['SQ_SYSTEM']->am;
726  $links = $this->getPageLinks();
727  $current = $this->getCurrentPageId();
728  $current_assetid = $links[intval($current) - 1]['minorid'];
729  ob_start();
730  if ($ordered) {
731  echo '<ol>';
732  } else {
733  echo '<ul>';
734  }
735  foreach ($links as $link_data) {
736  $assetid = $link_data['minorid'];
737  $page_asset = $am->getAsset($assetid);
738  if (is_null($page_asset)) {
739  continue;
740  }
741 
742  $name = $page_asset->_getName();
743  echo '<li>';
744  if ($assetid == $current_assetid && !$link_current) {
745  echo $name;
746  } else {
747  $href = $page_asset->getURL();
748  echo '<a href="'.$href.'">'.$name.'</a>';
749  }
750  echo '</li>';
751  }
752  if ($ordered) {
753  echo '</ol>';
754  } else {
755  echo '</ul>';
756  }
757  $toc = ob_get_clean();
758 
759  return $toc;
760 
761  }//end paintTOC()
762 
763 
772  function paintNavSelectionList($link_current=FALSE)
773  {
774  $am = $GLOBALS['SQ_SYSTEM']->am;
775  $links = $this->getPageLinks();
776  $current = $this->getCurrentPageId();
777  $current_assetid = $links[intval($current) - 1]['minorid'];
778  ob_start();
779  echo '<select id="'.$this->getPrefix().'nav_selection_list'.'" onchange="location.href=this.options[selectedIndex].value">';
780 
781  foreach ($links as $link_data) {
782  $assetid = $link_data['minorid'];
783  $page_asset = $am->getAsset($assetid);
784  if (is_null($page_asset)) {
785  continue;
786  }
787 
788  $name = $page_asset->_getName();
789  if ($assetid == $current_assetid && !$link_current) {
790  echo '<option value="" selected="selected">'.$name;
791  } else {
792  $href = $page_asset->getURL();
793  echo '<option value="'.$href.'">'.$name;
794  }
795  echo '</option>';
796  }
797 
798  echo '</select>';
799  $nav = ob_get_clean();
800 
801  return $nav;
802 
803  }//end paintNavSelectionList()
804 
805 
816  function sortOrder($linkid, $sort_order, $switch)
817  {
818  $this->_tmp['no_sort_order_paths'] = $switch;
819  $GLOBALS['SQ_SYSTEM']->am->updateLink($linkid,NULL,NULL,$sort_order);
820 
821  }//end sortOrder()
822 
848  function cloneComponents(&$clone, $components, $override=FALSE)
849  {
850  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
851  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
852 
853  if (!parent::cloneComponents($clone, $components, $override)) {
854  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
855  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
856  return FALSE;
857  }
858 
859  if (in_array('attributes', $components) || in_array('all', $components)) {
860  $current_ids = Array();
861  // save the information
862  // Bug fix #3659 'Multiple Page' cloning loses 'Multiple Page Page' web paths
863  // do this because, page_multiple_page saves its current child page asset ids
864  // is the attributes, if we dont make it empty, newly cloned asset takes
865  // child pages from the sorce as its own child
866  $clone->setAttrValue('current_page_ids', $current_ids);
867  $clone->saveAttributes();
868 
869  // Change the content of the content type to replace question id.
870  }
871  // if cloning we need this to update webpaths later
872  $this->_tmp['no_sort_order_paths'] = TRUE;
873 
874  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
875  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
876  return TRUE;
877 
878  }//end cloneComponents()
879 
880 }//end class
881 
882 ?>