PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/html/toolbar.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 302 lines | 123 code | 36 blank | 143 comment | 11 complexity | 71ee1c42ddabedc93a6887662d7b55cf MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. //Register the session storage class with the loader
  11. JLoader::register('JButton', dirname(__FILE__) . '/toolbar/button.php');
  12. /**
  13. * ToolBar handler
  14. *
  15. * @package Joomla.Platform
  16. * @subpackage HTML
  17. * @since 11.1
  18. */
  19. class JToolBar extends JObject
  20. {
  21. /**
  22. * Toolbar name
  23. *
  24. * @var string
  25. */
  26. protected $_name = array();
  27. /**
  28. * Toolbar array
  29. *
  30. * @var array
  31. */
  32. protected $_bar = array();
  33. /**
  34. * Loaded buttons
  35. *
  36. * @var array
  37. */
  38. protected $_buttons = array();
  39. /**
  40. * Directories, where button types can be stored.
  41. *
  42. * @var array
  43. */
  44. protected $_buttonPath = array();
  45. /**
  46. * Constructor
  47. *
  48. * @param string $name The toolbar name.
  49. *
  50. * @since 11.1
  51. */
  52. public function __construct($name = 'toolbar')
  53. {
  54. $this->_name = $name;
  55. // Set base path to find buttons.
  56. $this->_buttonPath[] = dirname(__FILE__) . '/toolbar/button';
  57. }
  58. /**
  59. * Stores the singleton instances of various toolbar.
  60. *
  61. * @var JToolbar
  62. * @since 11.3
  63. */
  64. protected static $instances = array();
  65. /**
  66. * Returns the global JToolBar object, only creating it if it
  67. * doesn't already exist.
  68. *
  69. * @param string $name The name of the toolbar.
  70. *
  71. * @return JToolBar The JToolBar object.
  72. *
  73. * @since 11.1
  74. */
  75. public static function getInstance($name = 'toolbar')
  76. {
  77. if (empty(self::$instances[$name]))
  78. {
  79. self::$instances[$name] = new JToolBar($name);
  80. }
  81. return self::$instances[$name];
  82. }
  83. /**
  84. * Set a value
  85. *
  86. * @return string The set value.
  87. *
  88. * @since 11.1
  89. */
  90. public function appendButton()
  91. {
  92. // Push button onto the end of the toolbar array.
  93. $btn = func_get_args();
  94. array_push($this->_bar, $btn);
  95. return true;
  96. }
  97. /**
  98. * Get the list of toolbar links.
  99. *
  100. * @return array
  101. *
  102. * @since 11.1
  103. */
  104. public function getItems()
  105. {
  106. return $this->_bar;
  107. }
  108. /**
  109. * Get the name of the toolbar.
  110. *
  111. * @return string
  112. *
  113. * @since 11.1
  114. */
  115. public function getName()
  116. {
  117. return $this->_name;
  118. }
  119. /**
  120. * Get a value.
  121. *
  122. * @return string
  123. *
  124. * @since 11.1
  125. */
  126. public function prependButton()
  127. {
  128. // Insert button into the front of the toolbar array.
  129. $btn = func_get_args();
  130. array_unshift($this->_bar, $btn);
  131. return true;
  132. }
  133. /**
  134. * Render a tool bar.
  135. *
  136. * @return string HTML for the toolbar.
  137. *
  138. * @since 11.1
  139. */
  140. public function render()
  141. {
  142. $html = array();
  143. // Start toolbar div.
  144. $html[] = '<div class="toolbar-list" id="' . $this->_name . '">';
  145. $html[] = '<ul>';
  146. // Render each button in the toolbar.
  147. foreach ($this->_bar as $button)
  148. {
  149. $html[] = $this->renderButton($button);
  150. }
  151. // End toolbar div.
  152. $html[] = '</ul>';
  153. $html[] = '<div class="clr"></div>';
  154. $html[] = '</div>';
  155. return implode("\n", $html);
  156. }
  157. /**
  158. * Render a button.
  159. *
  160. * @param object &$node A toolbar node.
  161. *
  162. * @return string
  163. *
  164. * @since 11.1
  165. */
  166. public function renderButton(&$node)
  167. {
  168. // Get the button type.
  169. $type = $node[0];
  170. $button = $this->loadButtonType($type);
  171. // Check for error.
  172. if ($button === false)
  173. {
  174. return JText::sprintf('JLIB_HTML_BUTTON_NOT_DEFINED', $type);
  175. }
  176. return $button->render($node);
  177. }
  178. /**
  179. * Loads a button type.
  180. *
  181. * @param string $type Button Type
  182. * @param boolean $new False by default
  183. *
  184. * @return object
  185. *
  186. * @since 11.1
  187. */
  188. public function loadButtonType($type, $new = false)
  189. {
  190. $signature = md5($type);
  191. if (isset($this->_buttons[$signature]) && $new === false)
  192. {
  193. return $this->_buttons[$signature];
  194. }
  195. if (!class_exists('JButton'))
  196. {
  197. JError::raiseWarning('SOME_ERROR_CODE', JText::_('JLIB_HTML_BUTTON_BASE_CLASS'));
  198. return false;
  199. }
  200. $buttonClass = 'JButton' . $type;
  201. if (!class_exists($buttonClass))
  202. {
  203. if (isset($this->_buttonPath))
  204. {
  205. $dirs = $this->_buttonPath;
  206. }
  207. else
  208. {
  209. $dirs = array();
  210. }
  211. $file = JFilterInput::getInstance()->clean(str_replace('_', DIRECTORY_SEPARATOR, strtolower($type)) . '.php', 'path');
  212. jimport('joomla.filesystem.path');
  213. if ($buttonFile = JPath::find($dirs, $file))
  214. {
  215. include_once $buttonFile;
  216. }
  217. else
  218. {
  219. JError::raiseWarning('SOME_ERROR_CODE', JText::sprintf('JLIB_HTML_BUTTON_NO_LOAD', $buttonClass, $buttonFile));
  220. return false;
  221. }
  222. }
  223. if (!class_exists($buttonClass))
  224. {
  225. //return JError::raiseError('SOME_ERROR_CODE', "Module file $buttonFile does not contain class $buttonClass.");
  226. return false;
  227. }
  228. $this->_buttons[$signature] = new $buttonClass($this);
  229. return $this->_buttons[$signature];
  230. }
  231. /**
  232. * Add a directory where JToolBar should search for button types in LIFO order.
  233. *
  234. * You may either pass a string or an array of directories.
  235. *
  236. * JToolbar will be searching for an element type in the same order you
  237. * added them. If the parameter type cannot be found in the custom folders,
  238. * it will look in libraries/joomla/html/toolbar/button.
  239. *
  240. * @param mixed $path Directory or directories to search.
  241. *
  242. * @return void
  243. *
  244. * @since 11.1
  245. * @see JToolbar
  246. */
  247. public function addButtonPath($path)
  248. {
  249. // Just force path to array.
  250. settype($path, 'array');
  251. // Loop through the path directories.
  252. foreach ($path as $dir)
  253. {
  254. // No surrounding spaces allowed!
  255. $dir = trim($dir);
  256. // Add trailing separators as needed.
  257. if (substr($dir, -1) != DIRECTORY_SEPARATOR)
  258. {
  259. // Directory
  260. $dir .= DIRECTORY_SEPARATOR;
  261. }
  262. // Add to the top of the search dirs.
  263. array_unshift($this->_buttonPath, $dir);
  264. }
  265. }
  266. }