Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 2 | /**
|
| 3 | * CodeIgniter
|
| 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Rick Ellis | dd1e323 | 2008-09-12 23:33:20 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 12 | * @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 Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 24 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 25 | * @link http://codeigniter.com/user_guide/helpers/form_helper.html
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 26 | */
|
| 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 Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 41 | if ( ! function_exists('form_open'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 42 | {
|
Derek Allard | 96537fd | 2008-06-06 13:41:53 +0000 | [diff] [blame] | 43 | function form_open($action = '', $attributes = '', $hidden = array())
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 44 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 45 | $CI =& get_instance();
|
| 46 |
|
Derek Allard | 96537fd | 2008-06-06 13:41:53 +0000 | [diff] [blame] | 47 | if ($attributes == '')
|
| 48 | {
|
| 49 | $attributes = 'method="post"';
|
| 50 | }
|
| 51 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 52 | $action = ( strpos($action, '://') === FALSE) ? $CI->config->site_url($action) : $action;
|
| 53 |
|
| 54 | $form = '<form action="'.$action.'"';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 55 |
|
Rick Ellis | c3907c6 | 2008-08-26 20:55:47 +0000 | [diff] [blame] | 56 | $form .= _attributes_to_string($attributes, TRUE);
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 57 |
|
| 58 | $form .= '>';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 59 |
|
Derek Allard | 69db8ef | 2008-09-25 13:04:12 +0000 | [diff] [blame] | 60 | if (is_array($hidden) AND count($hidden) > 0)
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 61 | {
|
| 62 | $form .= form_hidden($hidden);
|
| 63 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 64 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 65 | return $form;
|
| 66 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 67 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 68 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 81 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 82 | if ( ! function_exists('form_open_multipart'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 83 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 84 | 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 Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 89 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 90 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 103 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 104 | if ( ! function_exists('form_hidden'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 105 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 106 | function form_hidden($name, $value = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 107 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 108 | if ( ! is_array($name))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 109 | {
|
| 110 | return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
|
| 111 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 112 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 113 | $form = '';
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 114 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 115 | foreach ($name as $name => $value)
|
| 116 | {
|
Rick Ellis | f388511 | 2008-08-12 05:32:25 +0000 | [diff] [blame] | 117 | $form .= "\n";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 118 | $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
|
| 119 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 120 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 121 | return $form;
|
| 122 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 123 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 124 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 125 | // ------------------------------------------------------------------------
|
| 126 |
|
| 127 | /**
|
| 128 | * Text Input Field
|
| 129 | *
|
| 130 | * @access public
|
| 131 | * @param mixed
|
| 132 | * @param string
|
| 133 | * @param string
|
| 134 | * @return string
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 135 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 136 | if ( ! function_exists('form_input'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 137 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 138 | function form_input($data = '', $value = '', $extra = '')
|
| 139 | {
|
Derek Allard | 3640a0b | 2008-06-19 07:46:22 +0000 | [diff] [blame] | 140 | $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 141 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 142 | return "<input "._parse_form_attributes($data, $defaults).$extra." />";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 143 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 144 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 145 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 158 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 159 | if ( ! function_exists('form_password'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 160 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 161 | function form_password($data = '', $value = '', $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 162 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 163 | if ( ! is_array($data))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 164 | {
|
| 165 | $data = array('name' => $data);
|
| 166 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 167 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 168 | $data['type'] = 'password';
|
| 169 | return form_input($data, $value, $extra);
|
| 170 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 171 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 172 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 185 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 186 | if ( ! function_exists('form_upload'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 187 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 188 | function form_upload($data = '', $value = '', $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 189 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 190 | if ( ! is_array($data))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 191 | {
|
| 192 | $data = array('name' => $data);
|
| 193 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 194 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 195 | $data['type'] = 'file';
|
| 196 | return form_input($data, $value, $extra);
|
| 197 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 198 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 199 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 200 | // ------------------------------------------------------------------------
|
| 201 |
|
| 202 | /**
|
| 203 | * Textarea field
|
| 204 | *
|
| 205 | * @access public
|
| 206 | * @param mixed
|
| 207 | * @param string
|
| 208 | * @param string
|
| 209 | * @return string
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 210 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 211 | if ( ! function_exists('form_textarea'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 212 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 213 | function form_textarea($data = '', $value = '', $extra = '')
|
| 214 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 215 | $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 216 |
|
Rick Ellis | c3907c6 | 2008-08-26 20:55:47 +0000 | [diff] [blame] | 217 | if ( ! is_array($data) OR ! isset($data['value']))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 218 | {
|
| 219 | $val = $value;
|
| 220 | }
|
Rick Ellis | c3907c6 | 2008-08-26 20:55:47 +0000 | [diff] [blame] | 221 | else
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 222 | {
|
| 223 | $val = $data['value'];
|
| 224 | unset($data['value']); // textareas don't use the value attribute
|
| 225 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 226 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 227 | return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 228 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 229 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 230 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 242 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 243 | if ( ! function_exists('form_dropdown'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 244 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 245 | function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
|
Derek Allard | 4021b51 | 2008-01-04 22:26:12 +0000 | [diff] [blame] | 246 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 247 | if ( ! is_array($selected))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 248 | {
|
| 249 | $selected = array($selected);
|
| 250 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 251 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 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 Allard | 4021b51 | 2008-01-04 22:26:12 +0000 | [diff] [blame] | 261 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 262 | if ($extra != '') $extra = ' '.$extra;
|
Derek Allard | f8f0570 | 2008-01-18 14:39:23 +0000 | [diff] [blame] | 263 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 264 | $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
|
Derek Allard | 4021b51 | 2008-01-04 22:26:12 +0000 | [diff] [blame] | 265 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 266 | $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 267 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 268 | foreach ($options as $key => $val)
|
| 269 | {
|
| 270 | $key = (string) $key;
|
| 271 | $val = (string) $val;
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 272 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 273 | $sel = (in_array($key, $selected))?' selected="selected"':'';
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 274 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 275 | $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
|
| 276 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 277 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 278 | $form .= '</select>';
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 279 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 280 | return $form;
|
| 281 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 282 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 283 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 295 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 296 | if ( ! function_exists('form_checkbox'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 297 | {
|
Derek Allard | de7320b | 2008-05-06 13:51:02 +0000 | [diff] [blame] | 298 | function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 299 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 300 | $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 301 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 302 | if (is_array($data) AND array_key_exists('checked', $data))
|
| 303 | {
|
| 304 | $checked = $data['checked'];
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 305 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 306 | if ($checked == FALSE)
|
| 307 | {
|
| 308 | unset($data['checked']);
|
| 309 | }
|
| 310 | else
|
| 311 | {
|
| 312 | $data['checked'] = 'checked';
|
| 313 | }
|
| 314 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 315 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 316 | if ($checked == TRUE)
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 317 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 318 | $defaults['checked'] = 'checked';
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 319 | }
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 320 | else
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 321 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 322 | unset($defaults['checked']);
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 323 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 324 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 325 | return "<input "._parse_form_attributes($data, $defaults).$extra." />";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 326 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 327 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 328 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 340 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 341 | if ( ! function_exists('form_radio'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 342 | {
|
Derek Allard | de7320b | 2008-05-06 13:51:02 +0000 | [diff] [blame] | 343 | function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 344 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 345 | if ( ! is_array($data))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 346 | {
|
| 347 | $data = array('name' => $data);
|
| 348 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 349 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 350 | $data['type'] = 'radio';
|
| 351 | return form_checkbox($data, $value, $checked, $extra);
|
| 352 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 353 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 354 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 355 | // ------------------------------------------------------------------------
|
| 356 |
|
| 357 | /**
|
| 358 | * Submit Button
|
| 359 | *
|
| 360 | * @access public
|
| 361 | * @param mixed
|
| 362 | * @param string
|
| 363 | * @param string
|
| 364 | * @return string
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 365 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 366 | if ( ! function_exists('form_submit'))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 367 | {
|
| 368 | function form_submit($data = '', $value = '', $extra = '')
|
| 369 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 370 | $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 371 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 372 | return "<input "._parse_form_attributes($data, $defaults).$extra." />";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 373 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 374 | }
|
| 375 |
|
| 376 | // ------------------------------------------------------------------------
|
| 377 |
|
| 378 | /**
|
| 379 | * Reset Button
|
| 380 | *
|
| 381 | * @access public
|
| 382 | * @param mixed
|
| 383 | * @param string
|
| 384 | * @param string
|
| 385 | * @return string
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 386 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 387 | if ( ! function_exists('form_reset'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 388 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 389 | function form_reset($data = '', $value = '', $extra = '')
|
| 390 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 391 | $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 392 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 393 | return "<input "._parse_form_attributes($data, $defaults).$extra." />";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 394 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 395 | }
|
| 396 |
|
| 397 | // ------------------------------------------------------------------------
|
| 398 |
|
| 399 | /**
|
Derek Allard | 707d0e0 | 2008-03-18 11:50:00 +0000 | [diff] [blame] | 400 | * Form Button
|
| 401 | *
|
| 402 | * @access public
|
| 403 | * @param mixed
|
| 404 | * @param string
|
| 405 | * @param string
|
| 406 | * @return string
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 407 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 408 | if ( ! function_exists('form_button'))
|
Derek Allard | 707d0e0 | 2008-03-18 11:50:00 +0000 | [diff] [blame] | 409 | {
|
| 410 | function form_button($data = '', $content = '', $extra = '')
|
| 411 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 412 | $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'submit');
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 413 |
|
Derek Allard | 707d0e0 | 2008-03-18 11:50:00 +0000 | [diff] [blame] | 414 | if ( is_array($data) AND isset($data['content']))
|
| 415 | {
|
| 416 | $content = $data['content'];
|
| 417 | unset($data['content']); // content is not an attribute
|
| 418 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 419 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 420 | return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
|
Derek Allard | 707d0e0 | 2008-03-18 11:50:00 +0000 | [diff] [blame] | 421 | }
|
| 422 | }
|
| 423 |
|
| 424 | // ------------------------------------------------------------------------
|
| 425 |
|
| 426 | /**
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 427 | * 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 434 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 435 | if ( ! function_exists('form_label'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 436 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 437 | function form_label($label_text = '', $id = '', $attributes = array())
|
| 438 | {
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 439 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 440 | $label = '<label';
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 441 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 442 | if ($id != '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 443 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 444 | $label .= " for=\"$id\"";
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 445 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 446 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 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 Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 458 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 459 | }
|
| 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 472 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 473 | if ( ! function_exists('form_fieldset'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 474 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 475 | function form_fieldset($legend_text = '', $attributes = array())
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 476 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 477 | $fieldset = "<fieldset";
|
| 478 |
|
Rick Ellis | c3907c6 | 2008-08-26 20:55:47 +0000 | [diff] [blame] | 479 | $fieldset .= _attributes_to_string($attributes, FALSE);
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 480 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 481 | $fieldset .= ">\n";
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 482 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 483 | if ($legend_text != '')
|
| 484 | {
|
| 485 | $fieldset .= "<legend>$legend_text</legend>\n";
|
| 486 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 487 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 488 | return $fieldset;
|
| 489 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 490 | }
|
| 491 |
|
| 492 | // ------------------------------------------------------------------------
|
| 493 |
|
| 494 | /**
|
| 495 | * Fieldset Close Tag
|
| 496 | *
|
| 497 | * @access public
|
| 498 | * @param string
|
| 499 | * @return string
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 500 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 501 | if ( ! function_exists('form_fieldset_close'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 502 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 503 | function form_fieldset_close($extra = '')
|
| 504 | {
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 505 | return "</fieldset>".$extra;
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 506 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 507 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 508 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 509 | // ------------------------------------------------------------------------
|
| 510 |
|
| 511 | /**
|
| 512 | * Form Close Tag
|
| 513 | *
|
| 514 | * @access public
|
| 515 | * @param string
|
| 516 | * @return string
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 517 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 518 | if ( ! function_exists('form_close'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 519 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 520 | function form_close($extra = '')
|
| 521 | {
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 522 | return "</form>".$extra;
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 523 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 524 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 525 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 536 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 537 | if ( ! function_exists('form_prep'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 538 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 539 | function form_prep($str = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 540 | {
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 541 | // 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 548 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 549 | return $str;
|
| 550 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 551 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 552 | if ($str === '')
|
| 553 | {
|
| 554 | return '';
|
| 555 | }
|
| 556 |
|
| 557 | $temp = '__TEMP_AMPERSANDS__';
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 558 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 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.
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 567 | $str = str_replace(array("'", '"'), array("'", """), $str);
|
| 568 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 569 | // Decode the temp markers back to entities
|
| 570 | $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 571 | $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
|
| 572 |
|
| 573 | return $str;
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 574 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 575 | }
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 576 |
|
| 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 589 | */
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 590 | if ( ! 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 600 |
|
Rick Ellis | 862545a | 2008-10-20 20:39:14 +0000 | [diff] [blame] | 601 | return form_prep($_POST[$field]);
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 602 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 603 |
|
Rick Ellis | 862545a | 2008-10-20 20:39:14 +0000 | [diff] [blame] | 604 | return form_prep($OBJ->set_value($field, $default));
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 621 | */
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 622 | if ( ! 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 638 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 639 | $field = $_POST[$field];
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 640 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 641 | if (is_array($field))
|
| 642 | {
|
Rick Ellis | 7129d45 | 2008-09-20 04:22:25 +0000 | [diff] [blame] | 643 | if ( ! in_array($value, $field))
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 644 | {
|
| 645 | return '';
|
| 646 | }
|
| 647 | }
|
| 648 | else
|
| 649 | {
|
| 650 | if (($field == '' OR $value == '') OR ($field != $value))
|
| 651 | {
|
| 652 | return '';
|
| 653 | }
|
| 654 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 655 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 656 | return ' selected="selected"';
|
| 657 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 658 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 676 | */
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 677 | if ( ! 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 Ellis | 7129d45 | 2008-09-20 04:22:25 +0000 | [diff] [blame] | 684 | {
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 685 | if ( ! isset($_POST[$field]))
|
| 686 | {
|
| 687 | if (count($_POST) === 0)
|
| 688 | {
|
| 689 | return ' checked="checked"';
|
| 690 | }
|
| 691 | return '';
|
| 692 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 693 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 694 | $field = $_POST[$field];
|
| 695 |
|
| 696 | if (is_array($field))
|
| 697 | {
|
Rick Ellis | 7129d45 | 2008-09-20 04:22:25 +0000 | [diff] [blame] | 698 | if ( ! in_array($value, $field))
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 699 | {
|
| 700 | return '';
|
| 701 | }
|
| 702 | }
|
| 703 | else
|
| 704 | {
|
| 705 | if (($field == '' OR $value == '') OR ($field != $value))
|
| 706 | {
|
| 707 | return '';
|
| 708 | }
|
| 709 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 710 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 711 | return ' checked="checked"';
|
| 712 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 713 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 731 | */
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 732 | if ( ! 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 748 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 749 | $field = $_POST[$field];
|
| 750 |
|
| 751 | if (is_array($field))
|
| 752 | {
|
Rick Ellis | 7129d45 | 2008-09-20 04:22:25 +0000 | [diff] [blame] | 753 | if ( ! in_array($value, $field))
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 754 | {
|
| 755 | return '';
|
| 756 | }
|
| 757 | }
|
| 758 | else
|
| 759 | {
|
| 760 | if (($field == '' OR $value == '') OR ($field != $value))
|
| 761 | {
|
| 762 | return '';
|
| 763 | }
|
| 764 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 765 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 766 | return ' checked="checked"';
|
| 767 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 768 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 786 | */
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 787 | if ( ! 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 795 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 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
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 812 | */
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 813 | if ( ! function_exists('validation_errors'))
|
| 814 | {
|
| 815 | function validation_errors($prefix = '', $suffix = '')
|
| 816 | {
|
| 817 | if (FALSE === ($OBJ =& _get_validation_object()))
|
| 818 | {
|
| 819 | return '';
|
| 820 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 821 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 822 | return $OBJ->error_string($prefix, $suffix);
|
| 823 | }
|
| 824 | }
|
| 825 |
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 826 | // ------------------------------------------------------------------------
|
| 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 837 | */
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 838 | if ( ! function_exists('_parse_form_attributes'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 839 | {
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 840 | function _parse_form_attributes($attributes, $default)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 841 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 842 | if (is_array($attributes))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 843 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 844 | foreach ($default as $key => $val)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 845 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 846 | if (isset($attributes[$key]))
|
| 847 | {
|
| 848 | $default[$key] = $attributes[$key];
|
| 849 | unset($attributes[$key]);
|
| 850 | }
|
| 851 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 852 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 853 | if (count($attributes) > 0)
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 854 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 855 | $default = array_merge($default, $attributes);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 856 | }
|
| 857 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 858 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 859 | $att = '';
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 860 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 861 | foreach ($default as $key => $val)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 862 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 863 | if ($key == 'value')
|
| 864 | {
|
| 865 | $val = form_prep($val);
|
| 866 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 867 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 868 | $att .= $key . '="' . $val . '" ';
|
| 869 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 870 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 871 | return $att;
|
| 872 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 873 | }
|
| 874 |
|
Derek Allard | 1e6ab99 | 2008-06-06 11:37:34 +0000 | [diff] [blame] | 875 | // ------------------------------------------------------------------------
|
| 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 886 | */
|
Derek Allard | 1e6ab99 | 2008-06-06 11:37:34 +0000 | [diff] [blame] | 887 | if ( ! function_exists('_attributes_to_string'))
|
| 888 | {
|
| 889 | function _attributes_to_string($attributes, $formtag = FALSE)
|
| 890 | {
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 891 | 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 Allard | 1e6ab99 | 2008-06-06 11:37:34 +0000 | [diff] [blame] | 897 |
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 898 | return ' '.$attributes;
|
| 899 | }
|
Derek Allard | 1e6ab99 | 2008-06-06 11:37:34 +0000 | [diff] [blame] | 900 |
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 901 | if (is_object($attributes) AND count($attributes) > 0)
|
| 902 | {
|
| 903 | $attributes = (array)$attributes;
|
| 904 | }
|
Derek Allard | 1e6ab99 | 2008-06-06 11:37:34 +0000 | [diff] [blame] | 905 |
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 906 | 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 Allard | 1e6ab99 | 2008-06-06 11:37:34 +0000 | [diff] [blame] | 923 | }
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 924 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 925 | // ------------------------------------------------------------------------
|
| 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 Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 935 | */
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 936 | if ( ! function_exists('_get_validation_object'))
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 937 | {
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 938 | function &_get_validation_object()
|
| 939 | {
|
| 940 | $CI =& get_instance();
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 941 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 942 | // We set this as a variable since we're returning by reference
|
| 943 | $return = FALSE;
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 944 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 945 | if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
|
| 946 | {
|
| 947 | return $return;
|
| 948 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 949 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 950 | $object = $CI->load->_ci_classes['form_validation'];
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 951 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 952 | if ( ! isset($CI->$object) OR ! is_object($CI->$object))
|
| 953 | {
|
| 954 | return $return;
|
| 955 | }
|
Derek Allard | 39f6f5a | 2008-09-22 16:06:06 +0000 | [diff] [blame] | 956 |
|
Rick Ellis | 43e0fbb | 2008-08-26 19:22:31 +0000 | [diff] [blame] | 957 | return $CI->$object;
|
| 958 | }
|
| 959 | }
|
| 960 |
|
| 961 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 962 | /* End of file form_helper.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 963 | /* Location: ./system/helpers/form_helper.php */ |