AssetsHelper::bootstrap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 View Code Duplication
    public function bootstrap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
        $request = $this->getView()->getRequest();
195
        $plugin  = (string) $request->getParam('plugin');
196
        $prefix  = ($request->getParam('prefix')) ? $request->getParam('prefix') . '/' : null;
197
        $action  = (string) $request->getParam('action');
198
199
        $controller = (string) $request->getParam('controller');
200
        $widgetName = Str::slug($controller . '-' . $action) . '.js';
201
        $cssOptions = ['block' => 'css_bottom', 'fullBase' => true, 'force' => Configure::read('debug')];
202
203
        $this->Html->css($plugin . '.' . $prefix . 'styles.css', $cssOptions);
204
        $this->Html->less($plugin . '.' . $prefix . 'styles.less', $cssOptions);
205
        $this->Html->script([
206
            $plugin . '.' . $prefix . 'widget/' . $widgetName,
207
            $plugin . '.' . $prefix . 'script.js'
208
        ], ['block' => 'script_bottom', 'fullBase' => true]);
209
    }
210
211
    /**
212
     * Include materialize design.
213
     *
214
     * @return  $this
215
     */
216 View Code Duplication
    public function materialize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
217
    {
218
        $this->jquery();
219
220
        $this->Html->script('libs/materialize.min.js', $this->_setOptions([
221
            'alias'  => __FUNCTION__,
222
            'weight' => self::WEIGHT_LIB
223
        ]));
224
225
        $this->Html->css('libs/materialize.min.css', $this->_setOptions([
226
            'alias'  => __FUNCTION__,
227
            'weight' => self::WEIGHT_CORE
228
        ]));
229
230
        return $this;
231
    }
232
233
    /**
234
     * Include jquery slugify plugin.
235
     *
236
     * @return  $this
237
     */
238 View Code Duplication
    public function slugify()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
239
    {
240
        $this->jquery();
241
242
        $this->Html->script('libs/slugify.min.js', $this->_setOptions([
243
            'alias'  => __FUNCTION__,
244
            'weight' => self::WEIGHT_LIB
245
        ]));
246
247
        return $this;
248
    }
249
250
    /**
251
     * Include sweet alert.
252
     *
253
     * @return  $this
254
     */
255 View Code Duplication
    public function sweetAlert()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
256
    {
257
        $this->jquery();
258
259
        $this->Html->script('libs/sweetalert.min.js', $this->_setOptions([
260
            'alias'  => __FUNCTION__,
261
            'weight' => self::WEIGHT_LIB
262
        ]));
263
264
        $this->Html->css('libs/sweetalert.min.css', $this->_setOptions([
265
            'alias'  => __FUNCTION__,
266
            'weight' => self::WEIGHT_CORE
267
        ]));
268
269
        return $this;
270
    }
271
272
    /**
273
     * Include jquery table check all.
274
     *
275
     * @return  $this
276
     */
277 View Code Duplication
    public function tableCheckAll()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
278
    {
279
        $this->jquery();
280
281
        $this->Html->script(['libs/jquery-check-all.min.js'], $this->_setOptions([
282
            'alias'  => __FUNCTION__,
283
            'weight' => self::WEIGHT_LIB
284
        ]));
285
286
        return $this;
287
    }
288
289
    /**
290
     * Include toggle field js widget.
291
     *
292
     * @param   string $selector
293
     * @param   string $widget
294
     * @param   array $options
295
     * @return  $this
296
     */
297
    public function toggleField($selector = '.jsToggleField', $widget = 'JBZoo.FieldToggle', array $options = [])
298
    {
299
        $this->jqueryFactory();
300
        $request = $this->getView()->getRequest();
301
302
        $this->Html->script('Core.admin/widget/field-toggle.js', $this->_setOptions([
303
            'alias'  => __FUNCTION__,
304
            'weight' => self::WEIGHT_WIDGET
305
        ]));
306
307
        $options = Hash::merge(['token' => $request->getCookie('csrfToken')], $options);
308
        $this->Js->widget($selector, $widget, $options);
309
310
        return $this;
311
    }
312
313
    /**
314
     * Setup asset options.
315
     *
316
     * @param   array $options
317
     * @return  array
318
     */
319
    protected function _setOptions(array $options = [])
320
    {
321
        return Hash::merge($this->_options, $options);
322
    }
323
}
324