/administrator/components/com_menus/helpers/menus.php
PHP | 205 lines | 123 code | 27 blank | 55 comment | 17 complexity | 9dc89b749d14692298e5907f5359100d MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
- <?php
- /**
- * @version $Id$
- * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE.txt
- */
- // No direct access
- defined('_JEXEC') or die;
- /**
- * Menus component helper.
- *
- * @package Joomla.Administrator
- * @subpackage com_menus
- * @since 1.6
- */
- class MenusHelper
- {
- /**
- * Defines the valid request variables for the reverse lookup.
- */
- protected static $_filter = array('option', 'view', 'layout');
- /**
- * Configure the Linkbar.
- *
- * @param string The name of the active view.
- */
- public static function addSubmenu($vName)
- {
- JSubMenuHelper::addEntry(
- JText::_('Menus_Submenu_Menus'),
- 'index.php?option=com_menus&view=menus',
- $vName == 'menus'
- );
- JSubMenuHelper::addEntry(
- JText::_('Menus_Submenu_Items'),
- 'index.php?option=com_menus&view=items',
- $vName == 'items'
- );
- }
- /**
- * Gets a standard form of a link for lookups.
- *
- * @param mixed A link string or array of request variables.
- *
- * @return mixed A link in standard option-view-layout form, or false if the supplied response is invalid.
- */
- public static function getLinkKey($request)
- {
- if (empty($request)) {
- return false;
- }
- // Check if the link is in the form of index.php?...
- if (is_string($request))
- {
- $args = array();
- if (strpos($request, 'index.php') === 0) {
- parse_str(parse_url(htmlspecialchars_decode($request), PHP_URL_QUERY), $args);
- }
- else {
- parse_str($request, $args);
- }
- $request = $args;
- }
- // Only take the option, view and layout parts.
- foreach ($request as $name => $value)
- {
- if (!in_array($name, self::$_filter))
- {
- // Remove the variables we want to ignore.
- unset($request[$name]);
- }
- }
- ksort($request);
- return 'index.php?'.http_build_query($request,'','&');
- }
- /**
- * Get the menu list for create a menu module
- *
- * @return array The menu array list
- * @since 1.6
- */
- public static function getMenuTypes()
- {
- $db = JFactory::getDbo();
- $db->setQuery('SELECT a.menutype FROM #__menu_types AS a');
- return $db->loadResultArray();
- }
- /**
- * Get a list of menu links for one or all menus.
- *
- * @param string An option menu to filter the list on, otherwise all menu links are returned as a grouped array.
- * @param int An optional parent ID to pivot results around.
- * @param int An optional mode. If parent ID is set and mode=2, the parent and children are excluded from the list.
- * @param array An optional array of states
- */
- public static function getMenuLinks($menuType = null, $parentId = 0, $mode = 0, $published=array())
- {
- $db = JFactory::getDbo();
- $query = new JQuery;
- $query->select('a.id AS value, a.title AS text, a.level, a.menutype, a.type');
- $query->from('#__menu AS a');
- $query->join('LEFT', '`#__menu` AS b ON a.lft > b.lft AND a.rgt < b.rgt');
- // Filter by the type
- if ($menuType)
- {
- $query->where('(a.menutype = '.$db->quote($menuType).' OR a.parent_id = 0)');
- }
- if ($parentId)
- {
- if ($mode == 2)
- {
- // Prevent the parent and children from showing.
- $query->join('LEFT', '`#__menu` AS p ON p.id = '.(int) $parentId);
- $query->where('(a.lft <= p.lft OR a.rgt >= p.rgt)');
- }
- }
-
- if(!empty($published))
- {
- if (is_array($published)) $published = '(' . implode(',',$published) .')';
- $query->where('a.published IN ' . $published);
- }
- $query->group('a.id');
- $query->order('a.lft ASC');
- // Get the options.
- $db->setQuery($query);
- $links = $db->loadObjectList();
- // Check for a database error.
- if ($error = $db->getErrorMsg())
- {
- JError::raiseWarning(500, $error);
- return false;
- }
- // Pad the option text with spaces using depth level as a multiplier.
- foreach ($links as &$link)
- {
- $link->text = str_repeat('- ',$link->level).$link->text;
- }
- if (empty($menuType))
- {
- // If the menutype is empty, group the items by menutype.
- $query = new JQuery;
- $query->select('*');
- $query->from('#__menu_types');
- $query->where('menutype <> '.$db->quote(''));
- $query->order('title, menutype');
- $db->setQuery($query);
- $menuTypes = $db->loadObjectList();
- // Check for a database error.
- if ($error = $db->getErrorMsg())
- {
- JError::raiseWarning(500, $error);
- return false;
- }
- // Create a reverse lookup and aggregate the links.
- $rlu = array();
- foreach ($menuTypes as &$type)
- {
- $rlu[$type->menutype] = &$type;
- $type->links = array();
- }
- // Loop through the list of menu links.
- foreach ($links as &$link)
- {
- if (isset($rlu[$link->menutype]))
- {
- $rlu[$link->menutype]->links[] = &$link;
- // Cleanup garbage.
- unset($link->menutype);
- }
- }
- return $menuTypes;
- }
- else
- {
- return $links;
- }
- }
- }