blob: fc2592241f8067e0b45184a74c27668182a4ba57 [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
Rick Ellisdd1e3232008-09-12 23:33:20 +00009 * @copyright Copyright (c) 2008, 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 Ellisc3907c62008-08-26 20:55:47 +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 Allard39f6f5a2008-09-22 16:06:06 +000064
Derek Jones269b9422008-01-28 21:00:20 +000065 return $form;
66 }
Derek Allardf3e8a352008-01-04 14:30:38 +000067}
Derek Allard39f6f5a2008-09-22 16:06:06 +000068
Derek Allardf3e8a352008-01-04 14:30:38 +000069// ------------------------------------------------------------------------
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
Derek Allard39f6f5a2008-09-22 16:06:06 +000081 */
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}
Derek Allard39f6f5a2008-09-22 16:06:06 +000090
Derek Allardf3e8a352008-01-04 14:30:38 +000091// ------------------------------------------------------------------------
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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000103 */
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 Allard39f6f5a2008-09-22 16:06:06 +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 Allard39f6f5a2008-09-22 16:06:06 +0000120
Derek Jones269b9422008-01-28 21:00:20 +0000121 return $form;
122 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000123}
Derek Allard39f6f5a2008-09-22 16:06:06 +0000124
Derek Allardf3e8a352008-01-04 14:30:38 +0000125// ------------------------------------------------------------------------
126
127/**
128 * Text Input Field
129 *
130 * @access public
131 * @param mixed
132 * @param string
133 * @param string
134 * @return string
Derek Allard39f6f5a2008-09-22 16:06:06 +0000135 */
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}
Derek Allard39f6f5a2008-09-22 16:06:06 +0000145
Derek Allardf3e8a352008-01-04 14:30:38 +0000146// ------------------------------------------------------------------------
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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000158 */
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}
Derek Allard39f6f5a2008-09-22 16:06:06 +0000172
Derek Allardf3e8a352008-01-04 14:30:38 +0000173// ------------------------------------------------------------------------
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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000185 */
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}
Derek Allard39f6f5a2008-09-22 16:06:06 +0000199
Derek Allardf3e8a352008-01-04 14:30:38 +0000200// ------------------------------------------------------------------------
201
202/**
203 * Textarea field
204 *
205 * @access public
206 * @param mixed
207 * @param string
208 * @param string
209 * @return string
Derek Allard39f6f5a2008-09-22 16:06:06 +0000210 */
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 Allard39f6f5a2008-09-22 16:06:06 +0000216
Rick Ellisc3907c62008-08-26 20:55:47 +0000217 if ( ! is_array($data) OR ! isset($data['value']))
Derek Jones269b9422008-01-28 21:00:20 +0000218 {
219 $val = $value;
220 }
Rick Ellisc3907c62008-08-26 20:55:47 +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 Allard39f6f5a2008-09-22 16:06:06 +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}
Derek Allard39f6f5a2008-09-22 16:06:06 +0000230
Derek Allardf3e8a352008-01-04 14:30:38 +0000231// ------------------------------------------------------------------------
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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000242 */
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 Allard39f6f5a2008-09-22 16:06:06 +0000251
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000252 // 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 Allard39f6f5a2008-09-22 16:06:06 +0000272
Derek Jones269b9422008-01-28 21:00:20 +0000273 $sel = (in_array($key, $selected))?' selected="selected"':'';
Derek Allard39f6f5a2008-09-22 16:06:06 +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 Allard39f6f5a2008-09-22 16:06:06 +0000279
Derek Jones269b9422008-01-28 21:00:20 +0000280 return $form;
281 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000282}
Derek Allard39f6f5a2008-09-22 16:06:06 +0000283
Derek Allardf3e8a352008-01-04 14:30:38 +0000284// ------------------------------------------------------------------------
285
286/**
287 * Checkbox Field
288 *
289 * @access public
290 * @param mixed
291 * @param string
292 * @param bool
293 * @param string
294 * @return string
Derek Allard39f6f5a2008-09-22 16:06:06 +0000295 */
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 Allard39f6f5a2008-09-22 16:06:06 +0000301
Derek Jones269b9422008-01-28 21:00:20 +0000302 if (is_array($data) AND array_key_exists('checked', $data))
303 {
304 $checked = $data['checked'];
Derek Allard39f6f5a2008-09-22 16:06:06 +0000305
Derek Jones269b9422008-01-28 21:00:20 +0000306 if ($checked == FALSE)
307 {
308 unset($data['checked']);
309 }
310 else
311 {
312 $data['checked'] = 'checked';
313 }
314 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000315
Derek Jones269b9422008-01-28 21:00:20 +0000316 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}
Derek Allard39f6f5a2008-09-22 16:06:06 +0000328
Derek Allardf3e8a352008-01-04 14:30:38 +0000329// ------------------------------------------------------------------------
330
331/**
332 * Radio Button
333 *
334 * @access public
335 * @param mixed
336 * @param string
337 * @param bool
338 * @param string
339 * @return string
Derek Allard39f6f5a2008-09-22 16:06:06 +0000340 */
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}
Derek Allard39f6f5a2008-09-22 16:06:06 +0000354
Derek Allardf3e8a352008-01-04 14:30:38 +0000355// ------------------------------------------------------------------------
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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000386 */
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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000407 */
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 Allard39f6f5a2008-09-22 16:06:06 +0000413
Derek Allard707d0e02008-03-18 11:50:00 +0000414 if ( is_array($data) AND isset($data['content']))
415 {
416 $content = $data['content'];
417 unset($data['content']); // content is not an attribute
418 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000419
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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000434 */
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 Allard39f6f5a2008-09-22 16:06:06 +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 Allard39f6f5a2008-09-22 16:06:06 +0000446
Derek Jones269b9422008-01-28 21:00:20 +0000447 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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000472 */
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 Ellisc3907c62008-08-26 20:55:47 +0000479 $fieldset .= _attributes_to_string($attributes, FALSE);
Derek Allard39f6f5a2008-09-22 16:06:06 +0000480
Derek Jones269b9422008-01-28 21:00:20 +0000481 $fieldset .= ">\n";
Derek Allard39f6f5a2008-09-22 16:06:06 +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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000500 */
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}
Derek Allard39f6f5a2008-09-22 16:06:06 +0000508
Derek Allardf3e8a352008-01-04 14:30:38 +0000509// ------------------------------------------------------------------------
510
511/**
512 * Form Close Tag
513 *
514 * @access public
515 * @param string
516 * @return string
Derek Allard39f6f5a2008-09-22 16:06:06 +0000517 */
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}
Derek Allard39f6f5a2008-09-22 16:06:06 +0000525
Derek Allardf3e8a352008-01-04 14:30:38 +0000526// ------------------------------------------------------------------------
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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000536 */
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 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000548
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000549 return $str;
550 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000551
Derek Jones269b9422008-01-28 21:00:20 +0000552 if ($str === '')
553 {
554 return '';
555 }
556
557 $temp = '__TEMP_AMPERSANDS__';
Derek Allard39f6f5a2008-09-22 16:06:06 +0000558
Derek Jones269b9422008-01-28 21:00:20 +0000559 // 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.
Derek Allard39f6f5a2008-09-22 16:06:06 +0000567 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
568
Derek Jones269b9422008-01-28 21:00:20 +0000569 // Decode the temp markers back to entities
570 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
Derek Allard39f6f5a2008-09-22 16:06:06 +0000571 $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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000589 */
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000590if ( ! 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 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000600
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000601 return $_POST[$field];
602 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000603
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000604 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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000621 */
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000622if ( ! 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 Allard39f6f5a2008-09-22 16:06:06 +0000638
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000639 $field = $_POST[$field];
Derek Allard39f6f5a2008-09-22 16:06:06 +0000640
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000641 if (is_array($field))
642 {
Rick Ellis7129d452008-09-20 04:22:25 +0000643 if ( ! in_array($value, $field))
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000644 {
645 return '';
646 }
647 }
648 else
649 {
650 if (($field == '' OR $value == '') OR ($field != $value))
651 {
652 return '';
653 }
654 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000655
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000656 return ' selected="selected"';
657 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000658
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000659 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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000676 */
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000677if ( ! function_exists('set_checkbox'))
678{
679 function set_checkbox($field = '', $value = '', $default = FALSE)
680 {
681 $OBJ =& _get_validation_object();
682
683 if ($OBJ === FALSE)
Rick Ellis7129d452008-09-20 04:22:25 +0000684 {
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000685 if ( ! isset($_POST[$field]))
686 {
687 if (count($_POST) === 0)
688 {
689 return ' checked="checked"';
690 }
691 return '';
692 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000693
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000694 $field = $_POST[$field];
695
696 if (is_array($field))
697 {
Rick Ellis7129d452008-09-20 04:22:25 +0000698 if ( ! in_array($value, $field))
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000699 {
700 return '';
701 }
702 }
703 else
704 {
705 if (($field == '' OR $value == '') OR ($field != $value))
706 {
707 return '';
708 }
709 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000710
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000711 return ' checked="checked"';
712 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000713
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000714 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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000731 */
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000732if ( ! 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 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000748
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000749 $field = $_POST[$field];
750
751 if (is_array($field))
752 {
Rick Ellis7129d452008-09-20 04:22:25 +0000753 if ( ! in_array($value, $field))
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000754 {
755 return '';
756 }
757 }
758 else
759 {
760 if (($field == '' OR $value == '') OR ($field != $value))
761 {
762 return '';
763 }
764 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000765
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000766 return ' checked="checked"';
767 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000768
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000769 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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000786 */
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000787if ( ! function_exists('form_error'))
788{
789 function form_error($field = '', $prefix = '', $suffix = '')
790 {
791 if (FALSE === ($OBJ =& _get_validation_object()))
792 {
793 return '';
794 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000795
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000796 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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000812 */
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000813if ( ! function_exists('validation_errors'))
814{
815 function validation_errors($prefix = '', $suffix = '')
816 {
817 if (FALSE === ($OBJ =& _get_validation_object()))
818 {
819 return '';
820 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000821
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000822 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
Derek Allard39f6f5a2008-09-22 16:06:06 +0000837 */
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 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000852
Derek Jones269b9422008-01-28 21:00:20 +0000853 if (count($attributes) > 0)
Derek Allard39f6f5a2008-09-22 16:06:06 +0000854 {
Derek Jones269b9422008-01-28 21:00:20 +0000855 $default = array_merge($default, $attributes);
Derek Allardf3e8a352008-01-04 14:30:38 +0000856 }
857 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000858
Derek Jones269b9422008-01-28 21:00:20 +0000859 $att = '';
Derek Allard39f6f5a2008-09-22 16:06:06 +0000860
Derek Jones269b9422008-01-28 21:00:20 +0000861 foreach ($default as $key => $val)
Derek Allardf3e8a352008-01-04 14:30:38 +0000862 {
Derek Jones269b9422008-01-28 21:00:20 +0000863 if ($key == 'value')
864 {
865 $val = form_prep($val);
866 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000867
Derek Jones269b9422008-01-28 21:00:20 +0000868 $att .= $key . '="' . $val . '" ';
869 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000870
Derek Jones269b9422008-01-28 21:00:20 +0000871 return $att;
872 }
Derek Allardf3e8a352008-01-04 14:30:38 +0000873}
874
Derek Allard1e6ab992008-06-06 11:37:34 +0000875// ------------------------------------------------------------------------
876
877/**
878 * Attributes To String
879 *
880 * Helper function used by some of the form helpers
881 *
882 * @access private
883 * @param mixed
884 * @param bool
885 * @return string
Derek Allard39f6f5a2008-09-22 16:06:06 +0000886 */
Derek Allard1e6ab992008-06-06 11:37:34 +0000887if ( ! function_exists('_attributes_to_string'))
888{
889 function _attributes_to_string($attributes, $formtag = FALSE)
890 {
Derek Allard39f6f5a2008-09-22 16:06:06 +0000891 if (is_string($attributes) AND strlen($attributes) > 0)
892 {
893 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
894 {
895 $attributes .= ' method="post"';
896 }
Derek Allard1e6ab992008-06-06 11:37:34 +0000897
Derek Allard39f6f5a2008-09-22 16:06:06 +0000898 return ' '.$attributes;
899 }
Derek Allard1e6ab992008-06-06 11:37:34 +0000900
Derek Allard39f6f5a2008-09-22 16:06:06 +0000901 if (is_object($attributes) AND count($attributes) > 0)
902 {
903 $attributes = (array)$attributes;
904 }
Derek Allard1e6ab992008-06-06 11:37:34 +0000905
Derek Allard39f6f5a2008-09-22 16:06:06 +0000906 if (is_array($attributes) AND count($attributes) > 0)
907 {
908 $atts = '';
909
910 if ( ! isset($attributes['method']) AND $formtag === TRUE)
911 {
912 $atts .= ' method="post"';
913 }
914
915 foreach ($attributes as $key => $val)
916 {
917 $atts .= ' '.$key.'="'.$val.'"';
918 }
919
920 return $atts;
921 }
922 }
Derek Allard1e6ab992008-06-06 11:37:34 +0000923}
Derek Jones0b59f272008-05-13 04:22:33 +0000924
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000925// ------------------------------------------------------------------------
926
927/**
928 * Validation Object
929 *
930 * Determines what the form validation class was instantiated as, fetches
931 * the object and returns it.
932 *
933 * @access private
934 * @return mixed
Derek Allard39f6f5a2008-09-22 16:06:06 +0000935 */
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000936if ( ! function_exists('_get_validation_object'))
Derek Allard39f6f5a2008-09-22 16:06:06 +0000937{
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000938 function &_get_validation_object()
939 {
940 $CI =& get_instance();
Derek Allard39f6f5a2008-09-22 16:06:06 +0000941
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000942 // We set this as a variable since we're returning by reference
943 $return = FALSE;
Derek Allard39f6f5a2008-09-22 16:06:06 +0000944
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000945 if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
946 {
947 return $return;
948 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000949
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000950 $object = $CI->load->_ci_classes['form_validation'];
Derek Allard39f6f5a2008-09-22 16:06:06 +0000951
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000952 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
953 {
954 return $return;
955 }
Derek Allard39f6f5a2008-09-22 16:06:06 +0000956
Rick Ellis43e0fbb2008-08-26 19:22:31 +0000957 return $CI->$object;
958 }
959}
960
961
Derek Jones0b59f272008-05-13 04:22:33 +0000962/* End of file form_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000963/* Location: ./system/helpers/form_helper.php */