This repository has been archived by the owner on May 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormGenerator.php
368 lines (302 loc) · 11.7 KB
/
FormGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
class FormGenerator {
private $inputFields = array();
private $form;
public function makeForm($label, $action, $inputFields,
$description = "", $buttonText = "Submit", $buttonValue = 0,
$method = "GET", $enctype = "application/x-www-form-urlencoded",
$displayCapthca = false, $isDisabled = false) {
$this->form = new Form($label, $action, $inputFields, $description,
$buttonText, $buttonValue, $method, $enctype,
$displayCapthca, $isDisabled);
echo $this->form->getHtml();
}
// new textfield
public function addText($label, $name, $value="", $placeholder="", $description="", $isDisabled=false) {
$this->inputFields[] = new InputField($label, InputFieldType::TEXT, $name, $value, $placeholder, $description, $isDisabled);
}
// new password field
public function addPassword($label, $name, $value="", $placeholder="", $description="", $isDisabled=false) {
$this->inputFields[] = new InputField($label, InputFieldType::PASSWORD, $name, $value, $placeholder, $description, $isDisabled);
}
// new hidden field
public function addHidden($name, $value="") {
$this->inputFields[] = new InputField("", InputFieldType::HIDDEN, $name, $value);
}
// new file field
public function addFile($label, $name, $description="", $isDisabled=false) {
$this->inputFields[] = new InputField($label, InputFieldType::FILE, $name, "", "", $description, $isDisabled);
}
// new textarea
public function addTextarea($label, $name, $value="", $placeholder="", $description="", $isDisabled=false) {
$this->inputFields[] = new InputField($label, InputFieldType::TEXTAREA, $name, $value, $placeholder, $description, $isDisabled);
}
// new radio list
public function addRadio($label, $name, $itemList = array(), $selectedItem="", $description="", $isDisabled=false) {
$this->inputFields[] = new InputField($label, InputFieldType::RADIO, $name, $selectedItem, "", $description, $isDisabled, false, $itemList, array());
}
// new checkbox list
public function addCheckbox($label, $name, $itemList = array(), $selectedItems = array(), $description="", $isDisabled=false) {
$this->inputFields[] = new InputField($label, InputFieldType::CHECKBOX, $name, "", "", $description, $isDisabled, false, $itemList, $selectedItems);
}
// new select list
public function addSelect($label, $name, $itemList = array(), $selectedItems = array(), $description="", $isDisabled=false, $isMultiSelectable = false, $pleaseSelectEnabled = true) {
$this->inputFields[] = new InputField($label, InputFieldType::SELECT, $name, "", "", $description, $isDisabled, $isMultiSelectable, $itemList, $selectedItems, $pleaseSelectEnabled);
}
public function getInputFields() {
return $this->inputFields;
}
}
// html form elmeent
class Form {
// required parameters
private $label;
private $action;
private $inputFields = array(); // array of InputField Objects
// non required and default value params
private $description;
private $buttonText = "Submit";
private $buttonValue = 0;
private $method = "GET";
private $enctype = "application/x-www-form-urlencoded";
private $displayCapthca = false;
private $isDisabled = false;
// construct form
public function __construct($title, $action, $inputFields,
$description = "", $buttonText = "Submit", $buttonValue = 0,
$method = "GET", $enctype = "application/x-www-form-urlencoded",
$displayCapthca = false, $isDisabled = false) {
// input fields should be specified
if(!is_array($inputFields) || sizeof($inputFields) < 1 ) {
return false;
}
$this->title = $title;
$this->action = $action;
$this->inputFields = $inputFields;
$this->description = $description;
$this->buttonText = $buttonText;
$this->buttonValue = $buttonValue;
$this->method = $method;
$this->enctype = $enctype;
$this->displayCapthca = $displayCapthca;
$this->isDisabled = $isDisabled;
}
public function getHtml() {
$result = '<div class="form-container">';
$result .= '<form method="'.$this->method.'" action="'.$this->action.'" enctype="'.$this->enctype.'">';
$result .= '<div class="row">';
$result .= '<div class="col-md-12">';
$result .= '<h3>'.$this->title.'</h3>';
$result .= '<p>'.$this->description.'</p>';
$result .= '</div>';
$result .= '</div>';
$result .= '<div class="row">';
$result .= '<div class="col-md-12">';
foreach($this->inputFields as $key => $val) {
$result .= $val->getHtml();
}
$result .= '</div>';
$result .= '</div>';
$result .= '<div class="row">';
$result .= '<div class="col-md-12 text-right">';
$result .= '<input type="submit" class="btn btn-info" value="'.$this->buttonText.'" /></div>';
$result .= '</div>';
$result .= '</form>';
$result .= '</div>';
return $result;
}
}
class InputField{
// parameters
private $label;
private $name;
private $type;
private $value; // selected value for select, radio, checkbox
private $placeholder = "";
private $description;
private $isDisabled = false;
private $isMultiSelectable = false;
private $itemList = array(); // ($key=>value)
private $selectedItems = array(); // $key
private $pleaseSelectEnabled = true;
// construct with all parameters
public function __construct($label, $type, $name, $value = "", $placeholder = "",
$description = "", $isDisabled = false,
$isMultiSelectable = false, $itemList = array(),
$selectedItems = array(), $pleaseSelectEnabled = true) {
$this->title = $label;
$this->type = $type;
$this->name = $name;
$this->value = $value;
$this->placeholder = $placeholder;
$this->description = $description;
$this->isDisabled = $isDisabled;
$this->isMultiSelectable = $isMultiSelectable;
$this->itemList = $itemList;
$this->selectedItems = $selectedItems;
$this->pleaseSelectEnabled = $pleaseSelectEnabled;
}
// return html of form field
public function getHtml(){
$result = '<div class="form-group">'; // validation class will be here
$result .= $this->getFieldHtmlByType($this->type);
$result .= '</div>';
return $result;
}
//
private function getFieldHtmlByType($type){
$result = "";
if($this->isInput($type)) {
$result .= $this->getCommonLabelForField();
$result .= '<input value="'.$this->value.'" name="'.$this->name.'" id="'.$this->name.'" type="'.$type.'" placeholder="'.$this->placeholder.'" class="form-control" '.$this->getDisableOption().' aria-describedby="'.$this->name.'-help"/>';
$result .= $this->getCommonDescriptionForField();
}
else if($this->isTextarea($type)) {
$result .= $this->getCommonLabelForField();
$result .= '<textarea id="'.$this->name.'" name="'.$this->name.'" class="form-control" rows="4" placeholder="'.$this->placeholder.'" '.$this->getDisableOption().'>'.$this->value.'</textarea>';
$result .= $this->getCommonDescriptionForField();
}
else if ($this->isCheckboxOrRadio($type)) {
$result .= $this->addValueToSelectedItems($this->value);
$result .= '<div class="row">';
$result .= '<div class="col-md-12">';
$result .= $this->getCommonLabelForField();
$result .= $this->getCommonDescriptionForField();
$result .= '</div>';
$result .= '</div>';
$result .= '<div class="row">';
$result .= '<div class="col-md-12">';
$result .= $this->getCheckboxOrRadioOptions($type);
$result .= '</div>';
$result .= '</div>';
}
else if($this->isSelect($type)) {
$this->addValueToSelectedItems($this->value);
$multiSelectText = "";
$namePostfix = "";
$disabledText = "";
if($this->isMultiSelectable) {
$multiSelectText = 'MULTIPLE size="6"';
if($this->isCheckbox($type)) {
$namePostfix = "[]";
}
}
if($this->isDisabled) {
$disabledText = "disabled";
}
$result .= $this->getCommonLabelForField();
$result .= '<select name="'.$this->name.''.$namePostfix.'" class="form-control" '.$multiSelectText.' '.$disabledText.'>';
$result .= $this->getSelectOptions();
$result .= '</select>';
$result .= $this->getCommonDescriptionForField();
}
else if($this->isDatetimePicker($type)) {
$result .= $this->getCommonLabelForField();
}
return $result;
}
//add label
private function getCommonLabelForField() {
return '<label class="control-label" for="'.$this->name.'">'.$this->title.'</label>';
}
// add description below input field
private function getCommonDescriptionforField() {
if(strlen($this->description) > 0) {
return '<span id="'.$this->name.'-help" class="help-block">'.$this->description.'</span>';
}
}
// if value is set, and field requires selectedItems add value to selected items
private function addValueToSelectedItems($value){
if(strlen($value) > 0) {
$this->selectedItems[] = $value;
}
}
// add <option>'s to select
private function getSelectOptions() {
$result = "";
if($this->pleaseSelectEnabled) {
$result .= '<option value="null"> -- Please Select --</option>';
}
foreach($this->itemList as $key => $val) {
$selectedText = "";
if(in_array($key, $this->selectedItems)) {
$selectedText = 'selected="selected"';
}
$result .= '<option value="'.$key.'" '.$selectedText.'>'.$val.'</option>';
}
return $result;
}
//get items for checkbox or radio
private function getCheckboxOrRadioOptions($type) {
$result = "";
$namePostfix = "";
$disabledText = "";
if($this->isCheckbox($type)) {
$namePostfix = "[]";
}
if($this->isDisabled) {
$disabledText = "disabled";
}
foreach($this->itemList as $key => $val) {
$selectedText = "";
if(in_array($key, $this->selectedItems)) {
$selectedText = 'checked="checked"';
}
$result .= '<div class="'.$type. ' '.$disabledText.'">';
$result .= '<label>';
$result .= '<input type="'.$type.'" value="'.$key.'" name="'.$this->name.''.$namePostfix.'" '.$selectedText.' '.$disabledText.' />' . $val;
$result .= '</label>';
$result .= '</div>';
}
return $result;
}
private function isInput($type) {
$result = false;
if( !strcmp($type, InputFieldType::TEXT) ||
!strcmp($type, InputFieldType::PASSWORD) ||
!strcmp($type, InputFieldType::HIDDEN) ||
!strcmp($type, InputFieldType::FILE)
) {
$result = true;
}
return $result;
}
private function isCheckboxOrRadio($type) {
$result = false;
if( !strcmp($type, InputFieldType::RADIO) ||
!strcmp($type, InputFieldType::CHECKBOX)
) {
$result = true;
}
return $result;
}
private function isCheckbox($type) {
return (!strcmp($type, InputFieldType::CHECKBOX));
}
private function isTextarea($type) {
return (!strcmp($type, InputFieldType::TEXTAREA));
}
private function isSelect($type) {
return (!strcmp($type, InputFieldType::SELECT));
}
private function isDatetimePicker($type) {
return (!strcmp($type, InputFieldType::DATETIMEPICKER));
}
private function getDisableOption() {
return ($this->isDisabled ? ' disabled' : '');
}
}
// define input types
abstract class InputFieldType {
//HTML input constants
const TEXT = "text";
const PASSWORD = "password";
const HIDDEN = "hidden";
const FILE = "file";
const RADIO = "radio";
const CHECKBOX = "checkbox";
const TEXTAREA = "textarea";
const SELECT = "select";
const DATETIMEPICKER = "dateTimePicker";
}
?>