PageRenderTime 200ms CodeModel.GetById 65ms app.highlight 95ms RepoModel.GetById 26ms app.codeStats 1ms

/libraries/gantry/facets/menu/gantrymenu.class.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 251 lines | 187 code | 33 blank | 31 comment | 59 complexity | d4b3339e7277fe4f75eb108cfd0cefe7 MD5 | raw file
  1<?php
  2/**
  3 * @version		3.2.22 August 3, 2012
  4 * @author		RocketTheme http://www.rockettheme.com
  5 * @copyright 	Copyright (C) 2007 - 2012 RocketTheme, LLC
  6 * @license		http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7 *
  8 * Gantry uses the Joomla Framework (http://www.joomla.org), a GNU/GPLv2 content management system
  9 *
 10 */
 11
 12// no direct access
 13defined('GANTRY_VERSION') or die('Restricted access');
 14
 15// Include the syndicate functions only once
 16gantry_import('facets.menu.gantrymenutree');
 17
 18class GantryMenu {
 19
 20    var $_menudata = null;
 21    var $_formatter = null;
 22    var $_layout_path = null;
 23
 24    var $__cacheables = array(
 25        '_menudata'
 26    );
 27
 28    function __sleep() {
 29        return $this->__cacheables;
 30    }
 31
 32    // static function to get an instance of the GantryMenu
 33    function &getInstance(GantryRegistry $params) {
 34        global $gantry;
 35        $conf = JFactory::getConfig();
 36        if ($conf->getValue('config.caching') && $params->get("module_cache", 0)) {
 37            $user =& JFactory::getUser();
 38            $cache =& JFactory::getCache('Gantry');
 39            $cache->setCaching(true);
 40            $cache->setLifeTime($gantry->get("cache-time", $conf->getValue('config.cachetime') * 60));
 41            $args = array(&$params);
 42            $checksum = md5($args[0]->_raw);
 43
 44            $gantrymenu = $cache->get(array('GantryMenu', '_getInstance'), $args, 'GantryMenu-' . $user->get('aid', 0) . '-' . $checksum);
 45        }
 46        else {
 47            $gantrymenu = GantryMenu::_getInstance($params);
 48        }
 49
 50        return $gantrymenu;
 51    }
 52
 53    function &_getInstance($params) {
 54        $gantrymenu = new GantryMenu($params);
 55        return $gantrymenu;
 56    }
 57
 58    function GantryMenu($params) {
 59        if (empty($params)) {
 60            $params = new GantryRegistry();
 61        }
 62        $params->def('menutype', 'mainmenu');
 63        $params->def('class_sfx', '');
 64        $params->def('menu_images', 0);
 65
 66        // Added in 1.5
 67        $params->def('startLevel', 0);
 68        $params->def('endLevel', 0);
 69        $params->def('showAllChildren', 0);
 70
 71        $this->_menudata = GantryMenu::_getMenuData($params);
 72    }
 73
 74
 75    function render($params) {
 76
 77        $theme_name = $params->get('theme', 'basic');
 78        $this->_loadTheme($theme_name);
 79
 80        // Run the basic formatter
 81        GantryMenu::_applyBasicFormatting($this->_menudata);
 82
 83        if (!empty($this->_formatter)){
 84            $this->_formatter->format_tree($this->_menudata);
 85        }
 86
 87        // format the menu data $menu is passed to the layout
 88        $menu = &$this->_menudata;
 89
 90        $menurender = "Unable to render menu missing layout.php for theme " . $theme_name;
 91        if (!empty($this->_layout_path) && file_exists($this->_layout_path) && is_readable($this->_layout_path)){
 92            ob_start();
 93            require($this->_layout_path);
 94            $menurender = ob_get_contents();
 95            ob_end_clean();
 96        }
 97        return $menurender;
 98    }
 99
100    function _getMenuData(&$params) {
101        $menu = new GantryMenuTree();
102        $menu->_params = &$params;
103        $items = &JSite::getMenu();
104        // Get Menu Items
105        $rows = $items->getItems('menutype', $params->get('menutype'));
106        $maxdepth = $menu->getParameter('endLevel', 10);
107
108        // Build Menu Tree root down (orphan proof - child might have lower id than parent)
109        $user =& JFactory::getUser();
110        $ids = array();
111        $ids[0] = true;
112        $last = null;
113        $unresolved = array();
114        // pop the first item until the array is empty if there is any item
115        if (is_array($rows)) {
116            while (count($rows) && !is_null($row = array_shift($rows)))
117            {
118                $row->ionly = $params->get('menu_images_link');
119                if (!$menu->addNode($params, $row)) {
120                    if (!array_key_exists($row->id, $unresolved) || $unresolved[$row->id] < $maxdepth) {
121                        array_push($rows, $row);
122                        if (!isset($unresolved[$row->id])) $unresolved[$row->id] = 1;
123                        else $unresolved[$row->id]++;
124                    }
125                }
126            }
127        }
128        return $menu;
129    }
130
131
132    function _loadTheme($theme_name) {
133        global $gantry;
134        
135        // Load up the theme info if there is not one already
136        if (empty($this->_formatter)) {
137            $theme_parent_paths = array(
138                $gantry->templatePath . '/facets/menu/themes',
139                $gantry->gantryPath . '/facets/menu/themes'
140            );
141
142            foreach ($theme_parent_paths as $theme_parent_path) {
143                if (file_exists($theme_parent_path) && is_dir($theme_parent_path)) {
144                    $d = dir($theme_parent_path);
145                    while (false !== ($entry = $d->read())) {
146                        if ($entry != '.' && $entry != '..') {
147                            if ($entry == $theme_name && is_dir($theme_parent_path . DS . $entry)) {
148                                $formatter_file = $theme_parent_path . DS . $entry . DS . 'formatter.php';
149                                $layout_file = $theme_parent_path . DS . $entry . DS . 'layout.php';
150                                $formatter_class = 'GantryMenuFormatter' . ucfirst($entry);
151                                if (!file_exists($formatter_file)) {
152                                    return false;
153                                }
154                                // Load the Formatter File
155                                require_once($formatter_file);
156                                if (!class_exists($formatter_class)) {
157                                    return false;
158                                }
159                                $this->_formatter = new $formatter_class();
160                                if (file_exists($layout_file)) {
161                                    $this->_layout_path = $layout_file;
162                                }
163                                break(2); // exit top level foreach
164                            }
165                        }
166                    }
167                }
168            }
169        }
170    }
171
172    /**
173     * Perform the basic common formatting to all menu nodes
174     */
175    function _applyBasicFormatting(&$menu) {
176        //set the active tree branch
177        $joomlamenu = &JSite::getMenu();
178        $active = $joomlamenu->getActive();
179        if (isset($active) && isset($active->tree) && count($active->tree)) {
180            reset($active->tree);
181            while (list($key, $value) = each($active->tree)) {
182                $active_node =& $active->tree[$key];
183                $active_child =& $menu->findChild($active_node);
184                if ($active_child !== false) {
185                    $active_child->addListItemClass('active');
186                }
187            }
188        }
189
190        // set the current node
191        if (isset($active)) {
192            $current_child =& $menu->findChild($active->id);
193            if ($current_child !== false && !$current_child->menualias) {
194                $current_child->css_id = 'current';
195            }
196        }
197
198
199        // Limit the levels of the tree is called for By limitLevels
200        if ($menu->getParameter('limit_levels')) {
201            $start = $menu->getParameter('startLevel');
202            $end = $menu->getParameter('endLevel');
203
204            //Limit to the active path if the start is more the level 0
205            if ($start > 0) {
206                $found = false;
207                // get active path and find the start level that matches
208                if (isset($active) && isset($active->tree) && count($active->tree)) {
209                    reset($active->tree);
210                    while (list($key, $value) = each($active->tree)) {
211                        $active_child = $menu->findChild($active->tree[$key]);
212                        if ($active_child != null && $active_child !== false) {
213                            if ($active_child->level == $start - 1) {
214                                $menu->resetTop($active_child->id);
215                                $found = true;
216                                break;
217                            }
218                        }
219                    }
220                }
221                if (!$found) {
222                    $menu->_children = array();
223                }
224            }
225            //remove lower then the defined end level
226            $menu->removeLevel($end);
227        }
228
229        // Remove the child nodes that were not needed to display unless showAllChildren is set
230        $showAllChildren = $menu->getParameter('showAllChildren');
231        if (!$showAllChildren) {
232            if ($menu->hasChildren()) {
233                reset($menu->_children);
234                while (list($key, $value) = each($menu->_children)) {
235                    $toplevel =& $menu->_children[$key];
236                    if (isset($active) && isset($active->tree) && in_array($toplevel->id, $active->tree) !== false) {
237                        $last_active =& $menu->findChild($active->tree[count($active->tree) - 1]);
238                        if ($last_active !==  false) {
239                            $toplevel->removeIfNotInTree($active->tree, $last_active->id);
240                            //$toplevel->removeLevel($last_active->level+1);
241                        }
242                    }
243                    else {
244                        $toplevel->removeLevel($toplevel->level);
245                    }
246                }
247            }
248        }
249    }
250
251}