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\Form\Form; |
19
|
|
|
use Cake\View\View; |
20
|
|
|
use JBZoo\Data\Data; |
21
|
|
|
use JBZoo\Utils\Arr; |
22
|
|
|
use Cake\View\Helper; |
23
|
|
|
use Cake\Utility\Hash; |
24
|
|
|
use Cake\Core\Configure; |
25
|
|
|
use Core\View\Form\FormContext; |
26
|
|
|
use Cake\Collection\Collection; |
27
|
|
|
use Core\View\Form\ArrayContext; |
28
|
|
|
use Core\View\Form\EntityContext; |
29
|
|
|
use Cake\Datasource\EntityInterface; |
30
|
|
|
use Core\View\Helper\Traits\HelperTrait; |
31
|
|
|
use Core\View\Helper\Traits\MaterializeCssTrait; |
32
|
|
|
use Cake\View\Helper\FormHelper as CakeFormHelper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Class FormHelper |
36
|
|
|
* |
37
|
|
|
* @package Core\View\Helper |
38
|
|
|
* @property \Core\View\Helper\UrlHelper $Url |
39
|
|
|
* @property \Core\View\Helper\HtmlHelper $Html |
40
|
|
|
*/ |
41
|
|
|
class FormHelper extends CakeFormHelper |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
use HelperTrait, MaterializeCssTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* List of helpers used by this helper. |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
public $helpers = [ |
52
|
|
|
'Url' => ['className' => 'Core.Url'], |
53
|
|
|
'Html' => ['className' => 'Core.Html'], |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Hold js form type. |
58
|
|
|
* |
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
protected $_isJsForm = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* HtmlHelper constructor. |
65
|
|
|
* |
66
|
|
|
* @param View $View |
67
|
|
|
* @param array $config |
68
|
|
|
*/ |
69
|
|
|
public function __construct(View $View, array $config = []) |
70
|
|
|
{ |
71
|
|
|
$this->_defaultConfig = Hash::merge([ |
72
|
|
|
'materializeCss' => false, |
73
|
|
|
'btnPref' => Configure::read('Cms.btnPref'), |
74
|
|
|
'iconPref' => Configure::read('Cms.iconPref'), |
75
|
|
|
'classPrefix' => Configure::read('Cms.classPrefix'), |
76
|
|
|
], $this->_defaultConfig); |
77
|
|
|
|
78
|
|
|
$config = new Data($config); |
79
|
|
|
if ($config->get('materializeCss', false) === true) { |
80
|
|
|
$config |
81
|
|
|
->set('widgets', [ |
82
|
|
|
'file' => 'Core\View\Widget\MaterializeCss\FileWidget', |
83
|
|
|
'textarea' => 'Core\View\Widget\MaterializeCss\TextareaWidget', |
84
|
|
|
'checkbox' => 'Core\View\Widget\MaterializeCss\CheckboxWidget' |
85
|
|
|
]) |
86
|
|
|
->set('templates', 'Core.templates/materialize_css_form') |
87
|
|
|
->set('prepareBtnClass', function (Helper $form, $options, $button) { |
88
|
|
|
return $this->_prepareBtn($form, $options, $button); |
89
|
|
|
}) |
90
|
|
|
->set('prepareTooltip', function (Helper $html, $options, $tooltip) { |
91
|
|
|
return $this->_prepareTooltip($html, $options, $tooltip); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
parent::__construct($View, $config->getArrayCopy()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Creates file input widget. |
100
|
|
|
* |
101
|
|
|
* @param string $fieldName Name of a field, in the form "modelname.fieldname" |
102
|
|
|
* @param array $options Array of HTML attributes. |
103
|
|
|
* |
104
|
|
|
* @return string A generated file input. |
105
|
|
|
*/ |
106
|
|
|
public function file($fieldName, array $options = []) |
107
|
|
|
{ |
108
|
|
|
$content = parent::file($fieldName, $options); |
109
|
|
|
|
110
|
|
|
if ($this->getConfig('materializeCss', false) === false) { |
111
|
|
|
return $content; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$options = $this->_parseOptions($fieldName, $options); |
115
|
|
|
|
116
|
|
|
$options['type'] = __FUNCTION__; |
117
|
|
|
|
118
|
|
|
$result = $this->_inputContainerTemplate([ |
119
|
|
|
'error' => null, |
120
|
|
|
'errorSuffix' => null, |
121
|
|
|
'content' => $content, |
122
|
|
|
'options' => $options |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Form switcher. |
130
|
|
|
* |
131
|
|
|
* @param string $fieldName |
132
|
|
|
* @param array $options |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function switcher($fieldName, array $options = []) |
136
|
|
|
{ |
137
|
|
|
$input = parent::checkbox($fieldName, $options); |
|
|
|
|
138
|
|
|
|
139
|
|
|
if ($this->getConfig('materializeCss', false) === false) { |
140
|
|
|
return $input; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$options += [ |
144
|
|
|
'before' => __d('backend', 'Off'), |
145
|
|
|
'after' => __d('backend', 'On') |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
$title = (Arr::key('title', $options)) ? $options['title'] : $fieldName; |
149
|
|
|
|
150
|
|
|
if (!empty($title)) { |
151
|
|
|
$title = $this->Html->div('switch-title', $title); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$content = $this->formatTemplate(__FUNCTION__, [ |
155
|
|
|
'input' => $input, |
156
|
|
|
'title' => $title, |
157
|
|
|
'after' => $options['after'], |
158
|
|
|
'before' => $options['before'], |
159
|
|
|
'lever' => '<span class="lever"></span>' |
160
|
|
|
]); |
161
|
|
|
|
162
|
|
|
return $content; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Creates a `<button>` tag. |
167
|
|
|
* |
168
|
|
|
* @param string $title |
169
|
|
|
* @param array $options |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function button($title, array $options = []) |
173
|
|
|
{ |
174
|
|
|
$options = $this->addClass($options, $this->_class(__FUNCTION__)); |
175
|
|
|
$options = $this->_getBtnClass($options); |
176
|
|
|
|
177
|
|
|
list($title, $options) = $this->_createIcon($this->Html, $title, $options); |
178
|
|
|
|
179
|
|
|
return parent::button($title, $options); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Input check all. |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function checkAll() |
188
|
|
|
{ |
189
|
|
|
return $this->control('check-all', ['type' => 'checkbox', 'class' => 'jsCheckAll']); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Create html form. |
194
|
|
|
* |
195
|
|
|
* @param mixed $model |
196
|
|
|
* @param array $options |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function create($model = null, array $options = []) |
200
|
|
|
{ |
201
|
|
|
$options += ['process' => false, 'jsForm' => false]; |
202
|
|
|
$options = $this->addClass($options, $this->_class('form')); |
203
|
|
|
|
204
|
|
|
$isProcess = $options['process']; |
205
|
|
|
|
206
|
|
|
if ($isProcess !== false) { |
207
|
|
|
$_options = [ |
208
|
|
|
'url' => [ |
209
|
|
|
'plugin' => $this->request->getParam('plugin'), |
210
|
|
|
'controller' => $this->request->getParam('controller'), |
211
|
|
|
'action' => 'process' |
212
|
|
|
] |
213
|
|
|
]; |
214
|
|
|
|
215
|
|
|
$options['jsForm'] = true; |
216
|
|
|
$options = Hash::merge($_options, $options); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$isJsForm = $options['jsForm']; |
220
|
|
|
if ($isJsForm) { |
221
|
|
|
$this->_isJsForm = true; |
222
|
|
|
$options = $this->addClass($options, 'jsForm'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
unset($options['process'], $options['jsForm']); |
226
|
|
|
|
227
|
|
|
return parent::create($model, $options); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* End html form. |
232
|
|
|
* |
233
|
|
|
* @param array $secureAttributes |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
public function end(array $secureAttributes = []) |
237
|
|
|
{ |
238
|
|
|
if ($this->_isJsForm) { |
239
|
|
|
return implode('', [ |
240
|
|
|
$this->hidden('action', ['value' => '', 'class' => 'jsFormAction']), |
241
|
|
|
parent::end($secureAttributes) |
242
|
|
|
]); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return parent::end($secureAttributes); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Table row process checkbox. |
250
|
|
|
* |
251
|
|
|
* @param string $name |
252
|
|
|
* @param string $type |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
|
|
public function processCheck($type, $name) |
256
|
|
|
{ |
257
|
|
|
return $this->control($type . '.' . $name . '.id', ['type' => 'checkbox']); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Add the default suite of context providers provided. |
262
|
|
|
* |
263
|
|
|
* @return void |
264
|
|
|
*/ |
265
|
|
|
protected function _addDefaultContextProviders() |
266
|
|
|
{ |
267
|
|
|
$this->addContextProvider('orm', function ($request, $data) { |
268
|
|
|
if (is_array($data['entity']) || $data['entity'] instanceof \Traversable) { |
269
|
|
|
$pass = (new Collection($data['entity']))->first() !== null; |
270
|
|
|
if ($pass) { |
271
|
|
|
return new EntityContext($request, $data); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this->_addEntityContent($request, $data); |
276
|
|
|
}); |
277
|
|
|
|
278
|
|
|
$this->_addFormContextProvider(); |
279
|
|
|
$this->_addFormArrayProvider(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Add the entity suite of context providers provided. |
284
|
|
|
* |
285
|
|
|
* @param $request |
286
|
|
|
* @param $data |
287
|
|
|
* @return EntityContext |
288
|
|
|
*/ |
289
|
|
|
protected function _addEntityContent($request, $data) |
290
|
|
|
{ |
291
|
|
|
if ($data['entity'] instanceof EntityInterface) { |
292
|
|
|
return new EntityContext($request, $data); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
if (is_array($data['entity']) && empty($data['entity']['schema'])) { |
296
|
|
|
return new EntityContext($request, $data); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Add the array suite of context providers provided. |
302
|
|
|
* |
303
|
|
|
* @return void |
304
|
|
|
*/ |
305
|
|
|
protected function _addFormArrayProvider() |
306
|
|
|
{ |
307
|
|
|
$this->addContextProvider('array', function ($request, $data) { |
308
|
|
|
if (is_array($data['entity']) && isset($data['entity']['schema'])) { |
309
|
|
|
return new ArrayContext($request, $data['entity']); |
310
|
|
|
} |
311
|
|
|
}); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Add the form suite of context providers provided. |
316
|
|
|
* |
317
|
|
|
* @return void |
318
|
|
|
*/ |
319
|
|
|
protected function _addFormContextProvider() |
320
|
|
|
{ |
321
|
|
|
$this->addContextProvider('form', function ($request, $data) { |
322
|
|
|
if ($data['entity'] instanceof Form) { |
323
|
|
|
return new FormContext($request, $data); |
324
|
|
|
} |
325
|
|
|
}); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.