|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Manage Form |
|
5
|
|
|
* |
|
6
|
|
|
* @category lib |
|
7
|
|
|
* @author Judicaël Paquet <[email protected]> |
|
8
|
|
|
* @copyright Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93) |
|
9
|
|
|
* @license https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
|
10
|
|
|
* @version Release: 1.0.0 |
|
11
|
|
|
* @filesource https://github.com/las93/venus2 |
|
12
|
|
|
* @link https://github.com/las93 |
|
13
|
|
|
* @since 1.0 |
|
14
|
|
|
* |
|
15
|
|
|
* @tutorial == in the controller: |
|
16
|
|
|
* |
|
17
|
|
|
* $this->form |
|
18
|
|
|
* ->add('task', 'text') |
|
19
|
|
|
* ->add('dueDate', 'date') |
|
20
|
|
|
* ->add('save', 'submit'); |
|
21
|
|
|
* |
|
22
|
|
|
* $this->view |
|
23
|
|
|
* ->assign('form', $this->form->getForm()) |
|
24
|
|
|
* ->display(); |
|
25
|
|
|
* |
|
26
|
|
|
* in the template: |
|
27
|
|
|
* |
|
28
|
|
|
* {$form} |
|
29
|
|
|
* |
|
30
|
|
|
* == if you want test if the form is validated, you could do that: |
|
31
|
|
|
* |
|
32
|
|
|
* if ($this->form->isValid()) { ... } // after the form definition |
|
33
|
|
|
* |
|
34
|
|
|
* == check if the button is clicked: |
|
35
|
|
|
* |
|
36
|
|
|
* if ($this->form->get('save')->isClicked()) { ... } |
|
37
|
|
|
*/ |
|
38
|
|
|
namespace Venus\lib; |
|
39
|
|
|
|
|
40
|
|
|
use \Attila\lib\Entity as LibEntity; |
|
41
|
|
|
use \Venus\lib\Form\Checkbox as Checkbox; |
|
42
|
|
|
use \Venus\lib\Form\Container as Container; |
|
43
|
|
|
use \Venus\lib\Form\Label as Label; |
|
44
|
|
|
use \Venus\lib\Form\Input as Input; |
|
45
|
|
|
use \Venus\lib\Form\Radio as Radio; |
|
46
|
|
|
use \Venus\lib\Form\Select as Select; |
|
47
|
|
|
use \Venus\lib\Form\Textarea as Textarea; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* This class manage the Form |
|
51
|
|
|
* |
|
52
|
|
|
* @category lib |
|
53
|
|
|
* @author Judicaël Paquet <[email protected]> |
|
54
|
|
|
* @copyright Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93) |
|
55
|
|
|
* @license https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
|
56
|
|
|
* @version Release: 1.0.0 |
|
57
|
|
|
* @filesource https://github.com/las93/venus2 |
|
58
|
|
|
* @link https://github.com/las93 |
|
59
|
|
|
* @since 1.0 |
|
60
|
|
|
*/ |
|
61
|
|
|
class Form |
|
62
|
|
|
{ |
|
63
|
|
|
/** |
|
64
|
|
|
* Elements of the form |
|
65
|
|
|
* |
|
66
|
|
|
* @access private |
|
67
|
|
|
* @var array |
|
68
|
|
|
*/ |
|
69
|
|
|
private $_aElement = array(); |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Increment for form |
|
73
|
|
|
* |
|
74
|
|
|
* @access private |
|
75
|
|
|
* @var int |
|
76
|
|
|
*/ |
|
77
|
|
|
private static $_iFormIncrement = 0; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* number of form |
|
81
|
|
|
* |
|
82
|
|
|
* @access private |
|
83
|
|
|
* @var int |
|
84
|
|
|
*/ |
|
85
|
|
|
private $_iFormNumber = 0; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Separator between fields of form |
|
89
|
|
|
* |
|
90
|
|
|
* @access private |
|
91
|
|
|
* @var string |
|
92
|
|
|
*/ |
|
93
|
|
|
private $_sSeparator = '<br/>'; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* The entity to save with the formular |
|
97
|
|
|
* |
|
98
|
|
|
* @access private |
|
99
|
|
|
* @var string |
|
100
|
|
|
*/ |
|
101
|
|
|
private $_sSynchronizeEntity = null; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* The id of entity |
|
105
|
|
|
* |
|
106
|
|
|
* @access private |
|
107
|
|
|
* @var int |
|
108
|
|
|
*/ |
|
109
|
|
|
private $_iIdEntity = null; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* The entity to save with the formular |
|
113
|
|
|
* |
|
114
|
|
|
* @access private |
|
115
|
|
|
* @var int |
|
116
|
|
|
*/ |
|
117
|
|
|
private $_iIdEntityCreated = null; |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* constructor that it increment (static) for all use |
|
121
|
|
|
* |
|
122
|
|
|
* @access public |
|
123
|
|
|
*/ |
|
124
|
|
|
public function __construct() |
|
125
|
|
|
{ |
|
126
|
|
|
self::$_iFormIncrement++; |
|
127
|
|
|
$this->_iFormNumber = self::$_iFormIncrement; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* add an element in the form |
|
132
|
|
|
* |
|
133
|
|
|
* @access public |
|
134
|
|
|
* @param string $sName name |
|
135
|
|
|
* @param string|\Venus\lib\Form $mType type of field |
|
136
|
|
|
* @param string $sLabel label of field |
|
137
|
|
|
* @param mixed $mValue value of field |
|
138
|
|
|
* @parma mixed $mOptions options (for select) |
|
139
|
|
|
* @return \Venus\lib\Form |
|
140
|
|
|
*/ |
|
141
|
|
|
public function add($sName, $mType, $sLabel = null, $mValue = null, $mOptions = null) |
|
142
|
|
|
{ |
|
143
|
|
|
if ($mType instanceof Container) { |
|
144
|
|
|
|
|
145
|
|
|
$this->_aElement[$sName] = $mType; |
|
146
|
|
|
} else if ($mType === 'text' || $mType === 'submit' || $mType === 'password' || $mType === 'file' || $mType === 'tel' |
|
147
|
|
|
|| $mType === 'url' || $mType === 'email' || $mType === 'search' || $mType === 'date' || $mType === 'time' |
|
148
|
|
|
|| $mType === 'datetime' || $mType === 'month' || $mType === 'week' || $mType === 'number' || $mType === 'range' |
|
149
|
|
|
|| $mType === 'color') { |
|
150
|
|
|
|
|
151
|
|
|
$this->_aElement[$sName] = new Input($sName, $mType, $sLabel, $mValue); |
|
152
|
|
|
} elseif ($mType === 'textarea') { |
|
153
|
|
|
|
|
154
|
|
|
$this->_aElement[$sName] = new Textarea($sName, $sLabel, $mValue); |
|
155
|
|
|
} else if ($mType === 'select') { |
|
156
|
|
|
|
|
157
|
|
|
$this->_aElement[$sName] = new Select($sName, $mOptions, $sLabel, $mValue); |
|
158
|
|
|
} else if ($mType === 'label') { |
|
159
|
|
|
|
|
160
|
|
|
$this->_aElement[$sName] = new Label($sName); |
|
161
|
|
|
} else if ($mType === 'list_checkbox') { |
|
162
|
|
|
|
|
163
|
|
|
$i = 0; |
|
164
|
|
|
|
|
165
|
|
|
$this->_aElement[$sName.'_'.$i++] = new Label($sLabel); |
|
166
|
|
|
|
|
167
|
|
|
foreach ($mValue as $mKey => $sValue) { |
|
168
|
|
|
|
|
169
|
|
|
$this->_aElement[$sName.'_'.$i++] = new Checkbox($sName, $sValue, $mKey, $mOptions); |
|
170
|
|
|
} |
|
171
|
|
|
} else if ($mType === 'checkbox') { |
|
172
|
|
|
|
|
173
|
|
|
$this->_aElement[$sName] = new Checkbox($sName, $sLabel, $mValue, $mOptions); |
|
174
|
|
|
} else if ($mType === 'radio') { |
|
175
|
|
|
|
|
176
|
|
|
$this->_aElement[$sName.rand(100000, 999999)] = new Radio($sName, $sLabel, $mValue, $mOptions); |
|
177
|
|
|
} else if ($mType === 'date') { |
|
178
|
|
|
|
|
179
|
|
|
$aDay = array(); |
|
180
|
|
|
|
|
181
|
|
|
for ($i = 1; $i <= 31; $i++) { |
|
182
|
|
|
|
|
183
|
|
|
if ($i < 10) { $aDay['0'.$i] = '0'.$i; } |
|
184
|
|
|
else { $aDay[$i] = $i; } |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$this->_aElement[$sName.'_day'] = new Select($sName, $aDay); |
|
188
|
|
|
|
|
189
|
|
|
$aMonth = array( |
|
190
|
|
|
'01' => 'Jan', |
|
191
|
|
|
'02' => 'Feb', |
|
192
|
|
|
'03' => 'Mar', |
|
193
|
|
|
'04' => 'Apr', |
|
194
|
|
|
'05' => 'May', |
|
195
|
|
|
'06' => 'Jun', |
|
196
|
|
|
'07' => 'Jui', |
|
197
|
|
|
'08' => 'Aug', |
|
198
|
|
|
'09' => 'Sep', |
|
199
|
|
|
'10' => 'Oct', |
|
200
|
|
|
'11' => 'Nov', |
|
201
|
|
|
'12' => 'Dec', |
|
202
|
|
|
); |
|
203
|
|
|
|
|
204
|
|
|
$this->_aElement[$sName.'_month'] = new Select($sName, $aMonth); |
|
205
|
|
|
|
|
206
|
|
|
$aYear = array(); |
|
207
|
|
|
|
|
208
|
|
|
for ($i = 1900; $i <= 2013; $i++) { |
|
209
|
|
|
|
|
210
|
|
|
$aYear[$i] = $i; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$this->_aElement[$sName.'_year'] = new Select($sName, $aMonth); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return $this; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* get id entity created by the formular |
|
221
|
|
|
* |
|
222
|
|
|
* @access public |
|
223
|
|
|
* @return int |
|
224
|
|
|
*/ |
|
225
|
|
|
public function getIdEntityCreated() : int |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->_iIdEntityCreated; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* set id entity created by the formular |
|
232
|
|
|
* |
|
233
|
|
|
* @access public |
|
234
|
|
|
* @param int $iIdEntityCreated |
|
235
|
|
|
* @return Form |
|
236
|
|
|
*/ |
|
237
|
|
|
public function setIdEntityCreated(int $iIdEntityCreated) : Form |
|
238
|
|
|
{ |
|
239
|
|
|
$this->_iIdEntityCreated = $iIdEntityCreated; |
|
240
|
|
|
return $this; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* get form number |
|
245
|
|
|
* |
|
246
|
|
|
* @access public |
|
247
|
|
|
* @return int |
|
248
|
|
|
*/ |
|
249
|
|
|
public function getFormNumber() : int |
|
250
|
|
|
{ |
|
251
|
|
|
return $this->_iFormNumber; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* set id entity created by the formular |
|
256
|
|
|
* |
|
257
|
|
|
* @access public |
|
258
|
|
|
* @param int $iFormNumber |
|
259
|
|
|
* @return Form |
|
260
|
|
|
*/ |
|
261
|
|
|
public function setFormNumber(int $iFormNumber) : Form |
|
262
|
|
|
{ |
|
263
|
|
|
$this->_iFormNumber = $iFormNumber; |
|
264
|
|
|
return $this; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* get global form |
|
269
|
|
|
* |
|
270
|
|
|
* @access public |
|
271
|
|
|
* @return \Venus\lib\Form\Container |
|
272
|
|
|
*/ |
|
273
|
|
|
public function getForm() |
|
274
|
|
|
{ |
|
275
|
|
|
$oForm = $this->getFormInObject(); |
|
276
|
|
|
|
|
277
|
|
|
$sFormContent = $oForm->start; |
|
278
|
|
|
|
|
279
|
|
|
foreach ($oForm->form as $sValue) { |
|
280
|
|
|
|
|
281
|
|
|
$sFormContent .= $sValue.$this->_sSeparator; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$sFormContent .= $oForm->end; |
|
285
|
|
|
|
|
286
|
|
|
$oContainer = new Container; |
|
287
|
|
|
$oContainer->setView($sFormContent) |
|
288
|
|
|
->setForm($this); |
|
289
|
|
|
|
|
290
|
|
|
return $oContainer; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* get global object form |
|
296
|
|
|
* |
|
297
|
|
|
* @access public |
|
298
|
|
|
* @return \stdClass |
|
299
|
|
|
*/ |
|
300
|
|
|
public function getFormInObject() |
|
301
|
|
|
{ |
|
302
|
|
|
$sExKey = null; |
|
303
|
|
|
|
|
304
|
|
|
if ($this->_iIdEntity > 0 && $this->_sSynchronizeEntity !== null && count($_POST) < 1) { |
|
305
|
|
|
|
|
306
|
|
|
$sModelName = str_replace('Entity', 'Model', $this->_sSynchronizeEntity); |
|
307
|
|
|
$oModel = new $sModelName; |
|
308
|
|
|
|
|
309
|
|
|
$oEntity = new $this->_sSynchronizeEntity; |
|
310
|
|
|
$sPrimaryKey = LibEntity::getPrimaryKeyNameWithoutMapping($oEntity); |
|
311
|
|
|
$sMethodName = 'findOneBy'.$sPrimaryKey; |
|
312
|
|
|
$oCompleteEntity = call_user_func_array(array(&$oModel, $sMethodName), array($this->_iIdEntity)); |
|
313
|
|
|
|
|
314
|
|
|
if (is_object($oCompleteEntity)) { |
|
315
|
|
|
|
|
316
|
|
|
foreach ($this->_aElement as $sKey => $sValue) { |
|
317
|
|
|
|
|
318
|
|
|
if ($sValue instanceof \Venus\lib\Form\Radio) { |
|
319
|
|
|
|
|
320
|
|
|
$sExKey = $sKey; |
|
321
|
|
|
$sKey = substr($sKey, 0, -6); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
if ($sValue instanceof Form) { |
|
325
|
|
|
|
|
326
|
|
|
; |
|
327
|
|
|
} else { |
|
328
|
|
|
|
|
329
|
|
|
$sMethodNameInEntity = 'get_'.$sKey; |
|
330
|
|
|
$mValue = $oCompleteEntity->$sMethodNameInEntity(); |
|
331
|
|
|
|
|
332
|
|
|
if ($sValue instanceof \Venus\lib\Form\Radio && method_exists($this->_aElement[$sExKey], 'setValueChecked')) { |
|
333
|
|
|
|
|
334
|
|
|
$this->_aElement[$sExKey]->setValueChecked($mValue); |
|
335
|
|
|
} else if (isset($mValue) && method_exists($this->_aElement[$sKey], 'setValue')) { |
|
336
|
|
|
|
|
337
|
|
|
$this->_aElement[$sKey]->setValue($mValue); |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
$oForm = new \StdClass(); |
|
345
|
|
|
$oForm->start = '<form name="form'.$this->_iFormNumber.'" method="post"><input type="hidden" value="1" name="validform'.$this->_iFormNumber.'">'; |
|
346
|
|
|
$oForm->form = array(); |
|
347
|
|
|
|
|
348
|
|
|
foreach ($this->_aElement as $sKey => $sValue) { |
|
349
|
|
|
|
|
350
|
|
|
if ($sValue instanceof Container) { |
|
351
|
|
|
|
|
352
|
|
|
$oForm->form[$sKey] = $sValue; |
|
353
|
|
|
} else { |
|
354
|
|
|
|
|
355
|
|
|
$oForm->form[$sKey] = $sValue->fetch(); |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
$oForm->end = '</form>'; |
|
360
|
|
|
|
|
361
|
|
|
return $oForm; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* get an element of formular |
|
366
|
|
|
* |
|
367
|
|
|
* @access public |
|
368
|
|
|
* @param string $sName name |
|
369
|
|
|
* @return object |
|
370
|
|
|
*/ |
|
371
|
|
|
public function get($sName) |
|
372
|
|
|
{ |
|
373
|
|
|
return $this->_aElement[$sName]; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* get the form separator |
|
378
|
|
|
* |
|
379
|
|
|
* @access public |
|
380
|
|
|
* @return string |
|
381
|
|
|
*/ |
|
382
|
|
|
public function getSeparator() |
|
383
|
|
|
{ |
|
384
|
|
|
return $this->_sSeparator; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* set the form separator |
|
389
|
|
|
* |
|
390
|
|
|
* @access public |
|
391
|
|
|
* @param string $sSeparator separator between the fields |
|
392
|
|
|
* @return \Venus\lib\Form |
|
393
|
|
|
*/ |
|
394
|
|
|
public function setSeparator($sSeparator) |
|
395
|
|
|
{ |
|
396
|
|
|
$this->_sSeparator = $sSeparator; |
|
397
|
|
|
return $this; |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* set the entity to synchronize with the formular |
|
402
|
|
|
* |
|
403
|
|
|
* @access public |
|
404
|
|
|
* @param $sSynchronizeEntity |
|
405
|
|
|
* @param int $iId id of the primary key |
|
406
|
|
|
* @return Form |
|
407
|
|
|
* @internal param string $sSeparator separator between the fields |
|
408
|
|
|
*/ |
|
409
|
|
|
public function synchronizeEntity($sSynchronizeEntity, $iId = null) |
|
410
|
|
|
{ |
|
411
|
|
|
if ($iId !== null) { $this->_iIdEntity = $iId; } |
|
412
|
|
|
|
|
413
|
|
|
$this->_sSynchronizeEntity = $sSynchronizeEntity; |
|
414
|
|
|
return $this; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* add constraint |
|
419
|
|
|
* |
|
420
|
|
|
* @access public |
|
421
|
|
|
* @param string $sName field name |
|
422
|
|
|
* @param object $oConstraint constraint on the field |
|
423
|
|
|
* @return \Venus\lib\Form |
|
424
|
|
|
*/ |
|
425
|
|
|
public function addConstraint($sName, $oConstraint) |
|
426
|
|
|
{ |
|
427
|
|
|
if ($this->_aElement[$sName] instanceof Input || $this->_aElement[$sName] instanceof Textarea) { |
|
428
|
|
|
|
|
429
|
|
|
$this->_aElement[$sName]->setConstraint($oConstraint); |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
return $this; |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* get all elements |
|
437
|
|
|
* |
|
438
|
|
|
* @access public |
|
439
|
|
|
* @return array |
|
440
|
|
|
*/ |
|
441
|
|
|
public function getElement() { |
|
442
|
|
|
|
|
443
|
|
|
return $this->_aElement; |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* get all elements |
|
448
|
|
|
* |
|
449
|
|
|
* @access public |
|
450
|
|
|
* @return int |
|
451
|
|
|
*/ |
|
452
|
|
|
public function getIdEntity() { |
|
453
|
|
|
|
|
454
|
|
|
return $this->_iIdEntity; |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
/** |
|
458
|
|
|
* get all elements |
|
459
|
|
|
* |
|
460
|
|
|
* @access public |
|
461
|
|
|
* @return string |
|
462
|
|
|
*/ |
|
463
|
|
|
public function getSynchronizeEntity() { |
|
464
|
|
|
|
|
465
|
|
|
return $this->_sSynchronizeEntity; |
|
466
|
|
|
} |
|
467
|
|
|
} |
|
468
|
|
|
|