Passed
Push — 1.0.0-dev ( 066288...93958a )
by nguereza
09:45
created
core/libraries/Form.php 1 patch
Indentation   +294 added lines, -294 removed lines patch added patch discarded remove patch
@@ -1,322 +1,322 @@
 block discarded – undo
1 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 GNU GPL License (GPL)
9
-	 *
10
-	 * Copyright (C) 2017 Tony NGUEREZA
11
-	 *
12
-	 * This program is free software; you can redistribute it and/or
13
-	 * modify it under the terms of the GNU General Public License
14
-	 * as published by the Free Software Foundation; either version 3
15
-	 * of the License, or (at your option) any later version.
16
-	 *
17
-	 * This program is distributed in the hope that it will be useful,
18
-	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
-	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
-	 * GNU General Public License for more details.
21
-	 *
22
-	 * You should have received a copy of the GNU General Public License
23
-	 * along with this program; if not, write to the Free Software
24
-	 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
-	*/
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 GNU GPL License (GPL)
9
+     *
10
+     * Copyright (C) 2017 Tony NGUEREZA
11
+     *
12
+     * This program is free software; you can redistribute it and/or
13
+     * modify it under the terms of the GNU General Public License
14
+     * as published by the Free Software Foundation; either version 3
15
+     * of the License, or (at your option) any later version.
16
+     *
17
+     * This program is distributed in the hope that it will be useful,
18
+     * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+     * GNU General Public License for more details.
21
+     *
22
+     * You should have received a copy of the GNU General Public License
23
+     * along with this program; if not, write to the Free Software
24
+     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
+     */
26 26
 
27 27
 
28
-	class Form{
28
+    class Form{
29 29
 
30
-		/**
31
-		 * Generate the form opened tag
32
-		 * @param  string $path       the form action path
33
-		 * @param  array  $attributes the additional form attributes
34
-		 * @param  string $method     the form method like 'GET', 'POST'
35
-		 * @param  string $enctype    the form enctype like "multipart/form-data"
36
-		 * @return string             the generated form html
37
-		 */
38
-		public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){
39
-			if($path){
40
-				$path = Url::site_url($path);
41
-			}
42
-			$method = strtoupper($method);
43
-			$str = null;
44
-			$str .= '<form action = "'.$path.'" method = "'.$method.'"';
45
-			if(! empty($enctype)){
46
-				$str .= ' enctype = "'.$enctype.'" ';
47
-			}
48
-			if(! isset($attributes['accept-charset'])){
49
-				$attributes['accept-charset'] = get_config('charset', 'utf-8');
50
-			}
51
-			$str .= attributes_to_string($attributes);
52
-			$str .= '>';
53
-			//if CSRF is enabled in the configuration
54
-			if(get_config('csrf_enable', false) && $method == 'POST'){
55
-				$csrfValue = Security::generateCSRF();
56
-				$csrfName = get_config('csrf_key', 'csrf_key');
57
-				$str .= static::hidden($csrfName, $csrfValue);
58
-			}
59
-			return $str;
60
-		}
30
+        /**
31
+         * Generate the form opened tag
32
+         * @param  string $path       the form action path
33
+         * @param  array  $attributes the additional form attributes
34
+         * @param  string $method     the form method like 'GET', 'POST'
35
+         * @param  string $enctype    the form enctype like "multipart/form-data"
36
+         * @return string             the generated form html
37
+         */
38
+        public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){
39
+            if($path){
40
+                $path = Url::site_url($path);
41
+            }
42
+            $method = strtoupper($method);
43
+            $str = null;
44
+            $str .= '<form action = "'.$path.'" method = "'.$method.'"';
45
+            if(! empty($enctype)){
46
+                $str .= ' enctype = "'.$enctype.'" ';
47
+            }
48
+            if(! isset($attributes['accept-charset'])){
49
+                $attributes['accept-charset'] = get_config('charset', 'utf-8');
50
+            }
51
+            $str .= attributes_to_string($attributes);
52
+            $str .= '>';
53
+            //if CSRF is enabled in the configuration
54
+            if(get_config('csrf_enable', false) && $method == 'POST'){
55
+                $csrfValue = Security::generateCSRF();
56
+                $csrfName = get_config('csrf_key', 'csrf_key');
57
+                $str .= static::hidden($csrfName, $csrfValue);
58
+            }
59
+            return $str;
60
+        }
61 61
 
