JsHelper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 8
dl 0
loc 146
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeRender() 0 4 1
A getBuffer() 0 20 3
A setBuffer() 0 11 2
A widget() 0 22 4
A _setScriptVars() 0 29 1
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\Arr;
19
use Cake\Event\Event;
20
use Cake\Utility\Hash;
21
use Cake\Routing\Router;
22
23
/**
24
 * Class JsHelper
25
 *
26
 * @package Core\View\Helper
27
 * @property \Core\View\Helper\HtmlHelper $Html
28
 * @property \Core\View\Helper\DocumentHelper $Document
29
 */
30
class JsHelper extends AppHelper
31
{
32
33
    /**
34
     * Uses helpers.
35
     *
36
     * @var array
37
     */
38
    public $helpers = [
39
        'Core.Html',
40
        'Core.Document',
41
    ];
42
43
    /**
44
     * Hold buffer scripts.
45
     *
46
     * @var array
47
     */
48
    protected $_buffers = [];
49
50
    /**
51
     * Before render callback.
52
     *
53
     * @param Event $event
54
     * @param $viewFile
55
     * @return void
56
     * @SuppressWarnings("unused")
57
     */
58
    public function beforeRender(Event $event, $viewFile)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $viewFile is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    {
60
        $this->_setScriptVars();
61
    }
62
63
    /**
64
     * Get all holds in buffer scripts.
65
     *
66
     * @param array $options
67
     * @return null|string
68
     */
69
    public function getBuffer(array $options = [])
70
    {
71
        $docEol  = $this->Document->eol;
72
        $options = Hash::merge(['safe' => true], $options);
73
74
        if (Arr::key('block', $options)) {
75
            unset($options['block']);
76
        }
77
78
        if (count($this->_buffers)) {
79
            $scripts = $docEol .
80
                'jQuery (function($) {'  . $docEol .
81
                implode($docEol, $this->_buffers) . $docEol .
82
                '});' . $docEol;
83
84
            return $this->Html->scriptBlock($scripts, $options) . $docEol;
85
        }
86
87
        return null;
88
    }
89
90
    /**
91
     * Setup buffer script.
92
     *
93
     * @param string $script
94
     * @param bool|false $top
95
     * @return $this
96
     */
97
    public function setBuffer($script, $top = false)
98
    {
99
        $script = trim($script);
100
        if ($top) {
101
            array_unshift($this->_buffers, $script);
102
        } else {
103
            array_push($this->_buffers, $script);
104
        }
105
106
        return $this;
107
    }
108
109
    /**
110
     * Initialize java script widget.
111
     *
112
     * @param string $jSelector
113
     * @param string $widgetName
114
     * @param array $params
115
     * @param bool $return
116
     * @return string|null
117
     */
118
    public function widget($jSelector, $widgetName, array $params = [], $return = false)
119
    {
120
        static $included = [];
121
122
        $jSelector = is_array($jSelector) ? implode(', ', $jSelector) : $jSelector;
123
124
        $hash = $jSelector . ' /// ' . $widgetName;
125
126
        if (!Arr::key($hash, $included)) {
127
            $included[$hash] = true;
128
            $widgetName = str_replace('.', '', $widgetName);
129
            $initScript = '$("' . $jSelector . '").' . $widgetName . '(' . json_encode($params) . ');';
130
131
            if ($return) {
132
                return $this->Html->scriptBlock("\tjQuery(function($){" . $initScript . "});");
133
            }
134
135
            $this->setBuffer($initScript);
136
        }
137
138
        return null;
139
    }
140
141
    /**
142
     * Setup java script variables from server.
143
     *
144
     * @return void
145
     */
146
    protected function _setScriptVars()
147
    {
148
        $request = $this->getView()->getRequest();
149
150
        $vars = [
151
            'baseUrl' => Router::fullBaseUrl(),
152
            'alert' => [
153
                'ok'     => __d('alert', 'Ok'),
154
                'cancel' => __d('alert', 'Cancel'),
155
                'sure'   => __d('alert', 'Are you sure?'),
156
            ],
157
            'request' => [
158
                'url'    => $request->getPath(),
159
                'params' => [
160
                    'pass'       => $request->getParam('pass'),
161
                    'theme'      => $request->getParam('theme'),
162
                    'action'     => $request->getParam('action'),
163
                    'prefix'     => $request->getParam('prefix'),
164
                    'plugin'     => $request->getParam('plugin'),
165
                    'controller' => $request->getParam('controller'),
166
                ],
167
                'query'  => $request->getQueryParams(),
168
                'base'   => $request->getAttribute('base'),
169
                'here'   => $request->getRequestTarget(),
170
            ]
171
        ];
172
173
        $this->Html->scriptBlock('window.CMS = ' . json_encode($vars), ['block' => 'css_bottom']);
174
    }
175
}
176