Source for file menu.php

Documentation is available at menu.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  mod_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.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Tree based class to render the admin menu
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  mod_menu
  17.  * @since       1.5
  18.  */
  19. class JAdminCssMenu extends JObject
  20. {
  21.     /**
  22.      * CSS string to add to document head
  23.      *
  24.      * @var  string 
  25.      */
  26.     protected $_css = null;
  27.  
  28.     /**
  29.      * Root node
  30.      *
  31.      * @var  object 
  32.      */
  33.     protected $_root = null;
  34.  
  35.     /**
  36.      * Current working node
  37.      *
  38.      * @var  object 
  39.      */
  40.     protected $_current = null;
  41.  
  42.     /**
  43.      * Constructor
  44.      */
  45.     public function __construct()
  46.     {
  47.         $this->_root = new JMenuNode('ROOT');
  48.         $this->_current = $this->_root;
  49.     }
  50.  
  51.     /**
  52.      * Method to add a child
  53.      *
  54.      * @param   JMenuNode  &$node       The node to process
  55.      * @param   boolean    $setCurrent  True to set as current working node
  56.      *
  57.      * @return  void 
  58.      */
  59.     public function addChild(JMenuNode &$node$setCurrent false)
  60.     {
  61.         $this->_current->addChild($node);
  62.  
  63.         if ($setCurrent)
  64.         {
  65.             $this->_current = &$node;
  66.         }
  67.     }
  68.  
  69.     /**
  70.      * Method to get the parent
  71.      *
  72.      * @return  void 
  73.      */
  74.     public function getParent()
  75.     {
  76.         $this->_current = &$this->_current->getParent();
  77.     }
  78.  
  79.     /**
  80.      * Method to get the parent
  81.      *
  82.      * @return  void 
  83.      */
  84.     public function reset()
  85.     {
  86.         $this->_current = &$this->_root;
  87.     }
  88.  
  89.     /**
  90.      * Method to add a separator node
  91.      *
  92.      * @return  void 
  93.      */
  94.     public function addSeparator()
  95.     {
  96.         $this->addChild(new JMenuNode(nullnull'separator'false));
  97.     }
  98.  
  99.     /**
  100.      * Method to render the menu
  101.      *
  102.      * @param   string  $id     The id of the menu to be rendered
  103.      * @param   string  $class  The class of the menu to be rendered
  104.      *
  105.      * @return  void 
  106.      */
  107.     public function renderMenu($id 'menu'$class '')
  108.     {
  109.         $depth 1;
  110.  
  111.         if (!empty($id))
  112.         {
  113.             $id 'id="' $id '"';
  114.         }
  115.  
  116.         if (!empty($class))
  117.         {
  118.             $class 'class="' $class '"';
  119.         }
  120.  
  121.         // Recurse through children if they exist
  122.         while ($this->_current->hasChildren())
  123.         {
  124.             echo "<ul " $id " " $class ">\n";
  125.  
  126.             foreach ($this->_current->getChildren(as $child)
  127.             {
  128.                 $this->_current = $child;
  129.                 $this->renderLevel($depth++);
  130.             }
  131.  
  132.             echo "</ul>\n";
  133.         }
  134.  
  135.         if ($this->_css)
  136.         {
  137.             // Add style to document head
  138.             JFactory::getDocument()->addStyleDeclaration($this->_css);
  139.         }
  140.     }
  141.  
  142.     /**
  143.      * Method to render a given level of a menu
  144.      *
  145.      * @param   integer  $depth  The level of the menu to be rendered
  146.      *
  147.      * @return  void 
  148.      */
  149.     public function renderLevel($depth)
  150.     {
  151.         // Build the CSS class suffix
  152.         $class '';
  153.  
  154.         if ($this->_current->hasChildren())
  155.         {
  156.             $class ' class="dropdown"';
  157.         }
  158.  
  159.         if ($this->_current->class == 'separator')
  160.         {
  161.             $class ' class="divider"';
  162.         }
  163.  
  164.         if ($this->_current->hasChildren(&& $this->_current->class)
  165.         {
  166.             $class ' class="dropdown-submenu"';
  167.         }
  168.  
  169.         if ($this->_current->class == 'disabled')
  170.         {
  171.             $class ' class="disabled"';
  172.         }
  173.  
  174.         // Print the item
  175.         echo "<li" $class ">";
  176.  
  177.         // Print a link if it exists
  178.         $linkClass array();
  179.         $dataToggle '';
  180.         $dropdownCaret '';
  181.  
  182.         if ($this->_current->hasChildren())
  183.         {
  184.             $linkClass['dropdown-toggle';
  185.             $dataToggle ' data-toggle="dropdown"';
  186.  
  187.             if (!$this->_current->getParent()->hasParent())
  188.             {
  189.                 $dropdownCaret ' <span class="caret"></span>';
  190.             }
  191.         }
  192.  
  193.         if ($this->_current->link != null && $this->_current->getParent()->title != 'ROOT')
  194.         {
  195.             $iconClass $this->getIconClass($this->_current->class);
  196.  
  197.             if (!empty($iconClass))
  198.             {
  199.                 $linkClass[$iconClass;
  200.             }
  201.         }
  202.  
  203.         // Implode out $linkClass for rendering
  204.         $linkClass ' class="' implode(' '$linkClass'"';
  205.  
  206.         if ($this->_current->link != null && $this->_current->target != null)
  207.         {
  208.             echo "<a" $linkClass " " $dataToggle " href=\"" $this->_current->link "\" target=\"" $this->_current->target "\" >"
  209.                 . $this->_current->title $dropdownCaret "</a>";
  210.         }
  211.         elseif ($this->_current->link != null && $this->_current->target == null)
  212.         {
  213.             echo "<a" $linkClass " " $dataToggle " href=\"" $this->_current->link "\">" $this->_current->title $dropdownCaret "</a>";
  214.         }
  215.         elseif ($this->_current->title != null)
  216.         {
  217.             echo "<a" $linkClass " " $dataToggle ">" $this->_current->title $dropdownCaret "</a>";
  218.         }
  219.         else
  220.         {
  221.             echo "<span></span>";
  222.         }
  223.  
  224.         // Recurse through children if they exist
  225.         while ($this->_current->hasChildren())
  226.         {
  227.             if ($this->_current->class)
  228.             {
  229.                 $id '';
  230.  
  231.                 if (!empty($this->_current->id))
  232.                 {
  233.                     $id ' id="menu-' strtolower($this->_current->id'"';
  234.                 }
  235.  
  236.                 echo '<ul' $id ' class="dropdown-menu menu-component">' "\n";
  237.             }
  238.             else
  239.             {
  240.                 echo '<ul class="dropdown-menu">' "\n";
  241.             }
  242.  
  243.             foreach ($this->_current->getChildren(as $child)
  244.             {
  245.                 $this->_current = $child;
  246.                 $this->renderLevel($depth++);
  247.             }
  248.  
  249.             echo "</ul>\n";
  250.         }
  251.  
  252.         echo "</li>\n";
  253.     }
  254.  
  255.     /**
  256.      * Method to get the CSS class name for an icon identifier or create one if
  257.      * a custom image path is passed as the identifier
  258.      *
  259.      * @param   string  $identifier  Icon identification string
  260.      *
  261.      * @return  string    CSS class name
  262.      *
  263.      * @since   1.5
  264.      */
  265.     public function getIconClass($identifier)
  266.     {
  267.         static $classes;
  268.  
  269.         // Initialise the known classes array if it does not exist
  270.         if (!is_array($classes))
  271.         {
  272.             $classes array();
  273.         }
  274.  
  275.         /*
  276.          * If we don't already know about the class... build it and mark it
  277.          * known so we don't have to build it again
  278.          */
  279.         if (!isset($classes[$identifier]))
  280.         {
  281.             if (substr($identifier06== 'class:')
  282.             {
  283.                 // We were passed a class name
  284.                 $class substr($identifier6);
  285.                 $classes[$identifier"menu-$class";
  286.             }
  287.             else
  288.             {
  289.                 if ($identifier == null)
  290.                 {
  291.                     return null;
  292.                 }
  293.  
  294.                 // Build the CSS class for the icon
  295.                 $class preg_replace('#\.[^.]*$#'''basename($identifier));
  296.                 $class preg_replace('#\.\.[^A-Za-z0-9\.\_\- ]#'''$class);
  297.  
  298.                 $this->_css  .= "\n.menu-$class {\n.
  299.                         "\tbackground: url($identifier) no-repeat;\n.
  300.                         "}\n";
  301.  
  302.                 $classes[$identifier"menu-$class";
  303.             }
  304.         }
  305.  
  306.         return $classes[$identifier];
  307.     }
  308. }
  309.  
  310. /**
  311.  * A Node for JAdminCssMenu
  312.  *
  313.  * @package     Joomla.Administrator
  314.  * @subpackage  mod_menu
  315.  * @see         JAdminCssMenu
  316.  * @since       1.5
  317.  */
  318. class JMenuNode extends JObject
  319. {
  320.     /**
  321.      * Node Title
  322.      *
  323.      * @var  string 
  324.      */
  325.     public $title = null;
  326.  
  327.     /**
  328.      * Node Id
  329.      *
  330.      * @var  string 
  331.      */
  332.     public $id = null;
  333.  
  334.     /**
  335.      * Node Link
  336.      *
  337.      * @var  string 
  338.      */
  339.     public $link = null;
  340.  
  341.     /**
  342.      * Link Target
  343.      *
  344.      * @var  string 
  345.      */
  346.     public $target = null;
  347.  
  348.     /**
  349.      * CSS Class for node
  350.      *
  351.      * @var  string 
  352.      */
  353.     public $class = null;
  354.  
  355.     /**
  356.      * Active Node?
  357.      *
  358.      * @var  boolean 
  359.      */
  360.     public $active = false;
  361.  
  362.     /**
  363.      * Parent node
  364.      *
  365.      * @var  JMenuNode 
  366.      */
  367.     protected $_parent = null;
  368.  
  369.     /**
  370.      * Array of Children
  371.      *
  372.      * @var  array 
  373.      */
  374.     protected $_children = array();
  375.  
  376.     /**
  377.      * Constructor for the class.
  378.      *
  379.      * @param   string   $title      The title of the node
  380.      * @param   string   $link       The node link
  381.      * @param   string   $class      The CSS class for the node
  382.      * @param   boolean  $active     True if node is active, false otherwise
  383.      * @param   string   $target     The link target
  384.      * @param   string   $titleicon  The title icon for the node
  385.      */
  386.     public function __construct($title$link null$class null$active false$target null$titleicon null)
  387.     {
  388.         $this->title  = $titleicon $title $titleicon $title;
  389.         $this->link   = JFilterOutput::ampReplace($link);
  390.         $this->class  = $class;
  391.         $this->active = $active;
  392.  
  393.         $this->id = null;
  394.  
  395.         if (!empty($link&& $link !== '#')
  396.         {
  397.             $uri   new JUri($link);
  398.             $params $uri->getQuery(true);
  399.             $parts  array();
  400.  
  401.             foreach ($params as $value)
  402.             {
  403.                 $parts[str_replace(array('.''_')'-'$value);
  404.             }
  405.  
  406.             $this->id = implode('-'$parts);
  407.         }
  408.  
  409.         $this->target = $target;
  410.     }
  411.  
  412.     /**
  413.      * Add child to this node
  414.      *
  415.      * If the child already has a parent, the link is unset
  416.      *
  417.      * @param   JMenuNode  &$child  The child to be added
  418.      *
  419.      * @return  void 
  420.      */
  421.     public function addChild(JMenuNode &$child)
  422.     {
  423.         $child->setParent($this);
  424.     }
  425.  
  426.     /**
  427.      * Set the parent of a this node
  428.      *
  429.      * If the node already has a parent, the link is unset
  430.      *
  431.      * @param   JMenuNode  &$parent  The JMenuNode for parent to be set or null
  432.      *
  433.      * @return  void 
  434.      */
  435.     public function setParent(JMenuNode &$parent null)
  436.     {
  437.         $hash spl_object_hash($this);
  438.  
  439.         if (!is_null($this->_parent))
  440.         {
  441.             unset($this->_parent->children[$hash]);
  442.         }
  443.  
  444.         if (!is_null($parent))
  445.         {
  446.             $parent->_children[$hash$this;
  447.         }
  448.  
  449.         $this->_parent = $parent;
  450.     }
  451.  
  452.     /**
  453.      * Get the children of this node
  454.      *
  455.      * @return  array  The children
  456.      */
  457.     public function &getChildren()
  458.     {
  459.         return $this->_children;
  460.     }
  461.  
  462.     /**
  463.      * Get the parent of this node
  464.      *
  465.      * @return  mixed  JMenuNode object with the parent or null for no parent
  466.      */
  467.     public function &getParent()
  468.     {
  469.         return $this->_parent;
  470.     }
  471.  
  472.     /**
  473.      * Test if this node has children
  474.      *
  475.      * @return  boolean  True if there are children
  476.      */
  477.     public function hasChildren()
  478.     {
  479.         return (bool) count($this->_children);
  480.     }
  481.  
  482.     /**
  483.      * Test if this node has a parent
  484.      *
  485.      * @return  boolean  True if there is a parent
  486.      */
  487.     public function hasParent()
  488.     {
  489.         return $this->getParent(!= null;
  490.     }
  491. }

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