blob: 17022a3b3156182e99d177807645b3ffb7b2a123 [file] [log] [blame]
Derek Allard17f4d732007-01-27 17:23:13 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allard17f4d732007-01-27 17:23:13 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allard17f4d732007-01-27 17:23:13 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
Derek Allardd2df9bc2007-04-15 17:41:17 +000019 * CodeIgniter Form Helpers
Derek Allard17f4d732007-01-27 17:23:13 +000020 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author Rick Ellis
25 * @link http://www.codeigniter.com/user_guide/helpers/form_helper.html
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 */
41function form_open($action = '', $attributes = array(), $hidden = array())
42{
43 $CI =& get_instance();
44
45 $form = '<form action="'.$CI->config->site_url($action).'"';
46
47 if ( ! isset($attributes['method']))
48 {
49 $form .= ' method="post"';
50 }
51
52 if (is_array($attributes) AND count($attributes) > 0)
53 {
54 foreach ($attributes as $key => $val)
55 {
56 $form .= ' '.$key.'="'.$val.'"';
57 }
58 }
59
60 $form .= '>';
61
62 if (is_array($hidden) AND count($hidden > 0))
63 {
64 $form .= form_hidden($hidden);
65 }
66
67 return $form;
68}
69
70// ------------------------------------------------------------------------
71
72/**
73 * Form Declaration - Multipart type
74 *
75 * Creates the opening portion of the form, but with "multipart/form-data".
76 *
77 * @access public
78 * @param string the URI segments of the form destination
79 * @param array a key/value pair of attributes
80 * @param array a key/value pair hidden data
81 * @return string
82 */
83function form_open_multipart($action, $attributes = array(), $hidden = array())
84{
85 $attributes['enctype'] = 'multipart/form-data';
86 return form_open($action, $attributes, $hidden);
87}
88
89// ------------------------------------------------------------------------
90
91/**
92 * Hidden Input Field
93 *
94 * Generates hidden fields. You can pass a simple key/value string or an associative
95 * array with multiple values.
96 *
97 * @access public
98 * @param mixed
99 * @param string
100 * @return string
101 */
102function form_hidden($name, $value = '')
103{
104 if ( ! is_array($name))
105 {
106 return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
107 }
108
109 $form = '';
110 foreach ($name as $name => $value)
111 {
112 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
113 }
114
115 return $form;
116}
117
118// ------------------------------------------------------------------------
119
120/**
121 * Text Input Field
122 *
123 * @access public
124 * @param mixed
125 * @param string
126 * @param string
127 * @return string
128 */
129function form_input($data = '', $value = '', $extra = '')
130{
131 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50');
132
133 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
134}
135
136// ------------------------------------------------------------------------
137
138/**
139 * Password Field
140 *
141 * Identical to the input function but adds the "password" type
142 *
143 * @access public
144 * @param mixed
145 * @param string
146 * @param string
147 * @return string
148 */
149function form_password($data = '', $value = '', $extra = '')
150{
151 if ( ! is_array($data))
152 {
153 $data = array('name' => $data);
154 }
155
156 $data['type'] = 'password';
157 return form_input($data, $value, $extra);
158}
159
160// ------------------------------------------------------------------------
161
162/**
163 * Upload Field
164 *
165 * Identical to the input function but adds the "file" type
166 *
167 * @access public
168 * @param mixed
169 * @param string
170 * @param string
171 * @return string
172 */
173function form_upload($data = '', $value = '', $extra = '')
174{
175 if ( ! is_array($data))
176 {
177 $data = array('name' => $data);
178 }
179
180 $data['type'] = 'file';
181 return form_input($data, $value, $extra);
182}
183
184// ------------------------------------------------------------------------
185
186/**
187 * Textarea field
188 *
189 * @access public
190 * @param mixed
191 * @param string
192 * @param string
193 * @return string
194 */
195function form_textarea($data = '', $value = '', $extra = '')
196{
197 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
198
Derek Allardc4ac15f2007-02-15 17:22:31 +0000199 if ( ! is_array($data) OR ! isset($data['value']))
200 {
201 $val = $value;
202 }
203 else
204 {
205 $val = $data['value'];
206 unset($data['value']); // textareas don't use the value attribute
207 }
Derek Allard17f4d732007-01-27 17:23:13 +0000208
209 return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
210}
211
212// ------------------------------------------------------------------------
213
214/**
215 * Drop-down Menu
216 *
217 * @access public
218 * @param string
219 * @param array
220 * @param string
221 * @param string
222 * @return string
223 */
224function form_dropdown($name = '', $options = array(), $selected = '', $extra = '')
225{
226 if ($extra != '') $extra = ' '.$extra;
227
228 $form = '<select name="'.$name.'"'.$extra.">\n";
229
230 foreach ($options as $key => $val)
231 {
Derek Jonesc80593c2007-07-11 23:40:14 +0000232 $key = (string) $key;
233 $val = (string) $val;
234
Derek Allard17f4d732007-01-27 17:23:13 +0000235 $sel = ($selected != $key) ? '' : ' selected="selected"';
236
237 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
238 }
239
240 $form .= '</select>';
241
242 return $form;
243}
244
245// ------------------------------------------------------------------------
246
247/**
248 * Checkbox Field
249 *
250 * @access public
251 * @param mixed
252 * @param string
253 * @param bool
254 * @param string
255 * @return string
256 */
257function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '')
258{
259 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
260
261 if (is_array($data) AND array_key_exists('checked', $data))
262 {
263 $checked = $data['checked'];
264
265 if ($checked == FALSE)
Derek Allardcea5dbe2007-04-27 03:33:21 +0000266 {
Derek Allard17f4d732007-01-27 17:23:13 +0000267 unset($data['checked']);
Derek Allardcea5dbe2007-04-27 03:33:21 +0000268 }
269 else
270 {
271 $data['checked'] = 'checked';
272 }
Derek Allard17f4d732007-01-27 17:23:13 +0000273 }
274
275 if ($checked == TRUE)
276 $defaults['checked'] = 'checked';
277 else
278 unset($defaults['checked']);
279
280 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
281}
282
283// ------------------------------------------------------------------------
284
285/**
286 * Radio Button
287 *
288 * @access public
289 * @param mixed
290 * @param string
291 * @param bool
292 * @param string
293 * @return string
294 */
295function form_radio($data = '', $value = '', $checked = TRUE, $extra = '')
296{
297 if ( ! is_array($data))
298 {
299 $data = array('name' => $data);
300 }
301
302 $data['type'] = 'radio';
303 return form_checkbox($data, $value, $checked, $extra);
304}
305
306// ------------------------------------------------------------------------
307
308/**
309 * Submit Button
310 *
311 * @access public
312 * @param mixed
313 * @param string
314 * @param string
315 * @return string
316 */
317function form_submit($data = '', $value = '', $extra = '')
318{
319 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
320
321 return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
322}
323
324// ------------------------------------------------------------------------
325
326/**
327 * Form Close Tag
328 *
329 * @access public
330 * @param string
331 * @return string
332 */
333function form_close($extra = '')
334{
335 return "</form>\n".$extra;
336}
337
338// ------------------------------------------------------------------------
339
340/**
341 * Form Prep
342 *
343 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
344 *
345 * @access public
346 * @param string
347 * @return string
348 */
349function form_prep($str = '')
350{
351 if ($str === '')
352 {
353 return '';
354 }
355
356 $temp = '__TEMP_AMPERSANDS__';
357
358 // Replace entities to temporary markers so that
359 // htmlspecialchars won't mess them up
360 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
361 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
362
363 $str = htmlspecialchars($str);
364
365 // In case htmlspecialchars misses these.
366 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
367
368 // Decode the temp markers back to entities
369 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
370 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
371
372 return $str;
373}
374
375// ------------------------------------------------------------------------
376
377/**
378 * Parse the form attributes
379 *
380 * Helper function used by some of the form helpers
381 *
382 * @access private
383 * @param array
384 * @param array
385 * @return string
386 */
387function parse_form_attributes($attributes, $default)
388{
389 if (is_array($attributes))
390 {
391 foreach ($default as $key => $val)
392 {
393 if (isset($attributes[$key]))
394 {
395 $default[$key] = $attributes[$key];
396 unset($attributes[$key]);
397 }
398 }
399
400 if (count($attributes) > 0)
401 {
402 $default = array_merge($default, $attributes);
403 }
404 }
405
406 $att = '';
407 foreach ($default as $key => $val)
408 {
409 if ($key == 'value')
410 {
411 $val = form_prep($val);
412 }
413
414 $att .= $key . '="' . $val . '" ';
415 }
416
417 return $att;
418}
419
adminb0dd10f2006-08-25 17:25:49 +0000420?>