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 JBZoo\Utils\Str; |
19
|
|
|
use Cake\Utility\Hash; |
20
|
|
|
use Cake\Core\Configure; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class AssetsHelper |
24
|
|
|
* |
25
|
|
|
* @package Core\View\Helper |
26
|
|
|
* @property \Core\View\Helper\JsHelper $Js |
27
|
|
|
* @property \Cake\View\Helper\UrlHelper $Url |
28
|
|
|
* @property \Core\View\Helper\HtmlHelper $Html |
29
|
|
|
* |
30
|
|
|
* @SuppressWarnings("PHPMD.TooManyPublicMethods") |
31
|
|
|
*/ |
32
|
|
|
class AssetsHelper extends AppHelper |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
const WEIGHT_CORE = 1; |
36
|
|
|
const WEIGHT_LIB = 2; |
37
|
|
|
const WEIGHT_WIDGET = 3; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Use helpers. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
public $helpers = [ |
45
|
|
|
'Core.Js', |
46
|
|
|
'Url' => ['className' => 'Core.Url'], |
47
|
|
|
'Html' => ['className' => 'Core.Html'], |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Default assets options. |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $_options = [ |
56
|
|
|
'weight' => 10, |
57
|
|
|
'fullBase' => true, |
58
|
|
|
'block' => 'assets' |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Include bootstrap. |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function bootstrap() |
67
|
|
|
{ |
68
|
|
|
$this->jquery(); |
69
|
|
|
|
70
|
|
|
$this->Html->script('libs/bootstrap.min.js', $this->_setOptions([ |
71
|
|
|
'alias' => __FUNCTION__, |
72
|
|
|
'weight' => self::WEIGHT_LIB |
73
|
|
|
])); |
74
|
|
|
|
75
|
|
|
$this->Html->css('libs/bootstrap.min.css', $this->_setOptions([ |
76
|
|
|
'alias' => __FUNCTION__, |
77
|
|
|
'weight' => self::WEIGHT_CORE |
78
|
|
|
])); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Include fancybox. |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function fancyBox() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$this->jquery(); |
91
|
|
|
|
92
|
|
|
$this->Html->script('libs/fancybox.min.js', $this->_setOptions([ |
93
|
|
|
'alias' => __FUNCTION__, |
94
|
|
|
'weight' => self::WEIGHT_LIB |
95
|
|
|
])); |
96
|
|
|
|
97
|
|
|
$this->Html->css('libs/fancybox.min.css', $this->_setOptions([ |
98
|
|
|
'alias' => __FUNCTION__, |
99
|
|
|
'weight' => self::WEIGHT_CORE |
100
|
|
|
])); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Include font awesome. |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function fontAwesome() |
111
|
|
|
{ |
112
|
|
|
$this->Html->css('libs/font-awesome.min.css', $this->_setOptions([ |
113
|
|
|
'alias' => 'font-awesome', |
114
|
|
|
'weight' => self::WEIGHT_CORE |
115
|
|
|
])); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get sort assets included list. |
122
|
|
|
* |
123
|
|
|
* @param string $type |
124
|
|
|
* @return array|null |
125
|
|
|
*/ |
126
|
|
|
public function getAssets($type = 'css') |
127
|
|
|
{ |
128
|
|
|
return $this->Html->getAssets($type); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Include jQuery imgAreaSelect plugin. See https://github.com/odyniec/imgareaselect |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public function imgAreaSelect() |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$this->jquery(); |
139
|
|
|
|
140
|
|
|
$this->Html->script('libs/img-area-select.min.js', $this->_setOptions([ |
141
|
|
|
'alias' => __FUNCTION__, |
142
|
|
|
'weight' => self::WEIGHT_LIB |
143
|
|
|
])); |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Include jquery lib. |
150
|
|
|
* |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
public function jquery() |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$this->Html->script('libs/jquery.min.js', $this->_setOptions([ |
156
|
|
|
'alias' => __FUNCTION__, |
157
|
|
|
'weight' => self::WEIGHT_CORE |
158
|
|
|
])); |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Include jquery factory. |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
public function jqueryFactory() |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
$this->jquery(); |
171
|
|
|
|
172
|
|
|
$this->Html->script('libs/utils.min.js', $this->_setOptions([ |
173
|
|
|
'alias' => 'jquery-utils', |
174
|
|
|
'weight' => self::WEIGHT_LIB |
175
|
|
|
])); |
176
|
|
|
|
177
|
|
|
$this->Html->script('libs/jquery-factory.min.js', $this->_setOptions([ |
178
|
|
|
'weight' => self::WEIGHT_LIB, |
179
|
|
|
'alias' => 'jquery-factory' |
180
|
|
|
])); |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Autoload plugin assets. |
187
|
|
|
* |
188
|
|
|
* @return void |
189
|
|
|
* |
190
|
|
|
* @throws \JBZoo\Less\Exception |
191
|
|
|
*/ |
192
|
|
|
public function loadPluginAssets() |
193
|
|
|
{ |
194
|
|
|
$plugin = (string) $this->request->getParam('plugin'); |
195
|
|
|
$prefix = ($this->request->getParam('prefix')) ? $this->request->getParam('prefix') . '/' : null; |
196
|
|
|
$action = (string) $this->request->getParam('action'); |
197
|
|
|
|
198
|
|
|
$controller = (string) $this->request->getParam('controller'); |
199
|
|
|
$widgetName = Str::slug($controller . '-' . $action) . '.js'; |
200
|
|
|
$cssOptions = ['block' => 'css_bottom', 'fullBase' => true, 'force' => Configure::read('debug')]; |
201
|
|
|
|
202
|
|
|
$this->Html->css($plugin . '.' . $prefix . 'styles.css', $cssOptions); |
203
|
|
|
$this->Html->less($plugin . '.' . $prefix . 'styles.less', $cssOptions); |
204
|
|
|
$this->Html->script([ |
205
|
|
|
$plugin . '.' . $prefix . 'widget/' . $widgetName, |
206
|
|
|
$plugin . '.' . $prefix . 'script.js' |
207
|
|
|
], ['block' => 'script_bottom', 'fullBase' => true]); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Include materialize design. |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
|
View Code Duplication |
public function materialize() |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
$this->jquery(); |
218
|
|
|
|
219
|
|
|
$this->Html->script('libs/materialize.min.js', $this->_setOptions([ |
220
|
|
|
'alias' => __FUNCTION__, |
221
|
|
|
'weight' => self::WEIGHT_LIB |
222
|
|
|
])); |
223
|
|
|
|
224
|
|
|
$this->Html->css('libs/materialize.min.css', $this->_setOptions([ |
225
|
|
|
'alias' => __FUNCTION__, |
226
|
|
|
'weight' => self::WEIGHT_CORE |
227
|
|
|
])); |
228
|
|
|
|
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Include jquery slugify plugin. |
234
|
|
|
* |
235
|
|
|
* @return $this |
236
|
|
|
*/ |
237
|
|
View Code Duplication |
public function slugify() |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
$this->jquery(); |
240
|
|
|
|
241
|
|
|
$this->Html->script('libs/slugify.min.js', $this->_setOptions([ |
242
|
|
|
'alias' => __FUNCTION__, |
243
|
|
|
'weight' => self::WEIGHT_LIB |
244
|
|
|
])); |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Include sweet alert. |
251
|
|
|
* |
252
|
|
|
* @return $this |
253
|
|
|
*/ |
254
|
|
View Code Duplication |
public function sweetAlert() |
|
|
|
|
255
|
|
|
{ |
256
|
|
|
$this->jquery(); |
257
|
|
|
|
258
|
|
|
$this->Html->script('libs/sweetalert.min.js', $this->_setOptions([ |
259
|
|
|
'alias' => __FUNCTION__, |
260
|
|
|
'weight' => self::WEIGHT_LIB |
261
|
|
|
])); |
262
|
|
|
|
263
|
|
|
$this->Html->css('libs/sweetalert.min.css', $this->_setOptions([ |
264
|
|
|
'alias' => __FUNCTION__, |
265
|
|
|
'weight' => self::WEIGHT_CORE |
266
|
|
|
])); |
267
|
|
|
|
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Include jquery table check all. |
273
|
|
|
* |
274
|
|
|
* @return $this |
275
|
|
|
*/ |
276
|
|
View Code Duplication |
public function tableCheckAll() |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
$this->jquery(); |
279
|
|
|
|
280
|
|
|
$this->Html->script(['libs/jquery-check-all.min.js'], $this->_setOptions([ |
281
|
|
|
'alias' => __FUNCTION__, |
282
|
|
|
'weight' => self::WEIGHT_LIB |
283
|
|
|
])); |
284
|
|
|
|
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Include toggle field js widget. |
290
|
|
|
* |
291
|
|
|
* @param string $selector |
292
|
|
|
* @param string $widget |
293
|
|
|
* @param array $options |
294
|
|
|
* @return $this |
295
|
|
|
*/ |
296
|
|
|
public function toggleField($selector = '.jsToggleField', $widget = 'JBZoo.FieldToggle', array $options = []) |
297
|
|
|
{ |
298
|
|
|
$this->jqueryFactory(); |
299
|
|
|
|
300
|
|
|
$this->Html->script('Core.admin/widget/field-toggle.js', $this->_setOptions([ |
301
|
|
|
'alias' => __FUNCTION__, |
302
|
|
|
'weight' => self::WEIGHT_WIDGET |
303
|
|
|
])); |
304
|
|
|
|
305
|
|
|
$options = Hash::merge(['token' => $this->request->getCookie('csrfToken')], $options); |
306
|
|
|
$this->Js->widget($selector, $widget, $options); |
307
|
|
|
|
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Setup asset options. |
313
|
|
|
* |
314
|
|
|
* @param array $options |
315
|
|
|
* @return array |
316
|
|
|
*/ |
317
|
|
|
protected function _setOptions(array $options = []) |
318
|
|
|
{ |
319
|
|
|
return Hash::merge($this->_options, $options); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
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.