|
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\View\View; |
|
19
|
|
|
use Cake\Utility\Hash; |
|
20
|
|
|
use Cake\Core\Configure; |
|
21
|
|
|
use Core\View\Helper\Traits\HelperTrait; |
|
22
|
|
|
use Cake\View\Helper\FormHelper as CakeFormHelper; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class FormHelper |
|
26
|
|
|
* |
|
27
|
|
|
* @package Core\View\Helper |
|
28
|
|
|
*/ |
|
29
|
|
|
class FormHelper extends CakeFormHelper |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
use HelperTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* List of helpers used by this helper. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
public $helpers = [ |
|
40
|
|
|
'Url' => ['className' => 'Core.Url'], |
|
41
|
|
|
'Html' => ['className' => 'Core.Html'], |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Hold js form type. |
|
46
|
|
|
* |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $_isJsForm = false; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* HtmlHelper constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param View $View |
|
55
|
|
|
* @param array $config |
|
56
|
|
|
*/ |
|
57
|
|
View Code Duplication |
public function __construct(View $View, array $config = []) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
parent::__construct($View, $config); |
|
60
|
|
|
$this->_configWrite('btnPref', Configure::read('Cms.btnPref')); |
|
61
|
|
|
$this->_configWrite('iconPref', Configure::read('Cms.iconPref')); |
|
62
|
|
|
$this->_configWrite('classPrefix', Configure::read('Cms.classPrefix')); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Create html form. |
|
67
|
|
|
* |
|
68
|
|
|
* @param mixed $model |
|
69
|
|
|
* @param array $options |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function create($model = null, array $options = []) |
|
73
|
|
|
{ |
|
74
|
|
|
$options += ['process' => false, 'jsForm' => false]; |
|
75
|
|
|
$options = $this->addClass($options, $this->_class('form')); |
|
76
|
|
|
|
|
77
|
|
|
$isProcess = $options['process']; |
|
78
|
|
|
|
|
79
|
|
|
if ($isProcess != false) { |
|
80
|
|
|
$_options = [ |
|
81
|
|
|
'url' => [ |
|
82
|
|
|
'plugin' => $this->request->param('plugin'), |
|
83
|
|
|
'controller' => $this->request->param('controller'), |
|
84
|
|
|
'action' => 'process' |
|
85
|
|
|
] |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
$options['jsForm'] = true; |
|
89
|
|
|
$options = Hash::merge($_options, $options); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$isJsForm = $options['jsForm']; |
|
93
|
|
|
if ($isJsForm) { |
|
94
|
|
|
$this->_isJsForm = true; |
|
95
|
|
|
$options = $this->addClass($options, 'jsForm'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
unset($options['process']); |
|
99
|
|
|
unset($options['jsForm']); |
|
100
|
|
|
|
|
101
|
|
|
return parent::create($model, $options); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* End html form. |
|
106
|
|
|
* |
|
107
|
|
|
* @param array $secureAttributes |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function end(array $secureAttributes = []) |
|
111
|
|
|
{ |
|
112
|
|
|
if ($this->_isJsForm) { |
|
113
|
|
|
return implode('', [ |
|
114
|
|
|
$this->hidden('action', ['value' => '', 'class' => 'jsFormAction']), |
|
115
|
|
|
parent::end($secureAttributes) |
|
116
|
|
|
]); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return parent::end($secureAttributes); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
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.