Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/application/menu.php

Documentation is available at menu.php

  1. <?php
  2. /**
  3.  * @version        $Id: menu.php 6634 2007-02-15 18:27:18Z Jinx $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Application
  6.  * @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19.  * JMenu class
  20.  *
  21.  * @author Louis Landry   <[email protected]>
  22.  * @author Johan Janssens <[email protected]>
  23.  * @package        Joomla.Framework
  24.  * @subpackage    Application
  25.  * @since        1.5
  26.  */
  27. class JMenu extends JObject
  28. {
  29.     /**
  30.      * Array to hold the menu items
  31.      *
  32.      * @access private
  33.      * @param array 
  34.      */
  35.     var $_items array ();
  36.  
  37.     /**
  38.      * Identifier of the default menu item
  39.      *
  40.      * @access private
  41.      * @param integer 
  42.      */
  43.     var $_default 0;
  44.  
  45.     /**
  46.      * Identifier of the active menu item
  47.      *
  48.      * @access private
  49.      * @param integer 
  50.      */
  51.     var $_active 0;
  52.  
  53.  
  54.     /**
  55.      * Class constructor
  56.      *
  57.      * @access public
  58.      * @return boolean True on success
  59.      */
  60.     function __construct()
  61.     {
  62.         $this->_items $this->_load();
  63.  
  64.         $home 0;
  65.         $n count($this->_items );
  66.         foreach ($this->_items as $k => $item)
  67.         {
  68.             if ($item->home{
  69.                 $this->_default $item->id;
  70.             }
  71.         }
  72.     }
  73.  
  74.     /**
  75.      * Returns a reference to the global JMenu object, only creating it if it
  76.      * doesn't already exist.
  77.      *
  78.      * This method must be invoked as:
  79.      *         <pre>  $menu = &JMenu::getInstance();</pre>
  80.      *
  81.      * @access    public
  82.      * @return    JMenu    The Menu object.
  83.      * @since    1.5
  84.      */
  85.     function &getInstance()
  86.     {
  87.         static $instance;
  88.  
  89.         if (!$instance{
  90.             $instance new JMenu();
  91.         }
  92.  
  93.         return $instance;
  94.     }
  95.  
  96.     /**
  97.      * Get menu item by id
  98.      *
  99.      * @access public
  100.      * @param int The item id
  101.      * @return mixed The item object, or null if not found
  102.      */
  103.     function &getItem($id)
  104.     {
  105.         $result null;
  106.         if (isset($this->_items[$id])) {
  107.             $result &$this->_items[$id];
  108.         }
  109.  
  110.         return $result;
  111.     }
  112.  
  113.     /**
  114.      * Set the default item by id
  115.      *
  116.      * @param int The item id
  117.      * @access public
  118.      * @return True, if succesfull
  119.      */
  120.     function setDefault($id)
  121.     {
  122.         if(exists($this->_items[$id])) {
  123.             $this->_default $id;
  124.             return true;
  125.         }
  126.  
  127.         return false;
  128.     }
  129.  
  130.     /**
  131.      * Get menu item by id
  132.      *
  133.      * @access public
  134.      *
  135.      * @return object The item object
  136.      */
  137.     function &getDefault()
  138.     {
  139.         $item =$this->_items[$this->_default];
  140.         return $item;
  141.     }
  142.  
  143.     /**
  144.      * Set the default item by id
  145.      *
  146.      * @param int The item id
  147.      * @access public
  148.      * @return True, if succesfull
  149.      */
  150.     function setActive($id)
  151.     {
  152.         if(isset($this->_items[$id])) {
  153.             $this->_active $id;
  154.             return true;
  155.         }
  156.  
  157.         return false;
  158.     }
  159.  
  160.     /**
  161.      * Get menu item by id
  162.      *
  163.      * @access public
  164.      *
  165.      * @return object The item object
  166.      */
  167.     function &getActive()
  168.     {
  169.         $item =$this->_items[$this->_active];
  170.         return $item;
  171.     }
  172.  
  173.     /**
  174.      * Gets menu items by attribute
  175.      *
  176.      * @access public
  177.      * @param string     The field name
  178.      * @param string     The value of the field
  179.      * @param boolean     If true, only returns the first item found
  180.      * @return array 
  181.      */
  182.     function getItems($attribute$value$firstonly false)
  183.     {
  184.         $items array ();
  185.         foreach ($this->_items as  $item)
  186.         {
  187.             if is_object($item) )
  188.                 continue;
  189.                 
  190.             if ($item->$attribute == $value)
  191.             {
  192.                 if($firstonly{
  193.                     return $item;
  194.                 }
  195.  
  196.                 $items[$item;
  197.             }
  198.         }
  199.  
  200.         return $items;
  201.     }
  202.  
  203.     /**
  204.      * Gets the parameter object for a certain menu item
  205.      *
  206.      * @access public
  207.      * @param int The item id
  208.      * @return object JParameter object
  209.      */
  210.     function &getParams($id)
  211.     {
  212.         $ini '';
  213.         if ($menu =$this->getItem($id)) {
  214.             $ini $menu->params;
  215.         }
  216.         $result new JParameter$ini );
  217.  
  218.         return $result;
  219.     }
  220.  
  221.     /**
  222.      * Getter for the menu array
  223.      *
  224.      * @access public
  225.      * @param string $name The menu name
  226.      * @return array 
  227.      */
  228.     function getMenu({
  229.         return $this->_items;
  230.     }
  231.  
  232.     /**
  233.      * Method to check JMenu object authorization against an access control
  234.      * object and optionally an access extension object
  235.      *
  236.      * @access     public
  237.      * @param    integer    $id            The menu id
  238.      * @param    integer    $accessid    The users access identifier
  239.      * @return    boolean    True if authorized
  240.      */
  241.     function authorize($id$accessid 0)
  242.     {
  243.         $menu =$this->getItem($id);
  244.         return ($menu->access <= $accessid);
  245.     }
  246.  
  247.     /**
  248.      * Loads the entire menu table into memory
  249.      *
  250.      * @access protected
  251.      * @return array 
  252.      */
  253.     function _load()
  254.     {
  255.         static $menus;
  256.  
  257.         if (isset ($menus)) {
  258.             return $menus;
  259.         }
  260.         // Initialize some variables
  261.         $db        JFactory::getDBO();
  262.         $user    JFactory::getUser();
  263.         $sql    'SELECT m.*, c.option as component' .
  264.                 ' FROM #__menu AS m' .
  265.                 ' LEFT JOIN #__components AS c ON m.componentid = c.id'.
  266.                 ' WHERE m.published = 1'.
  267.                 ' ORDER BY m.sublevel, m.parent, m.ordering';
  268.         $db->setQuery($sql);
  269.  
  270.         if (!($menus $db->loadObjectList('id'))) {
  271.             JError::raiseWarning('SOME_ERROR_CODE'"Error loading Menus: ".$db->getErrorMsg());
  272.             return false;
  273.         }
  274.  
  275.         jimport('joomla.filter.output');
  276.  
  277.         foreach($menus as $key => $menu)
  278.         {
  279.             $menus[$key]->alias JOutputFilter::stringURLSafe($menu->name);
  280.  
  281.             //Get parent information
  282.             $parent_route '';
  283.             $parent_tree  array();
  284.             if($parent $menus[$key]->parent{
  285.                 $parent_route $menus[$parent]->route.'/';
  286.                 $parent_tree  $menus[$parent]->tree;
  287.             
  288.  
  289.             //Create tree
  290.             array_push($parent_tree$menus[$key]->id);
  291.             $menus[$key]->tree   $parent_tree;
  292.  
  293.             //Create route
  294.             $route $parent_route.$menus[$key]->alias;
  295.             $menus[$key]->route  $route;
  296.  
  297.             $url JURI::getInstance($menus[$key]->link);
  298.             $menus[$key]->query $url->getQuery(true);
  299.         }
  300.  
  301.         return $menus;
  302.     }
  303. }
  304. ?>

Documentation generated on Mon, 05 Mar 2007 21:10:34 +0000 by phpDocumentor 1.3.1