Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_site_map.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
19 
31 class Page_Site_Map extends Page
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  $this->_ser_attrs = TRUE;
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
56  function _getAllowedLinks()
57  {
58  $page_links = parent::_getAllowedLinks();
59  $page_links[SQ_LINK_NOTICE]['asset'] = Array('card' => 'M', 'exclusive' => FALSE);
60  return $page_links;
61 
62  }//end _getAllowedLinks()
63 
64 
71  function lockTypes()
72  {
73  $lock_types = parent::lockTypes();
74  $lock_types['content'] = ($lock_types['attributes'] | $lock_types['links']);
75  return $lock_types;
76 
77  }//end lockTypes()
78 
79 
88  function describeLink($linkid)
89  {
90  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid);
91  switch (strtolower($link['value'])) {
92  case 'root' :
93  return translate('cms_root_node_link_desc', translate('cms_site_map_asset_name'));;
94  break;
95  default :
96  return parent::describeLink($linkid);
97  break;
98  }
99 
100  }//end describeLink()
101 
102 
110  function printBody()
111  {
112  $cm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cache_manager');
113  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
114  $rootid = $this->_getRootNodeId();
115  $cache_key = $rootid.'-ctx'.$contextid;
116 
117  // Try to load from cache, with our root node as cache key
118  $cached_contents = $cm->loadFromCache($this->id, $this->type(), $cache_key);
119 
120  // if the cache is empty, go ahead and regenerate a new version
121  if ($cached_contents === FALSE) {
122 
123  $levels = ($this->attr('levels') != 0) ? $this->attr('levels') : NULL;
124 
125  $page_data = $GLOBALS['SQ_SYSTEM']->am->getAssetTree($rootid, $levels, array_keys($this->attr('exclude_list')), $this->attr('show_type_2') ? SQ_SC_LINK_BACKEND_NAV : SQ_SC_LINK_FRONTEND_NAV, $this->attr('include_types'), $this->attr('include_dependants'));
126 
127  $assetids = Array();
128 
129  foreach ($page_data as $assetid => $data) {
130  $assetids[] = $assetid;
131  $assetids = array_merge($assetids, array_keys($data));
132  }
133  global $SQ_SITE_MAP_ASSET_URLS;
134 
135  $SQ_SITE_MAP_ASSET_URLS = $GLOBALS['SQ_SYSTEM']->am->getAssetURL($assetids);
136 
137  ob_start();
138  if ($this->attr('icons')) {
139  echo '<script type="text/javascript" src="'.sq_web_path('lib').'/js/general.js"></script>';
140  }
141  ?>
142 
143  <?php
144  // figure out the columns for the site map
145  $num_columns = ($this->attr('columns') > 0) ? $this->attr('columns') : 1;
146  if (empty($page_data)) {
147  ob_end_clean();
148  return;
149  }
150 
151  $num_pages = count(array_keys($page_data[$rootid]));
152 
153  $num_chunks = ceil($num_pages / $num_columns);
154  // CHUNK chunk CHUNK chunk CHUNK it up!
155  $chunks = array_chunk($page_data[$rootid], $num_chunks, TRUE);
156  $root_data = $page_data[$rootid];
157  unset($page_data[$rootid]);
158 
159  // better than conditional html everywhere, well have different functions
160  // for each format
161  switch ($this->attr('map_format')) {
162  case 'unordered_list':
163  $this->_paintUnorderedList($root_data, $page_data);
164  break;
165 
166  default:
167  $this->_paintDefault($chunks, $page_data, $rootid);
168  }
169 
170  $cached_contents = ob_get_contents();
171  $cm->saveToCache($this->id, $this->type(), $cache_key, $cached_contents);
172  ob_end_clean();
173 
174  }//end if cached
175 
176  echo $cached_contents;
177 
178  }//end printBody()
179 
180 
187  function _getRootNodeId()
188  {
189  $root_asset = NULL;
190  $root_link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, 'asset', FALSE, 'root');
191  if (!empty($root_link)) {
192  $root_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($root_link['minorid'], $root_link['minor_type_code']);
193  }
194 
195  if (is_null($root_asset)) {
196  // firstly, get the sites that this page belongs to
197  $site_info = $GLOBALS['SQ_SYSTEM']->am->getRootURL();
198  if (empty($site_info)) return FALSE;
199  $rootid = $site_info['assetid'];
200  } else {
201  $rootid = $root_asset->id;
202 
203  $parameter_map = $this->getAttribute('parameter_map');
204  $dynamic_root = $parameter_map->getParameterValue('root_node');
205  if (!empty($dynamic_root)) {
206  $parents = $GLOBALS['SQ_SYSTEM']->am->getParents($dynamic_root);
207  if (isset($parents[$rootid])) {
208  // dynamic root is a child of the static root, so can be used
209  $rootid = $dynamic_root;
210  }
211  }
212 
213  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($root_asset);
214  unset($root_asset);
215  }
216  return $rootid;
217 
218  }//end _getRootNodeId()
219 
220 
232  function _paintUnorderedList($chunks, &$page_data, $level=1, $generating=FALSE)
233  {
234  $top_level = $this->attr('top_level');
235  if ($top_level == 0) $level = -1;
236 
237  if (!$generating) global $SQ_SITE_MAP_ASSET_URLS;
238 
239  $display_formats = $this->attr('display_formats');
240 
241  ?>
242  <ul>
243 <?php
244  foreach ($chunks as $item) {
245  $url = ($generating) ? '<'.'?php echo $SQ_SITE_MAP_ASSET_URLS[$item[\'assetid\']] ?'.'>' : $SQ_SITE_MAP_ASSET_URLS[$item['assetid']];
246 ?>
247  <li><?php
248  if ($this->attr('icons')) {
249  ?><script type="text/javascript">sq_print_icon("<?php echo $GLOBALS['SQ_SYSTEM']->am->getAssetIconURL($item['type_code']); ?>", "16", "16", "");</script>&nbsp;&nbsp;<?php
250  }
251 
252  $format = $this->_getDisplayFormat($item);
253  ?><a href="<?php echo $url;?>" title="<?php echo $item['name']; ?>"><?php echo $format; ?></a>
254 <?php
255  if (isset($page_data[$item['assetid']]) && $level + 1 <= $top_level) {
256 ?>
257  <?php
258  $this->_paintUnorderedList($page_data[$item['assetid']], $page_data, $level + 1, $generating);
259  }
260 ?>
261  </li>
262  <?php
263 
264  }
265  ?>
266  </ul>
267 <?php
268 
269  }//end _paintUnorderedList()
270 
271 
282  function _paintDefault($chunks=Array(), $page_data, $rootid)
283  {
284  ?>
285 
286  <table cellpadding="0" cellspacing="0" border="0">
287  <tr>
288  <?php
289  foreach ($chunks as $chunk) {
290  ?><td valign="top"><?php
291  $sub_chunk = Array();
292  $sub_chunk[$rootid] = $chunk;
293  $sub_chunk = $sub_chunk + $page_data;
294  $this->_defaultRecursePrintAssetList($sub_chunk, $rootid, 1);
295  unset($sub_chunk);
296  ?></td><?php
297  }
298  ?>
299  </tr>
300  </table>
301  <?php
302 
303  }//end _paintDefault()
304 
305 
317  function _defaultRecursePrintAssetList(&$page_data, $parent, $level, $generating=FALSE)
318  {
319  $top_level = $this->attr('top_level');
320  $stalks = $this->attr('stalks');
321 
322  if (!$generating) global $SQ_SITE_MAP_ASSET_URLS;
323 
324  ?>
325  <table cellpadding="0" cellspacing="0" border="0">
326  <?php
327  $num_kids = count($page_data[$parent]);
328  $i = 0;
329  foreach ($page_data[$parent] as $data) {
330  $end = ($i == $num_kids - 1);
331  $image1 = (($level == 1 && !$top_level) || !$stalks) ? 'blank.gif' : 'tree/stalk.gif';
332  $image2 = (($level == 1 && !$top_level) || !$stalks) ? 'blank.gif' : 'tree/branch.gif';
333 
334  $bg = ($end) ? '' : 'style="background-image: url(\''.sq_web_path('lib').'/web/images/'.$image1.'\')"';
335 
336  $url = ($generating) ? '<'.'?php echo $SQ_SITE_MAP_ASSET_URLS[$data[\'assetid\']] ?'.'>' : $SQ_SITE_MAP_ASSET_URLS[$data['assetid']];
337  ?>
338  <tr>
339  <td <?php echo $bg; ?>>
340  <img src="<?php echo sq_web_path('lib').'/web/images/'.$image2; ?>" width="20" height="20" border="0" alt="Level <?php echo $level; ?> Item" /></td>
341  <td >
342  <?php
343  if ($this->attr('icons')) {
344 ?>
345  <script type="text/javascript">sq_print_icon("<?php echo $GLOBALS['SQ_SYSTEM']->am->getAssetIconURL($data['type_code']); ?>", "16", "16", "");</script>&nbsp;&nbsp;
346  <?php }
347 
348  $format = $this->_getDisplayFormat($data);
349 ?>
350  <a href="<?php echo $url?>" title="<?php echo $data['name']; ?>"><?php echo $format; ?></a>
351  </td>
352  </tr>
353  <?php
354  if (!empty($page_data[$data['assetid']])) {
355  ?>
356  <tr>
357  <td <?php echo $bg; ?>><img src="<?php echo sq_web_path('lib').'/web/images/blank.gif'; ?>" width="1" height="1" alt=" " /></td>
358  <td>
359  <?php
360  $this->_defaultRecursePrintAssetList($page_data, $data['assetid'], ($level +1), $generating);
361  ?>
362  </td>
363  </tr>
364  <?php
365  }
366  $i++;
367  }//end foreach
368  ?>
369  </table>
370  <?php
371 
372  return TRUE;
373 
374  }//end _defaultRecursePrintAssetList()
375 
376 
385  function _getDisplayFormat($data)
386  {
387  if (!is_array($data)) return '';
388 
389  $display_formats = $this->attr('display_formats');
390 
391  $format = $data['name'];
392  $format_type_code = NULL;
393  if (isset($display_formats[$data['type_code']])) {
394  $format_type_code = $data['type_code'];
395  } else if (!empty($display_formats)) {
396  foreach ($display_formats as $type => $info) {
397  if ($info['inherit'] == 1) {
398  if ($GLOBALS['SQ_SYSTEM']->am->isTypeDecendant($data['type_code'], $type)) {
399  $format_type_code = $type;
400  break;
401  }
402  }
403  }
404  }
405 
406  if (!is_null($format_type_code)) {
407  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($data['assetid']);
408  $format = $display_formats[$format_type_code]['format'];
409 
410  preg_match_all('/%([^%]*)%/i', $format, $matches);
411  foreach ($matches[1] as $key => $keyword) {
412  $rep = $asset->getKeywordReplacement($keyword);
413  $format = preg_replace('!' . preg_quote($matches[0][$key], '!') . '!', $rep, $format);
414  }
415  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
416  }
417 
418  return $format;
419 
420  }//end _getDisplayFormat()
421 
422 
423 }//end class
424 
425 ?>