-
Notifications
You must be signed in to change notification settings - Fork 7
/
form.class.php
224 lines (211 loc) · 7.81 KB
/
form.class.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
<?php
/**
* JSON Form Generator
* HTML Form Generator from JSON format
*
* @author Arash Soleimani <[email protected]>
* @package json_form_generator
* @version 1.0
*
*/
class Form {
public $json;
public $method = "post";
public $action = "./";
public $html = "";
/**
* Class Constructor
* @param array $param ['file'=>YourFilePath, 'json'=>'json string']
*
* @example
* <code>
* $form = new Form(['file'=>'./example_form.json']);
* </code>
*/
function __construct($params){
if(isset($params['json'])){
$this->json = json_decode($params['json'], true);
}else{
$this->load($params['file']);
}
}
/**
* Parse field array (Specifies the generator function based on the type)
*
* @param array $data
*
* @return string
*/
private function parse($data){
$inputTypes = ['checkbox','color','date','datetime-local','email','file','hidden','image','month','number','password','radio','range','reset','search','submit','tel','text','time','url','week'];
if(in_array($data['type'], $inputTypes)){
return $this->input($data);
}else{
return $this->{$data['type']}($data);
}
}
/**
* Load json file
*
* @param string $filePath
*
* @return array
*/
public function load($filePath){
$data = json_decode(file_get_contents($filePath), true);
if($data){
$this->json = $data;
}else{
$this->errorMessage = "File not found";
}
}
/**
* Generate Input tag
*
* @param array $data {
* name, title, type, id, value, autocomplete, min, max, minlength, pattern,
* onclick, onchange, checked, required, disabled, readonly, autofocus,
* multiple, dir, class, step, size
* }
*
* @return string
*/
public function input($data){
$tag = '<label for="'.$data['name'].'">'.$data['title']."</label>";
$tag .= '<input name="'.$data['name'].'" ';
if(isset($data['type'])) $tag .= ' type="'.$data['type'].'"';
if(isset($data['id'])) $tag .= ' id="'.$data['id'].'"';
if(isset($data['value'])) $tag .= ' value="'.$data['value'].'"';
if(isset($data['autocomplete'])) $tag .= ' autocomplete="'.$data['autocomplete'].'"';
if(isset($data['min'])) $tag .= ' min="'.$data['min'].'"';
if(isset($data['max'])) $tag .= ' max="'.$data['max'].'"';
if(isset($data['minlength'])) $tag .= ' minlength="'.$data['minlength'].'"';
if(isset($data['placeholder'])) $tag .= ' placeholder="'.$data['placeholder'].'"';
if(isset($data['pattern'])) $tag .= ' pattern="'.$data['pattern'].'"';
if(isset($data['size'])) $tag .= ' size="'.$data['size'].'"';
if(isset($data['step'])) $tag .= ' step="'.$data['step'].'"';
if(isset($data['onClick'])) $tag .= ' onclick="'.$data['onClick'].'"';
if(isset($data['onChange'])) $tag .= ' onchange="'.$data['onChange'].'"';
if(isset($data['required'])) $tag .= ' required';
if(isset($data['checked'])) $tag .= ' checked';
if(isset($data['disabled'])) $tag .= ' disabled';
if(isset($data['readonly'])) $tag .= ' readonly';
if(isset($data['autofocus'])) $tag .= ' autofocus';
if(isset($data['multiple'])) $tag .= ' multiple';
if(isset($data['dir'])) $tag .= ' dir="'.$data['dir'].'"';
if(isset($data['class'])) $tag .= ' class="'.$data['class'].'"';
$tag .= " />";
return $tag;
}
/**
* Generate Select tag
*
* @param array $data {
* name, title, class, id, required, disabled, readonly, autofocus,
* onclick, onchange,
* options =>{
* 'key1'=>'value1',
* 'key2'=>'value2'
* }
* }
*
* @return string
*/
public function select($data){
$tag = '<label for="'.$data['name'].'">'.$data['title']."</label>";
$tag .= '<select name="'.$data['name'].'" ';
if(isset($data['class'])) $tag .= ' class="'.$data['class'].'"';
if(isset($data['id'])) $tag .= ' id="'.$data['id'].'"';
if(isset($data['onClick'])) $tag .= ' onclick="'.$data['onClick'].'"';
if(isset($data['onChange'])) $tag .= ' onchange="'.$data['onChange'].'"';
if(isset($data['required'])) $tag .= ' required';
if(isset($data['disabled'])) $tag .= ' disabled';
if(isset($data['readonly'])) $tag .= ' readonly';
if(isset($data['autofocus'])) $tag .= ' autofocus';
$tag .= '>';
if(isset($data['options'])){
foreach($data['options'] as $key=>$value){
$tag .= '<option value="'.$key.'">'.$value.'</option>';
}
}
$tag .= '</select>';
return $tag;
}
/**
* Generate Textarea tag
*
* @param array $data {
* name, title, onclick, onchange, required, disabled,
* readonly, autofocus, placeholder, value
* }
*
* @return string
*/
public function textarea($data){
$tag = '<label for="'.$data['name'].'">'.$data['title']."</label>";
$tag .= '<textarea name="'.$data['name'].'" ';
if(isset($data['onClick'])) $tag .= ' onclick="'.$data['onClick'].'"';
if(isset($data['onChange'])) $tag .= ' onchange="'.$data['onChange'].'"';
if(isset($data['required'])) $tag .= ' required';
if(isset($data['disabled'])) $tag .= ' disabled';
if(isset($data['readonly'])) $tag .= ' readonly';
if(isset($data['autofocus'])) $tag .= ' autofocus';
if(isset($data['placeholder'])) $tag .= ' placeholder="'.$data['placeholder'].'"';
$tag .= " >";
if(isset($data['value'])) $tag .= $data['value'];
$tag .= "</textarea>";
return $tag;
}
/**
* Generate Button tag
*
* @param array $data {
* name, class, id, disabled, autofocus, value
* }
*
* @return string
*/
public function button($data){
$tag = '<button name="'.$data['name'].'" ';
if(isset($data['class'])) $tag .= ' class="'.$data['class'].'"';
if(isset($data['id'])) $tag .= ' id="'.$data['id'].'"';
if(isset($data['onClick'])) $tag .= ' onclick="'.$data['onClick'].'"';
if(isset($data['disabled'])) $tag .= ' disabled';
if(isset($data['autofocus'])) $tag .= ' autofocus';
$tag .= ">".$data['title'];
$tag .= '</button>';
return $tag;
}
/**
* Render HTML tags from JSON
*
* @return string
*/
public function render(){
$data = $this->json;
$this->html = '<form name="'.$data['name'].'" method="'.$data['method'].'" action="'.$data['action'].'"';
if(isset($data['enctype'])) $this->html .= ' enctype="'.$data['enctype'].'"';
if(isset($data['target'])) $this->html .= ' target="'.$data['target'].'"';
if(isset($data['autocomplete'])) $this->html .= ' autocomplete="'.$data['autocomplete'].'"';
$this->html .= '>';
foreach($data['properties'] as $value){
$this->html .= $this->parse($value);
}
$this->html .= "</form>";
return $this->html;
}
/**
* Print generated form
*
*/
public function show(){
echo $this->render();
}
/**
* Show errors
*/
public function error(){
echo $this->errorMessage."\r\n";
}
}
?>