blob: 130daee6ad67c626733f7094ed1eafae3aa26bef [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
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
24 * @author ExpressionEngine Dev Team
25 * @link http://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
Barry Mienydd671972010-10-04 16:33:58 +020040 */
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('form_open'))
42{
43 function form_open($action = '', $attributes = '', $hidden = array())
44 {
45 $CI =& get_instance();
46
47 if ($attributes == '')
48 {
Derek Allard3241d732009-09-17 12:17:45 +000049 $attributes = 'method="post"';
Derek Allard2067d1a2008-11-13 22:59:24 +000050 }
51
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010052 // If an action is not a full URL then turn it into one
Phil Sturgeon133beaf2011-03-10 16:38:32 +000053 if ($action && strpos($action, '://') === FALSE)
54 {
Phil Sturgeon52e73182011-03-11 10:17:01 +000055 $action = $CI->config->site_url($action);
Phil Sturgeon133beaf2011-03-10 16:38:32 +000056 }
Derek Allard2067d1a2008-11-13 22:59:24 +000057
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010058 // If no action is provided then set to the current url
59 $action OR $action = $CI->config->site_url($CI->uri->uri_string());
60
Derek Allard2067d1a2008-11-13 22:59:24 +000061 $form = '<form action="'.$action.'"';
Barry Mienydd671972010-10-04 16:33:58 +020062
Derek Allard2067d1a2008-11-13 22:59:24 +000063 $form .= _attributes_to_string($attributes, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 $form .= '>';
66
Joël Cox08a245f2011-07-16 23:46:49 +020067 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
68 if ($CI->config->item('csrf_protection') === TRUE AND ! (strpos($action, $CI->config->site_url()) === FALSE OR strpos($form, 'method="get"')))
Derek Allard958543a2010-07-22 14:10:26 -040069 {
Greg Aker1f6f0ab2011-04-07 18:24:53 -050070 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
Greg Aker28b425a2010-09-15 11:43:23 -050071 }
72
73 if (is_array($hidden) AND count($hidden) > 0)
74 {
Greg Aker9ce43852011-04-19 12:58:52 -050075 $form .= sprintf("<div style=\"display:none\">%s</div>", form_hidden($hidden));
Derek Allard958543a2010-07-22 14:10:26 -040076 }
77
Derek Allard2067d1a2008-11-13 22:59:24 +000078 return $form;
79 }
80}
81
82// ------------------------------------------------------------------------
83
84/**
85 * Form Declaration - Multipart type
86 *
87 * Creates the opening portion of the form, but with "multipart/form-data".
88 *
89 * @access public
90 * @param string the URI segments of the form destination
91 * @param array a key/value pair of attributes
92 * @param array a key/value pair hidden data
93 * @return string
94 */
95if ( ! function_exists('form_open_multipart'))
96{
Ben Edmunds98963b32011-08-20 14:17:16 -050097 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
Derek Allard33d4b6a2010-01-17 07:23:00 +000099 if (is_string($attributes))
100 {
101 $attributes .= ' enctype="multipart/form-data"';
102 }
103 else
104 {
105 $attributes['enctype'] = 'multipart/form-data';
106 }
107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 return form_open($action, $attributes, $hidden);
109 }
110}
111
112// ------------------------------------------------------------------------
113
114/**
115 * Hidden Input Field
116 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500117 * Generates hidden fields. You can pass a simple key/value string or an associative
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 * array with multiple values.
119 *
120 * @access public
121 * @param mixed
122 * @param string
123 * @return string
124 */
125if ( ! function_exists('form_hidden'))
126{
Robin Sowell57fe4102009-03-26 18:58:46 +0000127 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000129 static $form;
130
131 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000133 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
135
Robin Sowell57fe4102009-03-26 18:58:46 +0000136 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000138 foreach ($name as $key => $val)
139 {
140 form_hidden($key, $val, TRUE);
141 }
142 return $form;
143 }
144
145 if ( ! is_array($value))
146 {
Derek Jones01a9b102009-07-17 18:30:36 +0000147 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" />'."\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000148 }
149 else
150 {
151 foreach ($value as $k => $v)
152 {
Barry Mienydd671972010-10-04 16:33:58 +0200153 $k = (is_int($k)) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000154 form_hidden($name.'['.$k.']', $v, TRUE);
155 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
157
158 return $form;
159 }
160}
161
162// ------------------------------------------------------------------------
163
164/**
165 * Text Input Field
166 *
167 * @access public
168 * @param mixed
169 * @param string
170 * @param string
171 * @return string
172 */
173if ( ! function_exists('form_input'))
174{
175 function form_input($data = '', $value = '', $extra = '')
176 {
177 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
178
179 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
180 }
181}
182
183// ------------------------------------------------------------------------
184
185/**
186 * Password Field
187 *
188 * Identical to the input function but adds the "password" type
189 *
190 * @access public
191 * @param mixed
192 * @param string
193 * @param string
194 * @return string
195 */
196if ( ! function_exists('form_password'))
197{
198 function form_password($data = '', $value = '', $extra = '')
199 {
200 if ( ! is_array($data))
201 {
202 $data = array('name' => $data);
203 }
204
205 $data['type'] = 'password';
206 return form_input($data, $value, $extra);
207 }
208}
209
210// ------------------------------------------------------------------------
211
212/**
213 * Upload Field
214 *
215 * Identical to the input function but adds the "file" type
216 *
217 * @access public
218 * @param mixed
219 * @param string
220 * @param string
221 * @return string
222 */
223if ( ! function_exists('form_upload'))
224{
225 function form_upload($data = '', $value = '', $extra = '')
226 {
227 if ( ! is_array($data))
228 {
229 $data = array('name' => $data);
230 }
231
232 $data['type'] = 'file';
233 return form_input($data, $value, $extra);
234 }
235}
236
237// ------------------------------------------------------------------------
238
239/**
240 * Textarea field
241 *
242 * @access public
243 * @param mixed
244 * @param string
245 * @param string
246 * @return string
247 */
248if ( ! function_exists('form_textarea'))
249{
250 function form_textarea($data = '', $value = '', $extra = '')
251 {
Phil Sturgeon7de31602011-08-13 10:26:22 -0600252 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000253
254 if ( ! is_array($data) OR ! isset($data['value']))
255 {
256 $val = $value;
257 }
258 else
259 {
Barry Mienydd671972010-10-04 16:33:58 +0200260 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 unset($data['value']); // textareas don't use the value attribute
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Jones01a9b102009-07-17 18:30:36 +0000264 $name = (is_array($data)) ? $data['name'] : $data;
265 return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".form_prep($val, $name)."</textarea>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 }
267}
268
269// ------------------------------------------------------------------------
270
271/**
Derek Jones26399292009-04-08 16:14:17 +0000272 * Multi-select menu
273 *
274 * @access public
275 * @param string
276 * @param array
277 * @param mixed
278 * @param string
279 * @return type
280 */
Derek Jones68788d52010-03-05 10:11:31 -0600281if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000282{
283 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
284 {
285 if ( ! strpos($extra, 'multiple'))
286 {
287 $extra .= ' multiple="multiple"';
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Jones26399292009-04-08 16:14:17 +0000290 return form_dropdown($name, $options, $selected, $extra);
291 }
292}
293
294// --------------------------------------------------------------------
295
296/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 * Drop-down Menu
298 *
299 * @access public
300 * @param string
301 * @param array
302 * @param string
303 * @param string
304 * @return string
305 */
306if ( ! function_exists('form_dropdown'))
307{
308 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
309 {
310 if ( ! is_array($selected))
311 {
312 $selected = array($selected);
313 }
314
315 // If no selected state was submitted we will attempt to set it automatically
316 if (count($selected) === 0)
317 {
318 // If the form name appears in the $_POST array we have a winner!
319 if (isset($_POST[$name]))
320 {
321 $selected = array($_POST[$name]);
322 }
323 }
324
325 if ($extra != '') $extra = ' '.$extra;
326
327 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
328
329 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 foreach ($options as $key => $val)
332 {
333 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000334
Derek Jones68788d52010-03-05 10:11:31 -0600335 if (is_array($val) && ! empty($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000336 {
337 $form .= '<optgroup label="'.$key.'">'."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000338
Derek Allard78a5fc92009-02-05 16:34:35 +0000339 foreach ($val as $optgroup_key => $optgroup_val)
340 {
Derek Allard86a840c2009-02-05 17:31:58 +0000341 $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000342
343 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
344 }
345
346 $form .= '</optgroup>'."\n";
347 }
348 else
349 {
350 $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
351
352 $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
353 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
355
356 $form .= '</select>';
357
358 return $form;
359 }
360}
361
362// ------------------------------------------------------------------------
363
364/**
365 * Checkbox Field
366 *
367 * @access public
368 * @param mixed
369 * @param string
370 * @param bool
371 * @param string
372 * @return string
373 */
374if ( ! function_exists('form_checkbox'))
375{
376 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
377 {
378 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
379
380 if (is_array($data) AND array_key_exists('checked', $data))
381 {
382 $checked = $data['checked'];
383
384 if ($checked == FALSE)
385 {
386 unset($data['checked']);
387 }
388 else
389 {
390 $data['checked'] = 'checked';
391 }
392 }
393
394 if ($checked == TRUE)
395 {
396 $defaults['checked'] = 'checked';
397 }
398 else
399 {
400 unset($defaults['checked']);
401 }
402
403 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
404 }
405}
406
407// ------------------------------------------------------------------------
408
409/**
410 * Radio Button
411 *
412 * @access public
413 * @param mixed
414 * @param string
415 * @param bool
416 * @param string
417 * @return string
418 */
419if ( ! function_exists('form_radio'))
420{
421 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
422 {
423 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200424 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 $data = array('name' => $data);
426 }
427
428 $data['type'] = 'radio';
429 return form_checkbox($data, $value, $checked, $extra);
430 }
431}
432
433// ------------------------------------------------------------------------
434
435/**
436 * Submit Button
437 *
438 * @access public
439 * @param mixed
440 * @param string
441 * @param string
442 * @return string
443 */
444if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200445{
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 function form_submit($data = '', $value = '', $extra = '')
447 {
448 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
449
450 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
451 }
452}
453
454// ------------------------------------------------------------------------
455
456/**
457 * Reset Button
458 *
459 * @access public
460 * @param mixed
461 * @param string
462 * @param string
463 * @return string
464 */
465if ( ! function_exists('form_reset'))
466{
467 function form_reset($data = '', $value = '', $extra = '')
468 {
469 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
470
471 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
472 }
473}
474
475// ------------------------------------------------------------------------
476
477/**
478 * Form Button
479 *
480 * @access public
481 * @param mixed
482 * @param string
483 * @param string
484 * @return string
485 */
486if ( ! function_exists('form_button'))
487{
488 function form_button($data = '', $content = '', $extra = '')
489 {
Derek Allard904094a2009-02-10 14:00:34 +0000490 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'button');
Derek Allard2067d1a2008-11-13 22:59:24 +0000491
492 if ( is_array($data) AND isset($data['content']))
493 {
494 $content = $data['content'];
495 unset($data['content']); // content is not an attribute
496 }
497
498 return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
499 }
500}
501
502// ------------------------------------------------------------------------
503
504/**
505 * Form Label Tag
506 *
507 * @access public
508 * @param string The text to appear onscreen
509 * @param string The id the label applies to
510 * @param string Additional attributes
511 * @return string
512 */
513if ( ! function_exists('form_label'))
514{
515 function form_label($label_text = '', $id = '', $attributes = array())
516 {
517
518 $label = '<label';
519
520 if ($id != '')
521 {
Barry Mienydd671972010-10-04 16:33:58 +0200522 $label .= " for=\"$id\"";
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
524
525 if (is_array($attributes) AND count($attributes) > 0)
526 {
527 foreach ($attributes as $key => $val)
528 {
529 $label .= ' '.$key.'="'.$val.'"';
530 }
531 }
532
533 $label .= ">$label_text</label>";
534
535 return $label;
536 }
537}
538
539// ------------------------------------------------------------------------
540/**
541 * Fieldset Tag
542 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500543 * Used to produce <fieldset><legend>text</legend>. To close fieldset
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * use form_fieldset_close()
545 *
546 * @access public
547 * @param string The legend text
548 * @param string Additional attributes
549 * @return string
550 */
551if ( ! function_exists('form_fieldset'))
552{
553 function form_fieldset($legend_text = '', $attributes = array())
554 {
555 $fieldset = "<fieldset";
556
557 $fieldset .= _attributes_to_string($attributes, FALSE);
558
559 $fieldset .= ">\n";
560
561 if ($legend_text != '')
562 {
563 $fieldset .= "<legend>$legend_text</legend>\n";
564 }
565
566 return $fieldset;
567 }
568}
569
570// ------------------------------------------------------------------------
571
572/**
573 * Fieldset Close Tag
574 *
575 * @access public
576 * @param string
577 * @return string
578 */
579if ( ! function_exists('form_fieldset_close'))
580{
581 function form_fieldset_close($extra = '')
582 {
583 return "</fieldset>".$extra;
584 }
585}
586
587// ------------------------------------------------------------------------
588
589/**
590 * Form Close Tag
591 *
592 * @access public
593 * @param string
594 * @return string
595 */
596if ( ! function_exists('form_close'))
597{
598 function form_close($extra = '')
599 {
600 return "</form>".$extra;
601 }
602}
603
604// ------------------------------------------------------------------------
605
606/**
607 * Form Prep
608 *
609 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
610 *
611 * @access public
612 * @param string
613 * @return string
614 */
615if ( ! function_exists('form_prep'))
616{
Derek Jones01a9b102009-07-17 18:30:36 +0000617 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
Derek Jones01a9b102009-07-17 18:30:36 +0000619 static $prepped_fields = array();
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // if the field name is an array we do this recursively
622 if (is_array($str))
623 {
624 foreach ($str as $key => $val)
625 {
626 $str[$key] = form_prep($val);
627 }
628
629 return $str;
630 }
631
632 if ($str === '')
633 {
634 return '';
635 }
636
Derek Jones3eb9fac2009-07-17 20:25:13 +0000637 // we've already prepped a field with this name
638 // @todo need to figure out a way to namespace this so
639 // that we know the *exact* field and not just one with
640 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000641 if (isset($prepped_fields[$field_name]))
642 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000643 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000644 }
freewil8cc0cfe2011-08-27 21:53:00 -0400645
646 $str = html_escape($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000647
Derek Jones01a9b102009-07-17 18:30:36 +0000648 if ($field_name != '')
649 {
Derek Jones68788d52010-03-05 10:11:31 -0600650 $prepped_fields[$field_name] = $field_name;
Derek Jones01a9b102009-07-17 18:30:36 +0000651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 return $str;
654 }
655}
656
657// ------------------------------------------------------------------------
658
659/**
660 * Form Value
661 *
662 * Grabs a value from the POST array for the specified field so you can
Derek Jones37f4b9c2011-07-01 17:56:50 -0500663 * re-populate an input field or textarea. If Form Validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 * is active it retrieves the info from the validation class
665 *
666 * @access public
667 * @param string
668 * @return mixed
669 */
670if ( ! function_exists('set_value'))
671{
672 function set_value($field = '', $default = '')
673 {
674 if (FALSE === ($OBJ =& _get_validation_object()))
675 {
676 if ( ! isset($_POST[$field]))
677 {
678 return $default;
679 }
680
Derek Jones01a9b102009-07-17 18:30:36 +0000681 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 }
683
Derek Jones01a9b102009-07-17 18:30:36 +0000684 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 }
686}
687
688// ------------------------------------------------------------------------
689
690/**
691 * Set Select
692 *
693 * Let's you set the selected value of a <select> menu via data in the POST array.
694 * If Form Validation is active it retrieves the info from the validation class
695 *
696 * @access public
697 * @param string
698 * @param string
699 * @param bool
700 * @return string
701 */
702if ( ! function_exists('set_select'))
703{
704 function set_select($field = '', $value = '', $default = FALSE)
705 {
706 $OBJ =& _get_validation_object();
707
708 if ($OBJ === FALSE)
709 {
710 if ( ! isset($_POST[$field]))
711 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000712 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
714 return ' selected="selected"';
715 }
716 return '';
717 }
718
719 $field = $_POST[$field];
720
721 if (is_array($field))
722 {
723 if ( ! in_array($value, $field))
724 {
725 return '';
726 }
727 }
728 else
729 {
730 if (($field == '' OR $value == '') OR ($field != $value))
731 {
732 return '';
733 }
734 }
735
736 return ' selected="selected"';
737 }
738
739 return $OBJ->set_select($field, $value, $default);
740 }
741}
742
743// ------------------------------------------------------------------------
744
745/**
746 * Set Checkbox
747 *
748 * Let's you set the selected value of a checkbox via the value in the POST array.
749 * If Form Validation is active it retrieves the info from the validation class
750 *
751 * @access public
752 * @param string
753 * @param string
754 * @param bool
755 * @return string
756 */
757if ( ! function_exists('set_checkbox'))
758{
759 function set_checkbox($field = '', $value = '', $default = FALSE)
760 {
761 $OBJ =& _get_validation_object();
762
763 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200764 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 if ( ! isset($_POST[$field]))
766 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000767 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 {
769 return ' checked="checked"';
770 }
771 return '';
772 }
773
774 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 if (is_array($field))
777 {
778 if ( ! in_array($value, $field))
779 {
780 return '';
781 }
782 }
783 else
784 {
785 if (($field == '' OR $value == '') OR ($field != $value))
786 {
787 return '';
788 }
789 }
790
791 return ' checked="checked"';
792 }
793
794 return $OBJ->set_checkbox($field, $value, $default);
795 }
796}
797
798// ------------------------------------------------------------------------
799
800/**
801 * Set Radio
802 *
803 * Let's you set the selected value of a radio field via info in the POST array.
804 * If Form Validation is active it retrieves the info from the validation class
805 *
806 * @access public
807 * @param string
808 * @param string
809 * @param bool
810 * @return string
811 */
812if ( ! function_exists('set_radio'))
813{
814 function set_radio($field = '', $value = '', $default = FALSE)
815 {
816 $OBJ =& _get_validation_object();
817
818 if ($OBJ === FALSE)
819 {
820 if ( ! isset($_POST[$field]))
821 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000822 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 {
824 return ' checked="checked"';
825 }
826 return '';
827 }
828
829 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 if (is_array($field))
832 {
833 if ( ! in_array($value, $field))
834 {
835 return '';
836 }
837 }
838 else
839 {
840 if (($field == '' OR $value == '') OR ($field != $value))
841 {
842 return '';
843 }
844 }
845
846 return ' checked="checked"';
847 }
848
849 return $OBJ->set_radio($field, $value, $default);
850 }
851}
852
853// ------------------------------------------------------------------------
854
855/**
856 * Form Error
857 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500858 * Returns the error for a specific form field. This is a helper for the
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 * form validation class.
860 *
861 * @access public
862 * @param string
863 * @param string
864 * @param string
865 * @return string
866 */
867if ( ! function_exists('form_error'))
868{
869 function form_error($field = '', $prefix = '', $suffix = '')
870 {
871 if (FALSE === ($OBJ =& _get_validation_object()))
872 {
873 return '';
874 }
875
876 return $OBJ->error($field, $prefix, $suffix);
877 }
878}
879
880// ------------------------------------------------------------------------
881
882/**
883 * Validation Error String
884 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500885 * Returns all the errors associated with a form submission. This is a helper
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 * function for the form validation class.
887 *
888 * @access public
889 * @param string
890 * @param string
891 * @return string
892 */
893if ( ! function_exists('validation_errors'))
894{
895 function validation_errors($prefix = '', $suffix = '')
896 {
897 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500898 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 return '';
900 }
901
902 return $OBJ->error_string($prefix, $suffix);
903 }
904}
905
906// ------------------------------------------------------------------------
907
908/**
909 * Parse the form attributes
910 *
911 * Helper function used by some of the form helpers
912 *
913 * @access private
914 * @param array
915 * @param array
916 * @return string
917 */
918if ( ! function_exists('_parse_form_attributes'))
919{
920 function _parse_form_attributes($attributes, $default)
921 {
922 if (is_array($attributes))
923 {
924 foreach ($default as $key => $val)
925 {
926 if (isset($attributes[$key]))
927 {
928 $default[$key] = $attributes[$key];
929 unset($attributes[$key]);
930 }
931 }
932
933 if (count($attributes) > 0)
934 {
935 $default = array_merge($default, $attributes);
936 }
937 }
938
939 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 foreach ($default as $key => $val)
942 {
943 if ($key == 'value')
944 {
Derek Jones01a9b102009-07-17 18:30:36 +0000945 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
947
948 $att .= $key . '="' . $val . '" ';
949 }
950
951 return $att;
952 }
953}
954
955// ------------------------------------------------------------------------
956
957/**
958 * Attributes To String
959 *
960 * Helper function used by some of the form helpers
961 *
962 * @access private
963 * @param mixed
964 * @param bool
965 * @return string
966 */
967if ( ! function_exists('_attributes_to_string'))
968{
969 function _attributes_to_string($attributes, $formtag = FALSE)
970 {
971 if (is_string($attributes) AND strlen($attributes) > 0)
972 {
973 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
974 {
975 $attributes .= ' method="post"';
976 }
977
Derek Allard3241d732009-09-17 12:17:45 +0000978 if ($formtag == TRUE AND strpos($attributes, 'accept-charset=') === FALSE)
979 {
980 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
981 }
982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 return ' '.$attributes;
984 }
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 if (is_object($attributes) AND count($attributes) > 0)
987 {
988 $attributes = (array)$attributes;
989 }
990
991 if (is_array($attributes) AND count($attributes) > 0)
992 {
Derek Allard3241d732009-09-17 12:17:45 +0000993 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000994
Derek Allard3241d732009-09-17 12:17:45 +0000995 if ( ! isset($attributes['method']) AND $formtag === TRUE)
996 {
997 $atts .= ' method="post"';
998 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000999
Derek Allard3241d732009-09-17 12:17:45 +00001000 if ( ! isset($attributes['accept-charset']) AND $formtag === TRUE)
1001 {
1002 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
1003 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001004
Derek Allard3241d732009-09-17 12:17:45 +00001005 foreach ($attributes as $key => $val)
1006 {
1007 $atts .= ' '.$key.'="'.$val.'"';
1008 }
1009
1010 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 }
1012 }
1013}
1014
1015// ------------------------------------------------------------------------
1016
1017/**
1018 * Validation Object
1019 *
1020 * Determines what the form validation class was instantiated as, fetches
1021 * the object and returns it.
1022 *
1023 * @access private
1024 * @return mixed
1025 */
1026if ( ! function_exists('_get_validation_object'))
1027{
1028 function &_get_validation_object()
1029 {
1030 $CI =& get_instance();
1031
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001032 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 $return = FALSE;
Derek Jones37f4b9c2011-07-01 17:56:50 -05001034
Greg Akerc6d918a2011-04-22 10:17:32 -05001035 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001037 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1038 {
1039 return $return;
1040 }
Derek Jones37f4b9c2011-07-01 17:56:50 -05001041
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001042 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 }
Derek Jones37f4b9c2011-07-01 17:56:50 -05001044
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001045 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 }
1047}
1048
1049
1050/* End of file form_helper.php */
patwork64e35cd2011-04-08 15:25:31 +02001051/* Location: ./system/helpers/form_helper.php */