PageRenderTime 43ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/html/toolbar.php

https://github.com/joebushi/joomla
PHP | 261 lines | 111 code | 33 blank | 117 comment | 14 complexity | c63fb50b8bab877b10606c54504dd1c5 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Framework
  5. * @subpackage HTML
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access
  10. defined('JPATH_BASE') or die;
  11. //Register the session storage class with the loader
  12. JLoader::register('JButton', dirname(__FILE__).DS.'toolbar'.DS.'button.php');
  13. /**
  14. * ToolBar handler
  15. *
  16. * @package Joomla.Framework
  17. * @subpackage HTML
  18. * @since 1.5
  19. */
  20. class JToolBar extends JObject
  21. {
  22. /**
  23. * Toolbar name
  24. *
  25. * @var string
  26. */
  27. protected $_name = array ();
  28. /**
  29. * Toolbar array
  30. *
  31. * @var array
  32. */
  33. protected $_bar = array ();
  34. /**
  35. * Loaded buttons
  36. *
  37. * @var array
  38. */
  39. protected $_buttons = array ();
  40. /**
  41. * Directories, where button types can be stored.
  42. *
  43. * @var array
  44. */
  45. protected $_buttonPath = array ();
  46. /**
  47. * Constructor
  48. *
  49. * @param string The toolbar name.
  50. */
  51. public function __construct($name = 'toolbar')
  52. {
  53. $this->_name = $name;
  54. // Set base path to find buttons.
  55. $this->_buttonPath[] = dirname(__FILE__).DS.'toolbar'.DS.'button';
  56. }
  57. /**
  58. * Returns the global JToolBar object, only creating it if it
  59. * doesn't already exist.
  60. *
  61. * @access public
  62. * @param string $name The name of the toolbar.
  63. * @return JToolBar The JToolBar object.
  64. */
  65. public static function getInstance($name = 'toolbar')
  66. {
  67. static $instances;
  68. if (!isset($instances)) {
  69. $instances = array ();
  70. }
  71. if (empty($instances[$name])) {
  72. $instances[$name] = new JToolBar($name);
  73. }
  74. return $instances[$name];
  75. }
  76. /**
  77. * Set a value
  78. *
  79. * @param string The name of the param.
  80. * @param string The value of the parameter.
  81. * @return string The set value.
  82. */
  83. public function appendButton()
  84. {
  85. // Push button onto the end of the toolbar array.
  86. $btn = func_get_args();
  87. array_push($this->_bar, $btn);
  88. return true;
  89. }
  90. /**
  91. * Get the list of toolbar links.
  92. *
  93. * @return array
  94. * @since 1.6
  95. */
  96. public function getItems()
  97. {
  98. return $this->_bar;
  99. }
  100. /**
  101. * Get the name of the toolbar.
  102. *
  103. * @return string
  104. * @since 1.6
  105. */
  106. public function getName()
  107. {
  108. return $this->_name;
  109. }
  110. /**
  111. * Get a value.
  112. *
  113. * @param string The name of the param.
  114. * @param mixed The default value if not found.
  115. * @return string
  116. */
  117. public function prependButton()
  118. {
  119. // Insert button into the front of the toolbar array.
  120. $btn = func_get_args();
  121. array_unshift($this->_bar, $btn);
  122. return true;
  123. }
  124. /**
  125. * Render.
  126. *
  127. * @param string The name of the control, or the default text area if a setup file is not found.
  128. * @return string HTML
  129. */
  130. public function render()
  131. {
  132. $html = array ();
  133. // Start toolbar div.
  134. $html[] = '<div class="toolbar-list" id="'.$this->_name.'">';
  135. $html[] = '<ul>';
  136. // Render each button in the toolbar.
  137. foreach ($this->_bar as $button) {
  138. $html[] = $this->renderButton($button);
  139. }
  140. // End toolbar div.
  141. $html[] = '</ul>';
  142. $html[] = '<div class="clr"></div>';
  143. $html[] = '</div>';
  144. return implode("\n", $html);
  145. }
  146. /**
  147. * Render a parameter type.
  148. *
  149. * @param object A param tag node.
  150. * @param string The control name.
  151. * @return array Any array of the label, the form element and the tooltip.
  152. */
  153. public function renderButton(&$node)
  154. {
  155. // Get the button type.
  156. $type = $node[0];
  157. $button = &$this->loadButtonType($type);
  158. // Check for error.
  159. if ($button === false) {
  160. return JText::_('Button not defined for type').' = '.$type;
  161. }
  162. return $button->render($node);
  163. }
  164. /**
  165. * Loads a button type.
  166. *
  167. * @param string buttonType
  168. * @return object
  169. * @since 1.5
  170. */
  171. public function loadButtonType($type, $new = false)
  172. {
  173. $signature = md5($type);
  174. if (isset ($this->_buttons[$signature]) && $new === false) {
  175. return $this->_buttons[$signature];
  176. }
  177. if (!class_exists('JButton'))
  178. {
  179. JError::raiseWarning('SOME_ERROR_CODE', 'Could not load button base class.');
  180. return false;
  181. }
  182. $buttonClass = 'JButton'.$type;
  183. if (!class_exists($buttonClass))
  184. {
  185. if (isset ($this->_buttonPath)) {
  186. $dirs = $this->_buttonPath;
  187. } else {
  188. $dirs = array ();
  189. }
  190. $file = JFilterInput::getInstance()->clean(str_replace('_', DS, strtolower($type)).'.php', 'path');
  191. jimport('joomla.filesystem.path');
  192. if ($buttonFile = JPath::find($dirs, $file)) {
  193. include_once $buttonFile;
  194. } else {
  195. JError::raiseWarning('SOME_ERROR_CODE', "Could not load module $buttonClass ($buttonFile).");
  196. return false;
  197. }
  198. }
  199. if (!class_exists($buttonClass))
  200. {
  201. //return JError::raiseError('SOME_ERROR_CODE', "Module file $buttonFile does not contain class $buttonClass.");
  202. return false;
  203. }
  204. $this->_buttons[$signature] = new $buttonClass($this);
  205. return $this->_buttons[$signature];
  206. }
  207. /**
  208. * Add a directory where JToolBar should search for button types.
  209. *
  210. * You may either pass a string or an array of directories.
  211. *
  212. * {@link JParameter} will be searching for an element type in the same order you
  213. * added them. If the parameter type cannot be found in the custom folders,
  214. * it will look in JParameter/types.
  215. *
  216. * @access public
  217. * @param string|array directory or directories to search.
  218. * @since 1.5
  219. */
  220. public function addButtonPath($path)
  221. {
  222. if (is_array($path)) {
  223. $this->_buttonPath = array_merge($this->_buttonPath, $path);
  224. } else {
  225. array_push($this->_buttonPath, $path);
  226. }
  227. }
  228. }