|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CakeCMS Core |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @package Core |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
|
12
|
|
|
* @link https://github.com/CakeCMS/Core". |
|
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Core\Utility; |
|
17
|
|
|
|
|
18
|
|
|
use Cake\Core\App; |
|
19
|
|
|
use JBZoo\Utils\Slug; |
|
20
|
|
|
use Cake\Utility\Inflector; |
|
21
|
|
|
use Core\Toolbar\ToolbarItem; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Toolbar |
|
25
|
|
|
* |
|
26
|
|
|
* @package Core\Utility |
|
27
|
|
|
*/ |
|
28
|
|
|
class Toolbar |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
const DEFAULT_NAME = 'toolbar'; |
|
32
|
|
|
const CLASS_TYPE = 'Toolbar'; |
|
33
|
|
|
const CLASS_NAME_PREFIX = 'ToolbarItem'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Button type objects. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $_buttons = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Toolbar array items. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $_items = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Toolbar name. |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $_name; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Stores the singleton instances of various toolbar. |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected static $_instances = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Toolbar constructor. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $name |
|
67
|
|
|
*/ |
|
68
|
|
|
public function __construct($name = self::DEFAULT_NAME) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->_name = $name; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set a value. |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function appendButton() |
|
79
|
|
|
{ |
|
80
|
|
|
$btn = func_get_args(); |
|
81
|
|
|
array_push($this->_items, $btn); |
|
82
|
|
|
|
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get toolbar instance. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $name |
|
90
|
|
|
* @return array|Toolbar |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function getInstance($name = self::DEFAULT_NAME) |
|
93
|
|
|
{ |
|
94
|
|
|
if (empty(self::$_instances[$name])) { |
|
95
|
|
|
self::$_instances[$name] = new Toolbar($name); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return self::$_instances[$name]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get toolbar items. |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getItems() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->_items; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get toolbar name. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getName() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->_name; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Load object of item type. |
|
123
|
|
|
* |
|
124
|
|
|
* @param $type |
|
125
|
|
|
* @return ToolbarItem|bool |
|
126
|
|
|
*/ |
|
127
|
|
|
public function loadItemType($type) |
|
128
|
|
|
{ |
|
129
|
|
|
$signature = md5($type); |
|
130
|
|
|
if (isset($this->_buttons[$signature])) { |
|
131
|
|
|
return $this->_buttons[$signature]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
list($plugin, $name) = pluginSplit($type); |
|
135
|
|
|
$alias = Slug::filter($name, '_'); |
|
136
|
|
|
$aliasClass = Inflector::classify($alias); |
|
137
|
|
|
|
|
138
|
|
|
$className = $this->_getItemClassName($plugin, $aliasClass); |
|
139
|
|
|
|
|
140
|
|
|
if (!$className) { |
|
|
|
|
|
|
141
|
|
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$this->_buttons[$signature] = new $className($this); |
|
145
|
|
|
return $this->_buttons[$signature]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Set a prepend value. |
|
150
|
|
|
* |
|
151
|
|
|
* @return bool |
|
152
|
|
|
*/ |
|
153
|
|
|
public function prependButton() |
|
154
|
|
|
{ |
|
155
|
|
|
$btn = func_get_args(); |
|
156
|
|
|
array_unshift($this->_items, $btn); |
|
157
|
|
|
|
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Render toolbar items. |
|
163
|
|
|
* |
|
164
|
|
|
* @return string |
|
165
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
|
166
|
|
|
*/ |
|
167
|
|
|
public function render() |
|
168
|
|
|
{ |
|
169
|
|
|
$i = 0; |
|
170
|
|
|
$output = []; |
|
171
|
|
|
$count = count($this->_items); |
|
172
|
|
|
foreach ($this->_items as $item) { |
|
173
|
|
|
$i++; |
|
174
|
|
|
$item['class'] = 'item-wrapper tb-item-' . $i; |
|
175
|
|
|
|
|
176
|
|
|
if ($i == 1) { |
|
177
|
|
|
$item['class'] .= ' first'; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if ($i == $count) { |
|
181
|
|
|
$item['class'] .= ' last'; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$output[] = $this->renderItem($item); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return implode(PHP_EOL, $output); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Render toolbar item html. |
|
192
|
|
|
* |
|
193
|
|
|
* @param $node |
|
194
|
|
|
* @return null|string |
|
195
|
|
|
*/ |
|
196
|
|
|
public function renderItem(&$node) |
|
197
|
|
|
{ |
|
198
|
|
|
$type = $node[0]; |
|
199
|
|
|
$item = $this->loadItemType($type); |
|
200
|
|
|
|
|
201
|
|
|
if ($item === false) { |
|
202
|
|
|
return null; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $item->render($node); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Get full class name. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $plugin |
|
212
|
|
|
* @param string $aliasClass |
|
213
|
|
|
* @return bool|string |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function _getItemClassName($plugin, $aliasClass) |
|
216
|
|
|
{ |
|
217
|
|
|
if ($plugin === null) { |
|
218
|
|
|
$plugin = 'Core'; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$buttonClass = $plugin . '.' . self::CLASS_NAME_PREFIX . $aliasClass; |
|
222
|
|
|
$className = App::className($buttonClass, self::CLASS_TYPE); |
|
223
|
|
|
|
|
224
|
|
|
if ($className === false) { |
|
225
|
|
|
$buttonClass = self::CLASS_NAME_PREFIX . $aliasClass; |
|
226
|
|
|
$className = App::className($buttonClass, self::CLASS_TYPE); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $className; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: