Beispiel #1
0
 /**
  * Return a tree navigation based on the given helper.
  * One helper is needed to use this tag.
  * The default helper is /application/helpers/navigation_helper->get_tree_navigation()
  * If you wish to change the
  *
  * @param	FTL_Binding object
  *
  * @return 	string
  *
  * @usage	<ion:tree_navigation [helper="navigation::your_helper_method"] />
  *
  */
 public static function tag_tree_navigation(FTL_Binding $tag)
 {
     // Page : Asked one through the page tag
     $page = $tag->get('page');
     // Current page
     if (is_null($page)) {
         $page = self::registry('page');
     }
     // If 404 : Put empty vars, so the menu will prints out without errors
     /*
     if ( !isset($page['id_page']))
     {
     	$page = array(
     		'id_page' => '',
     		'id_parent' => ''
     	);
     }
     */
     // Menu : Main menu by default
     $menu_name = $tag->getAttribute('menu', 'main');
     $id_menu = 1;
     foreach (self::registry('menus') as $menu) {
         if ($menu_name == $menu['name']) {
             $id_menu = $menu['id_menu'];
             break;
         }
     }
     // Attribute level, else parent page level + 1
     $from_level = $tag->getAttribute('level', 0);
     // Depth
     $depth = $tag->getAttribute('depth', -1);
     // Attribute : active class, first_class, last_class
     $active_class = $tag->getAttribute('active_class', 'active');
     $first_class = $tag->getAttribute('first_class', '');
     $last_class = $tag->getAttribute('last_class', '');
     // Display hidden navigation elements ?
     $display_hidden = $tag->getAttribute('display_hidden', FALSE);
     // Includes articles as menu elements
     $with_articles = $tag->getAttribute('articles', FALSE);
     // Attribute : HTML Tree container ID & class attribute
     $id = $tag->getAttribute('id');
     if (strpos($id, 'id') !== FALSE) {
         $id = str_replace('\'', '"', $id);
     }
     $class = $tag->getAttribute('class');
     if (strpos($active_class, 'class') !== FALSE) {
         $active_class = str_replace('\'', '"', $active_class);
     }
     // Attribute : Helper to use to print out the tree navigation
     $helper = $tag->getAttribute('helper', 'navigation');
     // Get helper method
     $helper_function = substr(strrchr($helper, ':'), 1) ? substr(strrchr($helper, ':'), 1) : 'get_tree_navigation';
     $helper = strpos($helper, ':') !== FALSE ? substr($helper, 0, strpos($helper, ':')) : $helper;
     // load the helper
     self::$ci->load->helper($helper);
     // Page from locals : By ref because of active_class definition
     // $pages =  $tag->locals->_pages;
     $pages = self::registry('pages');
     /* Get the reference parent page ID
      * Note : this is depending on the whished level.
      * If the curent page level > asked level, we need to find recursively the parent page which has the good level.
      * This is done to avoid tree cut when navigation to a child page
      *
      * e.g :
      *
      * On the "services" page and each subpage, we want the tree navigation composed by the sub-pages of "services"
      * We are in the page "offer"
      * We have to find out that the level 1 parent is "services"
      *
      *	Page structure				Level
      *
      *	home						0
      *	 |_ about					1		
      *	 |_ services				1		<- We want all the nested nav starting at level 1 from this parent page
      *	 	   |_ development		2
      *		   |_ design			2
      *				|_ offer		3		<- We are here.
      *				|_ portfolio	3	
      */
     $page_level = isset($page['level']) ? $page['level'] : 0;
     // Asked Level exists
     $parent_page = array('id_page' => $from_level > 0 ? $page['id_page'] : 0, 'id_parent' => isset($page['id_parent']) ? $page['id_parent'] : 0);
     if ($from_level !== FALSE) {
         $parent_page = array('id_page' => $from_level > 0 ? $page['id_page'] : 0, 'id_parent' => isset($page['id_parent']) ? $page['id_parent'] : 0);
     } else {
         foreach ($pages as $p) {
             // Parent page is the id_subnav page
             if ($p['id_page'] == $page['id_subnav']) {
                 $parent_page = $p;
             }
         }
     }
     // Find out the wished parent page
     while ($page_level >= $from_level && $from_level > 0) {
         $potential_parent_page = array();
         foreach ($pages as $p) {
             if ($p['id_page'] == $parent_page['id_parent']) {
                 $potential_parent_page = $p;
                 break;
             }
         }
         if (!empty($potential_parent_page)) {
             $parent_page = $potential_parent_page;
             $page_level = $parent_page['level'];
         } else {
             $page_level--;
         }
     }
     // Active pages array. Array of ID
     $active_pages = Structure::get_active_pages($pages, $page['id_page']);
     foreach ($pages as $key => $p) {
         $pages[$key]['active_class'] = in_array($p['id_page'], $active_pages) ? $active_class : '';
     }
     // Filter on 'appears'=>'1'
     $nav_pages = $pages;
     if ($display_hidden === FALSE) {
         $nav_pages = array_values(array_filter($pages, array('TagManager_Page', '_filter_appearing_pages')));
     }
     $final_nav_pages = $nav_pages_list = array();
     foreach ($nav_pages as $k => $np) {
         if ($np['id_menu'] == $id_menu) {
             $final_nav_pages[] = $np;
             $nav_pages_list[] = $np['id_page'];
         }
     }
     // Should we include articles ?
     $articles = FALSE;
     if ($with_articles == TRUE) {
         $entity = self::get_entity();
         $id_active_article = $entity['type'] == 'article' ? $entity['id_entity'] : NULL;
         foreach ($final_nav_pages as $key => $p) {
             // TODO : Change for future "Articles" lib call
             $tag->set('page', $p);
             $articles = TagManager_Article::get_articles($tag);
             // Set active article
             if (!is_null($id_active_article)) {
                 foreach ($articles as $akey => $a) {
                     if ($a['id_article'] == $id_active_article) {
                         $articles[$akey]['active_class'] = $active_class;
                         $articles[$akey]['is_active'] = TRUE;
                     }
                 }
             }
             $final_nav_pages[$key]['articles'] = $articles;
         }
     }
     // Get the tree navigation array
     $tree = Structure::get_tree_navigation($final_nav_pages, $parent_page['id_page'], $from_level, $depth, $articles);
     // Return the helper function
     if (function_exists($helper_function)) {
         return call_user_func($helper_function, $tree, $id, $class, $first_class, $last_class);
     }
 }