1
|
|
|
<?php |
2
|
|
|
defined('ROOT_PATH') || exit('Access denied'); |
3
|
|
|
/** |
4
|
|
|
* TNH Framework |
5
|
|
|
* |
6
|
|
|
* A simple PHP framework using HMVC architecture |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2017 TNH Framework |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
class Form { |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Generate the form opened tag |
35
|
|
|
* @param string $path the form action path |
36
|
|
|
* @param array $attributes the additional form attributes |
37
|
|
|
* @param string $method the form method like 'GET', 'POST' |
38
|
|
|
* @return string the generated form html |
39
|
|
|
*/ |
40
|
|
|
public static function open($path = null, array $attributes = array(), $method = 'POST') { |
41
|
|
|
if ($path) { |
42
|
|
|
$path = get_instance()->url->appUrl($path); |
43
|
|
|
} |
44
|
|
|
$method = strtoupper($method); |
45
|
|
|
$str = null; |
46
|
|
|
$str .= '<form action = "' . $path . '" method = "' . $method . '"'; |
47
|
|
|
if (!isset($attributes['accept-charset'])) { |
48
|
|
|
$attributes['accept-charset'] = get_config('charset', 'UTF-8'); |
49
|
|
|
} |
50
|
|
|
$str .= attributes_to_string($attributes); |
51
|
|
|
$str .= '>'; |
52
|
|
|
//if CSRF is enabled in the configuration |
53
|
|
|
if (get_config('csrf_enable', false) && $method == 'POST') { |
54
|
|
|
$csrfValue = get_instance()->security->generateCSRF(); |
55
|
|
|
$csrfName = get_config('csrf_key', 'csrf_key'); |
56
|
|
|
$str .= self::hidden($csrfName, $csrfValue); |
57
|
|
|
} |
58
|
|
|
return $str; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Generate the form opened tag for multipart like to send a file |
63
|
|
|
* @see Form::open() for more details |
64
|
|
|
* @return string the generated multipart form html |
65
|
|
|
*/ |
66
|
|
|
public static function openMultipart($path = null, array $attributes = array(), $method = 'POST') { |
67
|
|
|
$attributes['enctype'] = 'multipart/form-data'; |
68
|
|
|
return self::open($path, $attributes, $method); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Generate the form close |
73
|
|
|
* @return string the form close html |
74
|
|
|
*/ |
75
|
|
|
public static function close() { |
76
|
|
|
return '</form>'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Generate the form fieldset & legend |
81
|
|
|
* @param string $legend the legend tag value |
82
|
|
|
* @param array $fieldsetAttributes the fieldset additional HTML attributes |
83
|
|
|
* @param array $legendAttributes the legend additional HTML attributes. Is used only is $legend is not empty |
84
|
|
|
* @return string the generated fieldset value |
85
|
|
|
*/ |
86
|
|
|
public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()) { |
87
|
|
|
$str = '<fieldset' . attributes_to_string($fieldsetAttributes) . '>'; |
88
|
|
|
if ($legend) { |
89
|
|
|
$str .= '<legend' . attributes_to_string($legendAttributes) . '>' . $legend . '</legend>'; |
90
|
|
|
} |
91
|
|
|
return $str; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Generate the fieldset close tag |
96
|
|
|
* @return string the generated html for fieldset close |
97
|
|
|
*/ |
98
|
|
|
public static function fieldsetClose() { |
99
|
|
|
return '</fieldset>'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the error message for the given form field name. |
104
|
|
|
* This use the form validation information to get the error information. |
105
|
|
|
* @param string $name the form field name |
106
|
|
|
* @return string the error message if exists and null if not |
107
|
|
|
*/ |
108
|
|
|
public static function error($name) { |
109
|
|
|
$return = null; |
110
|
|
|
$obj = & get_instance(); |
111
|
|
|
if (isset($obj->formvalidation)) { |
112
|
|
|
$errors = $obj->formvalidation->getErrors(); |
113
|
|
|
if (isset($errors[$name])) { |
114
|
|
|
$return = $errors[$name]; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
return $return; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the form field value |
122
|
|
|
* @param string $name the form field name |
123
|
|
|
* @param mixed $default the default value if can not found the given form field name |
124
|
|
|
* @return mixed the form field value if is set, otherwise return the default value. |
125
|
|
|
*/ |
126
|
|
|
public static function value($name, $default = null) { |
127
|
|
|
$value = get_instance()->request->query($name); |
128
|
|
|
if ((is_array($value) && count($value) > 0) || strlen($value) > 0) { |
129
|
|
|
return $value; |
130
|
|
|
} |
131
|
|
|
return $default; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Generate the form label html content |
136
|
|
|
* @param string $label the title of the label |
137
|
|
|
* @param string $for the value of the label "for" attribute |
138
|
|
|
* @param array $attributes the additional attributes to be added |
139
|
|
|
* @return string the generated label html content |
140
|
|
|
*/ |
141
|
|
|
public static function label($label, $for = '', array $attributes = array()) { |
142
|
|
|
$str = '<label'; |
143
|
|
|
if ($for) { |
144
|
|
|
$attributes['for'] = $for; |
145
|
|
|
} |
146
|
|
|
$str .= attributes_to_string($attributes); |
147
|
|
|
$str .= '>'; |
148
|
|
|
$str .= $label . '</label>'; |
149
|
|
|
return $str; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Generate the form field for input like "text", "email", "password", etc. |
154
|
|
|
* @param string $name the form field name |
155
|
|
|
* @param mixed $value the form field value to be set |
156
|
|
|
* @param array $attributes the additional attributes to be added in the form input |
157
|
|
|
* @param string $type the type of the form field (password, text, submit, button, etc.) |
158
|
|
|
* @return string the generated form field html content for the input |
159
|
|
|
*/ |
160
|
|
|
public static function input($name, $value = null, array $attributes = array(), $type = 'text') { |
161
|
|
|
$str = null; |
162
|
|
|
$str .= '<input'; |
163
|
|
|
$attributes['type'] = $type; |
164
|
|
|
$attributes['name'] = $name; |
165
|
|
|
$attributes['value'] = $value; |
166
|
|
|
$str .= attributes_to_string($attributes); |
167
|
|
|
$str .= '/>'; |
168
|
|
|
return $str; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Generate the form field for "text" |
173
|
|
|
* @see Form::input() for more details |
174
|
|
|
*/ |
175
|
|
|
public static function text($name, $value = null, array $attributes = array()) { |
176
|
|
|
return self::input($name, $value, $attributes, 'text'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Generate the form field for "password" |
181
|
|
|
* @see Form::input() for more details |
182
|
|
|
*/ |
183
|
|
|
public static function password($name, $value = null, array $attributes = array()) { |
184
|
|
|
return self::input($name, $value, $attributes, 'password'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Generate the form field for "radio" |
189
|
|
|
* @see Form::input() for more details |
190
|
|
|
*/ |
191
|
|
|
public static function radio($name, $value = null, $checked = false, array $attributes = array()) { |
192
|
|
|
if ($checked) { |
193
|
|
|
$attributes['checked'] = true; |
194
|
|
|
} |
195
|
|
|
return self::input($name, $value, $attributes, 'radio'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Generate the form field for "checkbox" |
200
|
|
|
* @see Form::input() for more details |
201
|
|
|
*/ |
202
|
|
|
public static function checkbox($name, $value = null, $checked = false, array $attributes = array()) { |
203
|
|
|
if ($checked) { |
204
|
|
|
$attributes['checked'] = true; |
205
|
|
|
} |
206
|
|
|
return self::input($name, $value, $attributes, 'checkbox'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Generate the form field for "number" |
211
|
|
|
* @see Form::input() for more details |
212
|
|
|
*/ |
213
|
|
|
public static function number($name, $value = null, array $attributes = array()) { |
214
|
|
|
return self::input($name, $value, $attributes, 'number'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Generate the form field for "phone" |
219
|
|
|
* @see Form::input() for more details |
220
|
|
|
*/ |
221
|
|
|
public static function phone($name, $value = null, array $attributes = array()) { |
222
|
|
|
return self::input($name, $value, $attributes, 'phone'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Generate the form field for "email" |
227
|
|
|
* @see Form::input() for more details |
228
|
|
|
*/ |
229
|
|
|
public static function email($name, $value = null, array $attributes = array()) { |
230
|
|
|
return self::input($name, $value, $attributes, 'email'); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Generate the form field for "search" |
235
|
|
|
* @see Form::input() for more details |
236
|
|
|
*/ |
237
|
|
|
public static function search($name, $value = null, array $attributes = array()) { |
238
|
|
|
return self::input($name, $value, $attributes, 'search'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Generate the form field for "hidden" |
243
|
|
|
* @see Form::input() for more details |
244
|
|
|
*/ |
245
|
|
|
public static function hidden($name, $value = null, array $attributes = array()) { |
246
|
|
|
return self::input($name, $value, $attributes, 'hidden'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Generate the form field for "file" |
251
|
|
|
* @see Form::input() for more details |
252
|
|
|
*/ |
253
|
|
|
public static function file($name, array $attributes = array()) { |
254
|
|
|
return self::input($name, null, $attributes, 'file'); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Generate the form field for "button" |
259
|
|
|
* @see Form::input() for more details |
260
|
|
|
*/ |
261
|
|
|
public static function button($name, $value = null, array $attributes = array()) { |
262
|
|
|
return self::input($name, $value, $attributes, 'button'); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Generate the form field for "reset" |
267
|
|
|
* @see Form::input() for more details |
268
|
|
|
*/ |
269
|
|
|
public static function reset($name, $value = null, array $attributes = array()) { |
270
|
|
|
return self::input($name, $value, $attributes, 'reset'); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Generate the form field for "submit" |
275
|
|
|
* @see Form::input() for more details |
276
|
|
|
*/ |
277
|
|
|
public static function submit($name, $value = null, array $attributes = array()) { |
278
|
|
|
return self::input($name, $value, $attributes, 'submit'); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Generate the form field for textarea |
283
|
|
|
* @param string $name the name of the textarea field |
284
|
|
|
* @param string $value the textarea field value |
285
|
|
|
* @param array $attributes the additional attributes to be added |
286
|
|
|
* @return string the generated textarea form html content |
287
|
|
|
*/ |
288
|
|
|
public static function textarea($name, $value = '', array $attributes = array()) { |
289
|
|
|
$str = null; |
290
|
|
|
$str .= '<textarea name = "' . $name . '"'; |
291
|
|
|
$str .= attributes_to_string($attributes); |
292
|
|
|
$str .= '>'; |
293
|
|
|
$str .= $value . '</textarea>'; |
294
|
|
|
return $str; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Generate the form field for select |
299
|
|
|
* @param string $name the name of the form field |
300
|
|
|
* @param array $values the values used to populate the "option" tags |
301
|
|
|
* @param mixed $selected the selected value in the option list |
302
|
|
|
* @param array $attributes the additional attribute to be added |
303
|
|
|
* @return string the generated form field html content for select |
304
|
|
|
*/ |
305
|
|
|
public static function select($name, array $values = array(), $selected = null, array $attributes = array()) { |
306
|
|
|
$str = null; |
307
|
|
|
$str .= '<select name = "' . $name . '"'; |
308
|
|
|
$str .= attributes_to_string($attributes); |
309
|
|
|
$str .= '>'; |
310
|
|
|
foreach ($values as $key => $val) { |
311
|
|
|
$select = ''; |
312
|
|
|
if ((is_array($selected) && in_array($key, $selected)) || $key == $selected) { |
313
|
|
|
$select = ' selected'; |
314
|
|
|
} |
315
|
|
|
$str .= '<option value = "' . $key . '"' . $select . '>' . $val . '</option>'; |
316
|
|
|
} |
317
|
|
|
$str .= '</select>'; |
318
|
|
|
return $str; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
} |
322
|
|
|
|