PageRenderTime 298ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/html/toolbar.php

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