PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/reason_4.0/lib/core/minisite_templates/nav_classes/default.php

https://github.com/carleton/reason_package
PHP | 439 lines | 352 code | 16 blank | 71 comment | 61 complexity | 485326266c9b1f0ec6fae790ef8190f3 MD5 | raw file
  1. <?php
  2. /**
  3. * Default Minisite Navigation
  4. * @package reason
  5. * @subpackage minisite_navigation
  6. */
  7. /**
  8. * Include the Reason Header and the Tree Lister (which this extends)
  9. */
  10. include_once( 'reason_header.php' );
  11. reason_include_once( '/content_listers/tree.php3' );
  12. /**
  13. * Default Minisite Navigation Class
  14. *
  15. * Class used for building and displaying minisite navigation
  16. */
  17. class MinisiteNavigation extends tree_viewer
  18. {
  19. var $nice_urls = array();
  20. // Zero shows the root; 1 shows top-level; etc.
  21. var $start_depth = 0;
  22. var $end_depth = 99;
  23. var $display_parent_of_open_branch = false;
  24. var $link_to_current_page = false;
  25. function show_all_items_given_depths($start = 0,$end = 99)
  26. {
  27. $prev_start_depth = $this->start_depth;
  28. $prev_end_depth = $this->end_depth;
  29. $this->start_depth = $start;
  30. $this->end_depth = $end;
  31. $this->show_all_items();
  32. $this->start_depth = $prev_start_depth;
  33. $this->end_depth = $prev_end_depth;
  34. }
  35. function make_tree( &$item , &$root , $depth, $counter = 0 ) // {{{
  36. {
  37. $display_item = false;
  38. if($depth >= $this->end_depth)
  39. {
  40. $children = array();
  41. }
  42. elseif($this->should_show_children($item))
  43. {
  44. $children = $this->children( $item );
  45. }
  46. else
  47. {
  48. $children = array();
  49. }
  50. if ( $depth >= $this->start_depth )
  51. {
  52. $display_item = true;
  53. }
  54. elseif( $this->display_parent_of_open_branch && $depth == $this->start_depth-1 && $this->is_open( $item ) && !empty( $children ) )
  55. {
  56. $display_item = true;
  57. }
  58. if( $display_item )
  59. {
  60. $open = $this->is_open( $item );
  61. $class = $this->get_item_class($item, $open, $depth, $counter);
  62. echo '<li class="navListItem '.$class.'">';
  63. $this->show_item( $this->values[ $item ] );
  64. if( $open AND !empty( $children ))
  65. {
  66. $shown_children = array();
  67. $i = $counter;
  68. foreach($children as $child_id )
  69. {
  70. if( $this->values[ $child_id ]->get_value( 'nav_display' ) == 'Yes' )
  71. {
  72. $shown_children[$i] = $child_id;
  73. $i++;
  74. }
  75. }
  76. if(!empty($shown_children))
  77. {
  78. echo '<ul class="navList">';
  79. foreach($shown_children as $child_counter=>$child_id)
  80. {
  81. $this->make_tree( $child_id , $root, $depth +1,$child_counter);
  82. }
  83. echo '</ul>';
  84. }
  85. }
  86. echo '</li>';
  87. }
  88. else
  89. {
  90. if( $this->is_open( $item ) AND !empty( $children ) )
  91. {
  92. $i = 0;
  93. foreach($children as $child_id )
  94. {
  95. $c = $this->values[ $child_id ];
  96. if( $c->get_value( 'nav_display' ) == 'Yes' )
  97. {
  98. $this->make_tree( $child_id , $root, $depth +1, ++$i);
  99. }
  100. }
  101. }
  102. }
  103. } // }}}
  104. function get_item_class($id, $open, $depth = 0, $counter = 0)
  105. {
  106. if($open)
  107. {
  108. $class = 'open';
  109. if($this->cur_page_id == $id)
  110. $class .= ' current';
  111. elseif($this->values[ $this->cur_page_id ]->get_value( 'parent_id' ) == $id AND
  112. $this->values[ $this->cur_page_id ]->get_value( 'nav_display' ) == 'No')
  113. $class .= ' pseudoCurrent';
  114. }
  115. else
  116. $class = 'closed';
  117. if($counter)
  118. $class .= ' item'.$counter;
  119. $url = $this->values[$id]->get_value( 'url' );
  120. if(isset($this->values[$id]) && $url)
  121. {
  122. $class .= ' jump';
  123. // check to see if link point of the domain
  124. // add jump-internal or jump-external class
  125. if (( filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) !== FALSE ))
  126. {
  127. // check for virtual host redirects (e.g. sports.domain.edu)
  128. // also ignore ip addressed hosts
  129. if ( substr_count(REASON_HOST, '.') > 1
  130. && !preg_match('/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/', REASON_HOST) )
  131. {
  132. //lop off virtual host parts
  133. $host_array = explode('.', REASON_HOST);
  134. $c = count($host_array);
  135. $host = $host_array[$c - 2] .'.'. $host_array[$c - 1];
  136. } else {
  137. $host = REASON_HOST;
  138. }
  139. if ( preg_match( '/'.$host.'/', $url ) )
  140. {
  141. $class .= ' jump-internal';
  142. } else {
  143. $class .= ' jump-external';
  144. }
  145. }
  146. else // relative URL
  147. {
  148. $class .= ' jump-internal';
  149. }
  150. }
  151. if($this->values[ $id ]->get_value( 'unique_name'))
  152. $class .= ' uname-'.htmlspecialchars($this->values[ $id ]->get_value( 'unique_name'), ENT_QUOTES);
  153. return $class;
  154. }
  155. function show_all_items() // {{{
  156. {
  157. $root = $this->root_node();
  158. ob_start();
  159. $this->make_tree( $root , $root , 0);
  160. $tree = ob_get_contents();
  161. ob_end_clean();
  162. if(!empty($tree))
  163. {
  164. echo '<ul class="navListTop">';
  165. echo $tree;
  166. echo '</ul>'."\n";
  167. }
  168. } // }}}
  169. function main_nav_has_content()
  170. {
  171. return true;
  172. }
  173. function top_nav_has_content()
  174. {
  175. return false;
  176. }
  177. function show_top_nav()
  178. {
  179. // to be overloaded
  180. // sample code:
  181. // $this->show_all_items_given_depths(0,1);
  182. }
  183. function should_show_children($id)
  184. {
  185. return true;
  186. }
  187. /**
  188. * Forces the current page to be a link rather than unlinked text
  189. *
  190. * Use this method if the current page is in a mode which the user might want to exit
  191. * @return void
  192. */
  193. function make_current_page_a_link()
  194. {
  195. $this->link_to_current_page = true;
  196. }
  197. function should_link_to_current_page()
  198. {
  199. return $this->link_to_current_page;
  200. }
  201. function get_home_page_link_text()
  202. {
  203. $home_page = $this->values[ $this->root_node() ];
  204. if($home_page->get_value('link_name'))
  205. {
  206. return $home_page->get_value('link_name');
  207. }
  208. else
  209. {
  210. return $this->site_info->get_value('name').' Home';
  211. }
  212. }
  213. function show_item( &$item , $options = false) // {{{
  214. {
  215. $classes = array();
  216. if( $item->id() == $this->root_node() )
  217. {
  218. $page_name = '<span>'.$this->get_home_page_link_text().'</span>';
  219. $classes[] = 'home';
  220. }
  221. else
  222. {
  223. $page_name = $item->get_value( 'link_name' ) ? $item->get_value( 'link_name' ) : $item->get_value('name');
  224. }
  225. $page_name = strip_tags($page_name,'<span><strong><em>');
  226. if( $this->cur_page_id != $item->id() || $this->should_link_to_current_page() )
  227. {
  228. $link = $this->get_full_url($item->id());
  229. // if the selected page should not be shown in the nav, then we should highlight the parent of the
  230. // invisible page. This code checks to see if the current page is the parent of the selected
  231. // page and checks if the selected page should not be shown.
  232. // It also checks to see if the current page is the same as the item id in the case where link_to_urrent_page is set
  233. if($this->cur_page_id == $item->id()
  234. ||
  235. ( $this->values[ $this->cur_page_id ]->get_value( 'parent_id' ) == $item->id() AND
  236. $this->values[ $this->cur_page_id ]->get_value( 'nav_display' ) == 'No' )
  237. )
  238. {
  239. $prepend = '<strong>';
  240. $append = '</strong>';
  241. if($this->cur_page_id == $item->id())
  242. $classes[] = 'cur';
  243. else
  244. $classes[] = 'parent';
  245. }
  246. else
  247. {
  248. $prepend = '';
  249. $append = '';
  250. }
  251. if(!empty($classes))
  252. $class_attr = ' class="'.implode(' ',$classes).'"';
  253. else
  254. $class_attr = '';
  255. $link = '<a href="'.$link.'"'.$class_attr.$item->get_language_attribute().'>'.$prepend.$page_name.$append.'</a>';
  256. echo $link;
  257. }
  258. else
  259. {
  260. if(!empty($classes))
  261. $class_attr = ' class="'.implode(' ',$classes).'"';
  262. else
  263. $class_attr = '';
  264. echo '<strong'.$class_attr.$item->get_language_attribute().'>'.$page_name.'</strong>';
  265. }
  266. } // }}}
  267. function modify_base_url($base_url) // {{{
  268. // for extending
  269. {
  270. return $base_url;
  271. } // }}}
  272. /**
  273. * Recursive function to build the path of a page inside the site
  274. *
  275. * This method does not return a usable URL -- it only provides the part of the full URL
  276. * that comes after the site's base URL.
  277. * If you want to get a usable URL, try the method get_url_from_base() or get_full_url().
  278. *
  279. * @param integer $id The ID of the page
  280. * @param integer $depth The depth from the first get_nice_url() called in this stack
  281. * @return string The url of the page (relative to the base url of the site)
  282. */
  283. function get_nice_url( $id, $depth = 1) // {{{
  284. {
  285. $ret = false;
  286. if($depth > 60) // deeper than 60 we figure there is a problem
  287. {
  288. trigger_error('Apparent infinite loop; maximum get_nice_url() depth of 60 reached (id '.$id.')');
  289. return false;
  290. }
  291. if(!empty($id))
  292. {
  293. if( isset( $this->values[ $id ] ) )
  294. {
  295. if( isset( $this->nice_urls[ $id ] ) )
  296. $ret = $this->nice_urls[ $id ];
  297. elseif( $id == $this->root_node() )
  298. {
  299. $this->nice_urls[ $this->root_node() ] = $this->values[ $this->root_node() ]->get_value( 'url_fragment' );
  300. $ret = $this->nice_urls[ $this->root_node() ];
  301. }
  302. else
  303. {
  304. $depth++;
  305. $p_id = $this->parent( $id );
  306. if(isset($this->values[ $p_id ])) // need to check or else we will call get_nice_url on a page not in the site
  307. {
  308. $ret = $this->get_nice_url( $p_id, $depth ).'/'.$this->values[ $id ]->get_value( 'url_fragment' );
  309. }
  310. }
  311. }
  312. else
  313. {
  314. trigger_error('get_nice_url() called with an id not in site ('.$id.') at depth '.$depth);
  315. }
  316. }
  317. else
  318. {
  319. trigger_error('get_nice_url() called with an empty id at depth '.$depth);
  320. }
  321. return $ret;
  322. } // }}}
  323. /**
  324. * Gets the path to a page from the server root
  325. *
  326. * If you want textonly inclusion and/or awareness of external URL pages, you might try get_full_url().
  327. *
  328. * @param integer $id The ID of the page
  329. * @return string The url of the page (relative to the server root)
  330. */
  331. function get_url_from_base( $id )
  332. {
  333. static $base_url;
  334. static $base_prepped = false;
  335. if(!$base_prepped)
  336. {
  337. $trimmed_base = trim_slashes($this->site_info->get_value( 'base_url' ));
  338. if(empty($trimmed_base))
  339. $base_url = '';
  340. else
  341. $base_url = '/'.$trimmed_base;
  342. $base_url = $this->modify_base_url($base_url);
  343. }
  344. return $base_url.$this->get_nice_url( $id ).'/';
  345. }
  346. /**
  347. * Gets the full url of the page
  348. *
  349. * If the page is an external link, this method returns the page's url value.
  350. *
  351. * Otherwise, it returns a url that conforms to the parameters given.
  352. * This method pays attention to the textonly value of the page tree object, and appends that value if it exists.
  353. *
  354. * @param integer $id The ID of the page
  355. * @param boolean $as_uri If true, provides a fully qualified URL (e.g. a URI, like: http://www.somesite.com/sitebase/page/path/) If false, provides a URL relative to the base of the server
  356. * @param boolean $secure If true, uses https; otherwise uses http. This param only has an effect if $as_uri is true
  357. * @return string The url of the page
  358. */
  359. function get_full_url( $id, $as_uri = false, $secure = false )
  360. {
  361. if(empty($this->values[ $id ]))
  362. {
  363. return false;
  364. }
  365. else
  366. {
  367. $item =& $this->values[ $id ];
  368. if( !$item->get_value( 'url' ) )
  369. {
  370. $link = $this->get_url_from_base( $id );
  371. if ( !empty( $this->textonly ) )
  372. $link .= '?textonly=1';
  373. if($as_uri)
  374. {
  375. if($secure)
  376. {
  377. $link = securest_available_protocol() . '://'.REASON_HOST.$link;
  378. }
  379. else
  380. {
  381. $link = 'http://'.REASON_HOST.$link;
  382. }
  383. }
  384. }
  385. else
  386. {
  387. $link = $item->get_value( 'url' );
  388. }
  389. return $link;
  390. }
  391. }
  392. /**
  393. * Should include only those items needed by the minisite navigation builder
  394. *
  395. * @todo Figure out why $columns is set.
  396. */
  397. function grab_request() // {{{
  398. {
  399. $request = array_diff_key( conditional_stripslashes($_REQUEST), conditional_stripslashes($_COOKIE) );
  400. $columns = (isset($this->columns)) ? array_keys($this->columns) : array('');
  401. $cleanup_rules = array('site_id' => array('function' => 'turn_into_int', 'extra_args' => array('zero_to_null' => true)),
  402. 'page_id' => array('function' => 'turn_into_int', 'extra_args' => array('zero_to_null' => true)),
  403. 'textonly' => array('function' => 'turn_into_int'),
  404. 'editing' => array('function' => 'check_against_array', 'extra_args' => array('off', 'on')));
  405. // apply the cleanup rules
  406. $this->request = carl_clean_vars($request, $cleanup_rules);
  407. }
  408. function get_id_chain($page_id)
  409. {
  410. if(!isset($this->values[ $page_id ]))
  411. {
  412. trigger_error('Page id '.$page_id.' not in site');
  413. return array();
  414. }
  415. $chain = array($page_id);
  416. if($page_id == $this->root_node())
  417. {
  418. return $chain;
  419. }
  420. return array_merge($chain, $this->get_id_chain($this->parent($page_id)));
  421. }
  422. }
  423. ?>