1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Form; |
4
|
|
|
|
5
|
|
|
use Nip\Form\Elements\AbstractElement as ElementAbstract; |
6
|
|
|
use Nip\View; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class AbstractForm |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractForm |
13
|
|
|
{ |
14
|
|
|
use Traits\DataProcessingTrait; |
15
|
|
|
use Traits\HasAttributesTrait; |
16
|
|
|
use Traits\HasButtonsTrait; |
17
|
|
|
use Traits\HasClassTrait; |
18
|
|
|
use Traits\HasDisplayGroupsTrait; |
19
|
|
|
use Traits\HasElementsTrait; |
20
|
|
|
use Traits\HasErrorsTrait; |
21
|
|
|
use Traits\HasExecutionMethodsTrait; |
22
|
|
|
use Traits\HasRendererTrait; |
23
|
|
|
use Traits\MagicMethodElementsFormTrait; |
24
|
|
|
use Traits\MessagesTrait; |
25
|
|
|
use Traits\NewElementsMethods; |
26
|
|
|
|
27
|
|
|
const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded'; |
28
|
|
|
const ENCTYPE_MULTIPART = 'multipart/form-data'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $methods = ['delete', 'get', 'post', 'put']; |
34
|
|
|
|
35
|
|
|
protected $_options = []; |
36
|
|
|
|
37
|
|
|
protected $_decorators = []; |
38
|
|
|
protected $_cache; |
39
|
|
|
|
40
|
|
|
protected $controllerView = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* AbstractForm constructor. |
44
|
|
|
*/ |
45
|
26 |
|
public function __construct() |
46
|
|
|
{ |
47
|
26 |
|
$this->init(); |
48
|
26 |
|
$this->postInit(); |
49
|
26 |
|
} |
50
|
|
|
|
51
|
26 |
|
public function init() |
52
|
|
|
{ |
53
|
26 |
|
$this->initAction(); |
54
|
26 |
|
} |
55
|
|
|
|
56
|
26 |
|
protected function initAction() |
57
|
|
|
{ |
58
|
26 |
|
if (function_exists('current_url')) { |
59
|
|
|
$this->setAction(current_url()); |
60
|
|
|
} |
61
|
26 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $action |
65
|
|
|
* @return AbstractForm |
66
|
|
|
*/ |
67
|
|
|
public function setAction($action) |
68
|
|
|
{ |
69
|
|
|
return $this->setAttrib('action', (string)$action); |
70
|
|
|
} |
71
|
|
|
|
72
|
26 |
|
public function postInit() |
73
|
|
|
{ |
74
|
26 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $name |
78
|
|
|
* @return ElementAbstract|null |
79
|
|
|
*/ |
80
|
1 |
|
public function __get($name) |
81
|
|
|
{ |
82
|
1 |
|
$element = $this->getElement($name); |
83
|
1 |
|
if ($element) { |
|
|
|
|
84
|
1 |
|
return $element; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $key |
92
|
|
|
* @param $value |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function setOption($key, $value) |
96
|
|
|
{ |
97
|
|
|
$key = (string)$key; |
98
|
|
|
$this->_options[$key] = $value; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $key |
105
|
|
|
* @return mixed|null |
106
|
|
|
*/ |
107
|
|
|
public function getOption($key) |
108
|
|
|
{ |
109
|
|
|
$key = (string)$key; |
110
|
|
|
if (!isset($this->_options[$key])) { |
111
|
|
|
return null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->_options[$key]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $method |
119
|
|
|
* @return AbstractForm |
120
|
|
|
*/ |
121
|
|
|
public function setMethod($method) |
122
|
|
|
{ |
123
|
|
|
if (in_array($method, $this->methods)) { |
124
|
|
|
return $this->setAttrib('method', $method); |
125
|
|
|
} |
126
|
|
|
trigger_error('Method is not valid', E_USER_ERROR); |
127
|
|
|
|
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $key |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
11 |
|
public function getCache($key) |
137
|
|
|
{ |
138
|
11 |
|
return $this->_cache[$key]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $key |
143
|
|
|
* @param $value |
144
|
|
|
*/ |
145
|
11 |
|
public function setCache($key, $value) |
146
|
|
|
{ |
147
|
11 |
|
$this->_cache[$key] = $value; |
148
|
11 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $key |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
public function isCache($key) |
155
|
|
|
{ |
156
|
|
|
return isset($this->_cache[$key]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
public function getName() |
163
|
|
|
{ |
164
|
|
|
return get_class($this); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return View|null |
169
|
|
|
*/ |
170
|
|
|
public function getControllerView() |
171
|
|
|
{ |
172
|
|
|
if (!$this->controllerView) { |
173
|
|
|
$this->controllerView = app('kernel')->getDispatcher()->getCurrentController()->getView(); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->controllerView; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|