Source for file toolbar.php

Documentation is available at toolbar.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Toolbar
  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.  * ToolBar handler
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  Toolbar
  17.  * @since       1.5
  18.  */
  19. class JToolbar
  20. {
  21.     /**
  22.      * Toolbar name
  23.      *
  24.      * @var    string 
  25.      */
  26.     protected $_name = array();
  27.  
  28.     /**
  29.      * Toolbar array
  30.      *
  31.      * @var    array 
  32.      */
  33.     protected $_bar = array();
  34.  
  35.     /**
  36.      * Loaded buttons
  37.      *
  38.      * @var    array 
  39.      */
  40.     protected $_buttons = array();
  41.  
  42.     /**
  43.      * Directories, where button types can be stored.
  44.      *
  45.      * @var    array 
  46.      */
  47.     protected $_buttonPath = array();
  48.  
  49.     /**
  50.      * Stores the singleton instances of various toolbar.
  51.      *
  52.      * @var    JToolbar 
  53.      * @since  2.5
  54.      */
  55.     protected static $instances array();
  56.  
  57.     /**
  58.      * Constructor
  59.      *
  60.      * @param   string  $name  The toolbar name.
  61.      *
  62.      * @since   1.5
  63.      */
  64.     public function __construct($name 'toolbar')
  65.     {
  66.         $this->_name = $name;
  67.  
  68.         // Set base path to find buttons.
  69.         $this->_buttonPath[= __DIR__ . '/button';
  70.  
  71.     }
  72.  
  73.     /**
  74.      * Returns the global JToolbar object, only creating it if it
  75.      * doesn't already exist.
  76.      *
  77.      * @param   string  $name  The name of the toolbar.
  78.      *
  79.      * @return  JToolbar  The JToolbar object.
  80.      *
  81.      * @since   1.5
  82.      */
  83.     public static function getInstance($name 'toolbar')
  84.     {
  85.         if (empty(self::$instances[$name]))
  86.         {
  87.             self::$instances[$namenew JToolbar($name);
  88.         }
  89.  
  90.         return self::$instances[$name];
  91.     }
  92.  
  93.     /**
  94.      * Set a value
  95.      *
  96.      * @return  string  The set value.
  97.      *
  98.      * @since   1.5
  99.      */
  100.     public function appendButton()
  101.     {
  102.         // Push button onto the end of the toolbar array.
  103.         $btn func_get_args();
  104.         array_push($this->_bar$btn);
  105.         return true;
  106.     }
  107.  
  108.     /**
  109.      * Get the list of toolbar links.
  110.      *
  111.      * @return  array 
  112.      *
  113.      * @since   1.6
  114.      */
  115.     public function getItems()
  116.     {
  117.         return $this->_bar;
  118.     }
  119.  
  120.     /**
  121.      * Get the name of the toolbar.
  122.      *
  123.      * @return  string 
  124.      *
  125.      * @since   1.6
  126.      */
  127.     public function getName()
  128.     {
  129.         return $this->_name;
  130.     }
  131.  
  132.     /**
  133.      * Get a value.
  134.      *
  135.      * @return  string 
  136.      *
  137.      * @since   1.5
  138.      */
  139.     public function prependButton()
  140.     {
  141.         // Insert button into the front of the toolbar array.
  142.         $btn func_get_args();
  143.         array_unshift($this->_bar$btn);
  144.         return true;
  145.     }
  146.  
  147.     /**
  148.      * Render a tool bar.
  149.      *
  150.      * @return  string  HTML for the toolbar.
  151.      *
  152.      * @since   1.5
  153.      */
  154.     public function render()
  155.     {
  156.         $html array();
  157.  
  158.         // Start toolbar div.
  159.         $layout new JLayoutFile('joomla.toolbar.containeropen');
  160.  
  161.         $html[$layout->render(array('id' => $this->_name));
  162.  
  163.         // Render each button in the toolbar.
  164.         foreach ($this->_bar as $button)
  165.         {
  166.             $html[$this->renderButton($button);
  167.         }
  168.  
  169.         // End toolbar div.
  170.         $layout new JLayoutFile('joomla.toolbar.containerclose');
  171.  
  172.         $html[$layout->render(array());
  173.  
  174.         return implode(''$html);
  175.     }
  176.  
  177.     /**
  178.      * Render a button.
  179.      *
  180.      * @param   object  &$node  A toolbar node.
  181.      *
  182.      * @return  string 
  183.      *
  184.      * @since   1.5
  185.      */
  186.     public function renderButton(&$node)
  187.     {
  188.         // Get the button type.
  189.         $type $node[0];
  190.  
  191.         $button $this->loadButtonType($type);
  192.  
  193.         // Check for error.
  194.         if ($button === false)
  195.         {
  196.             return JText::sprintf('JLIB_HTML_BUTTON_NOT_DEFINED'$type);
  197.         }
  198.         return $button->render($node);
  199.     }
  200.  
  201.     /**
  202.      * Loads a button type.
  203.      *
  204.      * @param   string   $type  Button Type
  205.      * @param   boolean  $new   False by default
  206.      *
  207.      * @return  boolean 
  208.      *
  209.      * @since   1.5
  210.      */
  211.     public function loadButtonType($type$new false)
  212.     {
  213.         $signature md5($type);
  214.         if (isset($this->_buttons[$signature]&& $new === false)
  215.         {
  216.             return $this->_buttons[$signature];
  217.         }
  218.  
  219.         if (!class_exists('JToolbarButton'))
  220.         {
  221.             JLog::add(JText::_('JLIB_HTML_BUTTON_BASE_CLASS')JLog::WARNING'jerror');
  222.             return false;
  223.         }
  224.  
  225.         $buttonClass 'JToolbarButton' ucfirst($type);
  226.  
  227.         // @deprecated 12.3 Remove the acceptance of legacy classes starting with JButton.
  228.         $buttonClassOld 'JButton' ucfirst($type);
  229.         if (!class_exists($buttonClass))
  230.         {
  231.             if (!class_exists($buttonClassOld))
  232.             {
  233.                 if (isset($this->_buttonPath))
  234.                 {
  235.                     $dirs $this->_buttonPath;
  236.                 }
  237.                 else
  238.                 {
  239.                     $dirs array();
  240.                 }
  241.  
  242.                 $file JFilterInput::getInstance()->clean(str_replace('_'DIRECTORY_SEPARATORstrtolower($type)) '.php''path');
  243.  
  244.                 jimport('joomla.filesystem.path');
  245.                 if ($buttonFile JPath::find($dirs$file))
  246.                 {
  247.                     include_once $buttonFile;
  248.                 }
  249.                 else
  250.                 {
  251.                     JLog::add(JText::sprintf('JLIB_HTML_BUTTON_NO_LOAD'$buttonClass$buttonFile)JLog::WARNING'jerror');
  252.                     return false;
  253.                 }
  254.             }
  255.         }
  256.  
  257.         if (!class_exists($buttonClass&& !class_exists($buttonClassOld))
  258.         {
  259.             // @todo remove code: return    JError::raiseError('SOME_ERROR_CODE', "Module file $buttonFile does not contain class $buttonClass.");
  260.             return false;
  261.         }
  262.         $this->_buttons[$signaturenew $buttonClass($this);
  263.  
  264.         return $this->_buttons[$signature];
  265.     }
  266.  
  267.     /**
  268.      * Add a directory where JToolbar should search for button types in LIFO order.
  269.      *
  270.      * You may either pass a string or an array of directories.
  271.      *
  272.      * JToolbar will be searching for an element type in the same order you
  273.      * added them. If the parameter type cannot be found in the custom folders,
  274.      * it will look in libraries/joomla/html/toolbar/button.
  275.      *
  276.      * @param   mixed  $path  Directory or directories to search.
  277.      *
  278.      * @return  void 
  279.      *
  280.      * @since   boolean
  281.      * @see     JToolbar
  282.      */
  283.     public function addButtonPath($path)
  284.     {
  285.         // Just force path to array.
  286.         settype($path'array');
  287.  
  288.         // Loop through the path directories.
  289.         foreach ($path as $dir)
  290.         {
  291.             // No surrounding spaces allowed!
  292.             $dir trim($dir);
  293.  
  294.             // Add trailing separators as needed.
  295.             if (substr($dir-1!= DIRECTORY_SEPARATOR)
  296.             {
  297.                 // Directory
  298.                 $dir .= DIRECTORY_SEPARATOR;
  299.             }
  300.  
  301.             // Add to the top of the search dirs.
  302.             array_unshift($this->_buttonPath$dir);
  303.         }
  304.  
  305.     }
  306. }

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