|
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) |
|
|
|
|
|
|
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->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
|
|
|
'url' => $request->url, |
|
158
|
|
|
'params' => [ |
|
159
|
|
|
'pass' => $request->getParam('pass'), |
|
160
|
|
|
'theme' => $request->getParam('theme'), |
|
161
|
|
|
'action' => $request->getParam('action'), |
|
162
|
|
|
'prefix' => $request->getParam('prefix'), |
|
163
|
|
|
'plugin' => $request->getParam('plugin'), |
|
164
|
|
|
'controller' => $request->getParam('controller'), |
|
165
|
|
|
], |
|
166
|
|
|
'query' => $this->request->getQueryParams(), |
|
167
|
|
|
'base' => $request->getAttribute('base'), |
|
168
|
|
|
'here' => $request->getRequestTarget(), |
|
169
|
|
|
] |
|
170
|
|
|
]; |
|
171
|
|
|
|
|
172
|
|
|
$this->Html->scriptBlock('window.CMS = ' . json_encode($vars), ['block' => 'css_bottom']); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.