blob: 9610cee98221b4af317df9202da66ddd8ff6b2c0 [file] [log] [blame]
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
Derek Allard2067d1a2008-11-13 22:59:24 +000025 */
26
27// ------------------------------------------------------------------------
28
29/**
30 * CodeIgniter Form Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
41/**
42 * Form Declaration
43 *
44 * Creates the opening portion of the form.
45 *
46 * @access public
47 * @param string the URI segments of the form destination
48 * @param array a key/value pair of attributes
49 * @param array a key/value pair hidden data
50 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020051 */
Derek Allard2067d1a2008-11-13 22:59:24 +000052if ( ! function_exists('form_open'))
53{
54 function form_open($action = '', $attributes = '', $hidden = array())
55 {
56 $CI =& get_instance();
57
58 if ($attributes == '')
59 {
Derek Allard3241d732009-09-17 12:17:45 +000060 $attributes = 'method="post"';
Derek Allard2067d1a2008-11-13 22:59:24 +000061 }
62
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010063 // If an action is not a full URL then turn it into one
Phil Sturgeon133beaf2011-03-10 16:38:32 +000064 if ($action && strpos($action, '://') === FALSE)
65 {
Phil Sturgeon52e73182011-03-11 10:17:01 +000066 $action = $CI->config->site_url($action);
Phil Sturgeon133beaf2011-03-10 16:38:32 +000067 }
Derek Allard2067d1a2008-11-13 22:59:24 +000068
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010069 // If no action is provided then set to the current url
70 $action OR $action = $CI->config->site_url($CI->uri->uri_string());
71
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020072 $form = '<form action="'.$action.'"'._attributes_to_string($attributes, TRUE).">\n";
Barry Mienydd671972010-10-04 16:33:58 +020073
Phil Sturgeonf8f04ce2012-03-04 14:21:12 +000074 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
75 if ($CI->config->item('csrf_protection') === TRUE AND ! (strpos($action, $CI->config->base_url()) === FALSE OR strpos($form, 'method="get"')))
Derek Allard958543a2010-07-22 14:10:26 -040076 {
Greg Aker1f6f0ab2011-04-07 18:24:53 -050077 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
Greg Aker28b425a2010-09-15 11:43:23 -050078 }
79
80 if (is_array($hidden) AND count($hidden) > 0)
81 {
Greg Aker9ce43852011-04-19 12:58:52 -050082 $form .= sprintf("<div style=\"display:none\">%s</div>", form_hidden($hidden));
Derek Allard958543a2010-07-22 14:10:26 -040083 }
84
Derek Allard2067d1a2008-11-13 22:59:24 +000085 return $form;
86 }
87}
88
89// ------------------------------------------------------------------------
90
91/**
92 * Form Declaration - Multipart type
93 *
94 * Creates the opening portion of the form, but with "multipart/form-data".
95 *
96 * @access public
97 * @param string the URI segments of the form destination
98 * @param array a key/value pair of attributes
99 * @param array a key/value pair hidden data
100 * @return string
101 */
102if ( ! function_exists('form_open_multipart'))
103{
Ben Edmunds98963b32011-08-20 14:17:16 -0500104 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000106 if (is_string($attributes))
107 {
108 $attributes .= ' enctype="multipart/form-data"';
109 }
110 else
111 {
112 $attributes['enctype'] = 'multipart/form-data';
113 }
114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 return form_open($action, $attributes, $hidden);
116 }
117}
118
119// ------------------------------------------------------------------------
120
121/**
122 * Hidden Input Field
123 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500124 * Generates hidden fields. You can pass a simple key/value string or an associative
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * array with multiple values.
126 *
127 * @access public
128 * @param mixed
129 * @param string
130 * @return string
131 */
132if ( ! function_exists('form_hidden'))
133{
Robin Sowell57fe4102009-03-26 18:58:46 +0000134 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000136 static $form;
137
138 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000140 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142
Robin Sowell57fe4102009-03-26 18:58:46 +0000143 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000145 foreach ($name as $key => $val)
146 {
147 form_hidden($key, $val, TRUE);
148 }
149 return $form;
150 }
151
152 if ( ! is_array($value))
153 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200154 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000155 }
156 else
157 {
158 foreach ($value as $k => $v)
159 {
Barry Mienydd671972010-10-04 16:33:58 +0200160 $k = (is_int($k)) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000161 form_hidden($name.'['.$k.']', $v, TRUE);
162 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164
165 return $form;
166 }
167}
168
169// ------------------------------------------------------------------------
170
171/**
172 * Text Input Field
173 *
174 * @access public
175 * @param mixed
176 * @param string
177 * @param string
178 * @return string
179 */
180if ( ! function_exists('form_input'))
181{
182 function form_input($data = '', $value = '', $extra = '')
183 {
184 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
185
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200186 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188}
189
190// ------------------------------------------------------------------------
191
192/**
193 * Password Field
194 *
195 * Identical to the input function but adds the "password" type
196 *
197 * @access public
198 * @param mixed
199 * @param string
200 * @param string
201 * @return string
202 */
203if ( ! function_exists('form_password'))
204{
205 function form_password($data = '', $value = '', $extra = '')
206 {
207 if ( ! is_array($data))
208 {
209 $data = array('name' => $data);
210 }
211
212 $data['type'] = 'password';
213 return form_input($data, $value, $extra);
214 }
215}
216
217// ------------------------------------------------------------------------
218
219/**
220 * Upload Field
221 *
222 * Identical to the input function but adds the "file" type
223 *
224 * @access public
225 * @param mixed
226 * @param string
227 * @param string
228 * @return string
229 */
230if ( ! function_exists('form_upload'))
231{
232 function form_upload($data = '', $value = '', $extra = '')
233 {
234 if ( ! is_array($data))
235 {
236 $data = array('name' => $data);
237 }
238
239 $data['type'] = 'file';
240 return form_input($data, $value, $extra);
241 }
242}
243
244// ------------------------------------------------------------------------
245
246/**
247 * Textarea field
248 *
249 * @access public
250 * @param mixed
251 * @param string
252 * @param string
253 * @return string
254 */
255if ( ! function_exists('form_textarea'))
256{
257 function form_textarea($data = '', $value = '', $extra = '')
258 {
Phil Sturgeon7de31602011-08-13 10:26:22 -0600259 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000260
261 if ( ! is_array($data) OR ! isset($data['value']))
262 {
263 $val = $value;
264 }
265 else
266 {
Barry Mienydd671972010-10-04 16:33:58 +0200267 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 unset($data['value']); // textareas don't use the value attribute
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Jones01a9b102009-07-17 18:30:36 +0000271 $name = (is_array($data)) ? $data['name'] : $data;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200272 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, $name)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
274}
275
276// ------------------------------------------------------------------------
277
278/**
Derek Jones26399292009-04-08 16:14:17 +0000279 * Multi-select menu
280 *
281 * @access public
282 * @param string
283 * @param array
284 * @param mixed
285 * @param string
286 * @return type
287 */
Derek Jones68788d52010-03-05 10:11:31 -0600288if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000289{
290 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
291 {
292 if ( ! strpos($extra, 'multiple'))
293 {
294 $extra .= ' multiple="multiple"';
295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Jones26399292009-04-08 16:14:17 +0000297 return form_dropdown($name, $options, $selected, $extra);
298 }
299}
300
301// --------------------------------------------------------------------
302
303/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 * Drop-down Menu
305 *
306 * @access public
307 * @param string
308 * @param array
309 * @param string
310 * @param string
311 * @return string
312 */
313if ( ! function_exists('form_dropdown'))
314{
315 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
316 {
nihaopaul0ba29f52012-03-12 16:46:58 +0800317 // If name is really an array then we'll call the function again using the array
318 if ( is_array($name) ) {
319 if ( ! isset($name['options']))
320 {
321 $name['selected'] = false;
322 }
323 if ( ! isset($name['selected']))
324 {
325 $name['selected'] = false;
326 }
327 if ( ! isset($name['extra']))
328 {
329 $name['extra'] = false;
330 }
331 return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']);
332 }
333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 if ( ! is_array($selected))
335 {
336 $selected = array($selected);
337 }
338
339 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200340 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200342 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 }
344
345 if ($extra != '') $extra = ' '.$extra;
346
347 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
348
349 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 foreach ($options as $key => $val)
352 {
353 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000354
Derek Jones68788d52010-03-05 10:11:31 -0600355 if (is_array($val) && ! empty($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000356 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200357 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000358
Derek Allard78a5fc92009-02-05 16:34:35 +0000359 foreach ($val as $optgroup_key => $optgroup_val)
360 {
Derek Allard86a840c2009-02-05 17:31:58 +0000361 $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000362 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
363 }
364
365 $form .= '</optgroup>'."\n";
366 }
367 else
368 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200369 $form .= '<option value="'.$key.'"'.(in_array($key, $selected) ? ' selected="selected"' : '').'>'.(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000370 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
372
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200373 $form .= "</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000374
375 return $form;
376 }
377}
378
379// ------------------------------------------------------------------------
380
381/**
382 * Checkbox Field
383 *
384 * @access public
385 * @param mixed
386 * @param string
387 * @param bool
388 * @param string
389 * @return string
390 */
391if ( ! function_exists('form_checkbox'))
392{
393 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
394 {
395 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
396
397 if (is_array($data) AND array_key_exists('checked', $data))
398 {
399 $checked = $data['checked'];
400
401 if ($checked == FALSE)
402 {
403 unset($data['checked']);
404 }
405 else
406 {
407 $data['checked'] = 'checked';
408 }
409 }
410
411 if ($checked == TRUE)
412 {
413 $defaults['checked'] = 'checked';
414 }
415 else
416 {
417 unset($defaults['checked']);
418 }
419
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200420 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
422}
423
424// ------------------------------------------------------------------------
425
426/**
427 * Radio Button
428 *
429 * @access public
430 * @param mixed
431 * @param string
432 * @param bool
433 * @param string
434 * @return string
435 */
436if ( ! function_exists('form_radio'))
437{
438 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
439 {
440 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200441 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 $data = array('name' => $data);
443 }
444
445 $data['type'] = 'radio';
446 return form_checkbox($data, $value, $checked, $extra);
447 }
448}
449
450// ------------------------------------------------------------------------
451
452/**
453 * Submit Button
454 *
455 * @access public
456 * @param mixed
457 * @param string
458 * @param string
459 * @return string
460 */
461if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200462{
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 function form_submit($data = '', $value = '', $extra = '')
464 {
465 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200466 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468}
469
470// ------------------------------------------------------------------------
471
472/**
473 * Reset Button
474 *
475 * @access public
476 * @param mixed
477 * @param string
478 * @param string
479 * @return string
480 */
481if ( ! function_exists('form_reset'))
482{
483 function form_reset($data = '', $value = '', $extra = '')
484 {
485 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200486 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488}
489
490// ------------------------------------------------------------------------
491
492/**
493 * Form Button
494 *
495 * @access public
496 * @param mixed
497 * @param string
498 * @param string
499 * @return string
500 */
501if ( ! function_exists('form_button'))
502{
503 function form_button($data = '', $content = '', $extra = '')
504 {
Derek Allard904094a2009-02-10 14:00:34 +0000505 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'button');
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 if ( is_array($data) AND isset($data['content']))
507 {
508 $content = $data['content'];
509 unset($data['content']); // content is not an attribute
510 }
511
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200512 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514}
515
516// ------------------------------------------------------------------------
517
518/**
519 * Form Label Tag
520 *
521 * @access public
522 * @param string The text to appear onscreen
523 * @param string The id the label applies to
524 * @param string Additional attributes
525 * @return string
526 */
527if ( ! function_exists('form_label'))
528{
529 function form_label($label_text = '', $id = '', $attributes = array())
530 {
531
532 $label = '<label';
533
534 if ($id != '')
535 {
Barry Mienydd671972010-10-04 16:33:58 +0200536 $label .= " for=\"$id\"";
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
538
539 if (is_array($attributes) AND count($attributes) > 0)
540 {
541 foreach ($attributes as $key => $val)
542 {
543 $label .= ' '.$key.'="'.$val.'"';
544 }
545 }
546
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200547 return $label .= ">$label_text</label>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
549}
550
551// ------------------------------------------------------------------------
552/**
553 * Fieldset Tag
554 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500555 * Used to produce <fieldset><legend>text</legend>. To close fieldset
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 * use form_fieldset_close()
557 *
558 * @access public
559 * @param string The legend text
560 * @param string Additional attributes
561 * @return string
562 */
563if ( ! function_exists('form_fieldset'))
564{
565 function form_fieldset($legend_text = '', $attributes = array())
566 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200567 $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 if ($legend_text != '')
569 {
570 $fieldset .= "<legend>$legend_text</legend>\n";
571 }
572
573 return $fieldset;
574 }
575}
576
577// ------------------------------------------------------------------------
578
579/**
580 * Fieldset Close Tag
581 *
582 * @access public
583 * @param string
584 * @return string
585 */
586if ( ! function_exists('form_fieldset_close'))
587{
588 function form_fieldset_close($extra = '')
589 {
590 return "</fieldset>".$extra;
591 }
592}
593
594// ------------------------------------------------------------------------
595
596/**
597 * Form Close Tag
598 *
599 * @access public
600 * @param string
601 * @return string
602 */
603if ( ! function_exists('form_close'))
604{
605 function form_close($extra = '')
606 {
607 return "</form>".$extra;
608 }
609}
610
611// ------------------------------------------------------------------------
612
613/**
614 * Form Prep
615 *
616 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
617 *
618 * @access public
619 * @param string
620 * @return string
621 */
622if ( ! function_exists('form_prep'))
623{
Derek Jones01a9b102009-07-17 18:30:36 +0000624 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
Derek Jones01a9b102009-07-17 18:30:36 +0000626 static $prepped_fields = array();
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 // if the field name is an array we do this recursively
629 if (is_array($str))
630 {
631 foreach ($str as $key => $val)
632 {
633 $str[$key] = form_prep($val);
634 }
635
636 return $str;
637 }
638
639 if ($str === '')
640 {
641 return '';
642 }
643
Derek Jones3eb9fac2009-07-17 20:25:13 +0000644 // we've already prepped a field with this name
645 // @todo need to figure out a way to namespace this so
646 // that we know the *exact* field and not just one with
647 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000648 if (isset($prepped_fields[$field_name]))
649 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000650 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000651 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000652
Derek Jones01a9b102009-07-17 18:30:36 +0000653 if ($field_name != '')
654 {
Derek Jones68788d52010-03-05 10:11:31 -0600655 $prepped_fields[$field_name] = $field_name;
Derek Jones01a9b102009-07-17 18:30:36 +0000656 }
Barry Mienydd671972010-10-04 16:33:58 +0200657
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200658 return html_escape($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
660}
661
662// ------------------------------------------------------------------------
663
664/**
665 * Form Value
666 *
667 * Grabs a value from the POST array for the specified field so you can
Derek Jones37f4b9c2011-07-01 17:56:50 -0500668 * re-populate an input field or textarea. If Form Validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 * is active it retrieves the info from the validation class
670 *
671 * @access public
672 * @param string
673 * @return mixed
674 */
675if ( ! function_exists('set_value'))
676{
677 function set_value($field = '', $default = '')
678 {
679 if (FALSE === ($OBJ =& _get_validation_object()))
680 {
681 if ( ! isset($_POST[$field]))
682 {
683 return $default;
684 }
685
Derek Jones01a9b102009-07-17 18:30:36 +0000686 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 }
688
Derek Jones01a9b102009-07-17 18:30:36 +0000689 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
691}
692
693// ------------------------------------------------------------------------
694
695/**
696 * Set Select
697 *
698 * Let's you set the selected value of a <select> menu via data in the POST array.
699 * If Form Validation is active it retrieves the info from the validation class
700 *
701 * @access public
702 * @param string
703 * @param string
704 * @param bool
705 * @return string
706 */
707if ( ! function_exists('set_select'))
708{
709 function set_select($field = '', $value = '', $default = FALSE)
710 {
711 $OBJ =& _get_validation_object();
712
713 if ($OBJ === FALSE)
714 {
715 if ( ! isset($_POST[$field]))
716 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000717 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
719 return ' selected="selected"';
720 }
721 return '';
722 }
723
724 $field = $_POST[$field];
725
726 if (is_array($field))
727 {
728 if ( ! in_array($value, $field))
729 {
730 return '';
731 }
732 }
733 else
734 {
735 if (($field == '' OR $value == '') OR ($field != $value))
736 {
737 return '';
738 }
739 }
740
741 return ' selected="selected"';
742 }
743
744 return $OBJ->set_select($field, $value, $default);
745 }
746}
747
748// ------------------------------------------------------------------------
749
750/**
751 * Set Checkbox
752 *
753 * Let's you set the selected value of a checkbox via the value in the POST array.
754 * If Form Validation is active it retrieves the info from the validation class
755 *
756 * @access public
757 * @param string
758 * @param string
759 * @param bool
760 * @return string
761 */
762if ( ! function_exists('set_checkbox'))
763{
764 function set_checkbox($field = '', $value = '', $default = FALSE)
765 {
766 $OBJ =& _get_validation_object();
767
768 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200769 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 if ( ! isset($_POST[$field]))
771 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000772 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 {
774 return ' checked="checked"';
775 }
776 return '';
777 }
778
779 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 if (is_array($field))
782 {
783 if ( ! in_array($value, $field))
784 {
785 return '';
786 }
787 }
788 else
789 {
790 if (($field == '' OR $value == '') OR ($field != $value))
791 {
792 return '';
793 }
794 }
795
796 return ' checked="checked"';
797 }
798
799 return $OBJ->set_checkbox($field, $value, $default);
800 }
801}
802
803// ------------------------------------------------------------------------
804
805/**
806 * Set Radio
807 *
808 * Let's you set the selected value of a radio field via info in the POST array.
809 * If Form Validation is active it retrieves the info from the validation class
810 *
811 * @access public
812 * @param string
813 * @param string
814 * @param bool
815 * @return string
816 */
817if ( ! function_exists('set_radio'))
818{
819 function set_radio($field = '', $value = '', $default = FALSE)
820 {
821 $OBJ =& _get_validation_object();
822
823 if ($OBJ === FALSE)
824 {
825 if ( ! isset($_POST[$field]))
826 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000827 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 {
829 return ' checked="checked"';
830 }
831 return '';
832 }
833
834 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 if (is_array($field))
837 {
838 if ( ! in_array($value, $field))
839 {
840 return '';
841 }
842 }
843 else
844 {
845 if (($field == '' OR $value == '') OR ($field != $value))
846 {
847 return '';
848 }
849 }
850
851 return ' checked="checked"';
852 }
853
854 return $OBJ->set_radio($field, $value, $default);
855 }
856}
857
858// ------------------------------------------------------------------------
859
860/**
861 * Form Error
862 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500863 * Returns the error for a specific form field. This is a helper for the
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 * form validation class.
865 *
866 * @access public
867 * @param string
868 * @param string
869 * @param string
870 * @return string
871 */
872if ( ! function_exists('form_error'))
873{
874 function form_error($field = '', $prefix = '', $suffix = '')
875 {
876 if (FALSE === ($OBJ =& _get_validation_object()))
877 {
878 return '';
879 }
880
881 return $OBJ->error($field, $prefix, $suffix);
882 }
883}
884
885// ------------------------------------------------------------------------
886
887/**
888 * Validation Error String
889 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500890 * Returns all the errors associated with a form submission. This is a helper
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 * function for the form validation class.
892 *
893 * @access public
894 * @param string
895 * @param string
896 * @return string
897 */
898if ( ! function_exists('validation_errors'))
899{
900 function validation_errors($prefix = '', $suffix = '')
901 {
902 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500903 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 return '';
905 }
906
907 return $OBJ->error_string($prefix, $suffix);
908 }
909}
910
911// ------------------------------------------------------------------------
912
913/**
914 * Parse the form attributes
915 *
916 * Helper function used by some of the form helpers
917 *
918 * @access private
919 * @param array
920 * @param array
921 * @return string
922 */
923if ( ! function_exists('_parse_form_attributes'))
924{
925 function _parse_form_attributes($attributes, $default)
926 {
927 if (is_array($attributes))
928 {
929 foreach ($default as $key => $val)
930 {
931 if (isset($attributes[$key]))
932 {
933 $default[$key] = $attributes[$key];
934 unset($attributes[$key]);
935 }
936 }
937
938 if (count($attributes) > 0)
939 {
940 $default = array_merge($default, $attributes);
941 }
942 }
943
944 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200945
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 foreach ($default as $key => $val)
947 {
948 if ($key == 'value')
949 {
Derek Jones01a9b102009-07-17 18:30:36 +0000950 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
952
953 $att .= $key . '="' . $val . '" ';
954 }
955
956 return $att;
957 }
958}
959
960// ------------------------------------------------------------------------
961
962/**
963 * Attributes To String
964 *
965 * Helper function used by some of the form helpers
966 *
967 * @access private
968 * @param mixed
969 * @param bool
970 * @return string
971 */
972if ( ! function_exists('_attributes_to_string'))
973{
974 function _attributes_to_string($attributes, $formtag = FALSE)
975 {
976 if (is_string($attributes) AND strlen($attributes) > 0)
977 {
978 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
979 {
980 $attributes .= ' method="post"';
981 }
982
Derek Allard3241d732009-09-17 12:17:45 +0000983 if ($formtag == TRUE AND strpos($attributes, 'accept-charset=') === FALSE)
984 {
985 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
986 }
987
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200988 return ' '.$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 }
Barry Mienydd671972010-10-04 16:33:58 +0200990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 if (is_object($attributes) AND count($attributes) > 0)
992 {
993 $attributes = (array)$attributes;
994 }
995
Joel Kallman79c1c462011-12-18 19:25:45 -0500996 if (is_array($attributes) AND ($formtag === TRUE OR count($attributes) > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 {
Derek Allard3241d732009-09-17 12:17:45 +0000998 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000999
Derek Allard3241d732009-09-17 12:17:45 +00001000 if ( ! isset($attributes['method']) AND $formtag === TRUE)
1001 {
1002 $atts .= ' method="post"';
1003 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001004
Derek Allard3241d732009-09-17 12:17:45 +00001005 if ( ! isset($attributes['accept-charset']) AND $formtag === TRUE)
1006 {
1007 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
1008 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001009
Derek Allard3241d732009-09-17 12:17:45 +00001010 foreach ($attributes as $key => $val)
1011 {
1012 $atts .= ' '.$key.'="'.$val.'"';
1013 }
1014
1015 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 }
1017 }
1018}
1019
1020// ------------------------------------------------------------------------
1021
1022/**
1023 * Validation Object
1024 *
1025 * Determines what the form validation class was instantiated as, fetches
1026 * the object and returns it.
1027 *
1028 * @access private
1029 * @return mixed
1030 */
1031if ( ! function_exists('_get_validation_object'))
1032{
1033 function &_get_validation_object()
1034 {
1035 $CI =& get_instance();
1036
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001037 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001039
Greg Akerc6d918a2011-04-22 10:17:32 -05001040 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001042 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1043 {
1044 return $return;
1045 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001046
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001047 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001049
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001050 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 }
1052}
1053
Derek Allard2067d1a2008-11-13 22:59:24 +00001054/* End of file form_helper.php */
patwork64e35cd2011-04-08 15:25:31 +02001055/* Location: ./system/helpers/form_helper.php */