62
-		/**
63
-		 * Generate the form opened tag for multipart like to send a file
64
-		 * @see Form::open() for more details
65
-		 * @return string the generated multipart form html
66
-		 */
67
-		public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){
68
-			return self::open($path, $attributes, $method, 'multipart/form-data');
69
-		}
62
+        /**
63
+         * Generate the form opened tag for multipart like to send a file
64
+         * @see Form::open() for more details
65
+         * @return string the generated multipart form html
66
+         */
67
+        public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){
68
+            return self::open($path, $attributes, $method, 'multipart/form-data');
69
+        }
70 70
 
71
-		/**
72
-		 * Generate the form close
73
-		 * @return string the form close html
74
-		 */
75
-		public static function close(){
76
-			return '</form>';
77
-		}
71
+        /**
72
+         * Generate the form close
73
+         * @return string the form close html
74
+         */
75
+        public static function close(){
76
+            return '</form>';
77
+        }
78 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
-		}
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 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
-		}
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 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->returnErrors();
113
-				if(isset($errors[$name])){
114
-					list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter();
115
-					$return = $errorStart . $errors[$name] . $errorEnd;
116
-				}
117
-			}
118
-			return $return;
119
-		}
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->returnErrors();
113
+                if(isset($errors[$name])){
114
+                    list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter();
115
+                    $return = $errorStart . $errors[$name] . $errorEnd;
116
+                }
117
+            }
118
+            return $return;
119
+        }
120 120
 
121
-		/**
122
-		 * Get the form field value
123
-		 * @param  string $name    the form field name
124
-		 * @param  mixed $default the default value if can not found the given form field name
125
-		 * @return mixed the form field value if is set, otherwise return the default value.
126
-		 */
127
-		public static function value($name, $default = null){
128
-			$value = get_instance()->request->query($name);
129
-			if(strlen($value) > 0){
130
-				return $value;
131
-			}
132
-			return $default;
133
-		}
121
+        /**
122
+         * Get the form field value
123
+         * @param  string $name    the form field name
124
+         * @param  mixed $default the default value if can not found the given form field name
125
+         * @return mixed the form field value if is set, otherwise return the default value.
126
+         */
127
+        public static function value($name, $default = null){
128
+            $value = get_instance()->request->query($name);
129
+            if(strlen($value) > 0){
130
+                return $value;
131
+            }
132
+            return $default;
133
+        }
134 134
 
135
-		/**
136
-		 * Generate the form label html content
137
-		 * @param  string $label      the title of the label
138
-		 * @param  string $for        the value of the label "for" attribute
139
-		 * @param  array  $attributes the additional attributes to be added
140
-		 * @return string the generated label html content
141
-		 */
142
-		public static function label($label, $for = '', array $attributes = array()){
143
-			$str = '<label';
144
-			if($for){
145
-				$str .= ' for = "'.$for.'"';
146
-			}
147
-			$str .= attributes_to_string($attributes);
148
-			$str .= '>';
149
-			$str .= $label.'</label>';
150
-			return $str;
151
-		}
135
+        /**
136
+         * Generate the form label html content
137
+         * @param  string $label      the title of the label
138
+         * @param  string $for        the value of the label "for" attribute
139
+         * @param  array  $attributes the additional attributes to be added
140
+         * @return string the generated label html content
141
+         */
142
+        public static function label($label, $for = '', array $attributes = array()){
143
+            $str = '<label';
144
+            if($for){
145
+                $str .= ' for = "'.$for.'"';
146
+            }
147
+            $str .= attributes_to_string($attributes);
148
+            $str .= '>';
149
+            $str .= $label.'</label>';
150
+            return $str;
151
+        }
152 152
 
