Completed
Push — master ( ea5f79...c57fe7 )
by Cheren
08:37
created

AssetsHelper::toggleField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
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
     * Get sort assets included list.
61
     *
62
     * @param string $type
63
     * @return array|null
64
     */
65
    public function getAssets($type = 'css')
66
    {
67
        return $this->Html->getAssets($type);
68
    }
69
70
    /**
71
     * Include bootstrap.
72
     *
73
     * @return $this
74
     */
75 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...
76
    {
77
        $this->jquery();
78
79
        $this->Html->script('libs/bootstrap.min.js', $this->_setOptions([
80
            'weight' => self::WEIGHT_LIB,
81
            'alias'  => __FUNCTION__,
82
        ]));
83
84
        $this->Html->css('libs/bootstrap.min.css', $this->_setOptions([
85
            'weight' => self::WEIGHT_CORE,
86
            'alias'  => __FUNCTION__,
87
        ]));
88
89
        return $this;
90
    }
91
92
    /**
93
     * Include fancybox.
94
     *
95
     * @return $this
96
     */
97 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...
98
    {
99
        $this->jquery();
100
101
        $this->Html->script('libs/fancybox.min.js', $this->_setOptions([
102
            'weight' => self::WEIGHT_LIB,
103
            'alias'  => __FUNCTION__,
104
        ]));
105
106
        $this->Html->css('libs/fancybox.min.css', $this->_setOptions([
107
            'weight' => self::WEIGHT_CORE,
108
            'alias'  => __FUNCTION__,
109
        ]));
110
111
        return $this;
112
    }
113
114
    /**
115
     * Include font awesome.
116
     *
117
     * @return $this
118
     */
119
    public function fontAwesome()
120
    {
121
        $this->Html->css('libs/font-awesome.min.css', $this->_setOptions([
122
            'weight' => self::WEIGHT_CORE,
123
            'alias'  => 'font-awesome',
124
        ]));
125
126
        return $this;
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
     * Include materialize design.
168
     *
169
     * @return $this
170
     */
171 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...
172
    {
173
        $this->jquery();
174
175
        $this->Html->script('libs/materialize.min.js', $this->_setOptions([
176
            'weight' => self::WEIGHT_LIB,
177
            'alias'  => __FUNCTION__,
178
        ]));
179
180
        $this->Html->css('libs/materialize.min.css', $this->_setOptions([
181
            'weight' => self::WEIGHT_CORE,
182
            'alias'  => __FUNCTION__,
183
        ]));
184
185
        return $this;
186
    }
187
188
    /**
189
     * Include sweet alert.
190
     *
191
     * @return $this
192
     */
193 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...
194
    {
195
        $this->jquery();
196
197
        $this->Html->script('libs/sweetalert.min.js', $this->_setOptions([
198
            'weight' => self::WEIGHT_LIB,
199
            'alias'  => __FUNCTION__,
200
        ]));
201
202
        $this->Html->css('libs/sweetalert.min.css', $this->_setOptions([
203
            'weight' => self::WEIGHT_CORE,
204
            'alias'  => __FUNCTION__,
205
        ]));
206
207
        return $this;
208
    }
209
210
    /**
211
     * Include jquery table check all.
212
     *
213
     * @return $this
214
     */
215 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...
216
    {
217
        $this->jquery();
218
219
        $this->Html->script(['libs/jquery-check-all.min.js'], $this->_setOptions([
220
            'weight' => self::WEIGHT_LIB,
221
            'alias'  => __FUNCTION__,
222
        ]));
223
224
        return $this;
225
    }
226
227
    /**
228
     * Include toggle field js widget.
229
     *
230
     * @param string $selector
231
     * @param string $widget
232
     * @param array $options
233
     * @return $this
234
     */
235
    public function toggleField($selector = '.jsToggleField', $widget = 'JBZoo.FieldToggle', array $options = [])
236
    {
237
        $this->jqueryFactory();
238
        $this->Html->script('Core.admin/widget/field-toggle.js', $this->_setOptions([
239
            'weight' => self::WEIGHT_WIDGET,
240
            'alias'  => __FUNCTION__
241
        ]));
242
243
        $options = Hash::merge(['token' => $this->request->getCookie('csrfToken')], $options);
244
        $this->Js->widget($selector, $widget, $options);
245
246
        return $this;
247
    }
248
249
    /**
250
     * Autoload plugin assets.
251
     *
252
     * @return void
253
     */
254
    public function loadPluginAssets()
255
    {
256
        $plugin = (string) $this->request->getParam('plugin');
257
        $prefix = ($this->request->getParam('prefix')) ? $this->request->getParam('prefix') . '/' : null;
258
        $action = (string) $this->request->getParam('action');
259
260
        $controller = (string) $this->request->getParam('controller');
261
        $widgetName = Str::slug($controller . '-' . $action) . '.js';
262
        $cssOptions = ['block' => 'css_bottom', 'fullBase' => true, 'force' => Configure::read('debug')];
263
264
        $this->Html->css($plugin . '.' . $prefix . 'styles.css', $cssOptions);
265
        $this->Html->less($plugin . '.' . $prefix . 'styles.less', $cssOptions);
266
        $this->Html->script([
267
            $plugin . '.' . $prefix . 'widget/' . $widgetName,
268
            $plugin . '.' . $prefix . 'script.js',
269
        ], ['block' => 'script_bottom', 'fullBase' => true]);
270
    }
271
272
    /**
273
     * Setup asset options.
274
     *
275
     * @param array $options
276
     * @return array
277
     */
278
    protected function _setOptions(array $options = [])
279
    {
280
        return Hash::merge($this->_options, $options);
281
    }
282
}
283