blob: 0c74ac0ffd43c332ec8b09cb83d90dde16ab65dc [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 Allard993925b2008-08-21 12:43:31 +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 = '';
Derek Allardc5074182008-07-21 11:58:56 +0000114
Derek Jones269b9422008-01-28 21:00:20 +0000115 foreach ($name as $name => $value)
116 {
Rick Ellisf3885112008-08-12 05:32:25 +0000117 $form .= "\n";
Derek Jones269b9422008-01-28 21:00:20 +0000118 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
119 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000120
Derek Jones269b9422008-01-28 21:00:20 +0000121 return $form;
122 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000123}
124
125// ------------------------------------------------------------------------
126
127/**
128 * Text Input Field
129 *
130 * @access public
131 * @param mixed
132 * @param string
133 * @param string
134 * @return string
135 */
Derek Jones0b59f272008-05-13 04:22:33 +0000136if ( ! function_exists('form_input'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000137{
Derek Jones269b9422008-01-28 21:00:20 +0000138 function form_input($data = '', $value = '', $extra = '')
139 {
Derek Allard3640a0b2008-06-19 07:46:22 +0000140 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000141
Derek Allardc5074182008-07-21 11:58:56 +0000142 return "<input ".parse_form_attributes($data, $defaults).$extra." />";
Derek Jones269b9422008-01-28 21:00:20 +0000143 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000144}
145
146// ------------------------------------------------------------------------
147
148/**
149 * Password Field
150 *
151 * Identical to the input function but adds the "password" type
152 *
153 * @access public
154 * @param mixed
155 * @param string
156 * @param string
157 * @return string
158 */
Derek Jones0b59f272008-05-13 04:22:33 +0000159if ( ! function_exists('form_password'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000160{
Derek Jones269b9422008-01-28 21:00:20 +0000161 function form_password($data = '', $value = '', $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000162 {
Derek Jones0b59f272008-05-13 04:22:33 +0000163 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000164 {
165 $data = array('name' => $data);
166 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000167
Derek Jones269b9422008-01-28 21:00:20 +0000168 $data['type'] = 'password';
169 return form_input($data, $value, $extra);
170 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000171}
172
173// ------------------------------------------------------------------------
174
175/**
176 * Upload Field
177 *
178 * Identical to the input function but adds the "file" type
179 *
180 * @access public
181 * @param mixed
182 * @param string
183 * @param string
184 * @return string
185 */
Derek Jones0b59f272008-05-13 04:22:33 +0000186if ( ! function_exists('form_upload'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000187{
Derek Jones269b9422008-01-28 21:00:20 +0000188 function form_upload($data = '', $value = '', $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000189 {
Derek Jones0b59f272008-05-13 04:22:33 +0000190 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000191 {
192 $data = array('name' => $data);
193 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000194
Derek Jones269b9422008-01-28 21:00:20 +0000195 $data['type'] = 'file';
196 return form_input($data, $value, $extra);
197 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000198}
199
200// ------------------------------------------------------------------------
201
202/**
203 * Textarea field
204 *
205 * @access public
206 * @param mixed
207 * @param string
208 * @param string
209 * @return string
210 */
Derek Jones0b59f272008-05-13 04:22:33 +0000211if ( ! function_exists('form_textarea'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000212{
Derek Jones269b9422008-01-28 21:00:20 +0000213 function form_textarea($data = '', $value = '', $extra = '')
214 {
Derek Jones0b59f272008-05-13 04:22:33 +0000215 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
Derek Allardf3e8a352008-01-04 14:30:38 +0000216
Derek Allard993925b2008-08-21 12:43:31 +0000217 if ( ! is_array($data) OR ! isset($data['value']))
Derek Jones269b9422008-01-28 21:00:20 +0000218 {
219 $val = $value;
220 }
Derek Allard993925b2008-08-21 12:43:31 +0000221 else
Derek Jones269b9422008-01-28 21:00:20 +0000222 {
223 $val = $data['value'];
224 unset($data['value']); // textareas don't use the value attribute
225 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000226
Derek Allardc5074182008-07-21 11:58:56 +0000227 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>";
Derek Jones269b9422008-01-28 21:00:20 +0000228 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000229}
230
231// ------------------------------------------------------------------------
232
233/**
234 * Drop-down Menu
235 *
236 * @access public
237 * @param string
238 * @param array
239 * @param string
240 * @param string
241 * @return string
242 */
Derek Jones0b59f272008-05-13 04:22:33 +0000243if ( ! function_exists('form_dropdown'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000244{
Derek Jones269b9422008-01-28 21:00:20 +0000245 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
Derek Allard4021b512008-01-04 22:26:12 +0000246 {
Derek Jones0b59f272008-05-13 04:22:33 +0000247 if ( ! is_array($selected))
Derek Jones269b9422008-01-28 21:00:20 +0000248 {
249 $selected = array($selected);
250 }
Derek Allard4021b512008-01-04 22:26:12 +0000251
Derek Jones269b9422008-01-28 21:00:20 +0000252 if ($extra != '') $extra = ' '.$extra;
Derek Allardf8f05702008-01-18 14:39:23 +0000253
Derek Jones269b9422008-01-28 21:00:20 +0000254 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Derek Allard4021b512008-01-04 22:26:12 +0000255
Derek Jones269b9422008-01-28 21:00:20 +0000256 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000257
Derek Jones269b9422008-01-28 21:00:20 +0000258 foreach ($options as $key => $val)
259 {
260 $key = (string) $key;
261 $val = (string) $val;
Derek Allardf3e8a352008-01-04 14:30:38 +0000262
Derek Jones269b9422008-01-28 21:00:20 +0000263 $sel = (in_array($key, $selected))?' selected="selected"':'';
Derek Allardf3e8a352008-01-04 14:30:38 +0000264
Derek Jones269b9422008-01-28 21:00:20 +0000265 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
266 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000267
Derek Jones269b9422008-01-28 21:00:20 +0000268 $form .= '</select>';
Derek Allardf3e8a352008-01-04 14:30:38 +0000269
Derek Jones269b9422008-01-28 21:00:20 +0000270 return $form;
271 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000272}
273
274// ------------------------------------------------------------------------
275
276/**
277 * Checkbox Field
278 *
279 * @access public
280 * @param mixed
281 * @param string
282 * @param bool
283 * @param string
284 * @return string
285 */
Derek Jones0b59f272008-05-13 04:22:33 +0000286if ( ! function_exists('form_checkbox'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000287{
Derek Allardde7320b2008-05-06 13:51:02 +0000288 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000289 {
Derek Jones0b59f272008-05-13 04:22:33 +0000290 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000291
Derek Jones269b9422008-01-28 21:00:20 +0000292 if (is_array($data) AND array_key_exists('checked', $data))
293 {
294 $checked = $data['checked'];
295
296 if ($checked == FALSE)
297 {
298 unset($data['checked']);
299 }
300 else
301 {
302 $data['checked'] = 'checked';
303 }
304 }
305
306 if ($checked == TRUE)
Derek Allardc5074182008-07-21 11:58:56 +0000307 {
Derek Jones269b9422008-01-28 21:00:20 +0000308 $defaults['checked'] = 'checked';
Derek Allardc5074182008-07-21 11:58:56 +0000309 }
Derek Jones269b9422008-01-28 21:00:20 +0000310 else
Derek Allardc5074182008-07-21 11:58:56 +0000311 {
Derek Jones269b9422008-01-28 21:00:20 +0000312 unset($defaults['checked']);
Derek Allardc5074182008-07-21 11:58:56 +0000313 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000314
Derek Allardc5074182008-07-21 11:58:56 +0000315 return "<input ".parse_form_attributes($data, $defaults).$extra." />";
Derek Jones269b9422008-01-28 21:00:20 +0000316 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000317}
318
319// ------------------------------------------------------------------------
320
321/**
322 * Radio Button
323 *
324 * @access public
325 * @param mixed
326 * @param string
327 * @param bool
328 * @param string
329 * @return string
330 */
Derek Jones0b59f272008-05-13 04:22:33 +0000331if ( ! function_exists('form_radio'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000332{
Derek Allardde7320b2008-05-06 13:51:02 +0000333 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
Derek Jones269b9422008-01-28 21:00:20 +0000334 {
Derek Jones0b59f272008-05-13 04:22:33 +0000335 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000336 {
337 $data = array('name' => $data);
338 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000339
Derek Jones269b9422008-01-28 21:00:20 +0000340 $data['type'] = 'radio';
341 return form_checkbox($data, $value, $checked, $extra);
342 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000343}
344
345// ------------------------------------------------------------------------
346
347/**
348 * Submit Button
349 *
350 * @access public
351 * @param mixed
352 * @param string
353 * @param string
354 * @return string
Derek Jones269b9422008-01-28 21:00:20 +0000355 */
Derek Jones0b59f272008-05-13 04:22:33 +0000356if ( ! function_exists('form_submit'))
Derek Jones269b9422008-01-28 21:00:20 +0000357{
358 function form_submit($data = '', $value = '', $extra = '')
359 {
Derek Jones0b59f272008-05-13 04:22:33 +0000360 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000361
Derek Allardc5074182008-07-21 11:58:56 +0000362 return "<input ".parse_form_attributes($data, $defaults).$extra." />";
Derek Jones269b9422008-01-28 21:00:20 +0000363 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000364}
365
366// ------------------------------------------------------------------------
367
368/**
369 * Reset Button
370 *
371 * @access public
372 * @param mixed
373 * @param string
374 * @param string
375 * @return string
376 */
Derek Jones0b59f272008-05-13 04:22:33 +0000377if ( ! function_exists('form_reset'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000378{
Derek Jones269b9422008-01-28 21:00:20 +0000379 function form_reset($data = '', $value = '', $extra = '')
380 {
Derek Jones0b59f272008-05-13 04:22:33 +0000381 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000382
Derek Allardc5074182008-07-21 11:58:56 +0000383 return "<input ".parse_form_attributes($data, $defaults).$extra." />";
Derek Jones269b9422008-01-28 21:00:20 +0000384 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000385}
386
387// ------------------------------------------------------------------------
388
389/**
Derek Allard707d0e02008-03-18 11:50:00 +0000390 * Form Button
391 *
392 * @access public
393 * @param mixed
394 * @param string
395 * @param string
396 * @return string
397 */
Derek Jones0b59f272008-05-13 04:22:33 +0000398if ( ! function_exists('form_button'))
Derek Allard707d0e02008-03-18 11:50:00 +0000399{
400 function form_button($data = '', $content = '', $extra = '')
401 {
Derek Jones0b59f272008-05-13 04:22:33 +0000402 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'submit');
Derek Allard707d0e02008-03-18 11:50:00 +0000403
404 if ( is_array($data) AND isset($data['content']))
405 {
406 $content = $data['content'];
407 unset($data['content']); // content is not an attribute
408 }
409
Derek Allardc5074182008-07-21 11:58:56 +0000410 return "<button ".parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
Derek Allard707d0e02008-03-18 11:50:00 +0000411 }
412}
413
414// ------------------------------------------------------------------------
415
416/**
Derek Allardf3e8a352008-01-04 14:30:38 +0000417 * Form Label Tag
418 *
419 * @access public
420 * @param string The text to appear onscreen
421 * @param string The id the label applies to
422 * @param string Additional attributes
423 * @return string
424 */
Derek Jones0b59f272008-05-13 04:22:33 +0000425if ( ! function_exists('form_label'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000426{
Derek Jones269b9422008-01-28 21:00:20 +0000427 function form_label($label_text = '', $id = '', $attributes = array())
428 {
Derek Allardf3e8a352008-01-04 14:30:38 +0000429
Derek Jones269b9422008-01-28 21:00:20 +0000430 $label = '<label';
Derek Allardf3e8a352008-01-04 14:30:38 +0000431
Derek Jones269b9422008-01-28 21:00:20 +0000432 if ($id != '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000433 {
Derek Jones269b9422008-01-28 21:00:20 +0000434 $label .= " for=\"$id\"";
Derek Allardf3e8a352008-01-04 14:30:38 +0000435 }
Derek Jones269b9422008-01-28 21:00:20 +0000436
437 if (is_array($attributes) AND count($attributes) > 0)
438 {
439 foreach ($attributes as $key => $val)
440 {
441 $label .= ' '.$key.'="'.$val.'"';
442 }
443 }
444
445 $label .= ">$label_text</label>";
446
447 return $label;
Derek Allardf3e8a352008-01-04 14:30:38 +0000448 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000449}
450
451// ------------------------------------------------------------------------
452/**
453 * Fieldset Tag
454 *
455 * Used to produce <fieldset><legend>text</legend>. To close fieldset
456 * use form_fieldset_close()
457 *
458 * @access public
459 * @param string The legend text
460 * @param string Additional attributes
461 * @return string
462 */
Derek Jones0b59f272008-05-13 04:22:33 +0000463if ( ! function_exists('form_fieldset'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000464{
Derek Jones269b9422008-01-28 21:00:20 +0000465 function form_fieldset($legend_text = '', $attributes = array())
Derek Allardf3e8a352008-01-04 14:30:38 +0000466 {
Derek Jones269b9422008-01-28 21:00:20 +0000467 $fieldset = "<fieldset";
468
Derek Allard993925b2008-08-21 12:43:31 +0000469 $fieldset .= _attributes_to_string($attributes, FALSE);
Derek Allardf3e8a352008-01-04 14:30:38 +0000470
Derek Jones269b9422008-01-28 21:00:20 +0000471 $fieldset .= ">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000472
Derek Jones269b9422008-01-28 21:00:20 +0000473 if ($legend_text != '')
474 {
475 $fieldset .= "<legend>$legend_text</legend>\n";
476 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000477
Derek Jones269b9422008-01-28 21:00:20 +0000478 return $fieldset;
479 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000480}
481
482// ------------------------------------------------------------------------
483
484/**
485 * Fieldset Close Tag
486 *
487 * @access public
488 * @param string
489 * @return string
490 */
Derek Jones0b59f272008-05-13 04:22:33 +0000491if ( ! function_exists('form_fieldset_close'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000492{
Derek Jones269b9422008-01-28 21:00:20 +0000493 function form_fieldset_close($extra = '')
494 {
Derek Allardc5074182008-07-21 11:58:56 +0000495 return "</fieldset>".$extra;
Derek Jones269b9422008-01-28 21:00:20 +0000496 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000497}
498
499// ------------------------------------------------------------------------
500
501/**
502 * Form Close Tag
503 *
504 * @access public
505 * @param string
506 * @return string
507 */
Derek Jones0b59f272008-05-13 04:22:33 +0000508if ( ! function_exists('form_close'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000509{
Derek Jones269b9422008-01-28 21:00:20 +0000510 function form_close($extra = '')
511 {
Derek Allardc5074182008-07-21 11:58:56 +0000512 return "</form>".$extra;
Derek Jones269b9422008-01-28 21:00:20 +0000513 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000514}
515
516// ------------------------------------------------------------------------
517
518/**
519 * Form Prep
520 *
521 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
522 *
523 * @access public
524 * @param string
525 * @return string
526 */
Derek Jones0b59f272008-05-13 04:22:33 +0000527if ( ! function_exists('form_prep'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000528{
Derek Jones269b9422008-01-28 21:00:20 +0000529 function form_prep($str = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000530 {
Derek Jones269b9422008-01-28 21:00:20 +0000531 if ($str === '')
532 {
533 return '';
534 }
535
536 $temp = '__TEMP_AMPERSANDS__';
537
538 // Replace entities to temporary markers so that
539 // htmlspecialchars won't mess them up
540 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
541 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
542
543 $str = htmlspecialchars($str);
544
545 // In case htmlspecialchars misses these.
546 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
547
548 // Decode the temp markers back to entities
549 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
550 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
551
552 return $str;
Derek Allardf3e8a352008-01-04 14:30:38 +0000553 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000554}
555
556// ------------------------------------------------------------------------
557
558/**
559 * Parse the form attributes
560 *
561 * Helper function used by some of the form helpers
562 *
563 * @access private
564 * @param array
565 * @param array
566 * @return string
567 */
Derek Jones0b59f272008-05-13 04:22:33 +0000568if ( ! function_exists('parse_form_attributes'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000569{
Derek Jones269b9422008-01-28 21:00:20 +0000570 function parse_form_attributes($attributes, $default)
Derek Allardf3e8a352008-01-04 14:30:38 +0000571 {
Derek Jones269b9422008-01-28 21:00:20 +0000572 if (is_array($attributes))
Derek Allardf3e8a352008-01-04 14:30:38 +0000573 {
Derek Jones269b9422008-01-28 21:00:20 +0000574 foreach ($default as $key => $val)
Derek Allardf3e8a352008-01-04 14:30:38 +0000575 {
Derek Jones269b9422008-01-28 21:00:20 +0000576 if (isset($attributes[$key]))
577 {
578 $default[$key] = $attributes[$key];
579 unset($attributes[$key]);
580 }
581 }
582
583 if (count($attributes) > 0)
584 {
585 $default = array_merge($default, $attributes);
Derek Allardf3e8a352008-01-04 14:30:38 +0000586 }
587 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000588
Derek Jones269b9422008-01-28 21:00:20 +0000589 $att = '';
590 foreach ($default as $key => $val)
Derek Allardf3e8a352008-01-04 14:30:38 +0000591 {
Derek Jones269b9422008-01-28 21:00:20 +0000592 if ($key == 'value')
593 {
594 $val = form_prep($val);
595 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000596
Derek Jones269b9422008-01-28 21:00:20 +0000597 $att .= $key . '="' . $val . '" ';
598 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000599
Derek Jones269b9422008-01-28 21:00:20 +0000600 return $att;
601 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000602}
603
Derek Allard1e6ab992008-06-06 11:37:34 +0000604// ------------------------------------------------------------------------
605
606/**
607 * Attributes To String
608 *
609 * Helper function used by some of the form helpers
610 *
611 * @access private
612 * @param mixed
613 * @param bool
614 * @return string
615 */
616if ( ! function_exists('_attributes_to_string'))
617{
618 function _attributes_to_string($attributes, $formtag = FALSE)
619 {
620 if (is_string($attributes) AND strlen($attributes) > 0)
621 {
622 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
623 {
624 $attributes .= ' method="post"';
625 }
Derek Allard993925b2008-08-21 12:43:31 +0000626
627 return ' '.$attributes;
Derek Allard1e6ab992008-06-06 11:37:34 +0000628 }
629
630 if (is_object($attributes) AND count($attributes) > 0)
631 {
632 $attributes = (array)$attributes;
633 }
634
635 if (is_array($attributes) AND count($attributes) > 0)
636 {
637 $atts = '';
638
639 if ( ! isset($attributes['method']) AND $formtag === TRUE)
640 {
641 $atts .= ' method="post"';
642 }
643
644 foreach ($attributes as $key => $val)
645 {
646 $atts .= ' '.$key.'="'.$val.'"';
647 }
648
649 return $atts;
650 }
651 }
652}
Derek Jones0b59f272008-05-13 04:22:33 +0000653
654/* End of file form_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000655/* Location: ./system/helpers/form_helper.php */