153
-		/**
154
-		 * Generate the form field for input like "text", "email", "password", etc.
155
-		 * @param  string $name       the form field name
156
-		 * @param  mixed $value      the form field value to be set
157
-		 * @param  array  $attributes the additional attributes to be added in the form input
158
-		 * @param  string $type       the type of the form field (password, text, submit, button, etc.)
159
-		 * @return string             the generated form field html content for the input
160
-		 */
161
-		public static function input($name, $value = null, array $attributes = array(), $type = 'text'){
162
-			$str = null;
163
-			$str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"';
164
-			$str .= attributes_to_string($attributes);
165
-			$str .= '/>';
166
-			return $str;
167
-		}
153
+        /**
154
+         * Generate the form field for input like "text", "email", "password", etc.
155
+         * @param  string $name       the form field name
156
+         * @param  mixed $value      the form field value to be set
157
+         * @param  array  $attributes the additional attributes to be added in the form input
158
+         * @param  string $type       the type of the form field (password, text, submit, button, etc.)
159
+         * @return string             the generated form field html content for the input
160
+         */
161
+        public static function input($name, $value = null, array $attributes = array(), $type = 'text'){
162
+            $str = null;
163
+            $str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"';
164
+            $str .= attributes_to_string($attributes);
165
+            $str .= '/>';
166
+            return $str;
167
+        }
168 168
 		
169
-		/**
170
-		 * Generate the form field for "text"
171
-		 * @see Form::input() for more details
172
-		 */
173
-		public static function text($name, $value = null, array $attributes = array()){
174
-			return self::input($name, $value, $attributes, 'text');
175
-		}
169
+        /**
170
+         * Generate the form field for "text"
171
+         * @see Form::input() for more details
172
+         */
173
+        public static function text($name, $value = null, array $attributes = array()){
174
+            return self::input($name, $value, $attributes, 'text');
175
+        }
176 176
 
177
-		/**
178
-		 * Generate the form field for "password"
179
-		 * @see Form::input() for more details
180
-		 */
181
-		public static function password($name, $value = null, array $attributes = array()){
182
-			return self::input($name, $value, $attributes, 'password');
183
-		}
177
+        /**
178
+         * Generate the form field for "password"
179
+         * @see Form::input() for more details
180
+         */
181
+        public static function password($name, $value = null, array $attributes = array()){
182
+            return self::input($name, $value, $attributes, 'password');
183
+        }
184 184
 
185
-		/**
186
-		 * Generate the form field for "radio"
187
-		 * @see Form::input() for more details
188
-		 */
189
-		public static function radio($name, $value = null,  $checked = false, array $attributes = array()){
190
-			if($checked){
191
-				$attributes['checked'] = true;
192
-			}
193
-			return self::input($name, $value, $attributes, 'radio');
194
-		}
185
+        /**
186
+         * Generate the form field for "radio"
187
+         * @see Form::input() for more details
188
+         */
189
+        public static function radio($name, $value = null,  $checked = false, array $attributes = array()){
190
+            if($checked){
191
+                $attributes['checked'] = true;
192
+            }
193
+            return self::input($name, $value, $attributes, 'radio');
194
+        }
195 195
 
196
-		/**
197
-		 * Generate the form field for "checkbox"
198
-		 * @see Form::input() for more details
199
-		 */
200
-		public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){
201
-			if($checked){
202
-				$attributes['checked'] = true;
203
-			}
204
-			return self::input($name, $value, $attributes, 'checkbox');
205
-		}
196
+        /**
197
+         * Generate the form field for "checkbox"
198
+         * @see Form::input() for more details
199
+         */
200
+        public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){
201
+            if($checked){
202
+                $attributes['checked'] = true;
203
+            }
204
+            return self::input($name, $value, $attributes, 'checkbox');
205
+        }
206 206
 
