Passed
Push — 1.0.0-dev ( 407604...83bedf )
by nguereza
03:26
created

Form::select()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
nc 6
nop 4
dl 0
loc 17
rs 9.8333
c 2
b 0
f 0
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
	*/
26
27
28
	class Form{
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
		}
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
		}
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->returnErrors();
113
				$error =  isset($errors[$name]) ? $errors[$name] : null;
114
				if($error){
115
					list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter();
116
					$return = $errorStart . $error . $errorEnd;
117
				}
118
			}
119
			return $return;
120
		}
121
122
		/**
123
		 * Get the form field value
124
		 * @param  string $name    the form field name
125
		 * @param  mixed $default the default value if can not found the given form field name
126
		 * @return mixed the form field value if is set, otherwise return the default value.
127
		 */
128
		public static function value($name, $default = null){
129
			$value = get_instance()->request->query($name);
130
			return $value ? $value : $default;
131
		}
132
133
		/**
134
		 * Generate the form label html content
135
		 * @param  string $label      the title of the label
136
		 * @param  string $for        the value of the label "for" attribute
137
		 * @param  array  $attributes the additional attributes to be added
138
		 * @return string the generated label html content
139
		 */
140
		public static function label($label, $for = '', array $attributes = array()){
141
			$str = '<label';
142
			if($for){
143
				$str .= ' for = "'.$for.'"';
144
			}
145
			$str .= attributes_to_string($attributes);
146
			$str .= '>';
147
			$str .= $label.'</label>';
148
			return $str;
149
		}
150
151
		/**
152
		 * Generate the form field for input like "text", "email", "password", etc.
153
		 * @param  string $name       the form field name
154
		 * @param  mixed $value      the form field value to be set
155
		 * @param  array  $attributes the additional attributes to be added in the form input
156
		 * @param  string $type       the type of the form field (password, text, submit, button, etc.)
157
		 * @return string             the generated form field html content for the input
158
		 */
159
		public static function input($name, $value = null, array $attributes = array(), $type = 'text'){
160
			$str = null;
161
			$str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"';
162
			$str .= attributes_to_string($attributes);
163
			$str .= '/>';
164
			return $str;
165
		}
166
		
167
		/**
168
		 * Generate the form field for "text"
169
		 * @see Form::input() for more details
170
		 */
171
		public static function text($name, $value = null, array $attributes = array()){
172
			return self::input($name, $value, $attributes, 'text');
173
		}
174
175
		/**
176
		 * Generate the form field for "password"
177
		 * @see Form::input() for more details
178
		 */
179
		public static function password($name, $value = null, array $attributes = array()){
180
			return self::input($name, $value, $attributes, 'password');
181
		}
182
183
		/**
184
		 * Generate the form field for "radio"
185
		 * @see Form::input() for more details
186
		 */
187
		public static function radio($name, $value = null,  $checked = false, array $attributes = array()){
188
			if($checked){
189
				$attributes['checked'] = true;
190
			}
191
			return self::input($name, $value, $attributes, 'radio');
192
		}
193
194
		/**
195
		 * Generate the form field for "checkbox"
196
		 * @see Form::input() for more details
197
		 */
198
		public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){
199
			if($checked){
200
				$attributes['checked'] = true;
201
			}
202
			return self::input($name, $value, $attributes, 'checkbox');
203
		}
204
205
		/**
206
		 * Generate the form field for "number"
207
		 * @see Form::input() for more details
208
		 */
209
		public static function number($name, $value = null, array $attributes = array()){
210
			return self::input($name, $value, $attributes, 'number');
211
		}
212
213
		/**
214
		 * Generate the form field for "phone"
215
		 * @see Form::input() for more details
216
		 */
217
		public static function phone($name, $value = null, array $attributes = array()){
218
			return self::input($name, $value, $attributes, 'phone');
219
		}
220
221
		/**
222
		 * Generate the form field for "email"
223
		 * @see Form::input() for more details
224
		 */
225
		public static function email($name, $value = null, array $attributes = array()){
226
			return self::input($name, $value, $attributes, 'email');
227
		}
228
		
229
		/**
230
		 * Generate the form field for "search"
231
		 * @see Form::input() for more details
232
		 */
233
		public static function search($name, $value = null, array $attributes = array()){
234
			return self::input($name, $value, $attributes, 'search');
235
		}
236
		
237
		/**
238
		 * Generate the form field for "hidden"
239
		 * @see Form::input() for more details
240
		 */
241
		public static function hidden($name, $value = null, array $attributes = array()){
242
			return self::input($name, $value, $attributes, 'hidden');
243
		}
244
		
245
		/**
246
		 * Generate the form field for "file"
247
		 * @see Form::input() for more details
248
		 */
249
		public static function file($name, array $attributes = array()){
250
			return self::input($name, null, $attributes, 'file');
251
		}
252
		
253
		/**
254
		 * Generate the form field for "button"
255
		 * @see Form::input() for more details
256
		 */
257
		public static function button($name, $value = null, array $attributes = array()){
258
			return self::input($name, $value, $attributes, 'button');
259
		}
260
		
261
		/**
262
		 * Generate the form field for "reset"
263
		 * @see Form::input() for more details
264
		 */
265
		public static function reset($name, $value = null, array $attributes = array()){
266
			return self::input($name, $value, $attributes, 'reset');
267
		}
268
		
269
		/**
270
		 * Generate the form field for "submit"
271
		 * @see Form::input() for more details
272
		 */
273
		public static function submit($name, $value = null, array $attributes = array()){
274
			return self::input($name, $value, $attributes, 'submit');
275
		}
276
277
		/**
278
		 * Generate the form field for textarea
279
		 * @param  string $name       the name of the textarea field
280
		 * @param  string $value      the textarea field value
281
		 * @param  array  $attributes the additional attributes to be added
282
		 * @return string             the generated textarea form html content
283
		 */
284
		public static function textarea($name, $value = '', array $attributes = array()){
285
			$str = null;
286
			$str .= '<textarea name = "'.$name.'"';
287
			$str .= attributes_to_string($attributes);
288
			$str .= '>';
289
			$str .= $value.'</textarea>';
290
			return $str;
291
		}
292
		
293
		/**
294
		 * Generate the form field for select
295
		 * @param  string $name       the name of the form field
296
		 * @param  mixed|array $values      the values used to populate the "option" tags
297
		 * @param  mixed $selected   the selected value in the option list
298
		 * @param  array  $attributes the additional attribute to be added
299
		 * @return string             the generated form field html content for select
300
		 */
301
		public static function select($name, $values = null, $selected = null, array $attributes = array()){
302
			if(! is_array($values)){
303
				$values = array('' => $values);
304
			}
305
			$str = null;
306
			$str .= '<select name = "'.$name.'"';
307
			$str .= attributes_to_string($attributes);
308
			$str .= '>';
309
			foreach($values as $key => $val){
310
				$select = '';
311
				if($key == $selected){
312
					$select = 'selected';
313
				}
314
				$str .= '<option value = "'.$key.'" '.$select.'>'.$val.'</option>';
315
			}
316
			$str .= '</select>';
317
			return $str;
318
		}
319
320
	}
321