blob: d250d76e8b2ae7f55b8c91986785b30153d0b536 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardf3e8a352008-01-04 14:30:38 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardf3e8a352008-01-04 14:30:38 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardf3e8a352008-01-04 14:30:38 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
Derek Allardf3e8a352008-01-04 14:30:38 +000026 */
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 */
Derek Jones0b59f272008-05-13 04:22:33 +000041if ( ! function_exists('form_open'))
Derek Allardf3e8a352008-01-04 14:30:38 +000042{
Derek Allard96537fd2008-06-06 13:41:53 +000043 function form_open($action = '', $attributes = '', $hidden = array())
Derek Allardf3e8a352008-01-04 14:30:38 +000044 {
Derek Jones269b9422008-01-28 21:00:20 +000045 $CI =& get_instance();
46
Derek Allard96537fd2008-06-06 13:41:53 +000047 if ($attributes == '')
48 {
49 $attributes = 'method="post"';
50 }
51
Derek Jones269b9422008-01-28 21:00:20 +000052 $action = ( strpos($action, '://') === FALSE) ? $CI->config->site_url($action) : $action;
53
54 $form = '<form action="'.$action.'"';
Derek Allardf3e8a352008-01-04 14:30:38 +000055
Derek Allard1e6ab992008-06-06 11:37:34 +000056 $form .= _attributes_to_string($attributes, TRUE);
Derek Jones269b9422008-01-28 21:00:20 +000057
58 $form .= '>';
Derek Allardf3e8a352008-01-04 14:30:38 +000059
Derek Jones269b9422008-01-28 21:00:20 +000060 if (is_array($hidden) AND count($hidden > 0))
61 {
62 $form .= form_hidden($hidden);
63 }
Derek Allardf3e8a352008-01-04 14:30:38 +000064
Derek Jones269b9422008-01-28 21:00:20 +000065 return $form;
66 }
Derek Allardf3e8a352008-01-04 14:30:38 +000067}
68
69// ------------------------------------------------------------------------
70
71/**
72 * Form Declaration - Multipart type
73 *
74 * Creates the opening portion of the form, but with "multipart/form-data".
75 *
76 * @access public
77 * @param string the URI segments of the form destination
78 * @param array a key/value pair of attributes
79 * @param array a key/value pair hidden data
80 * @return string
81 */
Derek Jones0b59f272008-05-13 04:22:33 +000082if ( ! function_exists('form_open_multipart'))
Derek Allardf3e8a352008-01-04 14:30:38 +000083{
Derek Jones269b9422008-01-28 21:00:20 +000084 function form_open_multipart($action, $attributes = array(), $hidden = array())
85 {
86 $attributes['enctype'] = 'multipart/form-data';
87 return form_open($action, $attributes, $hidden);
88 }
Derek Allardf3e8a352008-01-04 14:30:38 +000089}
90
91// ------------------------------------------------------------------------
92
93/**
94 * Hidden Input Field
95 *
96 * Generates hidden fields. You can pass a simple key/value string or an associative
97 * array with multiple values.
98 *
99 * @access public
100 * @param mixed
101 * @param string
102 * @return string
103 */
Derek Jones0b59f272008-05-13 04:22:33 +0000104if ( ! function_exists('form_hidden'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000105{
Derek Jones269b9422008-01-28 21:00:20 +0000106 function form_hidden($name, $value = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000107 {
Derek Jones0b59f272008-05-13 04:22:33 +0000108 if ( ! is_array($name))
Derek Jones269b9422008-01-28 21:00:20 +0000109 {
110 return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
111 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000112
Derek Jones269b9422008-01-28 21:00:20 +0000113 $form = '';
114 foreach ($name as $name => $value)
115 {
116 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
117 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000118
Derek Jones269b9422008-01-28 21:00:20 +0000119 return $form;
120 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000121}
122
123// ------------------------------------------------------------------------
124
125/**
126 * Text Input Field
127 *
128 * @access public
129 * @param mixed
130 * @param string
131 * @param string
132 * @return string
133 */
Derek Jones0b59f272008-05-13 04:22:33 +0000134if ( ! function_exists('form_input'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000135{
Derek Jones269b9422008-01-28 21:00:20 +0000136 function form_input($data = '', $value = '', $extra = '')
137 {
Derek Jones0b59f272008-05-13 04:22:33 +0000138 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50');
Derek Allardf3e8a352008-01-04 14:30:38 +0000139
Derek Jones269b9422008-01-28 21:00:20 +0000140 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
141 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000142}
143
144// ------------------------------------------------------------------------
145
146/**
147 * Password Field
148 *
149 * Identical to the input function but adds the "password" type
150 *
151 * @access public
152 * @param mixed
153 * @param string
154 * @param string
155 * @return string
156 */
Derek Jones0b59f272008-05-13 04:22:33 +0000157if ( ! function_exists('form_password'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000158{
Derek Jones269b9422008-01-28 21:00:20 +0000159 function form_password($data = '', $value = '', $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000160 {
Derek Jones0b59f272008-05-13 04:22:33 +0000161 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000162 {
163 $data = array('name' => $data);
164 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000165
Derek Jones269b9422008-01-28 21:00:20 +0000166 $data['type'] = 'password';
167 return form_input($data, $value, $extra);
168 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000169}
170
171// ------------------------------------------------------------------------
172
173/**
174 * Upload Field
175 *
176 * Identical to the input function but adds the "file" type
177 *
178 * @access public
179 * @param mixed
180 * @param string
181 * @param string
182 * @return string
183 */
Derek Jones0b59f272008-05-13 04:22:33 +0000184if ( ! function_exists('form_upload'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000185{
Derek Jones269b9422008-01-28 21:00:20 +0000186 function form_upload($data = '', $value = '', $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000187 {
Derek Jones0b59f272008-05-13 04:22:33 +0000188 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000189 {
190 $data = array('name' => $data);
191 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000192
Derek Jones269b9422008-01-28 21:00:20 +0000193 $data['type'] = 'file';
194 return form_input($data, $value, $extra);
195 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000196}
197
198// ------------------------------------------------------------------------
199
200/**
201 * Textarea field
202 *
203 * @access public
204 * @param mixed
205 * @param string
206 * @param string
207 * @return string
208 */
Derek Jones0b59f272008-05-13 04:22:33 +0000209if ( ! function_exists('form_textarea'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000210{
Derek Jones269b9422008-01-28 21:00:20 +0000211 function form_textarea($data = '', $value = '', $extra = '')
212 {
Derek Jones0b59f272008-05-13 04:22:33 +0000213 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
Derek Allardf3e8a352008-01-04 14:30:38 +0000214
Derek Jones0b59f272008-05-13 04:22:33 +0000215 if ( ! is_array($data) OR ! isset($data['value']))
Derek Jones269b9422008-01-28 21:00:20 +0000216 {
217 $val = $value;
218 }
219 else
220 {
221 $val = $data['value'];
222 unset($data['value']); // textareas don't use the value attribute
223 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000224
Derek Jones269b9422008-01-28 21:00:20 +0000225 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
226 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000227}
228
229// ------------------------------------------------------------------------
230
231/**
232 * Drop-down Menu
233 *
234 * @access public
235 * @param string
236 * @param array
237 * @param string
238 * @param string
239 * @return string
240 */
Derek Jones0b59f272008-05-13 04:22:33 +0000241if ( ! function_exists('form_dropdown'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000242{
Derek Jones269b9422008-01-28 21:00:20 +0000243 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
Derek Allard4021b512008-01-04 22:26:12 +0000244 {
Derek Jones0b59f272008-05-13 04:22:33 +0000245 if ( ! is_array($selected))
Derek Jones269b9422008-01-28 21:00:20 +0000246 {
247 $selected = array($selected);
248 }
Derek Allard4021b512008-01-04 22:26:12 +0000249
Derek Jones269b9422008-01-28 21:00:20 +0000250 if ($extra != '') $extra = ' '.$extra;
Derek Allardf8f05702008-01-18 14:39:23 +0000251
Derek Jones269b9422008-01-28 21:00:20 +0000252 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Derek Allard4021b512008-01-04 22:26:12 +0000253
Derek Jones269b9422008-01-28 21:00:20 +0000254 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000255
Derek Jones269b9422008-01-28 21:00:20 +0000256 foreach ($options as $key => $val)
257 {
258 $key = (string) $key;
259 $val = (string) $val;
Derek Allardf3e8a352008-01-04 14:30:38 +0000260
Derek Jones269b9422008-01-28 21:00:20 +0000261 $sel = (in_array($key, $selected))?' selected="selected"':'';
Derek Allardf3e8a352008-01-04 14:30:38 +0000262
Derek Jones269b9422008-01-28 21:00:20 +0000263 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
264 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000265
Derek Jones269b9422008-01-28 21:00:20 +0000266 $form .= '</select>';
Derek Allardf3e8a352008-01-04 14:30:38 +0000267
Derek Jones269b9422008-01-28 21:00:20 +0000268 return $form;
269 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000270}
271
272// ------------------------------------------------------------------------
273
274/**
275 * Checkbox Field
276 *
277 * @access public
278 * @param mixed
279 * @param string
280 * @param bool
281 * @param string
282 * @return string
283 */
Derek Jones0b59f272008-05-13 04:22:33 +0000284if ( ! function_exists('form_checkbox'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000285{
Derek Allardde7320b2008-05-06 13:51:02 +0000286 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000287 {
Derek Jones0b59f272008-05-13 04:22:33 +0000288 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000289
Derek Jones269b9422008-01-28 21:00:20 +0000290 if (is_array($data) AND array_key_exists('checked', $data))
291 {
292 $checked = $data['checked'];
293
294 if ($checked == FALSE)
295 {
296 unset($data['checked']);
297 }
298 else
299 {
300 $data['checked'] = 'checked';
301 }
302 }
303
304 if ($checked == TRUE)
305 $defaults['checked'] = 'checked';
306 else
307 unset($defaults['checked']);
Derek Allardf3e8a352008-01-04 14:30:38 +0000308
Derek Jones269b9422008-01-28 21:00:20 +0000309 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
310 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000311}
312
313// ------------------------------------------------------------------------
314
315/**
316 * Radio Button
317 *
318 * @access public
319 * @param mixed
320 * @param string
321 * @param bool
322 * @param string
323 * @return string
324 */
Derek Jones0b59f272008-05-13 04:22:33 +0000325if ( ! function_exists('form_radio'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000326{
Derek Allardde7320b2008-05-06 13:51:02 +0000327 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
Derek Jones269b9422008-01-28 21:00:20 +0000328 {
Derek Jones0b59f272008-05-13 04:22:33 +0000329 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000330 {
331 $data = array('name' => $data);
332 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000333
Derek Jones269b9422008-01-28 21:00:20 +0000334 $data['type'] = 'radio';
335 return form_checkbox($data, $value, $checked, $extra);
336 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000337}
338
339// ------------------------------------------------------------------------
340
341/**
342 * Submit Button
343 *
344 * @access public
345 * @param mixed
346 * @param string
347 * @param string
348 * @return string
Derek Jones269b9422008-01-28 21:00:20 +0000349 */
Derek Jones0b59f272008-05-13 04:22:33 +0000350if ( ! function_exists('form_submit'))
Derek Jones269b9422008-01-28 21:00:20 +0000351{
352 function form_submit($data = '', $value = '', $extra = '')
353 {
Derek Jones0b59f272008-05-13 04:22:33 +0000354 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000355
Derek Jones269b9422008-01-28 21:00:20 +0000356 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
357 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000358}
359
360// ------------------------------------------------------------------------
361
362/**
363 * Reset Button
364 *
365 * @access public
366 * @param mixed
367 * @param string
368 * @param string
369 * @return string
370 */
Derek Jones0b59f272008-05-13 04:22:33 +0000371if ( ! function_exists('form_reset'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000372{
Derek Jones269b9422008-01-28 21:00:20 +0000373 function form_reset($data = '', $value = '', $extra = '')
374 {
Derek Jones0b59f272008-05-13 04:22:33 +0000375 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000376
Derek Jones269b9422008-01-28 21:00:20 +0000377 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
378 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000379}
380
381// ------------------------------------------------------------------------
382
383/**
Derek Allard707d0e02008-03-18 11:50:00 +0000384 * Form Button
385 *
386 * @access public
387 * @param mixed
388 * @param string
389 * @param string
390 * @return string
391 */
Derek Jones0b59f272008-05-13 04:22:33 +0000392if ( ! function_exists('form_button'))
Derek Allard707d0e02008-03-18 11:50:00 +0000393{
394 function form_button($data = '', $content = '', $extra = '')
395 {
Derek Jones0b59f272008-05-13 04:22:33 +0000396 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'submit');
Derek Allard707d0e02008-03-18 11:50:00 +0000397
398 if ( is_array($data) AND isset($data['content']))
399 {
400 $content = $data['content'];
401 unset($data['content']); // content is not an attribute
402 }
403
404 return "<button ".parse_form_attributes($data, $defaults).$extra.">".$content."</button>\n";
405 }
406}
407
408// ------------------------------------------------------------------------
409
410/**
Derek Allardf3e8a352008-01-04 14:30:38 +0000411 * Form Label Tag
412 *
413 * @access public
414 * @param string The text to appear onscreen
415 * @param string The id the label applies to
416 * @param string Additional attributes
417 * @return string
418 */
Derek Jones0b59f272008-05-13 04:22:33 +0000419if ( ! function_exists('form_label'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000420{
Derek Jones269b9422008-01-28 21:00:20 +0000421 function form_label($label_text = '', $id = '', $attributes = array())
422 {
Derek Allardf3e8a352008-01-04 14:30:38 +0000423
Derek Jones269b9422008-01-28 21:00:20 +0000424 $label = '<label';
Derek Allardf3e8a352008-01-04 14:30:38 +0000425
Derek Jones269b9422008-01-28 21:00:20 +0000426 if ($id != '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000427 {
Derek Jones269b9422008-01-28 21:00:20 +0000428 $label .= " for=\"$id\"";
Derek Allardf3e8a352008-01-04 14:30:38 +0000429 }
Derek Jones269b9422008-01-28 21:00:20 +0000430
431 if (is_array($attributes) AND count($attributes) > 0)
432 {
433 foreach ($attributes as $key => $val)
434 {
435 $label .= ' '.$key.'="'.$val.'"';
436 }
437 }
438
439 $label .= ">$label_text</label>";
440
441 return $label;
Derek Allardf3e8a352008-01-04 14:30:38 +0000442 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000443}
444
445// ------------------------------------------------------------------------
446/**
447 * Fieldset Tag
448 *
449 * Used to produce <fieldset><legend>text</legend>. To close fieldset
450 * use form_fieldset_close()
451 *
452 * @access public
453 * @param string The legend text
454 * @param string Additional attributes
455 * @return string
456 */
Derek Jones0b59f272008-05-13 04:22:33 +0000457if ( ! function_exists('form_fieldset'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000458{
Derek Jones269b9422008-01-28 21:00:20 +0000459 function form_fieldset($legend_text = '', $attributes = array())
Derek Allardf3e8a352008-01-04 14:30:38 +0000460 {
Derek Jones269b9422008-01-28 21:00:20 +0000461 $fieldset = "<fieldset";
462
Derek Allard1e6ab992008-06-06 11:37:34 +0000463 $fieldset .= _attributes_to_string($attributes, FALSE);
Derek Allardf3e8a352008-01-04 14:30:38 +0000464
Derek Jones269b9422008-01-28 21:00:20 +0000465 $fieldset .= ">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000466
Derek Jones269b9422008-01-28 21:00:20 +0000467 if ($legend_text != '')
468 {
469 $fieldset .= "<legend>$legend_text</legend>\n";
470 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000471
Derek Jones269b9422008-01-28 21:00:20 +0000472 return $fieldset;
473 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000474}
475
476// ------------------------------------------------------------------------
477
478/**
479 * Fieldset Close Tag
480 *
481 * @access public
482 * @param string
483 * @return string
484 */
Derek Jones0b59f272008-05-13 04:22:33 +0000485if ( ! function_exists('form_fieldset_close'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000486{
Derek Jones269b9422008-01-28 21:00:20 +0000487 function form_fieldset_close($extra = '')
488 {
489 return "</fieldset>\n".$extra;
490 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000491}
492
493// ------------------------------------------------------------------------
494
495/**
496 * Form Close Tag
497 *
498 * @access public
499 * @param string
500 * @return string
501 */
Derek Jones0b59f272008-05-13 04:22:33 +0000502if ( ! function_exists('form_close'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000503{
Derek Jones269b9422008-01-28 21:00:20 +0000504 function form_close($extra = '')
505 {
506 return "</form>\n".$extra;
507 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000508}
509
510// ------------------------------------------------------------------------
511
512/**
513 * Form Prep
514 *
515 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
516 *
517 * @access public
518 * @param string
519 * @return string
520 */
Derek Jones0b59f272008-05-13 04:22:33 +0000521if ( ! function_exists('form_prep'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000522{
Derek Jones269b9422008-01-28 21:00:20 +0000523 function form_prep($str = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000524 {
Derek Jones269b9422008-01-28 21:00:20 +0000525 if ($str === '')
526 {
527 return '';
528 }
529
530 $temp = '__TEMP_AMPERSANDS__';
531
532 // Replace entities to temporary markers so that
533 // htmlspecialchars won't mess them up
534 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
535 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
536
537 $str = htmlspecialchars($str);
538
539 // In case htmlspecialchars misses these.
540 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
541
542 // Decode the temp markers back to entities
543 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
544 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
545
546 return $str;
Derek Allardf3e8a352008-01-04 14:30:38 +0000547 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000548}
549
550// ------------------------------------------------------------------------
551
552/**
553 * Parse the form attributes
554 *
555 * Helper function used by some of the form helpers
556 *
557 * @access private
558 * @param array
559 * @param array
560 * @return string
561 */
Derek Jones0b59f272008-05-13 04:22:33 +0000562if ( ! function_exists('parse_form_attributes'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000563{
Derek Jones269b9422008-01-28 21:00:20 +0000564 function parse_form_attributes($attributes, $default)
Derek Allardf3e8a352008-01-04 14:30:38 +0000565 {
Derek Jones269b9422008-01-28 21:00:20 +0000566 if (is_array($attributes))
Derek Allardf3e8a352008-01-04 14:30:38 +0000567 {
Derek Jones269b9422008-01-28 21:00:20 +0000568 foreach ($default as $key => $val)
Derek Allardf3e8a352008-01-04 14:30:38 +0000569 {
Derek Jones269b9422008-01-28 21:00:20 +0000570 if (isset($attributes[$key]))
571 {
572 $default[$key] = $attributes[$key];
573 unset($attributes[$key]);
574 }
575 }
576
577 if (count($attributes) > 0)
578 {
579 $default = array_merge($default, $attributes);
Derek Allardf3e8a352008-01-04 14:30:38 +0000580 }
581 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000582
Derek Jones269b9422008-01-28 21:00:20 +0000583 $att = '';
584 foreach ($default as $key => $val)
Derek Allardf3e8a352008-01-04 14:30:38 +0000585 {
Derek Jones269b9422008-01-28 21:00:20 +0000586 if ($key == 'value')
587 {
588 $val = form_prep($val);
589 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000590
Derek Jones269b9422008-01-28 21:00:20 +0000591 $att .= $key . '="' . $val . '" ';
592 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000593
Derek Jones269b9422008-01-28 21:00:20 +0000594 return $att;
595 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000596}
597
Derek Allard1e6ab992008-06-06 11:37:34 +0000598// ------------------------------------------------------------------------
599
600/**
601 * Attributes To String
602 *
603 * Helper function used by some of the form helpers
604 *
605 * @access private
606 * @param mixed
607 * @param bool
608 * @return string
609 */
610if ( ! function_exists('_attributes_to_string'))
611{
612 function _attributes_to_string($attributes, $formtag = FALSE)
613 {
614 if (is_string($attributes) AND strlen($attributes) > 0)
615 {
616 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
617 {
618 $attributes .= ' method="post"';
619 }
620
621 return ' '.$attributes;
622 }
623
624 if (is_object($attributes) AND count($attributes) > 0)
625 {
626 $attributes = (array)$attributes;
627 }
628
629 if (is_array($attributes) AND count($attributes) > 0)
630 {
631 $atts = '';
632
633 if ( ! isset($attributes['method']) AND $formtag === TRUE)
634 {
635 $atts .= ' method="post"';
636 }
637
638 foreach ($attributes as $key => $val)
639 {
640 $atts .= ' '.$key.'="'.$val.'"';
641 }
642
643 return $atts;
644 }
645 }
646}
Derek Jones0b59f272008-05-13 04:22:33 +0000647
648/* End of file form_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000649/* Location: ./system/helpers/form_helper.php */