207
-		/**
208
-		 * Generate the form field for "number"
209
-		 * @see Form::input() for more details
210
-		 */
211
-		public static function number($name, $value = null, array $attributes = array()){
212
-			return self::input($name, $value, $attributes, 'number');
213
-		}
207
+        /**
208
+         * Generate the form field for "number"
209
+         * @see Form::input() for more details
210
+         */
211
+        public static function number($name, $value = null, array $attributes = array()){
212
+            return self::input($name, $value, $attributes, 'number');
213
+        }
214 214
 
215
-		/**
216
-		 * Generate the form field for "phone"
217
-		 * @see Form::input() for more details
218
-		 */
219
-		public static function phone($name, $value = null, array $attributes = array()){
220
-			return self::input($name, $value, $attributes, 'phone');
221
-		}
215
+        /**
216
+         * Generate the form field for "phone"
217
+         * @see Form::input() for more details
218
+         */
219
+        public static function phone($name, $value = null, array $attributes = array()){
220
+            return self::input($name, $value, $attributes, 'phone');
221
+        }
222 222
 
223
-		/**
224
-		 * Generate the form field for "email"
225
-		 * @see Form::input() for more details
226
-		 */
227
-		public static function email($name, $value = null, array $attributes = array()){
228
-			return self::input($name, $value, $attributes, 'email');
229
-		}
223
+        /**
224
+         * Generate the form field for "email"
225
+         * @see Form::input() for more details
226
+         */
227
+        public static function email($name, $value = null, array $attributes = array()){
228
+            return self::input($name, $value, $attributes, 'email');
229
+        }
230 230
 		
231
-		/**
232
-		 * Generate the form field for "search"
233
-		 * @see Form::input() for more details
234
-		 */
235
-		public static function search($name, $value = null, array $attributes = array()){
236
-			return self::input($name, $value, $attributes, 'search');
237
-		}
231
+        /**
232
+         * Generate the form field for "search"
233
+         * @see Form::input() for more details
234
+         */
235
+        public static function search($name, $value = null, array $attributes = array()){
236
+            return self::input($name, $value, $attributes, 'search');
237
+        }
238 238
 		
239
-		/**
240
-		 * Generate the form field for "hidden"
241
-		 * @see Form::input() for more details
242
-		 */
243
-		public static function hidden($name, $value = null, array $attributes = array()){
244
-			return self::input($name, $value, $attributes, 'hidden');
245
-		}
239
+        /**
240
+         * Generate the form field for "hidden"
241
+         * @see Form::input() for more details
242
+         */
243
+        public static function hidden($name, $value = null, array $attributes = array()){
244
+            return self::input($name, $value, $attributes, 'hidden');
245
+        }
246 246
 		
247
-		/**
248
-		 * Generate the form field for "file"
249
-		 * @see Form::input() for more details
250
-		 */
251
-		public static function file($name, array $attributes = array()){
252
-			return self::input($name, null, $attributes, 'file');
253
-		}
247
+        /**
248
+         * Generate the form field for "file"
249
+         * @see Form::input() for more details
250
+         */
251
+        public static function file($name, array $attributes = array()){
252
+            return self::input($name, null, $attributes, 'file');
253
+        }
254 254
 		
255
-		/**
256
-		 * Generate the form field for "button"
257
-		 * @see Form::input() for more details
258
-		 */
259
-		public static function button($name, $value = null, array $attributes = array()){
260
-			return self::input($name, $value, $attributes, 'button');
261
-		}
255
+        /**
256
+         * Generate the form field for "button"
257
+         * @see Form::input() for more details
258
+         */
259
+        public static function button($name, $value = null, array $attributes = array()){
260
+            return self::input($name, $value, $attributes, 'button');
261
+        }
262 262
 		
263
-		/**
264
-		 * Generate the form field for "reset"
265
-		 * @see Form::input() for more details
266
-		 */
267
-		public static function reset($name, $value = null, array $attributes = array()){
268
-			return self::input($name, $value, $attributes, 'reset');
269
-		}
263
+        /**
264
+         * Generate the form field for "reset"
265
+         * @see Form::input() for more details
266
+         */
267
+        public static function reset($name, $value = null, array $attributes = array()){
268
+            return self::input($name, $value, $attributes, 'reset');
269
+        }
270 270
 		
