1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Koch Framework |
5
|
|
|
* Jens-André Koch © 2005 - onwards. |
6
|
|
|
* |
7
|
|
|
* This file is part of "Koch Framework". |
8
|
|
|
* |
9
|
|
|
* License: GNU/GPL v2 or any later version, see LICENSE file. |
10
|
|
|
* |
11
|
|
|
* This program is free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Koch\Form\Generator; |
26
|
|
|
|
27
|
|
|
use Koch\Form\Form; |
28
|
|
|
use Koch\Form\FormGeneratorInterface; |
29
|
|
|
use Koch\Functions; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Koch Framework - Form Generator from a PHP Array description. |
33
|
|
|
* |
34
|
|
|
* Purpose: automatic form generation from an array. |
35
|
|
|
*/ |
36
|
|
|
class PHPArray extends Form implements FormGeneratorInterface |
37
|
|
|
{ |
38
|
|
|
public function __construct(array $form_array = null, $form_object = null) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
if (null !== $form_array) { |
|
|
|
|
41
|
|
|
if ($form_object === null) { |
|
|
|
|
42
|
|
|
// init parent Koch\Form\Form with name, method and action |
43
|
|
|
parent::__construct( |
44
|
|
|
$form_array['form']['name'], |
|
|
|
|
45
|
|
|
$form_array['form']['method'], |
|
|
|
|
46
|
|
|
$form_array['form']['action'] |
|
|
|
|
47
|
|
|
); |
48
|
|
|
} else { |
49
|
|
|
$form_object::__construct( |
|
|
|
|
50
|
|
|
$form_array['form']['name'], |
|
|
|
|
51
|
|
|
$form_array['form']['method'], |
|
|
|
|
52
|
|
|
$form_array['form']['action'] |
|
|
|
|
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// unset the key form inside form_array |
57
|
|
|
// because the "form" description is no longer needed |
58
|
|
|
// parent \Koch\Form\Form is already informed |
59
|
|
|
unset($form_array['form']); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->validateArrayAndgenerateForm($form_array); |
|
|
|
|
62
|
|
|
|
63
|
|
|
return $this; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function validateArrayAndgenerateForm($form_array) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
// first we ensure, that the formdescription meets certain requirements |
70
|
|
|
if (self::validateFormArrayStructure($form_array)) { |
|
|
|
|
71
|
|
|
// now that the form description is valid, we generate the form |
72
|
|
|
$this->generateFormByArray($form_array); |
|
|
|
|
73
|
|
|
} else { // the formdescription is invalid |
74
|
|
|
throw new \Koch\Exception\Exception('Obligatory formelements not present.', 30); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Level 1 - The form. |
80
|
|
|
* |
81
|
|
|
* $form_array_section is an array of the following structure: |
82
|
|
|
* |
83
|
|
|
* @todo $form_array_section description |
84
|
|
|
* |
85
|
|
|
* Level 2 - The formelements |
86
|
|
|
* |
87
|
|
|
* $form_array_element is an array of the following structure: |
88
|
|
|
* |
89
|
|
|
* Obligatory Elements to describe the Formelement |
90
|
|
|
* These array keys have to exist! |
91
|
|
|
* |
92
|
|
|
* [id] => resultsPerPage_show |
93
|
|
|
* [name] => resultsPerPage_show |
94
|
|
|
* [label] => Results per Page for Action Show |
95
|
|
|
* [description] => This defines the Number of Newsitems to show per Page in Newsmodule |
96
|
|
|
* [formfieldtype] => text |
97
|
|
|
* |
98
|
|
|
* Optional Elements to describe the Formelement |
99
|
|
|
* |
100
|
|
|
* [value] => 3 |
101
|
|
|
* [class] => cssClass |
102
|
|
|
* |
103
|
|
|
* @param $form_array the form array |
104
|
|
|
* |
105
|
|
|
* @return bool|null true/false |
106
|
|
|
*/ |
107
|
|
|
public static function validateFormArrayStructure($form_array) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$obligatory_form_array_elements = ['id', 'name', 'label', 'description', 'formfieldtype', 'value']; |
|
|
|
|
110
|
|
|
$optional_form_array_elements = ['class', 'decorator']; |
|
|
|
|
111
|
|
|
|
112
|
|
|
// loop over all elements of the form description array |
113
|
|
|
foreach ($form_array as $form_array_section => $form_array_elements) { |
|
|
|
|
114
|
|
|
#\Koch\Debug\Debug::firebug($form_array_elements); |
|
|
|
|
115
|
|
|
#\Koch\Debug\Debug::firebug($form_array_section); |
|
|
|
|
116
|
|
|
|
117
|
|
|
foreach ($form_array_elements as $form_array_element_number => $form_array_element) { |
|
|
|
|
118
|
|
|
#\Koch\Debug\Debug::firebug(array_keys($form_array_element)); |
|
|
|
|
119
|
|
|
#\Koch\Debug\Debug::firebug($obligatory_form_array_elements); |
|
|
|
|
120
|
|
|
|
121
|
|
|
// this does the validation. it ensures that required keys are present |
122
|
|
|
$report_differences_or_true = Functions::array_compare( |
|
|
|
|
123
|
|
|
$obligatory_form_array_elements, |
|
|
|
|
124
|
|
|
array_keys($form_array_element) |
|
|
|
|
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
// errorcheck for valid formfield elements |
128
|
|
|
if (is_array($report_differences_or_true) === false) { |
|
|
|
|
129
|
|
|
// form description arrays are identical |
130
|
|
|
return true; |
131
|
|
|
} else { |
132
|
|
|
// form description arrays are not identical |
133
|
|
|
throw new \Koch\Exception\Exception( |
134
|
|
|
'Form Array Structure not valid. The first array shows the obligatory form array elements. |
135
|
|
|
The second array shows your form definition. Please add the missing array keys with values.' |
136
|
|
|
. var_dump($report_differences_or_true) |
|
|
|
|
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function generateFormByArray($form_array) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
// debug display incomming form description array |
146
|
|
|
#\Koch\Debug\Debug::firebug($form_array); |
|
|
|
|
147
|
|
|
|
148
|
|
|
// loop over all elements of the form description array |
149
|
|
|
foreach ($form_array as $form_array_section => $form_array_elements) { |
|
|
|
|
150
|
|
|
#\Koch\Debug\Debug::firebug($form_array_elements); |
|
|
|
|
151
|
|
|
#\Koch\Debug\Debug::firebug($form_array_section); |
|
|
|
|
152
|
|
|
|
153
|
|
|
foreach ($form_array_elements as $form_array_element_number => $form_array_element) { |
|
|
|
|
154
|
|
|
#\Koch\Debug\Debug::firebug($form_array_element); |
|
|
|
|
155
|
|
|
|
156
|
|
|
// @todo ensure these elements exist !!! |
157
|
|
|
|
158
|
|
|
// add a new element to this form, position it by it's number in the array |
159
|
|
|
$this->addElement($form_array_element['formfieldtype'], $form_array_element_number); |
|
|
|
|
160
|
|
|
|
161
|
|
|
// fetch the new formelement object by its positional number |
162
|
|
|
$formelement = $this->getElementByPosition($form_array_element_number); |
|
|
|
|
163
|
|
|
|
164
|
|
|
#\Koch\Debug\Debug::firebug($formelement); |
|
|
|
|
165
|
|
|
|
166
|
|
|
// and apply the settings (id, name, description, value) to it |
167
|
|
|
$formelement->setID($form_array_element['id']); |
|
|
|
|
168
|
|
|
|
169
|
|
|
// provide array access to the form data (in $_POST) by prefixing it with the formulars name |
170
|
|
|
// @todo if you group formelements, add the name of the group here |
171
|
|
|
$formelement->setName($this->getName() . '[' . $form_array_section . '][' . $form_array_element['name'] . ']'); |
|
|
|
|
172
|
|
|
$formelement->setDescription($form_array_element['description']); |
|
|
|
|
173
|
|
|
|
174
|
|
|
// @todo consider this as formdebug display (sets formname as label) |
175
|
|
|
#$formelement->setLabel($this->getName().'['.$form_array_element['name'].']'); |
|
|
|
|
176
|
|
|
|
177
|
|
|
$formelement->setLabel($form_array_element['label']); |
|
|
|
|
178
|
|
|
|
179
|
|
|
// set the options['selected'] value as default value |
180
|
|
|
if ($form_array_element['options']['selected'] !== null) { |
|
|
|
|
181
|
|
|
$formelement->setDefault($form_array_element['options']['selected']); |
|
|
|
|
182
|
|
|
unset($form_array_element['options']['selected']); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* check if $form_array_element['value'] is of type array or single value |
187
|
|
|
* array indicates, that we have a request for |
188
|
|
|
* something like a multiselect formfield with several options. |
189
|
|
|
*/ |
190
|
|
|
if (is_array($form_array_element['value']) === false) { |
|
|
|
|
191
|
|
|
$formelement->setValue($form_array_element['value']); |
|
|
|
|
192
|
|
|
} else { |
193
|
|
|
$formelement->setOptions($form_array_element['value']); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/* |
197
|
|
|
* OPTIONAL ELEMENTS |
198
|
|
|
*/ |
199
|
|
|
|
200
|
|
|
// if we have a class attribute defined, then add it (optional) |
201
|
|
|
if ($form_array_element['class'] !== null) { |
|
|
|
|
202
|
|
|
$formelement->setCssClass($form_array_element['class']); |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/* |
206
|
|
|
* set a decorator for the formelement |
207
|
|
|
* optional because: the default decorator would be active |
208
|
|
|
*/ |
209
|
|
|
if ($form_array_element['decorator'] !== null) { |
|
|
|
|
210
|
|
|
if ($form_array_element['decorator'] instanceof \Koch\Form\Element\Decorator) { |
|
|
|
|
211
|
|
|
$formelement->setDecorator($form_array_element['decorator']); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// unset the form description array, because we are done with it |
218
|
|
|
unset($form_array); |
|
|
|
|
219
|
|
|
|
220
|
|
|
return $this->render(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Facade/Shortcut. |
225
|
|
|
*/ |
226
|
|
|
public function generate($array) |
227
|
|
|
{ |
228
|
|
|
$this->generateFormByArray($array); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function generateArrayByForm() |
232
|
|
|
{ |
233
|
|
|
// serialize an save the array |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.