Completed
Push — master ( cb024f...4e14d9 )
by Cheren
04:34
created

AssetsHelper::slugify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

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