271
-		/**
272
-		 * Generate the form field for "submit"
273
-		 * @see Form::input() for more details
274
-		 */
275
-		public static function submit($name, $value = null, array $attributes = array()){
276
-			return self::input($name, $value, $attributes, 'submit');
277
-		}
271
+        /**
272
+         * Generate the form field for "submit"
273
+         * @see Form::input() for more details
274
+         */
275
+        public static function submit($name, $value = null, array $attributes = array()){
276
+            return self::input($name, $value, $attributes, 'submit');
277
+        }
278 278
 
279
-		/**
280
-		 * Generate the form field for textarea
281
-		 * @param  string $name       the name of the textarea field
282
-		 * @param  string $value      the textarea field value
283
-		 * @param  array  $attributes the additional attributes to be added
284
-		 * @return string             the generated textarea form html content
285
-		 */
286
-		public static function textarea($name, $value = '', array $attributes = array()){
287
-			$str = null;
288
-			$str .= '<textarea name = "'.$name.'"';
289
-			$str .= attributes_to_string($attributes);
290
-			$str .= '>';
291
-			$str .= $value.'</textarea>';
292
-			return $str;
293
-		}
279
+        /**
280
+         * Generate the form field for textarea
281
+         * @param  string $name       the name of the textarea field
282
+         * @param  string $value      the textarea field value
283
+         * @param  array  $attributes the additional attributes to be added
284
+         * @return string             the generated textarea form html content
285
+         */
286
+        public static function textarea($name, $value = '', array $attributes = array()){
287
+            $str = null;
288
+            $str .= '<textarea name = "'.$name.'"';
289
+            $str .= attributes_to_string($attributes);
290
+            $str .= '>';
291
+            $str .= $value.'</textarea>';
292
+            return $str;
293
+        }
294 294
 		
295
-		/**
296
-		 * Generate the form field for select
297
-		 * @param  string $name       the name of the form field
298
-		 * @param  mixed|array $values      the values used to populate the "option" tags
299
-		 * @param  mixed $selected   the selected value in the option list
300
-		 * @param  array  $attributes the additional attribute to be added
301
-		 * @return string             the generated form field html content for select
302
-		 */
303
-		public static function select($name, $values = null, $selected = null, array $attributes = array()){
304
-			if(! is_array($values)){
305
-				$values = array('' => $values);
306
-			}
307
-			$str = null;
308
-			$str .= '<select name = "'.$name.'"';
309
-			$str .= attributes_to_string($attributes);
310
-			$str .= '>';
311
-			foreach($values as $key => $val){
312
-				$select = '';
313
-				if($key == $selected){
314
-					$select = 'selected';
315
-				}
316
-				$str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>';
317
-			}
318
-			$str .= '</select>';
319
-			return $str;
320
-		}
295
+        /**
296
+         * Generate the form field for select
297
+         * @param  string $name       the name of the form field
298
+         * @param  mixed|array $values      the values used to populate the "option" tags
299
+         * @param  mixed $selected   the selected value in the option list
300
+         * @param  array  $attributes the additional attribute to be added
301
+         * @return string             the generated form field html content for select
302
+         */
303
+        public static function select($name, $values = null, $selected = null, array $attributes = array()){
304
+            if(! is_array($values)){
305
+                $values = array('' => $values);
306
+            }
307
+            $str = null;
308
+            $str .= '<select name = "'.$name.'"';
309
+            $str .= attributes_to_string($attributes);
310
+            $str .= '>';
311
+            foreach($values as $key => $val){
312
+                $select = '';
313
+                if($key == $selected){
314
+                    $select = 'selected';
315
+                }
316
+                $str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>';
317
+            }
318
+            $str .= '</select>';
319
+            return $str;
320
+        }
321 321
 
322
-	}
322
+    }
Please login to merge, or discard this patch.