|
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; |
|
17
|
|
|
|
|
18
|
|
|
use Cake\ORM\Entity; |
|
19
|
|
|
use Cake\View\View; |
|
20
|
|
|
use JBZoo\Utils\Arr; |
|
21
|
|
|
use Cake\Utility\Hash; |
|
22
|
|
|
use Cake\Core\Configure; |
|
23
|
|
|
use Core\View\Helper\Traits\HelperTrait; |
|
24
|
|
|
use Core\View\Helper\Traits\IncludeTrait; |
|
25
|
|
|
use Cake\View\Helper\HtmlHelper as CakeHtmlHelper; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class HtmlHelper |
|
29
|
|
|
* |
|
30
|
|
|
* @package Core\View\Helper |
|
31
|
|
|
* @property \Core\View\Helper\LessHelper $Less |
|
32
|
|
|
* @property \Core\View\Helper\UrlHelper $Url |
|
33
|
|
|
* @property \Core\View\Helper\DocumentHelper $Document |
|
34
|
|
|
*/ |
|
35
|
|
|
class HtmlHelper extends CakeHtmlHelper |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
use HelperTrait, IncludeTrait; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* List of helpers used by this helper. |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
public $helpers = [ |
|
46
|
|
|
'Core.Less', |
|
47
|
|
|
'Core.Document', |
|
48
|
|
|
'Url' => ['className' => 'Core.Url'], |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Hold assets data. |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $_assets = [ |
|
57
|
|
|
'styles' => [], |
|
58
|
|
|
'scripts' => [], |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* HtmlHelper constructor. |
|
63
|
|
|
* |
|
64
|
|
|
* @param View $View |
|
65
|
|
|
* @param array $config |
|
66
|
|
|
*/ |
|
67
|
|
View Code Duplication |
public function __construct(View $View, array $config = []) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
parent::__construct($View, $config); |
|
70
|
|
|
$this->_configWrite('btnPref', Configure::read('Cms.btnPref')); |
|
71
|
|
|
$this->_configWrite('iconPref', Configure::read('Cms.iconPref')); |
|
72
|
|
|
$this->_configWrite('classPrefix', Configure::read('Cms.classPrefix')); |
|
73
|
|
|
$this->_configWrite('templates.icon', '<i class="{{class}}"{{attrs}}></i>'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Creates a link element for CSS stylesheets. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array|string $path |
|
80
|
|
|
* @param array $options |
|
81
|
|
|
* @return null|string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function css($path, array $options = []) |
|
84
|
|
|
{ |
|
85
|
|
|
$options += ['rel' => 'stylesheet']; |
|
86
|
|
|
return $this->_include($path, $options, 'css'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get sort assets included list. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $key |
|
93
|
|
|
* @return array|null |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getAssets($key) |
|
96
|
|
|
{ |
|
97
|
|
|
$value = Hash::get($this->_assets, $key); |
|
98
|
|
|
if (is_string($value)) { |
|
99
|
|
|
return $value; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return Hash::sort((array) $value, '{n}.weight', 'asc'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Create icon element. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $icon |
|
109
|
|
|
* @param array $options |
|
110
|
|
|
* @return null|string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function icon($icon = 'home', array $options = []) |
|
113
|
|
|
{ |
|
114
|
|
|
$iconPref = $this->_configRead('iconPref'); |
|
115
|
|
|
$_classes = [ |
|
116
|
|
|
$this->_class(__FUNCTION__), |
|
117
|
|
|
$iconPref, |
|
118
|
|
|
$iconPref . '-' . $icon, |
|
119
|
|
|
]; |
|
120
|
|
|
|
|
121
|
|
|
$options = $this->_addClass($options, implode(' ', $_classes)); |
|
122
|
|
|
$classes = $options['class']; |
|
123
|
|
|
unset($options['class']); |
|
124
|
|
|
|
|
125
|
|
|
$templater = $this->templater(); |
|
126
|
|
|
return $templater->format(__FUNCTION__, [ |
|
127
|
|
|
'class' => $classes, |
|
128
|
|
|
'attrs' => $templater->formatAttributes($options), |
|
129
|
|
|
]); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Creates a CSS stylesheets from less. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string|array $path |
|
136
|
|
|
* @param array $options |
|
137
|
|
|
* @return null|string |
|
138
|
|
|
*/ |
|
139
|
|
|
public function less($path, array $options = []) |
|
140
|
|
|
{ |
|
141
|
|
|
$cssPath = []; |
|
142
|
|
|
|
|
143
|
|
|
if (!Arr::key('force', $options)) { |
|
144
|
|
|
$options['force'] = false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if (is_array($path)) { |
|
148
|
|
|
foreach ($path as $i) { |
|
149
|
|
|
if ($result = $this->Less->process($i, $options['force'])) { |
|
150
|
|
|
$cssPath[] = $result; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if (is_string($path) && $result = $this->Less->process($path, $options['force'])) { |
|
156
|
|
|
$cssPath[] = $result; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $this->css($cssPath, $options); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Create an html link. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $title |
|
166
|
|
|
* @param null|string|array $url |
|
167
|
|
|
* @param array $options |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
public function link($title, $url = null, array $options = []) |
|
171
|
|
|
{ |
|
172
|
|
|
$options = $this->addClass($options, $this->_class(__FUNCTION__)); |
|
173
|
|
|
$options = Hash::merge([ |
|
174
|
|
|
'escapeTitle' => false, |
|
175
|
|
|
'clear' => false, |
|
176
|
|
|
'label' => $title, |
|
177
|
|
|
], $options); |
|
178
|
|
|
|
|
179
|
|
|
$isClear = (bool) $options['clear']; |
|
180
|
|
|
unset($options['clear']); |
|
181
|
|
|
|
|
182
|
|
|
$options = $this->_setTitleAttr($title, $options); |
|
183
|
|
|
|
|
184
|
|
|
// Set title in html tag. |
|
185
|
|
|
if ($this->_isEscapeTitle($title, $isClear, $options)) { |
|
186
|
|
|
$title = $this->tag('span', $title, ['class' => $this->_class(__FUNCTION__) . '-title']); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$options = $this->_getBtnClass($options); |
|
190
|
|
|
$options = $this->_getToolTipAttr($options); |
|
191
|
|
|
|
|
192
|
|
|
list($title, $options) = $this->_createIcon($this, $title, $options); |
|
193
|
|
|
unset($options['label']); |
|
194
|
|
|
|
|
195
|
|
|
return parent::link($title, $url, $options); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Returns one or many `<script>` tags depending on the number of scripts given. |
|
200
|
|
|
* |
|
201
|
|
|
* @param array|string $path |
|
202
|
|
|
* @param array $options |
|
203
|
|
|
* @return null|string |
|
204
|
|
|
*/ |
|
205
|
|
|
public function script($path, array $options = []) |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->_include($path, $options, 'script'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Create status icon or link. |
|
212
|
|
|
* |
|
213
|
|
|
* @param int $status |
|
214
|
|
|
* @param array $url |
|
215
|
|
|
* @param string $icon |
|
216
|
|
|
* @return null|string |
|
217
|
|
|
*/ |
|
218
|
|
|
public function status($status = 0, array $url = [], $icon = 'circle') |
|
219
|
|
|
{ |
|
220
|
|
|
$class = (int) $status === 1 ? 'ck-green' : 'ck-red'; |
|
221
|
|
|
if (count($url) === 0) { |
|
222
|
|
|
return $this->icon($icon, ['class' => $class]); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $this->link(null, 'javascript:void(0);', [ |
|
226
|
|
|
'icon' => $icon, |
|
227
|
|
|
'class' => $class, |
|
228
|
|
|
'data-url' => $this->Url->build($url) |
|
229
|
|
|
]); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Create and render ajax toggle element. |
|
234
|
|
|
* |
|
235
|
|
|
* @param Entity $entity |
|
236
|
|
|
* @param array $url |
|
237
|
|
|
* @param array $data |
|
238
|
|
|
* @return string |
|
239
|
|
|
*/ |
|
240
|
|
|
public function toggle(Entity $entity, array $url = [], array $data = []) |
|
241
|
|
|
{ |
|
242
|
|
|
if (count($url) === 0) { |
|
243
|
|
|
$url = [ |
|
244
|
|
|
'action' => 'toggle', |
|
245
|
|
|
'prefix' => $this->request->getParam('prefix'), |
|
246
|
|
|
'plugin' => $this->request->getParam('plugin'), |
|
247
|
|
|
'controller' => $this->request->getParam('controller'), |
|
248
|
|
|
(int) $entity->get('id'), |
|
249
|
|
|
(int) $entity->get('status') |
|
250
|
|
|
]; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$data = Hash::merge([ |
|
254
|
|
|
'url' => $url, |
|
255
|
|
|
'entity' => $entity, |
|
256
|
|
|
], $data); |
|
257
|
|
|
|
|
258
|
|
|
return $this->_View->element(__FUNCTION__, $data); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Check if need escape link title. |
|
263
|
|
|
* |
|
264
|
|
|
* @param string $title |
|
265
|
|
|
* @param bool $isClear |
|
266
|
|
|
* @param array $options |
|
267
|
|
|
* @return bool |
|
268
|
|
|
*/ |
|
269
|
|
|
protected function _isEscapeTitle($title, $isClear, array $options = []) |
|
270
|
|
|
{ |
|
271
|
|
|
return $options['escapeTitle'] === false && !empty($title) && !$isClear; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Setup default title attr. |
|
276
|
|
|
* |
|
277
|
|
|
* @param string $title |
|
278
|
|
|
* @param array $options |
|
279
|
|
|
* @return array |
|
280
|
|
|
*/ |
|
281
|
|
|
protected function _setTitleAttr($title, array $options = []) |
|
282
|
|
|
{ |
|
283
|
|
|
if (!Arr::key('title', $options)) { |
|
284
|
|
|
$options['title'] = strip_tags($title); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
return $options; |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.