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