PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/cms/toolbar/toolbar.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 302 lines | 123 code | 36 blank | 143 comment | 13 complexity | 53993feebd81597fa809bc5ae84c3b5e MD5 | raw file
  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. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * ToolBar handler
  12. *
  13. * @package Joomla.Libraries
  14. * @subpackage Toolbar
  15. * @since 1.5
  16. */
  17. class JToolbar
  18. {
  19. /**
  20. * Toolbar name
  21. *
  22. * @var string
  23. */
  24. protected $_name = array();
  25. /**
  26. * Toolbar array
  27. *
  28. * @var array
  29. */
  30. protected $_bar = array();
  31. /**
  32. * Loaded buttons
  33. *
  34. * @var array
  35. */
  36. protected $_buttons = array();
  37. /**
  38. * Directories, where button types can be stored.
  39. *
  40. * @var array
  41. */
  42. protected $_buttonPath = array();
  43. /**
  44. * Stores the singleton instances of various toolbar.
  45. *
  46. * @var JToolbar
  47. * @since 2.5
  48. */
  49. protected static $instances = array();
  50. /**
  51. * Constructor
  52. *
  53. * @param string $name The toolbar name.
  54. *
  55. * @since 1.5
  56. */
  57. public function __construct($name = 'toolbar')
  58. {
  59. $this->_name = $name;
  60. // Set base path to find buttons.
  61. $this->_buttonPath[] = __DIR__ . '/button';
  62. }
  63. /**
  64. * Returns the global JToolbar object, only creating it if it
  65. * doesn't already exist.
  66. *
  67. * @param string $name The name of the toolbar.
  68. *
  69. * @return JToolbar The JToolbar object.
  70. *
  71. * @since 1.5
  72. */
  73. public static function getInstance($name = 'toolbar')
  74. {
  75. if (empty(self::$instances[$name]))
  76. {
  77. self::$instances[$name] = new JToolbar($name);
  78. }
  79. return self::$instances[$name];
  80. }
  81. /**
  82. * Set a value
  83. *
  84. * @return string The set value.
  85. *
  86. * @since 1.5
  87. */
  88. public function appendButton()
  89. {
  90. // Push button onto the end of the toolbar array.
  91. $btn = func_get_args();
  92. array_push($this->_bar, $btn);
  93. return true;
  94. }
  95. /**
  96. * Get the list of toolbar links.
  97. *
  98. * @return array
  99. *
  100. * @since 1.6
  101. */
  102. public function getItems()
  103. {
  104. return $this->_bar;
  105. }
  106. /**
  107. * Get the name of the toolbar.
  108. *
  109. * @return string
  110. *
  111. * @since 1.6
  112. */
  113. public function getName()
  114. {
  115. return $this->_name;
  116. }
  117. /**
  118. * Get a value.
  119. *
  120. * @return string
  121. *
  122. * @since 1.5
  123. */
  124. public function prependButton()
  125. {
  126. // Insert button into the front of the toolbar array.
  127. $btn = func_get_args();
  128. array_unshift($this->_bar, $btn);
  129. return true;
  130. }
  131. /**
  132. * Render a tool bar.
  133. *
  134. * @return string HTML for the toolbar.
  135. *
  136. * @since 1.5
  137. */
  138. public function render()
  139. {
  140. $html = array();
  141. // Start toolbar div.
  142. $html[] = '<div class="btn-toolbar" id="' . $this->_name . '">';
  143. // Render each button in the toolbar.
  144. foreach ($this->_bar as $button)
  145. {
  146. $html[] = $this->renderButton($button);
  147. }
  148. // End toolbar div.
  149. $html[] = '</div>';
  150. return implode("\n", $html);
  151. }
  152. /**
  153. * Render a button.
  154. *
  155. * @param object &$node A toolbar node.
  156. *
  157. * @return string
  158. *
  159. * @since 1.5
  160. */
  161. public function renderButton(&$node)
  162. {
  163. // Get the button type.
  164. $type = $node[0];
  165. $button = $this->loadButtonType($type);
  166. // Check for error.
  167. if ($button === false)
  168. {
  169. return JText::sprintf('JLIB_HTML_BUTTON_NOT_DEFINED', $type);
  170. }
  171. return $button->render($node);
  172. }
  173. /**
  174. * Loads a button type.
  175. *
  176. * @param string $type Button Type
  177. * @param boolean $new False by default
  178. *
  179. * @return boolean
  180. *
  181. * @since 1.5
  182. */
  183. public function loadButtonType($type, $new = false)
  184. {
  185. $signature = md5($type);
  186. if (isset($this->_buttons[$signature]) && $new === false)
  187. {
  188. return $this->_buttons[$signature];
  189. }
  190. if (!class_exists('JToolbarButton'))
  191. {
  192. JLog::add(JText::_('JLIB_HTML_BUTTON_BASE_CLASS'), JLog::WARNING, 'jerror');
  193. return false;
  194. }
  195. $buttonClass = 'JToolbarButton' . ucfirst($type);
  196. // @deprecated 12.3 Remove the acceptance of legacy classes starting with JButton.
  197. $buttonClassOld = 'JButton' . ucfirst($type);
  198. if (!class_exists($buttonClass))
  199. {
  200. if (!class_exists($buttonClassOld))
  201. {
  202. if (isset($this->_buttonPath))
  203. {
  204. $dirs = $this->_buttonPath;
  205. }
  206. else
  207. {
  208. $dirs = array();
  209. }
  210. $file = JFilterInput::getInstance()->clean(str_replace('_', DIRECTORY_SEPARATOR, strtolower($type)) . '.php', 'path');
  211. jimport('joomla.filesystem.path');
  212. if ($buttonFile = JPath::find($dirs, $file))
  213. {
  214. include_once $buttonFile;
  215. }
  216. else
  217. {
  218. JLog::add(JText::sprintf('JLIB_HTML_BUTTON_NO_LOAD', $buttonClass, $buttonFile), JLog::WARNING, 'jerror');
  219. return false;
  220. }
  221. }
  222. }
  223. if (!class_exists($buttonClass) && !class_exists($buttonClassOld))
  224. {
  225. // @todo remove code: 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 boolean
  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. }