Source for file menu.php

Documentation is available at menu.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Menu
  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.  * JMenu class
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  Menu
  17.  * @since       1.5
  18.  */
  19. class JMenu
  20. {
  21.     /**
  22.      * Array to hold the menu items
  23.      *
  24.      * @var    array 
  25.      * @since  1.5
  26.      * @deprecated  4.0  Will convert to $items
  27.      */
  28.     protected $_items = array();
  29.  
  30.     /**
  31.      * Identifier of the default menu item
  32.      *
  33.      * @var    integer 
  34.      * @since  1.5
  35.      * @deprecated  4.0  Will convert to $default
  36.      */
  37.     protected $_default = array();
  38.  
  39.     /**
  40.      * Identifier of the active menu item
  41.      *
  42.      * @var    integer 
  43.      * @since  1.5
  44.      * @deprecated  4.0  Will convert to $active
  45.      */
  46.     protected $_active = 0;
  47.  
  48.     /**
  49.      * @var    array  JMenu instances container.
  50.      * @since  1.7
  51.      */
  52.     protected static $instances array();
  53.  
  54.     /**
  55.      * Class constructor
  56.      *
  57.      * @param   array  $options  An array of configuration options.
  58.      *
  59.      * @since   1.5
  60.      */
  61.     public function __construct($options array())
  62.     {
  63.         // Load the menu items
  64.         $this->load();
  65.  
  66.         foreach ($this->_items as $item)
  67.         {
  68.             if ($item->home)
  69.             {
  70.                 $this->_default[trim($item->language)$item->id;
  71.             }
  72.  
  73.             // Decode the item params
  74.             $result new JRegistry;
  75.             $result->loadString($item->params);
  76.             $item->params $result;
  77.         }
  78.     }
  79.  
  80.     /**
  81.      * Returns a JMenu object
  82.      *
  83.      * @param   string  $client   The name of the client
  84.      * @param   array   $options  An associative array of options
  85.      *
  86.      * @return  JMenu  A menu object.
  87.      *
  88.      * @since   1.5
  89.      * @throws  Exception
  90.      */
  91.     public static function getInstance($client$options array())
  92.     {
  93.         if (empty(self::$instances[$client]))
  94.         {
  95.             // Create a JMenu object
  96.             $classname 'JMenu' ucfirst($client);
  97.  
  98.             if (!class_exists($classname))
  99.             {
  100.                 // @deprecated 4.0 Everything in this block is deprecated but the warning is only logged after the file_exists
  101.                 // Load the menu object
  102.                 $info JApplicationHelper::getClientInfo($clienttrue);
  103.  
  104.                 if (is_object($info))
  105.                 {
  106.                     $path $info->path '/includes/menu.php';
  107.  
  108.                     if (file_exists($path))
  109.                     {
  110.                         JLog::add('Non-autoloadable JMenu subclasses are deprecated, support will be removed in 4.0.'JLog::WARNING'deprecated');
  111.                         include_once $path;
  112.                     }
  113.                 }
  114.             }
  115.  
  116.             if (class_exists($classname))
  117.             {
  118.                 self::$instances[$clientnew $classname($options);
  119.             }
  120.             else
  121.             {
  122.                 throw new Exception(JText::sprintf('JLIB_APPLICATION_ERROR_MENU_LOAD'$client)500);
  123.             }
  124.         }
  125.  
  126.         return self::$instances[$client];
  127.     }
  128.  
  129.     /**
  130.      * Get menu item by id
  131.      *
  132.      * @param   integer  $id  The item id
  133.      *
  134.      * @return  mixed    The item object, or null if not found
  135.      *
  136.      * @since   1.5
  137.      */
  138.     public function getItem($id)
  139.     {
  140.         $result null;
  141.  
  142.         if (isset($this->_items[$id]))
  143.         {
  144.             $result &$this->_items[$id];
  145.         }
  146.  
  147.         return $result;
  148.     }
  149.  
  150.     /**
  151.      * Set the default item by id and language code.
  152.      *
  153.      * @param   integer  $id        The menu item id.
  154.      * @param   string   $language  The language cod (since 1.6).
  155.      *
  156.      * @return  boolean  True, if successful
  157.      *
  158.      * @since   1.5
  159.      */
  160.     public function setDefault($id$language '')
  161.     {
  162.         if (isset($this->_items[$id]))
  163.         {
  164.             $this->_default[$language$id;
  165.  
  166.             return true;
  167.         }
  168.  
  169.         return false;
  170.     }
  171.  
  172.     /**
  173.      * Get the default item by language code.
  174.      *
  175.      * @param   string  $language  The language code, default value of * means all.
  176.      *
  177.      * @return  object  The item object
  178.      *
  179.      * @since   1.5
  180.      */
  181.     public function getDefault($language '*')
  182.     {
  183.         if (array_key_exists($language$this->_default))
  184.         {
  185.             return $this->_items[$this->_default[$language]];
  186.         }
  187.         elseif (array_key_exists('*'$this->_default))
  188.         {
  189.             return $this->_items[$this->_default['*']];
  190.         }
  191.         else
  192.         {
  193.             return 0;
  194.         }
  195.     }
  196.  
  197.     /**
  198.      * Set the default item by id
  199.      *
  200.      * @param   integer  $id  The item id
  201.      *
  202.      * @return  mixed  If successful the active item, otherwise null
  203.      *
  204.      * @since   1.5
  205.      */
  206.     public function setActive($id)
  207.     {
  208.         if (isset($this->_items[$id]))
  209.         {
  210.             $this->_active = $id;
  211.             $result &$this->_items[$id];
  212.  
  213.             return $result;
  214.         }
  215.  
  216.         return null;
  217.     }
  218.  
  219.     /**
  220.      * Get menu item by id.
  221.      *
  222.      * @return  object  The item object.
  223.      *
  224.      * @since   1.5
  225.      */
  226.     public function getActive()
  227.     {
  228.         if ($this->_active)
  229.         {
  230.             $item &$this->_items[$this->_active];
  231.  
  232.             return $item;
  233.         }
  234.  
  235.         return null;
  236.     }
  237.  
  238.     /**
  239.      * Gets menu items by attribute
  240.      *
  241.      * @param   mixed    $attributes  The field name(s).
  242.      * @param   mixed    $values      The value(s) of the field. If an array, need to match field names
  243.      *                                 each attribute may have multiple values to lookup for.
  244.      * @param   boolean  $firstonly   If true, only returns the first item found
  245.      *
  246.      * @return  array 
  247.      *
  248.      * @since   1.5
  249.      */
  250.     public function getItems($attributes$values$firstonly false)
  251.     {
  252.         $items array();
  253.         $attributes = (array) $attributes;
  254.         $values = (array) $values;
  255.  
  256.         foreach ($this->_items as $item)
  257.         {
  258.             if (!is_object($item))
  259.             {
  260.                 continue;
  261.             }
  262.  
  263.             $test true;
  264.  
  265.             for ($i 0$count count($attributes)$i $count$i++)
  266.             {
  267.                 if (is_array($values[$i]))
  268.                 {
  269.                     if (!in_array($item->$attributes[$i]$values[$i]))
  270.                     {
  271.                         $test false;
  272.                         break;
  273.                     }
  274.                 }
  275.                 else
  276.                 {
  277.                     if ($item->$attributes[$i!= $values[$i])
  278.                     {
  279.                         $test false;
  280.                         break;
  281.                     }
  282.                 }
  283.             }
  284.  
  285.             if ($test)
  286.             {
  287.                 if ($firstonly)
  288.                 {
  289.                     return $item;
  290.                 }
  291.  
  292.                 $items[$item;
  293.             }
  294.         }
  295.  
  296.         return $items;
  297.     }
  298.  
  299.     /**
  300.      * Gets the parameter object for a certain menu item
  301.      *
  302.      * @param   integer  $id  The item id
  303.      *
  304.      * @return  JRegistry  A JRegistry object
  305.      *
  306.      * @since   1.5
  307.      */
  308.     public function getParams($id)
  309.     {
  310.         if ($menu $this->getItem($id))
  311.         {
  312.             return $menu->params;
  313.         }
  314.         else
  315.         {
  316.             return new JRegistry;
  317.         }
  318.     }
  319.  
  320.     /**
  321.      * Getter for the menu array
  322.      *
  323.      * @return  array 
  324.      *
  325.      * @since   1.5
  326.      */
  327.     public function getMenu()
  328.     {
  329.         return $this->_items;
  330.     }
  331.  
  332.     /**
  333.      * Method to check JMenu object authorization against an access control
  334.      * object and optionally an access extension object
  335.      *
  336.      * @param   integer  $id  The menu id
  337.      *
  338.      * @return  boolean  True if authorised
  339.      *
  340.      * @since   1.5
  341.      */
  342.     public function authorise($id)
  343.     {
  344.         $menu $this->getItem($id);
  345.         $user JFactory::getUser();
  346.  
  347.         if ($menu)
  348.         {
  349.             return in_array((int) $menu->access$user->getAuthorisedViewLevels());
  350.         }
  351.         else
  352.         {
  353.             return true;
  354.         }
  355.     }
  356.  
  357.     /**
  358.      * Loads the menu items
  359.      *
  360.      * @return  array 
  361.      *
  362.      * @since   1.5
  363.      */
  364.     public function load()
  365.     {
  366.         return array();
  367.     }
  368. }

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