|
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\View\Helper\Traits; |
|
17
|
|
|
|
|
18
|
|
|
use JBZoo\Utils\Arr; |
|
19
|
|
|
use JBZoo\Utils\Str; |
|
20
|
|
|
use Cake\Utility\Hash; |
|
21
|
|
|
use Core\View\Helper\HtmlHelper; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class HelperTrait |
|
25
|
|
|
* |
|
26
|
|
|
* @package Core\View\Helper\Traits |
|
27
|
|
|
* @property HtmlHelper $Html |
|
28
|
|
|
*/ |
|
29
|
|
|
trait HelperTrait |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Adds the given class to the element options |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $options Array options/attributes to add a class to |
|
36
|
|
|
* @param string $class The class name being added. |
|
37
|
|
|
* @param string $key the key to use for class. |
|
38
|
|
|
* @return array Array of options with $key set. |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function _addClass(array $options = [], $class = null, $key = 'class') |
|
41
|
|
|
{ |
|
42
|
|
|
if (Arr::key($key, $options) && Str::trim($options[$key])) { |
|
43
|
|
|
$options[$key] .= ' ' . $class; |
|
44
|
|
|
} else { |
|
45
|
|
|
$options[$key] = $class; |
|
46
|
|
|
} |
|
47
|
|
|
return $options; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get class with union prefix. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $class |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function _class($class = 'cms') |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->_configRead('classPrefix') . '-' . Str::trim(Str::slug($class)); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Create current icon. |
|
63
|
|
|
* |
|
64
|
|
|
* @param HtmlHelper $html |
|
65
|
|
|
* @param string|int $title |
|
66
|
|
|
* @param array $options |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function _createIcon(HtmlHelper $html, $title, array $options = []) |
|
70
|
|
|
{ |
|
71
|
|
|
list($options, $iconOptions) = $this->_createIconAttr($options); |
|
72
|
|
|
|
|
73
|
|
|
if (Arr::key('createIcon', $iconOptions)) { |
|
74
|
|
|
unset($iconOptions['createIcon']); |
|
75
|
|
|
$title = $html->icon($options['icon'], $iconOptions) . PHP_EOL . $title; |
|
76
|
|
|
unset($options['icon']); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (Arr::key('iconInline', $options)) { |
|
80
|
|
|
unset($options['iconInline']); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return [$title, $options]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Create icon attributes. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $options |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function _createIconAttr(array $options = []) |
|
93
|
|
|
{ |
|
94
|
|
|
$iconOptions = ['class' => '']; |
|
95
|
|
|
|
|
96
|
|
|
if (Arr::key('icon', $options)) { |
|
97
|
|
|
if (Arr::key('iconClass', $options)) { |
|
98
|
|
|
$iconOptions = $this->_addClass($iconOptions, $options['iconClass']); |
|
99
|
|
|
unset($options['iconClass']); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
list ($options, $iconOptions) = $this->_setIconOptions($options, $iconOptions); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return [$options, $iconOptions]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Create and get button classes. |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $options |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function _getBtnClass(array $options = []) |
|
115
|
|
|
{ |
|
116
|
|
|
if (Arr::key('button', $options)) { |
|
117
|
|
|
$button = $options['button']; |
|
118
|
|
|
unset($options['button']); |
|
119
|
|
|
|
|
120
|
|
|
if (is_callable($this->getConfig('prepareBtnClass'))) { |
|
|
|
|
|
|
121
|
|
|
return (array) call_user_func($this->getConfig('prepareBtnClass'), $this, $options, $button); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$options = $this->_setBtnClass($button, $options); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $options; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Create and get tooltip attributes. |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $options |
|
134
|
|
|
* @param string $toggle |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function _getToolTipAttr(array $options = [], $toggle = 'tooltip') |
|
138
|
|
|
{ |
|
139
|
|
|
if (Arr::key('tooltip', $options)) { |
|
140
|
|
|
$tooltip = $options['tooltip']; |
|
141
|
|
|
unset($options['tooltip']); |
|
142
|
|
|
|
|
143
|
|
|
if (is_callable($this->getConfig('prepareTooltip'))) { |
|
|
|
|
|
|
144
|
|
|
return (array) call_user_func($this->getConfig('prepareTooltip'), $this, $options, $tooltip); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$_options = [ |
|
148
|
|
|
'data-toggle' => $toggle, |
|
149
|
|
|
'data-placement' => 'top', |
|
150
|
|
|
]; |
|
151
|
|
|
|
|
152
|
|
View Code Duplication |
if (Arr::key('tooltipPos', $options)) { |
|
|
|
|
|
|
153
|
|
|
$_options['data-placement'] = (string) $options['tooltipPos']; |
|
154
|
|
|
unset($options['tooltipPos']); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$options = $this->_setTooltipTitle($tooltip, $options); |
|
158
|
|
|
|
|
159
|
|
|
return Hash::merge($_options, $options); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $options; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Prepare before after content for input container. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string|int $type |
|
169
|
|
|
* @param string|int $value |
|
170
|
|
|
* @return null|string |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function _prepareBeforeAfterContainer($type = 'before', $value) |
|
173
|
|
|
{ |
|
174
|
|
|
$output = null; |
|
175
|
|
|
$iconClass = ($type === 'before') ? 'prefix' : 'postfix'; |
|
176
|
|
|
|
|
177
|
|
|
if ($value !== null) { |
|
178
|
|
|
$output = $value; |
|
179
|
|
|
if (is_string($output)) { |
|
180
|
|
|
$hasIcon = preg_match('/icon:[a-zA-Z]/', $output); |
|
181
|
|
|
if ($hasIcon > 0) { |
|
182
|
|
|
list (, $icon) = explode(':', $output, 2); |
|
183
|
|
|
$icon = Str::low($icon); |
|
184
|
|
|
$output = $this->Html->icon($icon, ['class' => $iconClass]); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $output; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Setup button classes by options. |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $button |
|
196
|
|
|
* @param array $options |
|
197
|
|
|
* @return array |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function _setBtnClass($button, array $options = []) |
|
200
|
|
|
{ |
|
201
|
|
|
if ($button !== true) { |
|
202
|
|
|
$classes = [$this->_configRead('btnPref')]; |
|
|
|
|
|
|
203
|
|
|
foreach ((array) $button as $button) { |
|
204
|
|
|
$classes[] = $this->_configRead('btnPref') . '-' . $button; |
|
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
$options = $this->_addClass($options, implode(' ', $classes)); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $options; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Setup icon options. |
|
214
|
|
|
* |
|
215
|
|
|
* @param array $options |
|
216
|
|
|
* @param array $iconOptions |
|
217
|
|
|
* @return array |
|
218
|
|
|
*/ |
|
219
|
|
|
protected function _setIconOptions(array $options = [], array $iconOptions = []) |
|
220
|
|
|
{ |
|
221
|
|
|
$icon = $options['icon']; |
|
222
|
|
|
if (Arr::key('iconInline', $options)) { |
|
223
|
|
|
$iconPrefix = $this->_configRead('iconPref'); |
|
|
|
|
|
|
224
|
|
|
$options = $this->_addClass($options, implode(' ', [ |
|
225
|
|
|
$this->_class('icon'), |
|
226
|
|
|
$iconPrefix, |
|
227
|
|
|
$iconPrefix . '-' . $icon |
|
228
|
|
|
])); |
|
229
|
|
|
|
|
230
|
|
|
unset($options['icon']); |
|
231
|
|
|
} else { |
|
232
|
|
|
$options['escape'] = false; |
|
233
|
|
|
$iconOptions['createIcon'] = true; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return [$options, $iconOptions]; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Setup tooltip title by options. |
|
241
|
|
|
* |
|
242
|
|
|
* @param string $tooltip |
|
243
|
|
|
* @param array $options |
|
244
|
|
|
* @return array |
|
245
|
|
|
*/ |
|
246
|
|
View Code Duplication |
protected function _setTooltipTitle($tooltip, array $options = []) |
|
|
|
|
|
|
247
|
|
|
{ |
|
248
|
|
|
if ($tooltip === true && !Arr::key('title', $options)) { |
|
249
|
|
|
$options['title'] = strip_tags($options['label']); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
if (is_string($tooltip)) { |
|
253
|
|
|
$options['title'] = $tooltip; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return $options; |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.