blob: 0f02bcf75b1ceb24c123394dd8352e803d261373 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Form Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
38 */
39
40// ------------------------------------------------------------------------
41
42/**
43 * Form Declaration
44 *
45 * Creates the opening portion of the form.
46 *
47 * @access public
48 * @param string the URI segments of the form destination
49 * @param array a key/value pair of attributes
50 * @param array a key/value pair hidden data
51 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020052 */
Derek Allard2067d1a2008-11-13 22:59:24 +000053if ( ! function_exists('form_open'))
54{
55 function form_open($action = '', $attributes = '', $hidden = array())
56 {
57 $CI =& get_instance();
58
59 if ($attributes == '')
60 {
Derek Allard3241d732009-09-17 12:17:45 +000061 $attributes = 'method="post"';
Derek Allard2067d1a2008-11-13 22:59:24 +000062 }
63
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010064 // If an action is not a full URL then turn it into one
Phil Sturgeon133beaf2011-03-10 16:38:32 +000065 if ($action && strpos($action, '://') === FALSE)
66 {
Phil Sturgeon52e73182011-03-11 10:17:01 +000067 $action = $CI->config->site_url($action);
Phil Sturgeon133beaf2011-03-10 16:38:32 +000068 }
Derek Allard2067d1a2008-11-13 22:59:24 +000069
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010070 // If no action is provided then set to the current url
71 $action OR $action = $CI->config->site_url($CI->uri->uri_string());
72
Derek Allard2067d1a2008-11-13 22:59:24 +000073 $form = '<form action="'.$action.'"';
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 $form .= _attributes_to_string($attributes, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 $form .= '>';
78
Joël Cox08a245f2011-07-16 23:46:49 +020079 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
80 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 -040081 {
Greg Aker1f6f0ab2011-04-07 18:24:53 -050082 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
Greg Aker28b425a2010-09-15 11:43:23 -050083 }
84
85 if (is_array($hidden) AND count($hidden) > 0)
86 {
Greg Aker9ce43852011-04-19 12:58:52 -050087 $form .= sprintf("<div style=\"display:none\">%s</div>", form_hidden($hidden));
Derek Allard958543a2010-07-22 14:10:26 -040088 }
89
Derek Allard2067d1a2008-11-13 22:59:24 +000090 return $form;
91 }
92}
93
94// ------------------------------------------------------------------------
95
96/**
97 * Form Declaration - Multipart type
98 *
99 * Creates the opening portion of the form, but with "multipart/form-data".
100 *
101 * @access public
102 * @param string the URI segments of the form destination
103 * @param array a key/value pair of attributes
104 * @param array a key/value pair hidden data
105 * @return string
106 */
107if ( ! function_exists('form_open_multipart'))
108{
Ben Edmunds98963b32011-08-20 14:17:16 -0500109 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000111 if (is_string($attributes))
112 {
113 $attributes .= ' enctype="multipart/form-data"';
114 }
115 else
116 {
117 $attributes['enctype'] = 'multipart/form-data';
118 }
119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 return form_open($action, $attributes, $hidden);
121 }
122}
123
124// ------------------------------------------------------------------------
125
126/**
127 * Hidden Input Field
128 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500129 * Generates hidden fields. You can pass a simple key/value string or an associative
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 * array with multiple values.
131 *
132 * @access public
133 * @param mixed
134 * @param string
135 * @return string
136 */
137if ( ! function_exists('form_hidden'))
138{
Robin Sowell57fe4102009-03-26 18:58:46 +0000139 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000141 static $form;
142
143 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000145 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
147
Robin Sowell57fe4102009-03-26 18:58:46 +0000148 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000150 foreach ($name as $key => $val)
151 {
152 form_hidden($key, $val, TRUE);
153 }
154 return $form;
155 }
156
157 if ( ! is_array($value))
158 {
Derek Jones01a9b102009-07-17 18:30:36 +0000159 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" />'."\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000160 }
161 else
162 {
163 foreach ($value as $k => $v)
164 {
Barry Mienydd671972010-10-04 16:33:58 +0200165 $k = (is_int($k)) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000166 form_hidden($name.'['.$k.']', $v, TRUE);
167 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
170 return $form;
171 }
172}
173
174// ------------------------------------------------------------------------
175
176/**
177 * Text Input Field
178 *
179 * @access public
180 * @param mixed
181 * @param string
182 * @param string
183 * @return string
184 */
185if ( ! function_exists('form_input'))
186{
187 function form_input($data = '', $value = '', $extra = '')
188 {
189 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
190
191 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
192 }
193}
194
195// ------------------------------------------------------------------------
196
197/**
198 * Password Field
199 *
200 * Identical to the input function but adds the "password" type
201 *
202 * @access public
203 * @param mixed
204 * @param string
205 * @param string
206 * @return string
207 */
208if ( ! function_exists('form_password'))
209{
210 function form_password($data = '', $value = '', $extra = '')
211 {
212 if ( ! is_array($data))
213 {
214 $data = array('name' => $data);
215 }
216
217 $data['type'] = 'password';
218 return form_input($data, $value, $extra);
219 }
220}
221
222// ------------------------------------------------------------------------
223
224/**
225 * Upload Field
226 *
227 * Identical to the input function but adds the "file" type
228 *
229 * @access public
230 * @param mixed
231 * @param string
232 * @param string
233 * @return string
234 */
235if ( ! function_exists('form_upload'))
236{
237 function form_upload($data = '', $value = '', $extra = '')
238 {
239 if ( ! is_array($data))
240 {
241 $data = array('name' => $data);
242 }
243
244 $data['type'] = 'file';
245 return form_input($data, $value, $extra);
246 }
247}
248
249// ------------------------------------------------------------------------
250
251/**
252 * Textarea field
253 *
254 * @access public
255 * @param mixed
256 * @param string
257 * @param string
258 * @return string
259 */
260if ( ! function_exists('form_textarea'))
261{
262 function form_textarea($data = '', $value = '', $extra = '')
263 {
Phil Sturgeon7de31602011-08-13 10:26:22 -0600264 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000265
266 if ( ! is_array($data) OR ! isset($data['value']))
267 {
268 $val = $value;
269 }
270 else
271 {
Barry Mienydd671972010-10-04 16:33:58 +0200272 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 unset($data['value']); // textareas don't use the value attribute
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Jones01a9b102009-07-17 18:30:36 +0000276 $name = (is_array($data)) ? $data['name'] : $data;
277 return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".form_prep($val, $name)."</textarea>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
279}
280
281// ------------------------------------------------------------------------
282
283/**
Derek Jones26399292009-04-08 16:14:17 +0000284 * Multi-select menu
285 *
286 * @access public
287 * @param string
288 * @param array
289 * @param mixed
290 * @param string
291 * @return type
292 */
Derek Jones68788d52010-03-05 10:11:31 -0600293if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000294{
295 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
296 {
297 if ( ! strpos($extra, 'multiple'))
298 {
299 $extra .= ' multiple="multiple"';
300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Jones26399292009-04-08 16:14:17 +0000302 return form_dropdown($name, $options, $selected, $extra);
303 }
304}
305
306// --------------------------------------------------------------------
307
308/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * Drop-down Menu
310 *
311 * @access public
312 * @param string
313 * @param array
314 * @param string
315 * @param string
316 * @return string
317 */
318if ( ! function_exists('form_dropdown'))
319{
320 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
321 {
322 if ( ! is_array($selected))
323 {
324 $selected = array($selected);
325 }
326
327 // If no selected state was submitted we will attempt to set it automatically
328 if (count($selected) === 0)
329 {
330 // If the form name appears in the $_POST array we have a winner!
331 if (isset($_POST[$name]))
332 {
333 $selected = array($_POST[$name]);
334 }
335 }
336
337 if ($extra != '') $extra = ' '.$extra;
338
339 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
340
341 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 foreach ($options as $key => $val)
344 {
345 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000346
Derek Jones68788d52010-03-05 10:11:31 -0600347 if (is_array($val) && ! empty($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000348 {
349 $form .= '<optgroup label="'.$key.'">'."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000350
Derek Allard78a5fc92009-02-05 16:34:35 +0000351 foreach ($val as $optgroup_key => $optgroup_val)
352 {
Derek Allard86a840c2009-02-05 17:31:58 +0000353 $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000354
355 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
356 }
357
358 $form .= '</optgroup>'."\n";
359 }
360 else
361 {
362 $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
363
364 $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
365 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
367
368 $form .= '</select>';
369
370 return $form;
371 }
372}
373
374// ------------------------------------------------------------------------
375
376/**
377 * Checkbox Field
378 *
379 * @access public
380 * @param mixed
381 * @param string
382 * @param bool
383 * @param string
384 * @return string
385 */
386if ( ! function_exists('form_checkbox'))
387{
388 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
389 {
390 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
391
392 if (is_array($data) AND array_key_exists('checked', $data))
393 {
394 $checked = $data['checked'];
395
396 if ($checked == FALSE)
397 {
398 unset($data['checked']);
399 }
400 else
401 {
402 $data['checked'] = 'checked';
403 }
404 }
405
406 if ($checked == TRUE)
407 {
408 $defaults['checked'] = 'checked';
409 }
410 else
411 {
412 unset($defaults['checked']);
413 }
414
415 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
416 }
417}
418
419// ------------------------------------------------------------------------
420
421/**
422 * Radio Button
423 *
424 * @access public
425 * @param mixed
426 * @param string
427 * @param bool
428 * @param string
429 * @return string
430 */
431if ( ! function_exists('form_radio'))
432{
433 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
434 {
435 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200436 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 $data = array('name' => $data);
438 }
439
440 $data['type'] = 'radio';
441 return form_checkbox($data, $value, $checked, $extra);
442 }
443}
444
445// ------------------------------------------------------------------------
446
447/**
448 * Submit Button
449 *
450 * @access public
451 * @param mixed
452 * @param string
453 * @param string
454 * @return string
455 */
456if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200457{
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 function form_submit($data = '', $value = '', $extra = '')
459 {
460 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
461
462 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
463 }
464}
465
466// ------------------------------------------------------------------------
467
468/**
469 * Reset Button
470 *
471 * @access public
472 * @param mixed
473 * @param string
474 * @param string
475 * @return string
476 */
477if ( ! function_exists('form_reset'))
478{
479 function form_reset($data = '', $value = '', $extra = '')
480 {
481 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
482
483 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
484 }
485}
486
487// ------------------------------------------------------------------------
488
489/**
490 * Form Button
491 *
492 * @access public
493 * @param mixed
494 * @param string
495 * @param string
496 * @return string
497 */
498if ( ! function_exists('form_button'))
499{
500 function form_button($data = '', $content = '', $extra = '')
501 {
Derek Allard904094a2009-02-10 14:00:34 +0000502 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'button');
Derek Allard2067d1a2008-11-13 22:59:24 +0000503
504 if ( is_array($data) AND isset($data['content']))
505 {
506 $content = $data['content'];
507 unset($data['content']); // content is not an attribute
508 }
509
510 return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
511 }
512}
513
514// ------------------------------------------------------------------------
515
516/**
517 * Form Label Tag
518 *
519 * @access public
520 * @param string The text to appear onscreen
521 * @param string The id the label applies to
522 * @param string Additional attributes
523 * @return string
524 */
525if ( ! function_exists('form_label'))
526{
527 function form_label($label_text = '', $id = '', $attributes = array())
528 {
529
530 $label = '<label';
531
532 if ($id != '')
533 {
Barry Mienydd671972010-10-04 16:33:58 +0200534 $label .= " for=\"$id\"";
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536
537 if (is_array($attributes) AND count($attributes) > 0)
538 {
539 foreach ($attributes as $key => $val)
540 {
541 $label .= ' '.$key.'="'.$val.'"';
542 }
543 }
544
545 $label .= ">$label_text</label>";
546
547 return $label;
548 }
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 {
567 $fieldset = "<fieldset";
568
569 $fieldset .= _attributes_to_string($attributes, FALSE);
570
571 $fieldset .= ">\n";
572
573 if ($legend_text != '')
574 {
575 $fieldset .= "<legend>$legend_text</legend>\n";
576 }
577
578 return $fieldset;
579 }
580}
581
582// ------------------------------------------------------------------------
583
584/**
585 * Fieldset Close Tag
586 *
587 * @access public
588 * @param string
589 * @return string
590 */
591if ( ! function_exists('form_fieldset_close'))
592{
593 function form_fieldset_close($extra = '')
594 {
595 return "</fieldset>".$extra;
596 }
597}
598
599// ------------------------------------------------------------------------
600
601/**
602 * Form Close Tag
603 *
604 * @access public
605 * @param string
606 * @return string
607 */
608if ( ! function_exists('form_close'))
609{
610 function form_close($extra = '')
611 {
612 return "</form>".$extra;
613 }
614}
615
616// ------------------------------------------------------------------------
617
618/**
619 * Form Prep
620 *
621 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
622 *
623 * @access public
624 * @param string
625 * @return string
626 */
627if ( ! function_exists('form_prep'))
628{
Derek Jones01a9b102009-07-17 18:30:36 +0000629 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Derek Jones01a9b102009-07-17 18:30:36 +0000631 static $prepped_fields = array();
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 // if the field name is an array we do this recursively
634 if (is_array($str))
635 {
636 foreach ($str as $key => $val)
637 {
638 $str[$key] = form_prep($val);
639 }
640
641 return $str;
642 }
643
644 if ($str === '')
645 {
646 return '';
647 }
648
Derek Jones3eb9fac2009-07-17 20:25:13 +0000649 // we've already prepped a field with this name
650 // @todo need to figure out a way to namespace this so
651 // that we know the *exact* field and not just one with
652 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000653 if (isset($prepped_fields[$field_name]))
654 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000655 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000656 }
freewil8cc0cfe2011-08-27 21:53:00 -0400657
658 $str = html_escape($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000659
Derek Jones01a9b102009-07-17 18:30:36 +0000660 if ($field_name != '')
661 {
Derek Jones68788d52010-03-05 10:11:31 -0600662 $prepped_fields[$field_name] = $field_name;
Derek Jones01a9b102009-07-17 18:30:36 +0000663 }
Barry Mienydd671972010-10-04 16:33:58 +0200664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 return $str;
666 }
667}
668
669// ------------------------------------------------------------------------
670
671/**
672 * Form Value
673 *
674 * Grabs a value from the POST array for the specified field so you can
Derek Jones37f4b9c2011-07-01 17:56:50 -0500675 * re-populate an input field or textarea. If Form Validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 * is active it retrieves the info from the validation class
677 *
678 * @access public
679 * @param string
680 * @return mixed
681 */
682if ( ! function_exists('set_value'))
683{
684 function set_value($field = '', $default = '')
685 {
686 if (FALSE === ($OBJ =& _get_validation_object()))
687 {
688 if ( ! isset($_POST[$field]))
689 {
690 return $default;
691 }
692
Derek Jones01a9b102009-07-17 18:30:36 +0000693 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 }
695
Derek Jones01a9b102009-07-17 18:30:36 +0000696 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 }
698}
699
700// ------------------------------------------------------------------------
701
702/**
703 * Set Select
704 *
705 * Let's you set the selected value of a <select> menu via data in the POST array.
706 * If Form Validation is active it retrieves the info from the validation class
707 *
708 * @access public
709 * @param string
710 * @param string
711 * @param bool
712 * @return string
713 */
714if ( ! function_exists('set_select'))
715{
716 function set_select($field = '', $value = '', $default = FALSE)
717 {
718 $OBJ =& _get_validation_object();
719
720 if ($OBJ === FALSE)
721 {
722 if ( ! isset($_POST[$field]))
723 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000724 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 {
726 return ' selected="selected"';
727 }
728 return '';
729 }
730
731 $field = $_POST[$field];
732
733 if (is_array($field))
734 {
735 if ( ! in_array($value, $field))
736 {
737 return '';
738 }
739 }
740 else
741 {
742 if (($field == '' OR $value == '') OR ($field != $value))
743 {
744 return '';
745 }
746 }
747
748 return ' selected="selected"';
749 }
750
751 return $OBJ->set_select($field, $value, $default);
752 }
753}
754
755// ------------------------------------------------------------------------
756
757/**
758 * Set Checkbox
759 *
760 * Let's you set the selected value of a checkbox via the value in the POST array.
761 * If Form Validation is active it retrieves the info from the validation class
762 *
763 * @access public
764 * @param string
765 * @param string
766 * @param bool
767 * @return string
768 */
769if ( ! function_exists('set_checkbox'))
770{
771 function set_checkbox($field = '', $value = '', $default = FALSE)
772 {
773 $OBJ =& _get_validation_object();
774
775 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200776 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 if ( ! isset($_POST[$field]))
778 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000779 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 {
781 return ' checked="checked"';
782 }
783 return '';
784 }
785
786 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200787
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 if (is_array($field))
789 {
790 if ( ! in_array($value, $field))
791 {
792 return '';
793 }
794 }
795 else
796 {
797 if (($field == '' OR $value == '') OR ($field != $value))
798 {
799 return '';
800 }
801 }
802
803 return ' checked="checked"';
804 }
805
806 return $OBJ->set_checkbox($field, $value, $default);
807 }
808}
809
810// ------------------------------------------------------------------------
811
812/**
813 * Set Radio
814 *
815 * Let's you set the selected value of a radio field via info in the POST array.
816 * If Form Validation is active it retrieves the info from the validation class
817 *
818 * @access public
819 * @param string
820 * @param string
821 * @param bool
822 * @return string
823 */
824if ( ! function_exists('set_radio'))
825{
826 function set_radio($field = '', $value = '', $default = FALSE)
827 {
828 $OBJ =& _get_validation_object();
829
830 if ($OBJ === FALSE)
831 {
832 if ( ! isset($_POST[$field]))
833 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000834 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 {
836 return ' checked="checked"';
837 }
838 return '';
839 }
840
841 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 if (is_array($field))
844 {
845 if ( ! in_array($value, $field))
846 {
847 return '';
848 }
849 }
850 else
851 {
852 if (($field == '' OR $value == '') OR ($field != $value))
853 {
854 return '';
855 }
856 }
857
858 return ' checked="checked"';
859 }
860
861 return $OBJ->set_radio($field, $value, $default);
862 }
863}
864
865// ------------------------------------------------------------------------
866
867/**
868 * Form Error
869 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500870 * Returns the error for a specific form field. This is a helper for the
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 * form validation class.
872 *
873 * @access public
874 * @param string
875 * @param string
876 * @param string
877 * @return string
878 */
879if ( ! function_exists('form_error'))
880{
881 function form_error($field = '', $prefix = '', $suffix = '')
882 {
883 if (FALSE === ($OBJ =& _get_validation_object()))
884 {
885 return '';
886 }
887
888 return $OBJ->error($field, $prefix, $suffix);
889 }
890}
891
892// ------------------------------------------------------------------------
893
894/**
895 * Validation Error String
896 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500897 * Returns all the errors associated with a form submission. This is a helper
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 * function for the form validation class.
899 *
900 * @access public
901 * @param string
902 * @param string
903 * @return string
904 */
905if ( ! function_exists('validation_errors'))
906{
907 function validation_errors($prefix = '', $suffix = '')
908 {
909 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500910 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 return '';
912 }
913
914 return $OBJ->error_string($prefix, $suffix);
915 }
916}
917
918// ------------------------------------------------------------------------
919
920/**
921 * Parse the form attributes
922 *
923 * Helper function used by some of the form helpers
924 *
925 * @access private
926 * @param array
927 * @param array
928 * @return string
929 */
930if ( ! function_exists('_parse_form_attributes'))
931{
932 function _parse_form_attributes($attributes, $default)
933 {
934 if (is_array($attributes))
935 {
936 foreach ($default as $key => $val)
937 {
938 if (isset($attributes[$key]))
939 {
940 $default[$key] = $attributes[$key];
941 unset($attributes[$key]);
942 }
943 }
944
945 if (count($attributes) > 0)
946 {
947 $default = array_merge($default, $attributes);
948 }
949 }
950
951 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200952
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 foreach ($default as $key => $val)
954 {
955 if ($key == 'value')
956 {
Derek Jones01a9b102009-07-17 18:30:36 +0000957 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 }
959
960 $att .= $key . '="' . $val . '" ';
961 }
962
963 return $att;
964 }
965}
966
967// ------------------------------------------------------------------------
968
969/**
970 * Attributes To String
971 *
972 * Helper function used by some of the form helpers
973 *
974 * @access private
975 * @param mixed
976 * @param bool
977 * @return string
978 */
979if ( ! function_exists('_attributes_to_string'))
980{
981 function _attributes_to_string($attributes, $formtag = FALSE)
982 {
983 if (is_string($attributes) AND strlen($attributes) > 0)
984 {
985 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
986 {
987 $attributes .= ' method="post"';
988 }
989
Derek Allard3241d732009-09-17 12:17:45 +0000990 if ($formtag == TRUE AND strpos($attributes, 'accept-charset=') === FALSE)
991 {
992 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
993 }
994
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 return ' '.$attributes;
996 }
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 if (is_object($attributes) AND count($attributes) > 0)
999 {
1000 $attributes = (array)$attributes;
1001 }
1002
1003 if (is_array($attributes) AND count($attributes) > 0)
1004 {
Derek Allard3241d732009-09-17 12:17:45 +00001005 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001006
Derek Allard3241d732009-09-17 12:17:45 +00001007 if ( ! isset($attributes['method']) AND $formtag === TRUE)
1008 {
1009 $atts .= ' method="post"';
1010 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001011
Derek Allard3241d732009-09-17 12:17:45 +00001012 if ( ! isset($attributes['accept-charset']) AND $formtag === TRUE)
1013 {
1014 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
1015 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001016
Derek Allard3241d732009-09-17 12:17:45 +00001017 foreach ($attributes as $key => $val)
1018 {
1019 $atts .= ' '.$key.'="'.$val.'"';
1020 }
1021
1022 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 }
1024 }
1025}
1026
1027// ------------------------------------------------------------------------
1028
1029/**
1030 * Validation Object
1031 *
1032 * Determines what the form validation class was instantiated as, fetches
1033 * the object and returns it.
1034 *
1035 * @access private
1036 * @return mixed
1037 */
1038if ( ! function_exists('_get_validation_object'))
1039{
1040 function &_get_validation_object()
1041 {
1042 $CI =& get_instance();
1043
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001044 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 $return = FALSE;
Derek Jones37f4b9c2011-07-01 17:56:50 -05001046
Greg Akerc6d918a2011-04-22 10:17:32 -05001047 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001049 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1050 {
1051 return $return;
1052 }
Derek Jones37f4b9c2011-07-01 17:56:50 -05001053
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001054 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 }
Derek Jones37f4b9c2011-07-01 17:56:50 -05001056
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001057 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 }
1059}
1060
1061
1062/* End of file form_helper.php */
patwork64e35cd2011-04-08 15:25:31 +02001063/* Location: ./system/helpers/form_helper.php */