blob: a67ea050daf8c1b47a4eeae604b3604598f28984 [file] [log] [blame]
Derek Allardf3e8a352008-01-04 14:30:38 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, EllisLab, Inc.
10 * @license http://www.codeigniter.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Form Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author Rick Ellis
25 * @link http://www.codeigniter.com/user_guide/helpers/form_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Form Declaration
32 *
33 * Creates the opening portion of the form.
34 *
35 * @access public
36 * @param string the URI segments of the form destination
37 * @param array a key/value pair of attributes
38 * @param array a key/value pair hidden data
39 * @return string
40 */
41function form_open($action = '', $attributes = array(), $hidden = array())
42{
43 $CI =& get_instance();
44
45 $form = '<form action="'.$CI->config->site_url($action).'"';
46
47 if ( ! isset($attributes['method']))
48 {
49 $form .= ' method="post"';
50 }
51
52 if (is_array($attributes) AND count($attributes) > 0)
53 {
54 foreach ($attributes as $key => $val)
55 {
56 $form .= ' '.$key.'="'.$val.'"';
57 }
58 }
59
60 $form .= '>';
61
62 if (is_array($hidden) AND count($hidden > 0))
63 {
64 $form .= form_hidden($hidden);
65 }
66
67 return $form;
68}
69
70// ------------------------------------------------------------------------
71
72/**
73 * Form Declaration - Multipart type
74 *
75 * Creates the opening portion of the form, but with "multipart/form-data".
76 *
77 * @access public
78 * @param string the URI segments of the form destination
79 * @param array a key/value pair of attributes
80 * @param array a key/value pair hidden data
81 * @return string
82 */
83function form_open_multipart($action, $attributes = array(), $hidden = array())
84{
85 $attributes['enctype'] = 'multipart/form-data';
86 return form_open($action, $attributes, $hidden);
87}
88
89// ------------------------------------------------------------------------
90
91/**
92 * Hidden Input Field
93 *
94 * Generates hidden fields. You can pass a simple key/value string or an associative
95 * array with multiple values.
96 *
97 * @access public
98 * @param mixed
99 * @param string
100 * @return string
101 */
102function form_hidden($name, $value = '')
103{
104 if ( ! is_array($name))
105 {
106 return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
107 }
108
109 $form = '';
110 foreach ($name as $name => $value)
111 {
112 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
113 }
114
115 return $form;
116}
117
118// ------------------------------------------------------------------------
119
120/**
121 * Text Input Field
122 *
123 * @access public
124 * @param mixed
125 * @param string
126 * @param string
127 * @return string
128 */
129function form_input($data = '', $value = '', $extra = '')
130{
131 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50');
132
133 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
134}
135
136// ------------------------------------------------------------------------
137
138/**
139 * Password Field
140 *
141 * Identical to the input function but adds the "password" type
142 *
143 * @access public
144 * @param mixed
145 * @param string
146 * @param string
147 * @return string
148 */
149function form_password($data = '', $value = '', $extra = '')
150{
151 if ( ! is_array($data))
152 {
153 $data = array('name' => $data);
154 }
155
156 $data['type'] = 'password';
157 return form_input($data, $value, $extra);
158}
159
160// ------------------------------------------------------------------------
161
162/**
163 * Upload Field
164 *
165 * Identical to the input function but adds the "file" type
166 *
167 * @access public
168 * @param mixed
169 * @param string
170 * @param string
171 * @return string
172 */
173function form_upload($data = '', $value = '', $extra = '')
174{
175 if ( ! is_array($data))
176 {
177 $data = array('name' => $data);
178 }
179
180 $data['type'] = 'file';
181 return form_input($data, $value, $extra);
182}
183
184// ------------------------------------------------------------------------
185
186/**
187 * Textarea field
188 *
189 * @access public
190 * @param mixed
191 * @param string
192 * @param string
193 * @return string
194 */
195function form_textarea($data = '', $value = '', $extra = '')
196{
197 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
198
199 if ( ! is_array($data) OR ! isset($data['value']))
200 {
201 $val = $value;
202 }
203 else
204 {
205 $val = $data['value'];
206 unset($data['value']); // textareas don't use the value attribute
207 }
208
209 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
210}
211
212// ------------------------------------------------------------------------
213
214/**
215 * Drop-down Menu
216 *
217 * @access public
218 * @param string
219 * @param array
220 * @param string
221 * @param string
222 * @return string
223 */
Derek Allard4021b512008-01-04 22:26:12 +0000224function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000225{
Derek Allard4021b512008-01-04 22:26:12 +0000226 if ( ! is_array($selected))
227 {
228 $selected = array($selected);
229 }
230
Derek Allardf3e8a352008-01-04 14:30:38 +0000231 if ($extra != '') $extra = ' '.$extra;
Derek Allard4021b512008-01-04 22:26:12 +0000232 $multiple = (count($selected > 1))?' multiple="multiple"':'';
233
234 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000235
236 foreach ($options as $key => $val)
237 {
238 $key = (string) $key;
239 $val = (string) $val;
240
Derek Allard4021b512008-01-04 22:26:12 +0000241 $sel = (in_array($key, $selected))?' selected="selected"':'';
Derek Allardf3e8a352008-01-04 14:30:38 +0000242
243 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
244 }
245
246 $form .= '</select>';
247
248 return $form;
249}
250
251// ------------------------------------------------------------------------
252
253/**
254 * Checkbox Field
255 *
256 * @access public
257 * @param mixed
258 * @param string
259 * @param bool
260 * @param string
261 * @return string
262 */
263function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '')
264{
265 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
266
267 if (is_array($data) AND array_key_exists('checked', $data))
268 {
269 $checked = $data['checked'];
270
271 if ($checked == FALSE)
272 {
273 unset($data['checked']);
274 }
275 else
276 {
277 $data['checked'] = 'checked';
278 }
279 }
280
281 if ($checked == TRUE)
282 $defaults['checked'] = 'checked';
283 else
284 unset($defaults['checked']);
285
286 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
287}
288
289// ------------------------------------------------------------------------
290
291/**
292 * Radio Button
293 *
294 * @access public
295 * @param mixed
296 * @param string
297 * @param bool
298 * @param string
299 * @return string
300 */
301function form_radio($data = '', $value = '', $checked = TRUE, $extra = '')
302{
303 if ( ! is_array($data))
304 {
305 $data = array('name' => $data);
306 }
307
308 $data['type'] = 'radio';
309 return form_checkbox($data, $value, $checked, $extra);
310}
311
312// ------------------------------------------------------------------------
313
314/**
315 * Submit Button
316 *
317 * @access public
318 * @param mixed
319 * @param string
320 * @param string
321 * @return string
322 */
323function form_submit($data = '', $value = '', $extra = '')
324{
325 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
326
327 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
328}
329
330// ------------------------------------------------------------------------
331
332/**
333 * Reset Button
334 *
335 * @access public
336 * @param mixed
337 * @param string
338 * @param string
339 * @return string
340 */
341function form_reset($data = '', $value = '', $extra = '')
342{
343 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
344
345 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
346}
347
348// ------------------------------------------------------------------------
349
350/**
351 * Form Label Tag
352 *
353 * @access public
354 * @param string The text to appear onscreen
355 * @param string The id the label applies to
356 * @param string Additional attributes
357 * @return string
358 */
359function form_label($label_text = '', $id = '', $attributes = array())
360{
361
362 $label = '<label';
363
364 if ($id != '')
365 {
366 $label .= " for=\"$id\"";
367 }
368
369 if (is_array($attributes) AND count($attributes) > 0)
370 {
371 foreach ($attributes as $key => $val)
372 {
373 $label .= ' '.$key.'="'.$val.'"';
374 }
375 }
376
377 $label .= ">$label_text</label>";
378
379 return $label;
380}
381
382// ------------------------------------------------------------------------
383/**
384 * Fieldset Tag
385 *
386 * Used to produce <fieldset><legend>text</legend>. To close fieldset
387 * use form_fieldset_close()
388 *
389 * @access public
390 * @param string The legend text
391 * @param string Additional attributes
392 * @return string
393 */
394function form_fieldset($legend_text = '', $attributes = array())
395{
396
397 $fieldset = "<fieldset";
398
399 if (is_array($attributes) AND count($attributes) > 0)
400 {
401 foreach ($attributes as $key => $val)
402 {
403 $fieldset .= ' '.$key.'="'.$val.'"';
404 }
405 }
406
407 $fieldset .= ">\n";
408
409 if ($legend_text != '')
410 {
411 $fieldset .= "<legend>$legend_text</legend>\n";
412 }
413
414
415
416 return $fieldset;
417}
418
419// ------------------------------------------------------------------------
420
421/**
422 * Fieldset Close Tag
423 *
424 * @access public
425 * @param string
426 * @return string
427 */
428function form_fieldset_close($extra = '')
429{
430 return "</fieldset>\n".$extra;
431}
432
433// ------------------------------------------------------------------------
434
435/**
436 * Form Close Tag
437 *
438 * @access public
439 * @param string
440 * @return string
441 */
442function form_close($extra = '')
443{
444 return "</form>\n".$extra;
445}
446
447// ------------------------------------------------------------------------
448
449/**
450 * Form Prep
451 *
452 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
453 *
454 * @access public
455 * @param string
456 * @return string
457 */
458function form_prep($str = '')
459{
460 if ($str === '')
461 {
462 return '';
463 }
464
465 $temp = '__TEMP_AMPERSANDS__';
466
467 // Replace entities to temporary markers so that
468 // htmlspecialchars won't mess them up
469 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
470 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
471
472 $str = htmlspecialchars($str);
473
474 // In case htmlspecialchars misses these.
475 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
476
477 // Decode the temp markers back to entities
478 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
479 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
480
481 return $str;
482}
483
484// ------------------------------------------------------------------------
485
486/**
487 * Parse the form attributes
488 *
489 * Helper function used by some of the form helpers
490 *
491 * @access private
492 * @param array
493 * @param array
494 * @return string
495 */
496function parse_form_attributes($attributes, $default)
497{
498 if (is_array($attributes))
499 {
500 foreach ($default as $key => $val)
501 {
502 if (isset($attributes[$key]))
503 {
504 $default[$key] = $attributes[$key];
505 unset($attributes[$key]);
506 }
507 }
508
509 if (count($attributes) > 0)
510 {
511 $default = array_merge($default, $attributes);
512 }
513 }
514
515 $att = '';
516 foreach ($default as $key => $val)
517 {
518 if ($key == 'value')
519 {
520 $val = form_prep($val);
521 }
522
523 $att .= $key . '="' . $val . '" ';
524 }
525
526 return $att;
527}
528
529?>