1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Container of Form lib |
5
|
|
|
* |
6
|
|
|
* @category Venus |
7
|
|
|
* @package Venus\lib\Form |
8
|
|
|
* @author Judicaël Paquet <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93) |
10
|
|
|
* @license https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
11
|
|
|
* @version Release: 1.0.0 |
12
|
|
|
* @filesource https://github.com/las93/venus2 |
13
|
|
|
* @link https://github.com/las93 |
14
|
|
|
* @since 1.0 |
15
|
|
|
*/ |
16
|
|
|
namespace Venus\lib\Form; |
17
|
|
|
|
18
|
|
|
use \Attila\lib\Entity as LibEntity; |
19
|
|
|
use \Venus\lib\Form as Form; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Container of Form lib |
23
|
|
|
* |
24
|
|
|
* @category Venus |
25
|
|
|
* @package Venus\lib\Form |
26
|
|
|
* @author Judicaël Paquet <[email protected]> |
27
|
|
|
* @copyright Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93) |
28
|
|
|
* @license https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
29
|
|
|
* @version Release: 1.0.0 |
30
|
|
|
* @filesource https://github.com/las93/venus2 |
31
|
|
|
* @link https://github.com/las93 |
32
|
|
|
* @since 1.0 |
33
|
|
|
*/ |
34
|
|
|
class Container |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* complete view |
38
|
|
|
* |
39
|
|
|
* @access private |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $_sView = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* form library with its entity |
46
|
|
|
* |
47
|
|
|
* @access private |
48
|
|
|
* @var Form |
49
|
|
|
*/ |
50
|
|
|
private $_oForm = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Block the save in the entity if you don't call handleRequest |
54
|
|
|
* |
55
|
|
|
* @access private |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
private $_bHandleRequestActivate = false; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Request of the formular |
62
|
|
|
* |
63
|
|
|
* @access private |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
private $_aRequest = null; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* get the Value |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
* @return \stdClass |
73
|
|
|
*/ |
74
|
|
|
public function createView() : \stdClass |
75
|
|
|
{ |
76
|
|
|
$oView = new \stdClass; |
77
|
|
|
$oView->form = $this->_sView; |
78
|
|
|
$oView->form_start = $this->_oForm->getFormInObject()->start; |
79
|
|
|
$oView->form_end = $this->_oForm->getFormInObject()->end; |
80
|
|
|
$oView->form_row = array(); |
81
|
|
|
|
82
|
|
|
foreach ($this->_oForm->getFormInObject()->form as $sKey => $mValue) { |
83
|
|
|
|
84
|
|
|
if ($mValue instanceof Container) { |
85
|
|
|
|
86
|
|
|
$oNewForm = $mValue->createView(); |
87
|
|
|
$oView->form_row[$sKey] = $oNewForm->form_row; |
88
|
|
|
} else { |
89
|
|
|
|
90
|
|
|
$oView->form_row[$sKey] = $mValue; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $oView; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* set the Value |
99
|
|
|
* |
100
|
|
|
* @access public |
101
|
|
|
* @param string $sView Display of form; |
102
|
|
|
* @return \Venus\lib\Form\Container |
103
|
|
|
*/ |
104
|
|
|
public function setView(string $sView) : Container |
105
|
|
|
{ |
106
|
|
|
$this->_sView = $sView; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* handle the request to do many actions on it |
112
|
|
|
* |
113
|
|
|
* @access public |
114
|
|
|
* @param array $aRequest request like $_POST |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function handleRequest(array $aRequest) : bool |
118
|
|
|
{ |
119
|
|
|
if (!count($_POST)) { return true; } |
120
|
|
|
|
121
|
|
|
// Validation |
122
|
|
|
foreach ($this->_oForm->getElement() as $sKey => $sValue) { |
123
|
|
|
|
124
|
|
|
if (!$sValue instanceof self && !$this->_validate($sValue)) { |
125
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Save |
131
|
|
|
if ($this->_oForm->getIdEntity() > 0 && $this->_oForm->getSynchronizeEntity() !== null && count($aRequest) > 0) { |
132
|
|
|
|
133
|
|
|
$sModelName = str_replace('Entity', 'Model', $this->_oForm->getSynchronizeEntity()); |
134
|
|
|
$oModel = new $sModelName; |
|
|
|
|
135
|
|
|
|
136
|
|
|
$oEntity = new $this->_oForm->getSynchronizeEntity(); |
|
|
|
|
137
|
|
|
$sPrimaryKey = LibEntity::getPrimaryKeyNameWithoutMapping($oEntity); |
138
|
|
|
$sMethodName = 'set_'.$sPrimaryKey; |
139
|
|
|
|
140
|
|
|
call_user_func_array(array(&$oEntity, $sMethodName), array($this->_oForm->getIdEntity())); |
141
|
|
|
|
142
|
|
View Code Duplication |
foreach ($this->_oForm->getElement() as $sKey => $sValue) { |
|
|
|
|
143
|
|
|
|
144
|
|
|
$sMethodName = 'set_'.$sValue->getName().''; |
145
|
|
|
call_user_func_array(array(&$oEntity, $sMethodName), array($aRequest[$sValue->getName()])); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$oEntity->save(); |
149
|
|
|
} else if ($this->_oForm->getSynchronizeEntity() !== null && isset($aRequest) && count($aRequest) > 0) { |
150
|
|
|
|
151
|
|
|
$oEntity = new $this->_oForm->_sSynchronizeEntity; |
|
|
|
|
152
|
|
|
|
153
|
|
View Code Duplication |
foreach ($this->_oForm->getElement() as $sKey => $sValue) { |
|
|
|
|
154
|
|
|
|
155
|
|
|
$sMethodName = 'set_'.$sValue->getName().''; |
156
|
|
|
call_user_func_array(array(&$oEntity, $sMethodName), array($aRequest[$sValue->getName()])); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$this->_oForm->setIdEntityCreated($oEntity->save()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->_bHandleRequestActivate = true; |
163
|
|
|
$this->_aRequest = $aRequest; |
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* set Form lib with its entity |
169
|
|
|
* |
170
|
|
|
* @access public |
171
|
|
|
* @param Form $oForm request like $_POST |
172
|
|
|
* @return \Venus\lib\Form\Container |
173
|
|
|
*/ |
174
|
|
|
public function setForm(Form $oForm) : Container |
175
|
|
|
{ |
176
|
|
|
$this->_oForm = $oForm; |
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* if this form is validate and save |
182
|
|
|
* |
183
|
|
|
* @access public |
184
|
|
|
* @return boolean |
185
|
|
|
*/ |
186
|
|
|
public function isValid() : bool |
187
|
|
|
{ |
188
|
|
|
if ($this->_bHandleRequestActivate === true) { return true; } else { return false; } |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* if this form is validate and save |
193
|
|
|
* |
194
|
|
|
* @access public |
195
|
|
|
* @return boolean |
196
|
|
|
*/ |
197
|
|
|
public function isSubmitted() : bool |
198
|
|
|
{ |
199
|
|
|
if (isset($_POST['validform'.$this->_oForm->getFormNumber()]) && $_POST['validform'.$this->_oForm->getFormNumber()] == 1) { |
200
|
|
|
|
201
|
|
|
return true; |
202
|
|
|
} else { |
203
|
|
|
|
204
|
|
|
return false; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* if this form is validate and save |
210
|
|
|
* |
211
|
|
|
* @access public |
212
|
|
|
* @param string $sElementName element name what we want test the click |
213
|
|
|
* @return boolean |
214
|
|
|
*/ |
215
|
|
|
public function isClicked(string $sElementName) : bool |
216
|
|
|
{ |
217
|
|
|
if (isset($_POST[$sElementName]) && $_POST[$sElementName]) { return true; } else { return false; } |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* if the element is valide or not (with the constraint) |
222
|
|
|
* |
223
|
|
|
* @access private |
224
|
|
|
* @param object $oElement element of formular |
225
|
|
|
* @return boolean |
226
|
|
|
*/ |
227
|
|
|
private function _validate($oElement) : bool |
228
|
|
|
{ |
229
|
|
|
foreach ($oElement->getConstraint() as $oConstraint) { |
230
|
|
|
|
231
|
|
|
if (!$oConstraint->validate($_POST[$oElement->getName()])) { |
232
|
|
|
|
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return true; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.