blob: 21b7df42f4c2de19144fe52ad847044a0838b696 [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 = '';
Derek Allardc5074182008-07-21 11:58:56 +0000114
Derek Jones269b9422008-01-28 21:00:20 +0000115 foreach ($name as $name => $value)
116 {
117 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
118 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000119
Derek Jones269b9422008-01-28 21:00:20 +0000120 return $form;
121 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000122}
123
124// ------------------------------------------------------------------------
125
126/**
127 * Text Input Field
128 *
129 * @access public
130 * @param mixed
131 * @param string
132 * @param string
133 * @return string
134 */
Derek Jones0b59f272008-05-13 04:22:33 +0000135if ( ! function_exists('form_input'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000136{
Derek Jones269b9422008-01-28 21:00:20 +0000137 function form_input($data = '', $value = '', $extra = '')
138 {
Derek Allard3640a0b2008-06-19 07:46:22 +0000139 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000140
Derek Allardc5074182008-07-21 11:58:56 +0000141 return "<input ".parse_form_attributes($data, $defaults).$extra." />";
Derek Jones269b9422008-01-28 21:00:20 +0000142 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000143}
144
145// ------------------------------------------------------------------------
146
147/**
148 * Password Field
149 *
150 * Identical to the input function but adds the "password" type
151 *
152 * @access public
153 * @param mixed
154 * @param string
155 * @param string
156 * @return string
157 */
Derek Jones0b59f272008-05-13 04:22:33 +0000158if ( ! function_exists('form_password'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000159{
Derek Jones269b9422008-01-28 21:00:20 +0000160 function form_password($data = '', $value = '', $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000161 {
Derek Jones0b59f272008-05-13 04:22:33 +0000162 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000163 {
164 $data = array('name' => $data);
165 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000166
Derek Jones269b9422008-01-28 21:00:20 +0000167 $data['type'] = 'password';
168 return form_input($data, $value, $extra);
169 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000170}
171
172// ------------------------------------------------------------------------
173
174/**
175 * Upload Field
176 *
177 * Identical to the input function but adds the "file" type
178 *
179 * @access public
180 * @param mixed
181 * @param string
182 * @param string
183 * @return string
184 */
Derek Jones0b59f272008-05-13 04:22:33 +0000185if ( ! function_exists('form_upload'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000186{
Derek Jones269b9422008-01-28 21:00:20 +0000187 function form_upload($data = '', $value = '', $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000188 {
Derek Jones0b59f272008-05-13 04:22:33 +0000189 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000190 {
191 $data = array('name' => $data);
192 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000193
Derek Jones269b9422008-01-28 21:00:20 +0000194 $data['type'] = 'file';
195 return form_input($data, $value, $extra);
196 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000197}
198
199// ------------------------------------------------------------------------
200
201/**
202 * Textarea field
203 *
204 * @access public
205 * @param mixed
206 * @param string
207 * @param string
208 * @return string
209 */
Derek Jones0b59f272008-05-13 04:22:33 +0000210if ( ! function_exists('form_textarea'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000211{
Derek Jones269b9422008-01-28 21:00:20 +0000212 function form_textarea($data = '', $value = '', $extra = '')
213 {
Derek Jones0b59f272008-05-13 04:22:33 +0000214 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
Derek Allardf3e8a352008-01-04 14:30:38 +0000215
Derek Jones0b59f272008-05-13 04:22:33 +0000216 if ( ! is_array($data) OR ! isset($data['value']))
Derek Jones269b9422008-01-28 21:00:20 +0000217 {
218 $val = $value;
219 }
220 else
221 {
222 $val = $data['value'];
223 unset($data['value']); // textareas don't use the value attribute
224 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000225
Derek Allardc5074182008-07-21 11:58:56 +0000226 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>";
Derek Jones269b9422008-01-28 21:00:20 +0000227 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000228}
229
230// ------------------------------------------------------------------------
231
232/**
233 * Drop-down Menu
234 *
235 * @access public
236 * @param string
237 * @param array
238 * @param string
239 * @param string
240 * @return string
241 */
Derek Jones0b59f272008-05-13 04:22:33 +0000242if ( ! function_exists('form_dropdown'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000243{
Derek Jones269b9422008-01-28 21:00:20 +0000244 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
Derek Allard4021b512008-01-04 22:26:12 +0000245 {
Derek Jones0b59f272008-05-13 04:22:33 +0000246 if ( ! is_array($selected))
Derek Jones269b9422008-01-28 21:00:20 +0000247 {
248 $selected = array($selected);
249 }
Derek Allard4021b512008-01-04 22:26:12 +0000250
Derek Jones269b9422008-01-28 21:00:20 +0000251 if ($extra != '') $extra = ' '.$extra;
Derek Allardf8f05702008-01-18 14:39:23 +0000252
Derek Jones269b9422008-01-28 21:00:20 +0000253 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Derek Allard4021b512008-01-04 22:26:12 +0000254
Derek Jones269b9422008-01-28 21:00:20 +0000255 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000256
Derek Jones269b9422008-01-28 21:00:20 +0000257 foreach ($options as $key => $val)
258 {
259 $key = (string) $key;
260 $val = (string) $val;
Derek Allardf3e8a352008-01-04 14:30:38 +0000261
Derek Jones269b9422008-01-28 21:00:20 +0000262 $sel = (in_array($key, $selected))?' selected="selected"':'';
Derek Allardf3e8a352008-01-04 14:30:38 +0000263
Derek Jones269b9422008-01-28 21:00:20 +0000264 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
265 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000266
Derek Jones269b9422008-01-28 21:00:20 +0000267 $form .= '</select>';
Derek Allardf3e8a352008-01-04 14:30:38 +0000268
Derek Jones269b9422008-01-28 21:00:20 +0000269 return $form;
270 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000271}
272
273// ------------------------------------------------------------------------
274
275/**
276 * Checkbox Field
277 *
278 * @access public
279 * @param mixed
280 * @param string
281 * @param bool
282 * @param string
283 * @return string
284 */
Derek Jones0b59f272008-05-13 04:22:33 +0000285if ( ! function_exists('form_checkbox'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000286{
Derek Allardde7320b2008-05-06 13:51:02 +0000287 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000288 {
Derek Jones0b59f272008-05-13 04:22:33 +0000289 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000290
Derek Jones269b9422008-01-28 21:00:20 +0000291 if (is_array($data) AND array_key_exists('checked', $data))
292 {
293 $checked = $data['checked'];
294
295 if ($checked == FALSE)
296 {
297 unset($data['checked']);
298 }
299 else
300 {
301 $data['checked'] = 'checked';
302 }
303 }
304
305 if ($checked == TRUE)
Derek Allardc5074182008-07-21 11:58:56 +0000306 {
Derek Jones269b9422008-01-28 21:00:20 +0000307 $defaults['checked'] = 'checked';
Derek Allardc5074182008-07-21 11:58:56 +0000308 }
Derek Jones269b9422008-01-28 21:00:20 +0000309 else
Derek Allardc5074182008-07-21 11:58:56 +0000310 {
Derek Jones269b9422008-01-28 21:00:20 +0000311 unset($defaults['checked']);
Derek Allardc5074182008-07-21 11:58:56 +0000312 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000313
Derek Allardc5074182008-07-21 11:58:56 +0000314 return "<input ".parse_form_attributes($data, $defaults).$extra." />";
Derek Jones269b9422008-01-28 21:00:20 +0000315 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000316}
317
318// ------------------------------------------------------------------------
319
320/**
321 * Radio Button
322 *
323 * @access public
324 * @param mixed
325 * @param string
326 * @param bool
327 * @param string
328 * @return string
329 */
Derek Jones0b59f272008-05-13 04:22:33 +0000330if ( ! function_exists('form_radio'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000331{
Derek Allardde7320b2008-05-06 13:51:02 +0000332 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
Derek Jones269b9422008-01-28 21:00:20 +0000333 {
Derek Jones0b59f272008-05-13 04:22:33 +0000334 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000335 {
336 $data = array('name' => $data);
337 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000338
Derek Jones269b9422008-01-28 21:00:20 +0000339 $data['type'] = 'radio';
340 return form_checkbox($data, $value, $checked, $extra);
341 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000342}
343
344// ------------------------------------------------------------------------
345
346/**
347 * Submit Button
348 *
349 * @access public
350 * @param mixed
351 * @param string
352 * @param string
353 * @return string
Derek Jones269b9422008-01-28 21:00:20 +0000354 */
Derek Jones0b59f272008-05-13 04:22:33 +0000355if ( ! function_exists('form_submit'))
Derek Jones269b9422008-01-28 21:00:20 +0000356{
357 function form_submit($data = '', $value = '', $extra = '')
358 {
Derek Jones0b59f272008-05-13 04:22:33 +0000359 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000360
Derek Allardc5074182008-07-21 11:58:56 +0000361 return "<input ".parse_form_attributes($data, $defaults).$extra." />";
Derek Jones269b9422008-01-28 21:00:20 +0000362 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000363}
364
365// ------------------------------------------------------------------------
366
367/**
368 * Reset Button
369 *
370 * @access public
371 * @param mixed
372 * @param string
373 * @param string
374 * @return string
375 */
Derek Jones0b59f272008-05-13 04:22:33 +0000376if ( ! function_exists('form_reset'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000377{
Derek Jones269b9422008-01-28 21:00:20 +0000378 function form_reset($data = '', $value = '', $extra = '')
379 {
Derek Jones0b59f272008-05-13 04:22:33 +0000380 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000381
Derek Allardc5074182008-07-21 11:58:56 +0000382 return "<input ".parse_form_attributes($data, $defaults).$extra." />";
Derek Jones269b9422008-01-28 21:00:20 +0000383 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000384}
385
386// ------------------------------------------------------------------------
387
388/**
Derek Allard707d0e02008-03-18 11:50:00 +0000389 * Form Button
390 *
391 * @access public
392 * @param mixed
393 * @param string
394 * @param string
395 * @return string
396 */
Derek Jones0b59f272008-05-13 04:22:33 +0000397if ( ! function_exists('form_button'))
Derek Allard707d0e02008-03-18 11:50:00 +0000398{
399 function form_button($data = '', $content = '', $extra = '')
400 {
Derek Jones0b59f272008-05-13 04:22:33 +0000401 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'submit');
Derek Allard707d0e02008-03-18 11:50:00 +0000402
403 if ( is_array($data) AND isset($data['content']))
404 {
405 $content = $data['content'];
406 unset($data['content']); // content is not an attribute
407 }
408
Derek Allardc5074182008-07-21 11:58:56 +0000409 return "<button ".parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
Derek Allard707d0e02008-03-18 11:50:00 +0000410 }
411}
412
413// ------------------------------------------------------------------------
414
415/**
Derek Allardf3e8a352008-01-04 14:30:38 +0000416 * Form Label Tag
417 *
418 * @access public
419 * @param string The text to appear onscreen
420 * @param string The id the label applies to
421 * @param string Additional attributes
422 * @return string
423 */
Derek Jones0b59f272008-05-13 04:22:33 +0000424if ( ! function_exists('form_label'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000425{
Derek Jones269b9422008-01-28 21:00:20 +0000426 function form_label($label_text = '', $id = '', $attributes = array())
427 {
Derek Allardf3e8a352008-01-04 14:30:38 +0000428
Derek Jones269b9422008-01-28 21:00:20 +0000429 $label = '<label';
Derek Allardf3e8a352008-01-04 14:30:38 +0000430
Derek Jones269b9422008-01-28 21:00:20 +0000431 if ($id != '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000432 {
Derek Jones269b9422008-01-28 21:00:20 +0000433 $label .= " for=\"$id\"";
Derek Allardf3e8a352008-01-04 14:30:38 +0000434 }
Derek Jones269b9422008-01-28 21:00:20 +0000435
436 if (is_array($attributes) AND count($attributes) > 0)
437 {
438 foreach ($attributes as $key => $val)
439 {
440 $label .= ' '.$key.'="'.$val.'"';
441 }
442 }
443
444 $label .= ">$label_text</label>";
445
446 return $label;
Derek Allardf3e8a352008-01-04 14:30:38 +0000447 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000448}
449
450// ------------------------------------------------------------------------
451/**
452 * Fieldset Tag
453 *
454 * Used to produce <fieldset><legend>text</legend>. To close fieldset
455 * use form_fieldset_close()
456 *
457 * @access public
458 * @param string The legend text
459 * @param string Additional attributes
460 * @return string
461 */
Derek Jones0b59f272008-05-13 04:22:33 +0000462if ( ! function_exists('form_fieldset'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000463{
Derek Jones269b9422008-01-28 21:00:20 +0000464 function form_fieldset($legend_text = '', $attributes = array())
Derek Allardf3e8a352008-01-04 14:30:38 +0000465 {
Derek Jones269b9422008-01-28 21:00:20 +0000466 $fieldset = "<fieldset";
467
Derek Allard1e6ab992008-06-06 11:37:34 +0000468 $fieldset .= _attributes_to_string($attributes, FALSE);
Derek Allardf3e8a352008-01-04 14:30:38 +0000469
Derek Jones269b9422008-01-28 21:00:20 +0000470 $fieldset .= ">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000471
Derek Jones269b9422008-01-28 21:00:20 +0000472 if ($legend_text != '')
473 {
474 $fieldset .= "<legend>$legend_text</legend>\n";
475 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000476
Derek Jones269b9422008-01-28 21:00:20 +0000477 return $fieldset;
478 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000479}
480
481// ------------------------------------------------------------------------
482
483/**
484 * Fieldset Close Tag
485 *
486 * @access public
487 * @param string
488 * @return string
489 */
Derek Jones0b59f272008-05-13 04:22:33 +0000490if ( ! function_exists('form_fieldset_close'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000491{
Derek Jones269b9422008-01-28 21:00:20 +0000492 function form_fieldset_close($extra = '')
493 {
Derek Allardc5074182008-07-21 11:58:56 +0000494 return "</fieldset>".$extra;
Derek Jones269b9422008-01-28 21:00:20 +0000495 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000496}
497
498// ------------------------------------------------------------------------
499
500/**
501 * Form Close Tag
502 *
503 * @access public
504 * @param string
505 * @return string
506 */
Derek Jones0b59f272008-05-13 04:22:33 +0000507if ( ! function_exists('form_close'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000508{
Derek Jones269b9422008-01-28 21:00:20 +0000509 function form_close($extra = '')
510 {
Derek Allardc5074182008-07-21 11:58:56 +0000511 return "</form>".$extra;
Derek Jones269b9422008-01-28 21:00:20 +0000512 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000513}
514
515// ------------------------------------------------------------------------
516
517/**
518 * Form Prep
519 *
520 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
521 *
522 * @access public
523 * @param string
524 * @return string
525 */
Derek Jones0b59f272008-05-13 04:22:33 +0000526if ( ! function_exists('form_prep'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000527{
Derek Jones269b9422008-01-28 21:00:20 +0000528 function form_prep($str = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000529 {
Derek Jones269b9422008-01-28 21:00:20 +0000530 if ($str === '')
531 {
532 return '';
533 }
534
535 $temp = '__TEMP_AMPERSANDS__';
536
537 // Replace entities to temporary markers so that
538 // htmlspecialchars won't mess them up
539 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
540 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
541
542 $str = htmlspecialchars($str);
543
544 // In case htmlspecialchars misses these.
545 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
546
547 // Decode the temp markers back to entities
548 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
549 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
550
551 return $str;
Derek Allardf3e8a352008-01-04 14:30:38 +0000552 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000553}
554
555// ------------------------------------------------------------------------
556
557/**
558 * Parse the form attributes
559 *
560 * Helper function used by some of the form helpers
561 *
562 * @access private
563 * @param array
564 * @param array
565 * @return string
566 */
Derek Jones0b59f272008-05-13 04:22:33 +0000567if ( ! function_exists('parse_form_attributes'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000568{
Derek Jones269b9422008-01-28 21:00:20 +0000569 function parse_form_attributes($attributes, $default)
Derek Allardf3e8a352008-01-04 14:30:38 +0000570 {
Derek Jones269b9422008-01-28 21:00:20 +0000571 if (is_array($attributes))
Derek Allardf3e8a352008-01-04 14:30:38 +0000572 {
Derek Jones269b9422008-01-28 21:00:20 +0000573 foreach ($default as $key => $val)
Derek Allardf3e8a352008-01-04 14:30:38 +0000574 {
Derek Jones269b9422008-01-28 21:00:20 +0000575 if (isset($attributes[$key]))
576 {
577 $default[$key] = $attributes[$key];
578 unset($attributes[$key]);
579 }
580 }
581
582 if (count($attributes) > 0)
583 {
584 $default = array_merge($default, $attributes);
Derek Allardf3e8a352008-01-04 14:30:38 +0000585 }
586 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000587
Derek Jones269b9422008-01-28 21:00:20 +0000588 $att = '';
589 foreach ($default as $key => $val)
Derek Allardf3e8a352008-01-04 14:30:38 +0000590 {
Derek Jones269b9422008-01-28 21:00:20 +0000591 if ($key == 'value')
592 {
593 $val = form_prep($val);
594 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000595
Derek Jones269b9422008-01-28 21:00:20 +0000596 $att .= $key . '="' . $val . '" ';
597 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000598
Derek Jones269b9422008-01-28 21:00:20 +0000599 return $att;
600 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000601}
602
Derek Allard1e6ab992008-06-06 11:37:34 +0000603// ------------------------------------------------------------------------
604
605/**
606 * Attributes To String
607 *
608 * Helper function used by some of the form helpers
609 *
610 * @access private
611 * @param mixed
612 * @param bool
613 * @return string
614 */
615if ( ! function_exists('_attributes_to_string'))
616{
617 function _attributes_to_string($attributes, $formtag = FALSE)
618 {
619 if (is_string($attributes) AND strlen($attributes) > 0)
620 {
621 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
622 {
623 $attributes .= ' method="post"';
624 }
625
626 return ' '.$attributes;
627 }
628
629 if (is_object($attributes) AND count($attributes) > 0)
630 {
631 $attributes = (array)$attributes;
632 }
633
634 if (is_array($attributes) AND count($attributes) > 0)
635 {
636 $atts = '';
637
638 if ( ! isset($attributes['method']) AND $formtag === TRUE)
639 {
640 $atts .= ' method="post"';
641 }
642
643 foreach ($attributes as $key => $val)
644 {
645 $atts .= ' '.$key.'="'.$val.'"';
646 }
647
648 return $atts;
649 }
650 }
651}
Derek Jones0b59f272008-05-13 04:22:33 +0000652
653/* End of file form_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000654/* Location: ./system/helpers/form_helper.php */