|
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 Cake\Utility\Hash; |
|
21
|
|
|
use Cake\Core\Configure; |
|
22
|
|
|
use Core\View\Form\FormContext; |
|
23
|
|
|
use Cake\Collection\Collection; |
|
24
|
|
|
use Core\View\Form\ArrayContext; |
|
25
|
|
|
use Core\View\Form\EntityContext; |
|
26
|
|
|
use Cake\Datasource\EntityInterface; |
|
27
|
|
|
use Core\View\Helper\Traits\HelperTrait; |
|
28
|
|
|
use Cake\View\Helper\FormHelper as CakeFormHelper; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Class FormHelper |
|
32
|
|
|
* |
|
33
|
|
|
* @package Core\View\Helper |
|
34
|
|
|
* @property \Core\View\Helper\UrlHelper $Url |
|
35
|
|
|
* @property \Core\View\Helper\HtmlHelper $Html |
|
36
|
|
|
*/ |
|
37
|
|
|
class FormHelper extends CakeFormHelper |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
use HelperTrait; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* List of helpers used by this helper. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
public $helpers = [ |
|
48
|
|
|
'Url' => ['className' => 'Core.Url'], |
|
49
|
|
|
'Html' => ['className' => 'Core.Html'], |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Hold js form type. |
|
54
|
|
|
* |
|
55
|
|
|
* @var bool |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $_isJsForm = false; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* HtmlHelper constructor. |
|
61
|
|
|
* |
|
62
|
|
|
* @param View $View |
|
63
|
|
|
* @param array $config |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function __construct(View $View, array $config = []) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
parent::__construct($View, $config); |
|
68
|
|
|
$this->_configWrite('btnPref', Configure::read('Cms.btnPref')); |
|
69
|
|
|
$this->_configWrite('iconPref', Configure::read('Cms.iconPref')); |
|
70
|
|
|
$this->_configWrite('classPrefix', Configure::read('Cms.classPrefix')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Creates a `<button>` tag. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $title |
|
77
|
|
|
* @param array $options |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function button($title, array $options = []) |
|
81
|
|
|
{ |
|
82
|
|
|
$options = $this->addClass($options, $this->_class(__FUNCTION__)); |
|
83
|
|
|
$options = $this->_getBtnClass($options); |
|
84
|
|
|
|
|
85
|
|
|
list($title, $options) = $this->_createIcon($this->Html, $title, $options); |
|
86
|
|
|
|
|
87
|
|
|
return parent::button($title, $options); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Create html form. |
|
92
|
|
|
* |
|
93
|
|
|
* @param mixed $model |
|
94
|
|
|
* @param array $options |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function create($model = null, array $options = []) |
|
98
|
|
|
{ |
|
99
|
|
|
$options += ['process' => false, 'jsForm' => false]; |
|
100
|
|
|
$options = $this->addClass($options, $this->_class('form')); |
|
101
|
|
|
|
|
102
|
|
|
$isProcess = $options['process']; |
|
103
|
|
|
|
|
104
|
|
|
if ($isProcess != false) { |
|
105
|
|
|
$_options = [ |
|
106
|
|
|
'url' => [ |
|
107
|
|
|
'plugin' => $this->request->param('plugin'), |
|
108
|
|
|
'controller' => $this->request->param('controller'), |
|
109
|
|
|
'action' => 'process' |
|
110
|
|
|
] |
|
111
|
|
|
]; |
|
112
|
|
|
|
|
113
|
|
|
$options['jsForm'] = true; |
|
114
|
|
|
$options = Hash::merge($_options, $options); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$isJsForm = $options['jsForm']; |
|
118
|
|
|
if ($isJsForm) { |
|
119
|
|
|
$this->_isJsForm = true; |
|
120
|
|
|
$options = $this->addClass($options, 'jsForm'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
unset($options['process']); |
|
124
|
|
|
unset($options['jsForm']); |
|
125
|
|
|
|
|
126
|
|
|
return parent::create($model, $options); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* End html form. |
|
131
|
|
|
* |
|
132
|
|
|
* @param array $secureAttributes |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
|
|
public function end(array $secureAttributes = []) |
|
136
|
|
|
{ |
|
137
|
|
|
if ($this->_isJsForm) { |
|
138
|
|
|
return implode('', [ |
|
139
|
|
|
$this->hidden('action', ['value' => '', 'class' => 'jsFormAction']), |
|
140
|
|
|
parent::end($secureAttributes) |
|
141
|
|
|
]); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return parent::end($secureAttributes); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Add the default suite of context providers provided. |
|
149
|
|
|
* |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function _addDefaultContextProviders() |
|
153
|
|
|
{ |
|
154
|
|
|
$this->addContextProvider('orm', function ($request, $data) { |
|
|
|
|
|
|
155
|
|
|
if (is_array($data['entity']) || $data['entity'] instanceof \Traversable) { |
|
156
|
|
|
$pass = (new Collection($data['entity']))->first() !== null; |
|
157
|
|
|
if ($pass) { |
|
158
|
|
|
return new EntityContext($request, $data); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $this->_addEntityContent($request, $data); |
|
163
|
|
|
}); |
|
164
|
|
|
|
|
165
|
|
|
$this->_addFormContextProvider(); |
|
166
|
|
|
$this->_addFormArrayProvider(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Add the entity suite of context providers provided. |
|
171
|
|
|
* |
|
172
|
|
|
* @param $request |
|
173
|
|
|
* @param $data |
|
174
|
|
|
* @return EntityContext |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function _addEntityContent($request, $data) |
|
177
|
|
|
{ |
|
178
|
|
|
if ($data['entity'] instanceof EntityInterface) { |
|
179
|
|
|
return new EntityContext($request, $data); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if (is_array($data['entity']) && empty($data['entity']['schema'])) { |
|
183
|
|
|
return new EntityContext($request, $data); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Add the form suite of context providers provided. |
|
189
|
|
|
* |
|
190
|
|
|
* @return void |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function _addFormContextProvider() |
|
193
|
|
|
{ |
|
194
|
|
|
$this->addContextProvider('form', function ($request, $data) { |
|
|
|
|
|
|
195
|
|
|
if ($data['entity'] instanceof Form) { |
|
196
|
|
|
return new FormContext($request, $data); |
|
197
|
|
|
} |
|
198
|
|
|
}); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Add the array suite of context providers provided. |
|
203
|
|
|
* |
|
204
|
|
|
* @return void |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function _addFormArrayProvider() |
|
207
|
|
|
{ |
|
208
|
|
|
$this->addContextProvider('array', function ($request, $data) { |
|
|
|
|
|
|
209
|
|
|
if (is_array($data['entity']) && isset($data['entity']['schema'])) { |
|
210
|
|
|
return new ArrayContext($request, $data['entity']); |
|
211
|
|
|
} |
|
212
|
|
|
}); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
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.