Completed
Push — master ( 8034fc...198927 )
by Cheren
02:28
created

JsHelper::getBuffer()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 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 Cake\Event\Event;
19
use Cake\Utility\Hash;
20
use Cake\Routing\Router;
21
22
/**
23
 * Class JsHelper
24
 *
25
 * @package Core\View\Helper
26
 * @property \Core\View\Helper\HtmlHelper $Html
27
 * @property \Core\View\Helper\DocumentHelper $Document
28
 */
29
class JsHelper extends AppHelper
30
{
31
32
    /**
33
     * Uses helpers.
34
     *
35
     * @var array
36
     */
37
    public $helpers = [
38
        'Core.Html',
39
        'Core.Document',
40
    ];
41
42
    /**
43
     * Hold buffer scripts.
44
     *
45
     * @var array
46
     */
47
    protected $_buffers = [];
48
49
    /**
50
     * Before render callback.
51
     *
52
     * @param Event $event
53
     * @param $viewFile
54
     * @return void
55
     * @SuppressWarnings("unused")
56
     */
57
    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...
58
    {
59
        $this->_setScriptVars();
60
    }
61
62
    /**
63
     * Get all holds in buffer scripts.
64
     *
65
     * @param array $options
66
     * @return null|string
67
     */
68
    public function getBuffer(array $options = [])
69
    {
70
        $docEol  = $this->Document->eol;
71
        $options = Hash::merge(['safe' => true], $options);
72
73
        if (isset($options['block'])) {
74
            unset($options['block']);
75
        }
76
77
        if (count($this->_buffers)) {
78
            $scripts = $docEol .
79
                'jQuery (function($) {'  . $docEol .
80
                implode($docEol, $this->_buffers) . $docEol .
81
                '});' . $docEol;
82
83
            return $this->Html->scriptBlock($scripts, $options) . $docEol;
84
        }
85
86
        return null;
87
    }
88
89
    /**
90
     * Setup buffer script.
91
     *
92
     * @param string $script
93
     * @param bool|false $top
94
     * @return $this
95
     */
96
    public function setBuffer($script, $top = false)
97
    {
98
        $script = trim($script);
99
        if ($top) {
100
            array_unshift($this->_buffers, $script);
101
        } else {
102
            array_push($this->_buffers, $script);
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * Initialize java script widget.
110
     *
111
     * @param string $jSelector
112
     * @param string $widgetName
113
     * @param array $params
114
     * @param bool $return
115
     * @return string|null
116
     */
117
    public function widget($jSelector, $widgetName, array $params = [], $return = false)
118
    {
119
        static $included = [];
120
121
        $jSelector = is_array($jSelector) ? implode(', ', $jSelector) : $jSelector;
122
123
        $hash = $jSelector . ' /// ' . $widgetName;
124
125
        if (!isset($included[$hash])) {
126
            $included[$hash] = true;
127
            $widgetName = str_replace('.', '', $widgetName);
128
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->request;
149
        $vars = [
150
            'baseUrl' => Router::fullBaseUrl(),
151
            'alert' => [
152
                'ok'     => __d('alert', 'Ok'),
153
                'cancel' => __d('alert', 'Cancel'),
154
                'sure'   => __d('alert', 'Are you sure?'),
155
            ],
156
            'request' => [
157
                'params' => $request->params,
158
                'query'  => $request->query,
159
                'data'   => $request->data,
160
                'url'    => $request->url,
161
                'base'   => $request->base,
162
                'here'   => $request->here,
163
            ]
164
        ];
165
166
        $this->Html->scriptBlock('window.CMS = ' . json_encode($vars), ['block' => 'css_bottom']);
167
    }
168
}
169