blob: a96adb97dc55b4280b40a9868180d3af348537fa [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
Rick Ellis43e0fbb2008-08-26 19:22: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
Rick Ellis43e0fbb2008-08-26 19:22:31 +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
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000217 if ( ! is_array($data) OR ! isset($data['value']))
Derek Jones269b9422008-01-28 21:00:20 +0000218 {
219 $val = $value;
220 }
Rick Ellis43e0fbb2008-08-26 19:22: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
Rick Ellis43e0fbb2008-08-26 19:22:31 +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 }
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000251
252 // If no selected state was submitted we will attempt to set it automatically
253 if (count($selected) === 0)
254 {
255 // If the form name appears in the $_POST array we have a winner!
256 if (isset($_POST[$name]))
257 {
258 $selected = array($_POST[$name]);
259 }
260 }
Derek Allard4021b512008-01-04 22:26:12 +0000261
Derek Jones269b9422008-01-28 21:00:20 +0000262 if ($extra != '') $extra = ' '.$extra;
Derek Allardf8f05702008-01-18 14:39:23 +0000263
Derek Jones269b9422008-01-28 21:00:20 +0000264 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Derek Allard4021b512008-01-04 22:26:12 +0000265
Derek Jones269b9422008-01-28 21:00:20 +0000266 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000267
Derek Jones269b9422008-01-28 21:00:20 +0000268 foreach ($options as $key => $val)
269 {
270 $key = (string) $key;
271 $val = (string) $val;
Derek Allardf3e8a352008-01-04 14:30:38 +0000272
Derek Jones269b9422008-01-28 21:00:20 +0000273 $sel = (in_array($key, $selected))?' selected="selected"':'';
Derek Allardf3e8a352008-01-04 14:30:38 +0000274
Derek Jones269b9422008-01-28 21:00:20 +0000275 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
276 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000277
Derek Jones269b9422008-01-28 21:00:20 +0000278 $form .= '</select>';
Derek Allardf3e8a352008-01-04 14:30:38 +0000279
Derek Jones269b9422008-01-28 21:00:20 +0000280 return $form;
281 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000282}
283
284// ------------------------------------------------------------------------
285
286/**
287 * Checkbox Field
288 *
289 * @access public
290 * @param mixed
291 * @param string
292 * @param bool
293 * @param string
294 * @return string
295 */
Derek Jones0b59f272008-05-13 04:22:33 +0000296if ( ! function_exists('form_checkbox'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000297{
Derek Allardde7320b2008-05-06 13:51:02 +0000298 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000299 {
Derek Jones0b59f272008-05-13 04:22:33 +0000300 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000301
Derek Jones269b9422008-01-28 21:00:20 +0000302 if (is_array($data) AND array_key_exists('checked', $data))
303 {
304 $checked = $data['checked'];
305
306 if ($checked == FALSE)
307 {
308 unset($data['checked']);
309 }
310 else
311 {
312 $data['checked'] = 'checked';
313 }
314 }
315
316 if ($checked == TRUE)
Derek Allardc5074182008-07-21 11:58:56 +0000317 {
Derek Jones269b9422008-01-28 21:00:20 +0000318 $defaults['checked'] = 'checked';
Derek Allardc5074182008-07-21 11:58:56 +0000319 }
Derek Jones269b9422008-01-28 21:00:20 +0000320 else
Derek Allardc5074182008-07-21 11:58:56 +0000321 {
Derek Jones269b9422008-01-28 21:00:20 +0000322 unset($defaults['checked']);
Derek Allardc5074182008-07-21 11:58:56 +0000323 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000324
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000325 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
Derek Jones269b9422008-01-28 21:00:20 +0000326 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000327}
328
329// ------------------------------------------------------------------------
330
331/**
332 * Radio Button
333 *
334 * @access public
335 * @param mixed
336 * @param string
337 * @param bool
338 * @param string
339 * @return string
340 */
Derek Jones0b59f272008-05-13 04:22:33 +0000341if ( ! function_exists('form_radio'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000342{
Derek Allardde7320b2008-05-06 13:51:02 +0000343 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
Derek Jones269b9422008-01-28 21:00:20 +0000344 {
Derek Jones0b59f272008-05-13 04:22:33 +0000345 if ( ! is_array($data))
Derek Jones269b9422008-01-28 21:00:20 +0000346 {
347 $data = array('name' => $data);
348 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000349
Derek Jones269b9422008-01-28 21:00:20 +0000350 $data['type'] = 'radio';
351 return form_checkbox($data, $value, $checked, $extra);
352 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000353}
354
355// ------------------------------------------------------------------------
356
357/**
358 * Submit Button
359 *
360 * @access public
361 * @param mixed
362 * @param string
363 * @param string
364 * @return string
Derek Jones269b9422008-01-28 21:00:20 +0000365 */
Derek Jones0b59f272008-05-13 04:22:33 +0000366if ( ! function_exists('form_submit'))
Derek Jones269b9422008-01-28 21:00:20 +0000367{
368 function form_submit($data = '', $value = '', $extra = '')
369 {
Derek Jones0b59f272008-05-13 04:22:33 +0000370 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000371
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000372 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
Derek Jones269b9422008-01-28 21:00:20 +0000373 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000374}
375
376// ------------------------------------------------------------------------
377
378/**
379 * Reset 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_reset'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000388{
Derek Jones269b9422008-01-28 21:00:20 +0000389 function form_reset($data = '', $value = '', $extra = '')
390 {
Derek Jones0b59f272008-05-13 04:22:33 +0000391 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Derek Allardf3e8a352008-01-04 14:30:38 +0000392
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000393 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
Derek Jones269b9422008-01-28 21:00:20 +0000394 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000395}
396
397// ------------------------------------------------------------------------
398
399/**
Derek Allard707d0e02008-03-18 11:50:00 +0000400 * Form Button
401 *
402 * @access public
403 * @param mixed
404 * @param string
405 * @param string
406 * @return string
407 */
Derek Jones0b59f272008-05-13 04:22:33 +0000408if ( ! function_exists('form_button'))
Derek Allard707d0e02008-03-18 11:50:00 +0000409{
410 function form_button($data = '', $content = '', $extra = '')
411 {
Derek Jones0b59f272008-05-13 04:22:33 +0000412 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'submit');
Derek Allard707d0e02008-03-18 11:50:00 +0000413
414 if ( is_array($data) AND isset($data['content']))
415 {
416 $content = $data['content'];
417 unset($data['content']); // content is not an attribute
418 }
419
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000420 return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
Derek Allard707d0e02008-03-18 11:50:00 +0000421 }
422}
423
424// ------------------------------------------------------------------------
425
426/**
Derek Allardf3e8a352008-01-04 14:30:38 +0000427 * Form Label Tag
428 *
429 * @access public
430 * @param string The text to appear onscreen
431 * @param string The id the label applies to
432 * @param string Additional attributes
433 * @return string
434 */
Derek Jones0b59f272008-05-13 04:22:33 +0000435if ( ! function_exists('form_label'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000436{
Derek Jones269b9422008-01-28 21:00:20 +0000437 function form_label($label_text = '', $id = '', $attributes = array())
438 {
Derek Allardf3e8a352008-01-04 14:30:38 +0000439
Derek Jones269b9422008-01-28 21:00:20 +0000440 $label = '<label';
Derek Allardf3e8a352008-01-04 14:30:38 +0000441
Derek Jones269b9422008-01-28 21:00:20 +0000442 if ($id != '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000443 {
Derek Jones269b9422008-01-28 21:00:20 +0000444 $label .= " for=\"$id\"";
Derek Allardf3e8a352008-01-04 14:30:38 +0000445 }
Derek Jones269b9422008-01-28 21:00:20 +0000446
447 if (is_array($attributes) AND count($attributes) > 0)
448 {
449 foreach ($attributes as $key => $val)
450 {
451 $label .= ' '.$key.'="'.$val.'"';
452 }
453 }
454
455 $label .= ">$label_text</label>";
456
457 return $label;
Derek Allardf3e8a352008-01-04 14:30:38 +0000458 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000459}
460
461// ------------------------------------------------------------------------
462/**
463 * Fieldset Tag
464 *
465 * Used to produce <fieldset><legend>text</legend>. To close fieldset
466 * use form_fieldset_close()
467 *
468 * @access public
469 * @param string The legend text
470 * @param string Additional attributes
471 * @return string
472 */
Derek Jones0b59f272008-05-13 04:22:33 +0000473if ( ! function_exists('form_fieldset'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000474{
Derek Jones269b9422008-01-28 21:00:20 +0000475 function form_fieldset($legend_text = '', $attributes = array())
Derek Allardf3e8a352008-01-04 14:30:38 +0000476 {
Derek Jones269b9422008-01-28 21:00:20 +0000477 $fieldset = "<fieldset";
478
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000479 $fieldset .= _attributes_to_string($attributes, FALSE);
Derek Allardf3e8a352008-01-04 14:30:38 +0000480
Derek Jones269b9422008-01-28 21:00:20 +0000481 $fieldset .= ">\n";
Derek Allardf3e8a352008-01-04 14:30:38 +0000482
Derek Jones269b9422008-01-28 21:00:20 +0000483 if ($legend_text != '')
484 {
485 $fieldset .= "<legend>$legend_text</legend>\n";
486 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000487
Derek Jones269b9422008-01-28 21:00:20 +0000488 return $fieldset;
489 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000490}
491
492// ------------------------------------------------------------------------
493
494/**
495 * Fieldset Close Tag
496 *
497 * @access public
498 * @param string
499 * @return string
500 */
Derek Jones0b59f272008-05-13 04:22:33 +0000501if ( ! function_exists('form_fieldset_close'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000502{
Derek Jones269b9422008-01-28 21:00:20 +0000503 function form_fieldset_close($extra = '')
504 {
Derek Allardc5074182008-07-21 11:58:56 +0000505 return "</fieldset>".$extra;
Derek Jones269b9422008-01-28 21:00:20 +0000506 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000507}
508
509// ------------------------------------------------------------------------
510
511/**
512 * Form Close Tag
513 *
514 * @access public
515 * @param string
516 * @return string
517 */
Derek Jones0b59f272008-05-13 04:22:33 +0000518if ( ! function_exists('form_close'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000519{
Derek Jones269b9422008-01-28 21:00:20 +0000520 function form_close($extra = '')
521 {
Derek Allardc5074182008-07-21 11:58:56 +0000522 return "</form>".$extra;
Derek Jones269b9422008-01-28 21:00:20 +0000523 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000524}
525
526// ------------------------------------------------------------------------
527
528/**
529 * Form Prep
530 *
531 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
532 *
533 * @access public
534 * @param string
535 * @return string
536 */
Derek Jones0b59f272008-05-13 04:22:33 +0000537if ( ! function_exists('form_prep'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000538{
Derek Jones269b9422008-01-28 21:00:20 +0000539 function form_prep($str = '')
Derek Allardf3e8a352008-01-04 14:30:38 +0000540 {
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000541 // if the field name is an array we do this recursively
542 if (is_array($str))
543 {
544 foreach ($str as $key => $val)
545 {
546 $str[$key] = form_prep($val);
547 }
548
549 return $str;
550 }
551
Derek Jones269b9422008-01-28 21:00:20 +0000552 if ($str === '')
553 {
554 return '';
555 }
556
557 $temp = '__TEMP_AMPERSANDS__';
558
559 // Replace entities to temporary markers so that
560 // htmlspecialchars won't mess them up
561 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
562 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
563
564 $str = htmlspecialchars($str);
565
566 // In case htmlspecialchars misses these.
567 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
568
569 // Decode the temp markers back to entities
570 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
571 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
572
573 return $str;
Derek Allardf3e8a352008-01-04 14:30:38 +0000574 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000575}
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000576
577// ------------------------------------------------------------------------
578
579/**
580 * Form Value
581 *
582 * Grabs a value from the POST array for the specified field so you can
583 * re-populate an input field or textarea. If Form Validation
584 * is active it retrieves the info from the validation class
585 *
586 * @access public
587 * @param string
588 * @return mixed
589 */
590if ( ! function_exists('set_value'))
591{
592 function set_value($field = '', $default = '')
593 {
594 if (FALSE === ($OBJ =& _get_validation_object()))
595 {
596 if ( ! isset($_POST[$field]))
597 {
598 return $default;
599 }
600
601 return $_POST[$field];
602 }
603
604 return $OBJ->set_value($field, $default);
605 }
606}
607
608// ------------------------------------------------------------------------
609
610/**
611 * Set Select
612 *
613 * Let's you set the selected value of a <select> menu via data in the POST array.
614 * If Form Validation is active it retrieves the info from the validation class
615 *
616 * @access public
617 * @param string
618 * @param string
619 * @param bool
620 * @return string
621 */
622if ( ! function_exists('set_select'))
623{
624 function set_select($field = '', $value = '', $default = FALSE)
625 {
626 $OBJ =& _get_validation_object();
627
628 if ($OBJ === FALSE)
629 {
630 if ( ! isset($_POST[$field]))
631 {
632 if (count($_POST) === 0)
633 {
634 return ' selected="selected"';
635 }
636 return '';
637 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000638
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000639 $field = $_POST[$field];
640
641 if (is_array($field))
642 {
643 if ( ! in_array($value, $field, TRUE))
644 {
645 return '';
646 }
647 }
648 else
649 {
650 if (($field == '' OR $value == '') OR ($field != $value))
651 {
652 return '';
653 }
654 }
655
656 return ' selected="selected"';
657 }
658
659 return $OBJ->set_select($field, $value, $default);
660 }
661}
662
663// ------------------------------------------------------------------------
664
665/**
666 * Set Checkbox
667 *
668 * Let's you set the selected value of a checkbox via the value in the POST array.
669 * If Form Validation is active it retrieves the info from the validation class
670 *
671 * @access public
672 * @param string
673 * @param string
674 * @param bool
675 * @return string
676 */
677if ( ! function_exists('set_checkbox'))
678{
679 function set_checkbox($field = '', $value = '', $default = FALSE)
680 {
681 $OBJ =& _get_validation_object();
682
683 if ($OBJ === FALSE)
684 {
685 if ( ! isset($_POST[$field]))
686 {
687 if (count($_POST) === 0)
688 {
689 return ' checked="checked"';
690 }
691 return '';
692 }
693
694 $field = $_POST[$field];
695
696 if (is_array($field))
697 {
698 if ( ! in_array($value, $field, TRUE))
699 {
700 return '';
701 }
702 }
703 else
704 {
705 if (($field == '' OR $value == '') OR ($field != $value))
706 {
707 return '';
708 }
709 }
710
711 return ' checked="checked"';
712 }
713
714 return $OBJ->set_checkbox($field, $value, $default);
715 }
716}
717
718// ------------------------------------------------------------------------
719
720/**
721 * Set Radio
722 *
723 * Let's you set the selected value of a radio field via info in the POST array.
724 * If Form Validation is active it retrieves the info from the validation class
725 *
726 * @access public
727 * @param string
728 * @param string
729 * @param bool
730 * @return string
731 */
732if ( ! function_exists('set_radio'))
733{
734 function set_radio($field = '', $value = '', $default = FALSE)
735 {
736 $OBJ =& _get_validation_object();
737
738 if ($OBJ === FALSE)
739 {
740 if ( ! isset($_POST[$field]))
741 {
742 if (count($_POST) === 0)
743 {
744 return ' checked="checked"';
745 }
746 return '';
747 }
748
749 $field = $_POST[$field];
750
751 if (is_array($field))
752 {
753 if ( ! in_array($value, $field, TRUE))
754 {
755 return '';
756 }
757 }
758 else
759 {
760 if (($field == '' OR $value == '') OR ($field != $value))
761 {
762 return '';
763 }
764 }
765
766 return ' checked="checked"';
767 }
768
769 return $OBJ->set_radio($field, $value, $default);
770 }
771}
772
773// ------------------------------------------------------------------------
774
775/**
776 * Form Error
777 *
778 * Returns the error for a specific form field. This is a helper for the
779 * form validation class.
780 *
781 * @access public
782 * @param string
783 * @param string
784 * @param string
785 * @return string
786 */
787if ( ! function_exists('form_error'))
788{
789 function form_error($field = '', $prefix = '', $suffix = '')
790 {
791 if (FALSE === ($OBJ =& _get_validation_object()))
792 {
793 return '';
794 }
795
796 return $OBJ->error($field, $prefix, $suffix);
797 }
798}
799
800// ------------------------------------------------------------------------
801
802/**
803 * Validation Error String
804 *
805 * Returns all the errors associated with a form submission. This is a helper
806 * function for the form validation class.
807 *
808 * @access public
809 * @param string
810 * @param string
811 * @return string
812 */
813if ( ! function_exists('validation_errors'))
814{
815 function validation_errors($prefix = '', $suffix = '')
816 {
817 if (FALSE === ($OBJ =& _get_validation_object()))
818 {
819 return '';
820 }
821
822 return $OBJ->error_string($prefix, $suffix);
823 }
824}
825
Derek Allardf3e8a352008-01-04 14:30:38 +0000826// ------------------------------------------------------------------------
827
828/**
829 * Parse the form attributes
830 *
831 * Helper function used by some of the form helpers
832 *
833 * @access private
834 * @param array
835 * @param array
836 * @return string
837 */
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000838if ( ! function_exists('_parse_form_attributes'))
Derek Allardf3e8a352008-01-04 14:30:38 +0000839{
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000840 function _parse_form_attributes($attributes, $default)
Derek Allardf3e8a352008-01-04 14:30:38 +0000841 {
Derek Jones269b9422008-01-28 21:00:20 +0000842 if (is_array($attributes))
Derek Allardf3e8a352008-01-04 14:30:38 +0000843 {
Derek Jones269b9422008-01-28 21:00:20 +0000844 foreach ($default as $key => $val)
Derek Allardf3e8a352008-01-04 14:30:38 +0000845 {
Derek Jones269b9422008-01-28 21:00:20 +0000846 if (isset($attributes[$key]))
847 {
848 $default[$key] = $attributes[$key];
849 unset($attributes[$key]);
850 }
851 }
852
853 if (count($attributes) > 0)
854 {
855 $default = array_merge($default, $attributes);
Derek Allardf3e8a352008-01-04 14:30:38 +0000856 }
857 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000858
Derek Jones269b9422008-01-28 21:00:20 +0000859 $att = '';
860 foreach ($default as $key => $val)
Derek Allardf3e8a352008-01-04 14:30:38 +0000861 {
Derek Jones269b9422008-01-28 21:00:20 +0000862 if ($key == 'value')
863 {
864 $val = form_prep($val);
865 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000866
Derek Jones269b9422008-01-28 21:00:20 +0000867 $att .= $key . '="' . $val . '" ';
868 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000869
Derek Jones269b9422008-01-28 21:00:20 +0000870 return $att;
871 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000872}
873
Derek Allard1e6ab992008-06-06 11:37:34 +0000874// ------------------------------------------------------------------------
875
876/**
877 * Attributes To String
878 *
879 * Helper function used by some of the form helpers
880 *
881 * @access private
882 * @param mixed
883 * @param bool
884 * @return string
885 */
886if ( ! function_exists('_attributes_to_string'))
887{
888 function _attributes_to_string($attributes, $formtag = FALSE)
889 {
890 if (is_string($attributes) AND strlen($attributes) > 0)
891 {
892 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
893 {
894 $attributes .= ' method="post"';
895 }
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000896
897 return ' '.$attributes;
Derek Allard1e6ab992008-06-06 11:37:34 +0000898 }
899
900 if (is_object($attributes) AND count($attributes) > 0)
901 {
902 $attributes = (array)$attributes;
903 }
904
905 if (is_array($attributes) AND count($attributes) > 0)
906 {
907 $atts = '';
908
909 if ( ! isset($attributes['method']) AND $formtag === TRUE)
910 {
911 $atts .= ' method="post"';
912 }
913
914 foreach ($attributes as $key => $val)
915 {
916 $atts .= ' '.$key.'="'.$val.'"';
917 }
918
919 return $atts;
920 }
921 }
922}
Derek Jones0b59f272008-05-13 04:22:33 +0000923
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000924// ------------------------------------------------------------------------
925
926/**
927 * Validation Object
928 *
929 * Determines what the form validation class was instantiated as, fetches
930 * the object and returns it.
931 *
932 * @access private
933 * @return mixed
934 */
935if ( ! function_exists('_get_validation_object'))
936{
937 function &_get_validation_object()
938 {
939 $CI =& get_instance();
940
941 // We set this as a variable since we're returning by reference
942 $return = FALSE;
943
944 if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
945 {
946 return $return;
947 }
948
949 $object = $CI->load->_ci_classes['form_validation'];
950
951 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
952 {
953 return $return;
954 }
955
956 return $CI->$object;
957 }
958}
959
960
Derek Jones0b59f272008-05-13 04:22:33 +0000961/* End of file form_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000962/* Location: ./system/helpers/form_helper.php */