Source for file menus.php

Documentation is available at menus.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_menus
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Menus component helper.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_menus
  17.  * @since       1.6
  18.  */
  19. {
  20.     /**
  21.      * Defines the valid request variables for the reverse lookup.
  22.      */
  23.     protected static $_filter array('option''view''layout');
  24.  
  25.     /**
  26.      * Configure the Linkbar.
  27.      *
  28.      * @param   string    The name of the active view.
  29.      */
  30.     public static function addSubmenu($vName)
  31.     {
  32.         JHtmlSidebar::addEntry(
  33.             JText::_('COM_MENUS_SUBMENU_MENUS'),
  34.             'index.php?option=com_menus&view=menus',
  35.             $vName == 'menus'
  36.         );
  37.         JHtmlSidebar::addEntry(
  38.             JText::_('COM_MENUS_SUBMENU_ITEMS'),
  39.             'index.php?option=com_menus&view=items',
  40.             $vName == 'items'
  41.         );
  42.     }
  43.  
  44.     /**
  45.      * Gets a list of the actions that can be performed.
  46.      *
  47.      * @param   integer  The menu ID.
  48.      *
  49.      * @return  JObject 
  50.      * @since   1.6
  51.      */
  52.     public static function getActions($parentId 0)
  53.     {
  54.         $user JFactory::getUser();
  55.         $result new JObject;
  56.  
  57.         if (empty($parentId))
  58.         {
  59.             $assetName 'com_menus';
  60.         }
  61.         else
  62.         {
  63.             $assetName 'com_menus.item.' . (int) $parentId;
  64.         }
  65.  
  66.         $actions JAccess::getActions('com_menus');
  67.  
  68.         foreach ($actions as $action)
  69.         {
  70.             $result->set($action->name$user->authorise($action->name$assetName));
  71.         }
  72.  
  73.         return $result;
  74.     }
  75.  
  76.     /**
  77.      * Gets a standard form of a link for lookups.
  78.      *
  79.      * @param   mixed    A link string or array of request variables.
  80.      *
  81.      * @return  mixed  A link in standard option-view-layout form, or false if the supplied response is invalid.
  82.      */
  83.     public static function getLinkKey($request)
  84.     {
  85.         if (empty($request))
  86.         {
  87.             return false;
  88.         }
  89.  
  90.         // Check if the link is in the form of index.php?...
  91.         if (is_string($request))
  92.         {
  93.             $args array();
  94.             if (strpos($request'index.php'=== 0)
  95.             {
  96.                 parse_str(parse_url(htmlspecialchars_decode($request)PHP_URL_QUERY)$args);
  97.             }
  98.             else
  99.             {
  100.                 parse_str($request$args);
  101.             }
  102.             $request $args;
  103.         }
  104.  
  105.         // Only take the option, view and layout parts.
  106.         foreach ($request as $name => $value)
  107.         {
  108.             if ((!in_array($nameself::$_filter)) && (!($name == 'task' && !array_key_exists('view'$request))))
  109.             {
  110.                 // Remove the variables we want to ignore.
  111.                 unset($request[$name]);
  112.             }
  113.         }
  114.  
  115.         ksort($request);
  116.  
  117.         return 'index.php?' http_build_query($request'''&');
  118.     }
  119.  
  120.     /**
  121.      * Get the menu list for create a menu module
  122.      *
  123.      * @return    array    The menu array list
  124.      * @since        1.6
  125.      */
  126.     public static function getMenuTypes()
  127.     {
  128.         $db JFactory::getDbo();
  129.         $query $db->getQuery(true)
  130.             ->select('a.menutype')
  131.             ->from('#__menu_types AS a');
  132.         $db->setQuery($query);
  133.  
  134.         return $db->loadColumn();
  135.     }
  136.  
  137.     /**
  138.      * Get a list of menu links for one or all menus.
  139.      *
  140.      * @param   string    An option menu to filter the list on, otherwise all menu links are returned as a grouped array.
  141.      * @param   integer   An optional parent ID to pivot results around.
  142.      * @param   integer   An optional mode. If parent ID is set and mode=2, the parent and children are excluded from the list.
  143.      * @param   array     An optional array of states
  144.      */
  145.     public static function getMenuLinks($menuType null$parentId 0$mode 0$published array()$languages array())
  146.     {
  147.         $db JFactory::getDbo();
  148.         $query $db->getQuery(true)
  149.             ->select('a.id AS value, a.title AS text, a.alias, a.level, a.menutype, a.type, a.template_style_id, a.checked_out')
  150.             ->from('#__menu AS a')
  151.             ->join('LEFT'$db->quoteName('#__menu'' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
  152.  
  153.         // Filter by the type
  154.         if ($menuType)
  155.         {
  156.             $query->where('(a.menutype = ' $db->quote($menuType' OR a.parent_id = 0)');
  157.         }
  158.  
  159.         if ($parentId)
  160.         {
  161.             if ($mode == 2)
  162.             {
  163.                 // Prevent the parent and children from showing.
  164.                 $query->join('LEFT''#__menu AS p ON p.id = ' . (int) $parentId)
  165.                     ->where('(a.lft <= p.lft OR a.rgt >= p.rgt)');
  166.             }
  167.         }
  168.  
  169.         if (!empty($languages))
  170.         {
  171.             if (is_array($languages))
  172.             {
  173.                 $languages '(' implode(','array_map(array($db'quote')$languages)) ')';
  174.             }
  175.             $query->where('a.language IN ' $languages);
  176.         }
  177.  
  178.         if (!empty($published))
  179.         {
  180.             if (is_array($published))
  181.             {
  182.                 $published '(' implode(','$published')';
  183.             }
  184.             $query->where('a.published IN ' $published);
  185.         }
  186.  
  187.         $query->where('a.published != -2')
  188.             ->group('a.id, a.title, a.level, a.menutype, a.type, a.template_style_id, a.checked_out, a.lft')
  189.             ->order('a.lft ASC');
  190.  
  191.         // Get the options.
  192.         $db->setQuery($query);
  193.  
  194.         try
  195.         {
  196.             $links $db->loadObjectList();
  197.         }
  198.         catch (RuntimeException $e)
  199.         {
  200.             JError::raiseWarning(500$e->getMessage());
  201.             return false;
  202.         }
  203.  
  204.         if (empty($menuType))
  205.         {
  206.             // If the menutype is empty, group the items by menutype.
  207.             $query->clear()
  208.                 ->select('*')
  209.                 ->from('#__menu_types')
  210.                 ->where('menutype <> ' $db->quote(''))
  211.                 ->order('title, menutype');
  212.             $db->setQuery($query);
  213.  
  214.             try
  215.             {
  216.                 $menuTypes $db->loadObjectList();
  217.             }
  218.             catch (RuntimeException $e)
  219.             {
  220.                 JError::raiseWarning(500$e->getMessage());
  221.                 return false;
  222.             }
  223.  
  224.             // Create a reverse lookup and aggregate the links.
  225.             $rlu array();
  226.             foreach ($menuTypes as &$type)
  227.             {
  228.                 $rlu[$type->menutype$type;
  229.                 $type->links array();
  230.             }
  231.  
  232.             // Loop through the list of menu links.
  233.             foreach ($links as &$link)
  234.             {
  235.                 if (isset($rlu[$link->menutype]))
  236.                 {
  237.                     $rlu[$link->menutype]->links[$link;
  238.  
  239.                     // Cleanup garbage.
  240.                     unset($link->menutype);
  241.                 }
  242.             }
  243.  
  244.             return $menuTypes;
  245.         }
  246.         else
  247.         {
  248.             return $links;
  249.         }
  250.     }
  251.  
  252.     static public function getAssociations($pk)
  253.     {
  254.         $associations array();
  255.         $db JFactory::getDbo();
  256.         $query $db->getQuery(true)
  257.             ->from('#__menu as m')
  258.             ->join('INNER''#__associations as a ON a.id=m.id AND a.context=' $db->quote('com_menus.item'))
  259.             ->join('INNER''#__associations as a2 ON a.key=a2.key')
  260.             ->join('INNER''#__menu as m2 ON a2.id=m2.id')
  261.             ->where('m.id=' . (int) $pk)
  262.             ->select('m2.language, m2.id');
  263.         $db->setQuery($query);
  264.  
  265.         try
  266.         {
  267.             $menuitems $db->loadObjectList('language');
  268.         }
  269.         catch (RuntimeException $e)
  270.         {
  271.             throw new Exception($e->getMessage()500);
  272.         }
  273.  
  274.         foreach ($menuitems as $tag => $item)
  275.         {
  276.             // Do not return itself as result
  277.             if ((int) $item->id != $pk)
  278.             {
  279.                 $associations[$tag$item->id;
  280.             }
  281.         }
  282.         return $associations;
  283.     }
  284. }

Documentation generated on Tue, 19 Nov 2013 15:07:58 +0100 by phpDocumentor 1.4.3