blob: cc08f4fe45b80d34ad739bf87f002f1a40ef068c [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 Jones269b9422008-01-28 21:00:20 +000043 function form_open($action = '', $attributes = array(), $hidden = array())
Derek Allardf3e8a352008-01-04 14:30:38 +000044 {
Derek Jones269b9422008-01-28 21:00:20 +000045 $CI =& get_instance();
46
47 $action = ( strpos($action, '://') === FALSE) ? $CI->config->site_url($action) : $action;
48
49 $form = '<form action="'.$action.'"';
Derek Allardf3e8a352008-01-04 14:30:38 +000050
Derek Allard1e6ab992008-06-06 11:37:34 +000051 $form .= _attributes_to_string($attributes, TRUE);
Derek Jones269b9422008-01-28 21:00:20 +000052
53 $form .= '>';
Derek Allardf3e8a352008-01-04 14:30:38 +000054
Derek Jones269b9422008-01-28 21:00:20 +000055 if (is_array($hidden) AND count($hidden > 0))
56 {
57 $form .= form_hidden($hidden);
58 }
Derek Allardf3e8a352008-01-04 14:30:38 +000059
Derek Jones269b9422008-01-28 21:00:20 +000060 return $form;
61 }
Derek Allardf3e8a352008-01-04 14:30:38 +000062}
63
64// ------------------------------------------------------------------------
65
66/**
67 * Form Declaration - Multipart type
68 *
69 * Creates the opening portion of the form, but with "multipart/form-data".
70 *
71 * @access public
72 * @param string the URI segments of the form destination
73 * @param array a key/value pair of attributes
74 * @param array a key/value pair hidden data
75 * @return string
76 */
Derek Jones0b59f272008-05-13 04:22:33 +000077if ( ! function_exists('form_open_multipart'))
Derek Allardf3e8a352008-01-04 14:30:38 +000078{
Derek Jones269b9422008-01-28 21:00:20 +000079 function form_open_multipart($action, $attributes = array(), $hidden = array())
80 {
81 $attributes['enctype'] = 'multipart/form-data';
82 return form_open($action, $attributes, $hidden);
83 }
Derek Allardf3e8a352008-01-04 14:30:38 +000084}
85
86// ------------------------------------------------------------------------
87
88/**
89 * Hidden Input Field
90 *
91 * Generates hidden fields. You can pass a simple key/value string or an associative
92 * array with multiple values.
93 *
94 * @access public
95 * @param mixed
96 * @param string
97 * @return string
98 */
Derek Jones0b59f272008-05-13 04:22:33 +000099if ( ! function_exists('form_hidden'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000100{
Derek Jones269b9422008-01-28 21:00:20 +0000101 function form_hidden($name, $value = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000102 {
Derek Jones0b59f272008-05-13 04:22:33 +0000103 if ( ! is_array($name))
Derek Jones269b9422008-01-28 21:00:20 +0000104 {
105 return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
106 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000107
Derek Jones269b9422008-01-28 21:00:20 +0000108 $form = '';
109 foreach ($name as $name => $value)
110 {
111 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
112 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000113
Derek Jones269b9422008-01-28 21:00:20 +0000114 return $form;
115 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000116}
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 */
Derek Jones0b59f272008-05-13 04:22:33 +0000129if ( ! function_exists('form_input'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000130{
Derek Jones269b9422008-01-28 21:00:20 +0000131 function form_input($data = '', $value = '', $extra = '')
132 {
Derek Jones0b59f272008-05-13 04:22:33 +0000133 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50');
Derek Allardf3e8a352008-01-04 14:30:38 +0000134
Derek Jones269b9422008-01-28 21:00:20 +0000135 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
136 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000137}
138
139// ------------------------------------------------------------------------
140
141/**
142 * Password Field
143 *
144 * Identical to the input function but adds the "password" type
145 *
146 * @access public
147 * @param mixed
148 * @param string
149 * @param string
150 * @return string
151 */
Derek Jones0b59f272008-05-13 04:22:33 +0000152if ( ! function_exists('form_password'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000153{
Derek Jones269b9422008-01-28 21:00:20 +0000154 function form_password($data = '', $value = '', $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000155 {
Derek Jones0b59f272008-05-13 04:22:33 +0000156 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000157 {
158 $data = array('name' => $data);
159 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000160
Derek Jones269b9422008-01-28 21:00:20 +0000161 $data['type'] = 'password';
162 return form_input($data, $value, $extra);
163 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000164}
165
166// ------------------------------------------------------------------------
167
168/**
169 * Upload Field
170 *
171 * Identical to the input function but adds the "file" type
172 *
173 * @access public
174 * @param mixed
175 * @param string
176 * @param string
177 * @return string
178 */
Derek Jones0b59f272008-05-13 04:22:33 +0000179if ( ! function_exists('form_upload'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000180{
Derek Jones269b9422008-01-28 21:00:20 +0000181 function form_upload($data = '', $value = '', $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000182 {
Derek Jones0b59f272008-05-13 04:22:33 +0000183 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000184 {
185 $data = array('name' => $data);
186 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000187
Derek Jones269b9422008-01-28 21:00:20 +0000188 $data['type'] = 'file';
189 return form_input($data, $value, $extra);
190 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000191}
192
193// ------------------------------------------------------------------------
194
195/**
196 * Textarea field
197 *
198 * @access public
199 * @param mixed
200 * @param string
201 * @param string
202 * @return string
203 */
Derek Jones0b59f272008-05-13 04:22:33 +0000204if ( ! function_exists('form_textarea'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000205{
Derek Jones269b9422008-01-28 21:00:20 +0000206 function form_textarea($data = '', $value = '', $extra = '')
207 {
Derek Jones0b59f272008-05-13 04:22:33 +0000208 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
Derek Allardf3e8a352008-01-04 14:30:38 +0000209
Derek Jones0b59f272008-05-13 04:22:33 +0000210 if ( ! is_array($data) OR ! isset($data['value']))
Derek Jones269b9422008-01-28 21:00:20 +0000211 {
212 $val = $value;
213 }
214 else
215 {
216 $val = $data['value'];
217 unset($data['value']); // textareas don't use the value attribute
218 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000219
Derek Jones269b9422008-01-28 21:00:20 +0000220 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
221 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000222}
223
224// ------------------------------------------------------------------------
225
226/**
227 * Drop-down Menu
228 *
229 * @access public
230 * @param string
231 * @param array
232 * @param string
233 * @param string
234 * @return string
235 */
Derek Jones0b59f272008-05-13 04:22:33 +0000236if ( ! function_exists('form_dropdown'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000237{
Derek Jones269b9422008-01-28 21:00:20 +0000238 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
Derek Allard4021b512008-01-04 22:26:12 +0000239 {
Derek Jones0b59f272008-05-13 04:22:33 +0000240 if ( ! is_array($selected))
Derek Jones269b9422008-01-28 21:00:20 +0000241 {
242 $selected = array($selected);
243 }
Derek Allard4021b512008-01-04 22:26:12 +0000244
Derek Jones269b9422008-01-28 21:00:20 +0000245 if ($extra != '') $extra = ' '.$extra;
Derek Allardf8f05702008-01-18 14:39:23 +0000246
Derek Jones269b9422008-01-28 21:00:20 +0000247 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Derek Allard4021b512008-01-04 22:26:12 +0000248
Derek Jones269b9422008-01-28 21:00:20 +0000249 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000250
Derek Jones269b9422008-01-28 21:00:20 +0000251 foreach ($options as $key => $val)
252 {
253 $key = (string) $key;
254 $val = (string) $val;
Derek Allardf3e8a352008-01-04 14:30:38 +0000255
Derek Jones269b9422008-01-28 21:00:20 +0000256 $sel = (in_array($key, $selected))?' selected="selected"':'';
Derek Allardf3e8a352008-01-04 14:30:38 +0000257
Derek Jones269b9422008-01-28 21:00:20 +0000258 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
259 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000260
Derek Jones269b9422008-01-28 21:00:20 +0000261 $form .= '</select>';
Derek Allardf3e8a352008-01-04 14:30:38 +0000262
Derek Jones269b9422008-01-28 21:00:20 +0000263 return $form;
264 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000265}
266
267// ------------------------------------------------------------------------
268
269/**
270 * Checkbox Field
271 *
272 * @access public
273 * @param mixed
274 * @param string
275 * @param bool
276 * @param string
277 * @return string
278 */
Derek Jones0b59f272008-05-13 04:22:33 +0000279if ( ! function_exists('form_checkbox'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000280{
Derek Allardde7320b2008-05-06 13:51:02 +0000281 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000282 {
Derek Jones0b59f272008-05-13 04:22:33 +0000283 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000284
Derek Jones269b9422008-01-28 21:00:20 +0000285 if (is_array($data) AND array_key_exists('checked', $data))
286 {
287 $checked = $data['checked'];
288
289 if ($checked == FALSE)
290 {
291 unset($data['checked']);
292 }
293 else
294 {
295 $data['checked'] = 'checked';
296 }
297 }
298
299 if ($checked == TRUE)
300 $defaults['checked'] = 'checked';
301 else
302 unset($defaults['checked']);
Derek Allardf3e8a352008-01-04 14:30:38 +0000303
Derek Jones269b9422008-01-28 21:00:20 +0000304 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
305 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000306}
307
308// ------------------------------------------------------------------------
309
310/**
311 * Radio Button
312 *
313 * @access public
314 * @param mixed
315 * @param string
316 * @param bool
317 * @param string
318 * @return string
319 */
Derek Jones0b59f272008-05-13 04:22:33 +0000320if ( ! function_exists('form_radio'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000321{
Derek Allardde7320b2008-05-06 13:51:02 +0000322 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
Derek Jones269b9422008-01-28 21:00:20 +0000323 {
Derek Jones0b59f272008-05-13 04:22:33 +0000324 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000325 {
326 $data = array('name' => $data);
327 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000328
Derek Jones269b9422008-01-28 21:00:20 +0000329 $data['type'] = 'radio';
330 return form_checkbox($data, $value, $checked, $extra);
331 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000332}
333
334// ------------------------------------------------------------------------
335
336/**
337 * Submit Button
338 *
339 * @access public
340 * @param mixed
341 * @param string
342 * @param string
343 * @return string
Derek Jones269b9422008-01-28 21:00:20 +0000344 */
Derek Jones0b59f272008-05-13 04:22:33 +0000345if ( ! function_exists('form_submit'))
Derek Jones269b9422008-01-28 21:00:20 +0000346{
347 function form_submit($data = '', $value = '', $extra = '')
348 {
Derek Jones0b59f272008-05-13 04:22:33 +0000349 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000350
Derek Jones269b9422008-01-28 21:00:20 +0000351 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
352 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000353}
354
355// ------------------------------------------------------------------------
356
357/**
358 * Reset Button
359 *
360 * @access public
361 * @param mixed
362 * @param string
363 * @param string
364 * @return string
365 */
Derek Jones0b59f272008-05-13 04:22:33 +0000366if ( ! function_exists('form_reset'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000367{
Derek Jones269b9422008-01-28 21:00:20 +0000368 function form_reset($data = '', $value = '', $extra = '')
369 {
Derek Jones0b59f272008-05-13 04:22:33 +0000370 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000371
Derek Jones269b9422008-01-28 21:00:20 +0000372 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
373 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000374}
375
376// ------------------------------------------------------------------------
377
378/**
Derek Allard707d0e02008-03-18 11:50:00 +0000379 * Form Button
380 *
381 * @access public
382 * @param mixed
383 * @param string
384 * @param string
385 * @return string
386 */
Derek Jones0b59f272008-05-13 04:22:33 +0000387if ( ! function_exists('form_button'))
Derek Allard707d0e02008-03-18 11:50:00 +0000388{
389 function form_button($data = '', $content = '', $extra = '')
390 {
Derek Jones0b59f272008-05-13 04:22:33 +0000391 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'submit');
Derek Allard707d0e02008-03-18 11:50:00 +0000392
393 if ( is_array($data) AND isset($data['content']))
394 {
395 $content = $data['content'];
396 unset($data['content']); // content is not an attribute
397 }
398
399 return "<button ".parse_form_attributes($data, $defaults).$extra.">".$content."</button>\n";
400 }
401}
402
403// ------------------------------------------------------------------------
404
405/**
Derek Allardf3e8a352008-01-04 14:30:38 +0000406 * Form Label Tag
407 *
408 * @access public
409 * @param string The text to appear onscreen
410 * @param string The id the label applies to
411 * @param string Additional attributes
412 * @return string
413 */
Derek Jones0b59f272008-05-13 04:22:33 +0000414if ( ! function_exists('form_label'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000415{
Derek Jones269b9422008-01-28 21:00:20 +0000416 function form_label($label_text = '', $id = '', $attributes = array())
417 {
Derek Allardf3e8a352008-01-04 14:30:38 +0000418
Derek Jones269b9422008-01-28 21:00:20 +0000419 $label = '<label';
Derek Allardf3e8a352008-01-04 14:30:38 +0000420
Derek Jones269b9422008-01-28 21:00:20 +0000421 if ($id != '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000422 {
Derek Jones269b9422008-01-28 21:00:20 +0000423 $label .= " for=\"$id\"";
Derek Allardf3e8a352008-01-04 14:30:38 +0000424 }
Derek Jones269b9422008-01-28 21:00:20 +0000425
426 if (is_array($attributes) AND count($attributes) > 0)
427 {
428 foreach ($attributes as $key => $val)
429 {
430 $label .= ' '.$key.'="'.$val.'"';
431 }
432 }
433
434 $label .= ">$label_text</label>";
435
436 return $label;
Derek Allardf3e8a352008-01-04 14:30:38 +0000437 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000438}
439
440// ------------------------------------------------------------------------
441/**
442 * Fieldset Tag
443 *
444 * Used to produce <fieldset><legend>text</legend>. To close fieldset
445 * use form_fieldset_close()
446 *
447 * @access public
448 * @param string The legend text
449 * @param string Additional attributes
450 * @return string
451 */
Derek Jones0b59f272008-05-13 04:22:33 +0000452if ( ! function_exists('form_fieldset'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000453{
Derek Jones269b9422008-01-28 21:00:20 +0000454 function form_fieldset($legend_text = '', $attributes = array())
Derek Allardf3e8a352008-01-04 14:30:38 +0000455 {
Derek Jones269b9422008-01-28 21:00:20 +0000456
457 $fieldset = "<fieldset";
458
Derek Allard1e6ab992008-06-06 11:37:34 +0000459 $fieldset .= _attributes_to_string($attributes, FALSE);
Derek Allardf3e8a352008-01-04 14:30:38 +0000460
Derek Jones269b9422008-01-28 21:00:20 +0000461 $fieldset .= ">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000462
Derek Jones269b9422008-01-28 21:00:20 +0000463 if ($legend_text != '')
464 {
465 $fieldset .= "<legend>$legend_text</legend>\n";
466 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000467
468
469
Derek Jones269b9422008-01-28 21:00:20 +0000470 return $fieldset;
471 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000472}
473
474// ------------------------------------------------------------------------
475
476/**
477 * Fieldset Close Tag
478 *
479 * @access public
480 * @param string
481 * @return string
482 */
Derek Jones0b59f272008-05-13 04:22:33 +0000483if ( ! function_exists('form_fieldset_close'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000484{
Derek Jones269b9422008-01-28 21:00:20 +0000485 function form_fieldset_close($extra = '')
486 {
487 return "</fieldset>\n".$extra;
488 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000489}
490
491// ------------------------------------------------------------------------
492
493/**
494 * Form Close Tag
495 *
496 * @access public
497 * @param string
498 * @return string
499 */
Derek Jones0b59f272008-05-13 04:22:33 +0000500if ( ! function_exists('form_close'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000501{
Derek Jones269b9422008-01-28 21:00:20 +0000502 function form_close($extra = '')
503 {
504 return "</form>\n".$extra;
505 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000506}
507
508// ------------------------------------------------------------------------
509
510/**
511 * Form Prep
512 *
513 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
514 *
515 * @access public
516 * @param string
517 * @return string
518 */
Derek Jones0b59f272008-05-13 04:22:33 +0000519if ( ! function_exists('form_prep'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000520{
Derek Jones269b9422008-01-28 21:00:20 +0000521 function form_prep($str = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000522 {
Derek Jones269b9422008-01-28 21:00:20 +0000523 if ($str === '')
524 {
525 return '';
526 }
527
528 $temp = '__TEMP_AMPERSANDS__';
529
530 // Replace entities to temporary markers so that
531 // htmlspecialchars won't mess them up
532 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
533 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
534
535 $str = htmlspecialchars($str);
536
537 // In case htmlspecialchars misses these.
538 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
539
540 // Decode the temp markers back to entities
541 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
542 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
543
544 return $str;
Derek Allardf3e8a352008-01-04 14:30:38 +0000545 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000546}
547
548// ------------------------------------------------------------------------
549
550/**
551 * Parse the form attributes
552 *
553 * Helper function used by some of the form helpers
554 *
555 * @access private
556 * @param array
557 * @param array
558 * @return string
559 */
Derek Jones0b59f272008-05-13 04:22:33 +0000560if ( ! function_exists('parse_form_attributes'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000561{
Derek Jones269b9422008-01-28 21:00:20 +0000562 function parse_form_attributes($attributes, $default)
Derek Allardf3e8a352008-01-04 14:30:38 +0000563 {
Derek Jones269b9422008-01-28 21:00:20 +0000564 if (is_array($attributes))
Derek Allardf3e8a352008-01-04 14:30:38 +0000565 {
Derek Jones269b9422008-01-28 21:00:20 +0000566 foreach ($default as $key => $val)
Derek Allardf3e8a352008-01-04 14:30:38 +0000567 {
Derek Jones269b9422008-01-28 21:00:20 +0000568 if (isset($attributes[$key]))
569 {
570 $default[$key] = $attributes[$key];
571 unset($attributes[$key]);
572 }
573 }
574
575 if (count($attributes) > 0)
576 {
577 $default = array_merge($default, $attributes);
Derek Allardf3e8a352008-01-04 14:30:38 +0000578 }
579 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000580
Derek Jones269b9422008-01-28 21:00:20 +0000581 $att = '';
582 foreach ($default as $key => $val)
Derek Allardf3e8a352008-01-04 14:30:38 +0000583 {
Derek Jones269b9422008-01-28 21:00:20 +0000584 if ($key == 'value')
585 {
586 $val = form_prep($val);
587 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000588
Derek Jones269b9422008-01-28 21:00:20 +0000589 $att .= $key . '="' . $val . '" ';
590 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000591
Derek Jones269b9422008-01-28 21:00:20 +0000592 return $att;
593 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000594}
595
Derek Allard1e6ab992008-06-06 11:37:34 +0000596// ------------------------------------------------------------------------
597
598/**
599 * Attributes To String
600 *
601 * Helper function used by some of the form helpers
602 *
603 * @access private
604 * @param mixed
605 * @param bool
606 * @return string
607 */
608if ( ! function_exists('_attributes_to_string'))
609{
610 function _attributes_to_string($attributes, $formtag = FALSE)
611 {
612 if (is_string($attributes) AND strlen($attributes) > 0)
613 {
614 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
615 {
616 $attributes .= ' method="post"';
617 }
618
619 return ' '.$attributes;
620 }
621
622 if (is_object($attributes) AND count($attributes) > 0)
623 {
624 $attributes = (array)$attributes;
625 }
626
627 if (is_array($attributes) AND count($attributes) > 0)
628 {
629 $atts = '';
630
631 if ( ! isset($attributes['method']) AND $formtag === TRUE)
632 {
633 $atts .= ' method="post"';
634 }
635
636 foreach ($attributes as $key => $val)
637 {
638 $atts .= ' '.$key.'="'.$val.'"';
639 }
640
641 return $atts;
642 }
643 }
644}
Derek Jones0b59f272008-05-13 04:22:33 +0000645
646/* End of file form_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000647/* Location: ./system/helpers/form_helper.php */