Source for file menu.php

Documentation is available at menu.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  HTML
  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
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Utility class working with menu select lists
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  HTML
  17.  * @since       1.5
  18.  */
  19. abstract class JHtmlMenu
  20. {
  21.     /**
  22.      * Cached array of the menus.
  23.      *
  24.      * @var    array 
  25.      * @since  1.6
  26.      */
  27.     protected static $menus null;
  28.  
  29.     /**
  30.      * Cached array of the menus items.
  31.      *
  32.      * @var    array 
  33.      * @since  1.6
  34.      */
  35.     protected static $items null;
  36.  
  37.     /**
  38.      * Get a list of the available menus.
  39.      *
  40.      * @return  string 
  41.      *
  42.      * @since   1.6
  43.      */
  44.     public static function menus()
  45.     {
  46.         if (empty(static::$menus))
  47.         {
  48.             $db JFactory::getDbo();
  49.             $query $db->getQuery(true)
  50.                 ->select('menutype AS value, title AS text')
  51.                 ->from($db->quoteName('#__menu_types'))
  52.                 ->order('title');
  53.             $db->setQuery($query);
  54.             static::$menus $db->loadObjectList();
  55.         }
  56.  
  57.         return static::$menus;
  58.     }
  59.  
  60.     /**
  61.      * Returns an array of menu items grouped by menu.
  62.      *
  63.      * @param   array  $config  An array of configuration options.
  64.      *
  65.      * @return  array 
  66.      *
  67.      * @since   1.6
  68.      */
  69.     public static function menuitems($config array())
  70.     {
  71.         if (empty(static::$items))
  72.         {
  73.             $menus static::menus();
  74.  
  75.             $db JFactory::getDbo();
  76.             $query $db->getQuery(true)
  77.                 ->select('a.id AS value, a.title AS text, a.level, a.menutype')
  78.                 ->from('#__menu AS a')
  79.                 ->where('a.parent_id > 0')
  80.                 ->where('a.type <> ' $db->quote('url'))
  81.                 ->where('a.client_id = 0');
  82.  
  83.             // Filter on the published state
  84.             if (isset($config['published']))
  85.             {
  86.                 if (is_numeric($config['published']))
  87.                 {
  88.                     $query->where('a.published = ' . (int) $config['published']);
  89.                 }
  90.                 elseif ($config['published'=== '')
  91.                 {
  92.                     $query->where('a.published IN (0,1)');
  93.                 }
  94.             }
  95.  
  96.             $query->order('a.lft');
  97.  
  98.             $db->setQuery($query);
  99.             $items $db->loadObjectList();
  100.  
  101.             // Collate menu items based on menutype
  102.             $lookup array();
  103.  
  104.             foreach ($items as &$item)
  105.             {
  106.                 if (!isset($lookup[$item->menutype]))
  107.                 {
  108.                     $lookup[$item->menutypearray();
  109.                 }
  110.  
  111.                 $lookup[$item->menutype][&$item;
  112.  
  113.                 $item->text str_repeat('- '$item->level$item->text;
  114.             }
  115.  
  116.             static::$items array();
  117.  
  118.             foreach ($menus as &$menu)
  119.             {
  120.                 // Start group:
  121.                 static::$items[JHtml::_('select.optgroup'$menu->text);
  122.  
  123.                 // Special "Add to this Menu" option:
  124.                 static::$items[JHtml::_('select.option'$menu->value '.1'JText::_('JLIB_HTML_ADD_TO_THIS_MENU'));
  125.  
  126.                 // Menu items:
  127.                 if (isset($lookup[$menu->value]))
  128.                 {
  129.                     foreach ($lookup[$menu->valueas &$item)
  130.                     {
  131.                         static::$items[JHtml::_('select.option'$menu->value '.' $item->value$item->text);
  132.                     }
  133.                 }
  134.  
  135.                 // Finish group:
  136.                 static::$items[JHtml::_('select.optgroup'$menu->text);
  137.             }
  138.         }
  139.  
  140.         return static::$items;
  141.     }
  142.  
  143.     /**
  144.      * Displays an HTML select list of menu items.
  145.      *
  146.      * @param   string  $name      The name of the control.
  147.      * @param   string  $selected  The value of the selected option.
  148.      * @param   string  $attribs   Attributes for the control.
  149.      * @param   array   $config    An array of options for the control.
  150.      *
  151.      * @return  string 
  152.      *
  153.      * @since   1.6
  154.      */
  155.     public static function menuitemlist($name$selected null$attribs null$config array())
  156.     {
  157.         static $count;
  158.  
  159.         $options static::menuitems($config);
  160.  
  161.         return JHtml::_(
  162.             'select.genericlist'$options$name,
  163.             array(
  164.                 'id' => isset($config['id']$config['id''assetgroups_' (++$count),
  165.                 'list.attr' => (is_null($attribs'class="inputbox" size="1"' $attribs),
  166.                 'list.select' => (int) $selected,
  167.                 'list.translate' => false
  168.             )
  169.         );
  170.     }
  171.  
  172.     /**
  173.      * Build the select list for Menu Ordering
  174.      *
  175.      * @param   object   &$row  The row object
  176.      * @param   integer  $id    The id for the row. Must exist to enable menu ordering
  177.      *
  178.      * @return  string 
  179.      *
  180.      * @since   1.5
  181.      */
  182.     public static function ordering(&$row$id)
  183.     {
  184.         if ($id)
  185.         {
  186.             $db JFactory::getDbo();
  187.             $query $db->getQuery(true)
  188.                 ->select('ordering AS value, title AS text')
  189.                 ->from($db->quoteName('#__menu'))
  190.                 ->where($db->quoteName('menutype'' = ' $db->quote($row->menutype))
  191.                 ->where($db->quoteName('parent_id'' = ' . (int) $row->parent_id)
  192.                 ->where($db->quoteName('published'' != -2')
  193.                 ->order('ordering');
  194.             $order JHtml::_('list.genericordering'$query);
  195.             $ordering JHtml::_(
  196.                 'select.genericlist'$order'ordering',
  197.                 array('list.attr' => 'class="inputbox" size="1"''list.select' => (int) $row->ordering)
  198.             );
  199.         }
  200.         else
  201.         {
  202.             $ordering '<input type="hidden" name="ordering" value="' $row->ordering . '" />' JText::_('JGLOBAL_NEWITEMSLAST_DESC');
  203.         }
  204.  
  205.         return $ordering;
  206.     }
  207.  
  208.     /**
  209.      * Build the multiple select list for Menu Links/Pages
  210.      *
  211.      * @param   boolean  $all         True if all can be selected
  212.      * @param   boolean  $unassigned  True if unassigned can be selected
  213.      *
  214.      * @return  string 
  215.      *
  216.      * @since   1.5
  217.      */
  218.     public static function linkoptions($all false$unassigned false)
  219.     {
  220.         $db JFactory::getDbo();
  221.  
  222.         // Get a list of the menu items
  223.         $query $db->getQuery(true)
  224.             ->select('m.id, m.parent_id, m.title, m.menutype')
  225.             ->from($db->quoteName('#__menu'' AS m')
  226.             ->where($db->quoteName('m.published'' = 1')
  227.             ->order('m.menutype, m.parent_id, m.ordering');
  228.         $db->setQuery($query);
  229.  
  230.         $mitems $db->loadObjectList();
  231.  
  232.         if (!$mitems)
  233.         {
  234.             $mitems array();
  235.         }
  236.  
  237.         // Establish the hierarchy of the menu
  238.         $children array();
  239.  
  240.         // First pass - collect children
  241.         foreach ($mitems as $v)
  242.         {
  243.             $pt $v->parent_id;
  244.             $list @$children[$pt$children[$ptarray();
  245.             array_push($list$v);
  246.             $children[$pt$list;
  247.         }
  248.  
  249.         // Second pass - get an indent list of the items
  250.         $list static::treerecurse((int) $mitems[0]->parent_id''array()$children999900);
  251.  
  252.         // Code that adds menu name to Display of Page(s)
  253.         $mitems array();
  254.  
  255.         if ($all $unassigned)
  256.         {
  257.             $mitems[JHtml::_('select.option''<OPTGROUP>'JText::_('JOPTION_MENUS'));
  258.  
  259.             if ($all)
  260.             {
  261.                 $mitems[JHtml::_('select.option'0JText::_('JALL'));
  262.             }
  263.  
  264.             if ($unassigned)
  265.             {
  266.                 $mitems[JHtml::_('select.option'-1JText::_('JOPTION_UNASSIGNED'));
  267.             }
  268.  
  269.             $mitems[JHtml::_('select.option''</OPTGROUP>');
  270.         }
  271.  
  272.         $lastMenuType null;
  273.         $tmpMenuType null;
  274.  
  275.         foreach ($list as $list_a)
  276.         {
  277.             if ($list_a->menutype != $lastMenuType)
  278.             {
  279.                 if ($tmpMenuType)
  280.                 {
  281.                     $mitems[JHtml::_('select.option''</OPTGROUP>');
  282.                 }
  283.  
  284.                 $mitems[JHtml::_('select.option''<OPTGROUP>'$list_a->menutype);
  285.                 $lastMenuType $list_a->menutype;
  286.                 $tmpMenuType $list_a->menutype;
  287.             }
  288.  
  289.             $mitems[JHtml::_('select.option'$list_a->id$list_a->title);
  290.         }
  291.  
  292.         if ($lastMenuType !== null)
  293.         {
  294.             $mitems[JHtml::_('select.option''</OPTGROUP>');
  295.         }
  296.  
  297.         return $mitems;
  298.     }
  299.  
  300.     /**
  301.      * Build the list representing the menu tree
  302.      *
  303.      * @param   integer  $id         Id of the menu item
  304.      * @param   string   $indent     The indentation string
  305.      * @param   array    $list       The list to process
  306.      * @param   array    &$children  The children of the current item
  307.      * @param   integer  $maxlevel   The maximum number of levels in the tree
  308.      * @param   integer  $level      The starting level
  309.      * @param   string   $type       Type of link: component, URL, alias, separator
  310.      *
  311.      * @return  array 
  312.      *
  313.      * @since   1.5
  314.      */
  315.     public static function treerecurse($id$indent$list&$children$maxlevel 9999$level 0$type 1)
  316.     {
  317.         if (@$children[$id&& $level <= $maxlevel)
  318.         {
  319.             foreach ($children[$idas $v)
  320.             {
  321.                 $id $v->id;
  322.  
  323.                 if ($type)
  324.                 {
  325.                     $pre '<sup>|_</sup>&#160;';
  326.                     $spacer '.&#160;&#160;&#160;&#160;&#160;&#160;';
  327.                 }
  328.                 else
  329.                 {
  330.                     $pre '- ';
  331.                     $spacer '&#160;&#160;';
  332.                 }
  333.  
  334.                 if ($v->parent_id == 0)
  335.                 {
  336.                     $txt $v->title;
  337.                 }
  338.                 else
  339.                 {
  340.                     $txt $pre $v->title;
  341.                 }
  342.  
  343.                 $list[$id$v;
  344.                 $list[$id]->treename $indent $txt;
  345.                 $list[$id]->children count(@$children[$id]);
  346.                 $list static::treerecurse($id$indent $spacer$list$children$maxlevel$level 1$type);
  347.             }
  348.         }
  349.  
  350.         return $list;
  351.     }
  352. }

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