Subversion-Projekte lars-tiefland.ci

Revision

Revision 2257 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
2414 lars 9
 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
68 lars 10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 *
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
2414 lars 32
 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33
 * @license	https://opensource.org/licenses/MIT	MIT License
68 lars 34
 * @link	https://codeigniter.com
35
 * @since	Version 1.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * CodeIgniter Form Helpers
42
 *
43
 * @package		CodeIgniter
44
 * @subpackage	Helpers
45
 * @category	Helpers
46
 * @author		EllisLab Dev Team
47
 * @link		https://codeigniter.com/user_guide/helpers/form_helper.html
48
 */
49
 
50
// ------------------------------------------------------------------------
51
 
52
if ( ! function_exists('form_open'))
53
{
54
	/**
55
	 * Form Declaration
56
	 *
57
	 * Creates the opening portion of the form.
58
	 *
59
	 * @param	string	the URI segments of the form destination
60
	 * @param	array	a key/value pair of attributes
61
	 * @param	array	a key/value pair hidden data
62
	 * @return	string
63
	 */
64
	function form_open($action = '', $attributes = array(), $hidden = array())
65
	{
66
		$CI =& get_instance();
67
 
68
		// If no action is provided then set to the current url
69
		if ( ! $action)
70
		{
71
			$action = $CI->config->site_url($CI->uri->uri_string());
72
		}
73
		// If an action is not a full URL then turn it into one
74
		elseif (strpos($action, '://') === FALSE)
75
		{
76
			$action = $CI->config->site_url($action);
77
		}
78
 
79
		$attributes = _attributes_to_string($attributes);
80
 
81
		if (stripos($attributes, 'method=') === FALSE)
82
		{
83
			$attributes .= ' method="post"';
84
		}
85
 
86
		if (stripos($attributes, 'accept-charset=') === FALSE)
87
		{
88
			$attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
89
		}
90
 
91
		$form = '<form action="'.$action.'"'.$attributes.">\n";
92
 
93
		if (is_array($hidden))
94
		{
95
			foreach ($hidden as $name => $value)
96
			{
1257 lars 97
				$form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value).'" />'."\n";
68 lars 98
			}
99
		}
100
 
2049 lars 101
		// Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
102
		if ($CI->config->item('csrf_protection') === TRUE && strpos($action, $CI->config->base_url()) !== FALSE && ! stripos($form, 'method="get"'))
103
		{
104
			// Prepend/append random-length "white noise" around the CSRF
105
			// token input, as a form of protection against BREACH attacks
106
			if (FALSE !== ($noise = $CI->security->get_random_bytes(1)))
107
			{
108
				list(, $noise) = unpack('c', $noise);
109
			}
110
			else
111
			{
112
				$noise = mt_rand(-128, 127);
113
			}
114
 
115
			// Prepend if $noise has a negative value, append if positive, do nothing for zero
116
			$prepend = $append = '';
117
			if ($noise < 0)
118
			{
119
				$prepend = str_repeat(" ", abs($noise));
120
			}
121
			elseif ($noise > 0)
122
			{
123
				$append  = str_repeat(" ", $noise);
124
			}
125
 
126
			$form .= sprintf(
127
				'%s<input type="hidden" name="%s" value="%s" />%s%s',
128
				$prepend,
129
				$CI->security->get_csrf_token_name(),
130
				$CI->security->get_csrf_hash(),
131
				$append,
132
				"\n"
133
			);
134
		}
135
 
68 lars 136
		return $form;
137
	}
138
}
139
 
140
// ------------------------------------------------------------------------
141
 
142
if ( ! function_exists('form_open_multipart'))
143
{
144
	/**
145
	 * Form Declaration - Multipart type
146
	 *
147
	 * Creates the opening portion of the form, but with "multipart/form-data".
148
	 *
149
	 * @param	string	the URI segments of the form destination
150
	 * @param	array	a key/value pair of attributes
151
	 * @param	array	a key/value pair hidden data
152
	 * @return	string
153
	 */
154
	function form_open_multipart($action = '', $attributes = array(), $hidden = array())
155
	{
156
		if (is_string($attributes))
157
		{
158
			$attributes .= ' enctype="multipart/form-data"';
159
		}
160
		else
161
		{
162
			$attributes['enctype'] = 'multipart/form-data';
163
		}
164
 
165
		return form_open($action, $attributes, $hidden);
166
	}
167
}
168
 
169
// ------------------------------------------------------------------------
170
 
171
if ( ! function_exists('form_hidden'))
172
{
173
	/**
174
	 * Hidden Input Field
175
	 *
176
	 * Generates hidden fields. You can pass a simple key/value string or
177
	 * an associative array with multiple values.
178
	 *
179
	 * @param	mixed	$name		Field name
180
	 * @param	string	$value		Field value
181
	 * @param	bool	$recursing
182
	 * @return	string
183
	 */
184
	function form_hidden($name, $value = '', $recursing = FALSE)
185
	{
186
		static $form;
187
 
188
		if ($recursing === FALSE)
189
		{
190
			$form = "\n";
191
		}
192
 
193
		if (is_array($name))
194
		{
195
			foreach ($name as $key => $val)
196
			{
197
				form_hidden($key, $val, TRUE);
198
			}
199
 
200
			return $form;
201
		}
202
 
203
		if ( ! is_array($value))
204
		{
205
			$form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value)."\" />\n";
206
		}
207
		else
208
		{
209
			foreach ($value as $k => $v)
210
			{
211
				$k = is_int($k) ? '' : $k;
212
				form_hidden($name.'['.$k.']', $v, TRUE);
213
			}
214
		}
215
 
216
		return $form;
217
	}
218
}
219
 
220
// ------------------------------------------------------------------------
221
 
222
if ( ! function_exists('form_input'))
223
{
224
	/**
225
	 * Text Input Field
226
	 *
227
	 * @param	mixed
228
	 * @param	string
229
	 * @param	mixed
230
	 * @return	string
231
	 */
232
	function form_input($data = '', $value = '', $extra = '')
233
	{
234
		$defaults = array(
235
			'type' => 'text',
236
			'name' => is_array($data) ? '' : $data,
237
			'value' => $value
238
		);
239
 
240
		return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
241
	}
242
}
243
 
244
// ------------------------------------------------------------------------
245
 
246
if ( ! function_exists('form_password'))
247
{
248
	/**
249
	 * Password Field
250
	 *
251
	 * Identical to the input function but adds the "password" type
252
	 *
253
	 * @param	mixed
254
	 * @param	string
255
	 * @param	mixed
256
	 * @return	string
257
	 */
258
	function form_password($data = '', $value = '', $extra = '')
259
	{
260
		is_array($data) OR $data = array('name' => $data);
261
		$data['type'] = 'password';
262
		return form_input($data, $value, $extra);
263
	}
264
}
265
 
266
// ------------------------------------------------------------------------
267
 
268
if ( ! function_exists('form_upload'))
269
{
270
	/**
271
	 * Upload Field
272
	 *
273
	 * Identical to the input function but adds the "file" type
274
	 *
275
	 * @param	mixed
276
	 * @param	string
277
	 * @param	mixed
278
	 * @return	string
279
	 */
280
	function form_upload($data = '', $value = '', $extra = '')
281
	{
282
		$defaults = array('type' => 'file', 'name' => '');
283
		is_array($data) OR $data = array('name' => $data);
284
		$data['type'] = 'file';
285
 
286
		return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
287
	}
288
}
289
 
290
// ------------------------------------------------------------------------
291
 
292
if ( ! function_exists('form_textarea'))
293
{
294
	/**
295
	 * Textarea field
296
	 *
297
	 * @param	mixed	$data
298
	 * @param	string	$value
299
	 * @param	mixed	$extra
300
	 * @return	string
301
	 */
302
	function form_textarea($data = '', $value = '', $extra = '')
303
	{
304
		$defaults = array(
305
			'name' => is_array($data) ? '' : $data,
306
			'cols' => '40',
307
			'rows' => '10'
308
		);
309
 
310
		if ( ! is_array($data) OR ! isset($data['value']))
311
		{
312
			$val = $value;
313
		}
314
		else
315
		{
316
			$val = $data['value'];
317
			unset($data['value']); // textareas don't use the value attribute
318
		}
319
 
320
		return '<textarea '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
321
			.html_escape($val)
322
			."</textarea>\n";
323
	}
324
}
325
 
326
// ------------------------------------------------------------------------
327
 
328
if ( ! function_exists('form_multiselect'))
329
{
330
	/**
331
	 * Multi-select menu
332
	 *
333
	 * @param	string
334
	 * @param	array
335
	 * @param	mixed
336
	 * @param	mixed
337
	 * @return	string
338
	 */
339
	function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
340
	{
341
		$extra = _attributes_to_string($extra);
342
		if (stripos($extra, 'multiple') === FALSE)
343
		{
344
			$extra .= ' multiple="multiple"';
345
		}
346
 
347
		return form_dropdown($name, $options, $selected, $extra);
348
	}
349
}
350
 
351
// --------------------------------------------------------------------
352
 
353
if ( ! function_exists('form_dropdown'))
354
{
355
	/**
356
	 * Drop-down Menu
357
	 *
358
	 * @param	mixed	$data
359
	 * @param	mixed	$options
360
	 * @param	mixed	$selected
361
	 * @param	mixed	$extra
362
	 * @return	string
363
	 */
364
	function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
365
	{
366
		$defaults = array();
367
 
368
		if (is_array($data))
369
		{
370
			if (isset($data['selected']))
371
			{
372
				$selected = $data['selected'];
373
				unset($data['selected']); // select tags don't have a selected attribute
374
			}
375
 
376
			if (isset($data['options']))
377
			{
378
				$options = $data['options'];
379
				unset($data['options']); // select tags don't use an options attribute
380
			}
381
		}
382
		else
383
		{
384
			$defaults = array('name' => $data);
385
		}
386
 
387
		is_array($selected) OR $selected = array($selected);
388
		is_array($options) OR $options = array($options);
389
 
390
		// If no selected state was submitted we will attempt to set it automatically
391
		if (empty($selected))
392
		{
393
			if (is_array($data))
394
			{
395
				if (isset($data['name'], $_POST[$data['name']]))
396
				{
397
					$selected = array($_POST[$data['name']]);
398
				}
399
			}
400
			elseif (isset($_POST[$data]))
401
			{
402
				$selected = array($_POST[$data]);
403
			}
404
		}
405
 
406
		$extra = _attributes_to_string($extra);
407
 
408
		$multiple = (count($selected) > 1 && stripos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
409
 
410
		$form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
411
 
412
		foreach ($options as $key => $val)
413
		{
414
			$key = (string) $key;
415
 
416
			if (is_array($val))
417
			{
418
				if (empty($val))
419
				{
420
					continue;
421
				}
422
 
423
				$form .= '<optgroup label="'.$key."\">\n";
424
 
425
				foreach ($val as $optgroup_key => $optgroup_val)
426
				{
427
					$sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
428
					$form .= '<option value="'.html_escape($optgroup_key).'"'.$sel.'>'
429
						.(string) $optgroup_val."</option>\n";
430
				}
431
 
432
				$form .= "</optgroup>\n";
433
			}
434
			else
435
			{
436
				$form .= '<option value="'.html_escape($key).'"'
437
					.(in_array($key, $selected) ? ' selected="selected"' : '').'>'
438
					.(string) $val."</option>\n";
439
			}
440
		}
441
 
442
		return $form."</select>\n";
443
	}
444
}
445
 
446
// ------------------------------------------------------------------------
447
 
448
if ( ! function_exists('form_checkbox'))
449
{
450
	/**
451
	 * Checkbox Field
452
	 *
453
	 * @param	mixed
454
	 * @param	string
455
	 * @param	bool
456
	 * @param	mixed
457
	 * @return	string
458
	 */
459
	function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
460
	{
461
		$defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
462
 
463
		if (is_array($data) && array_key_exists('checked', $data))
464
		{
465
			$checked = $data['checked'];
466
 
467
			if ($checked == FALSE)
468
			{
469
				unset($data['checked']);
470
			}
471
			else
472
			{
473
				$data['checked'] = 'checked';
474
			}
475
		}
476
 
477
		if ($checked == TRUE)
478
		{
479
			$defaults['checked'] = 'checked';
480
		}
481
		else
482
		{
483
			unset($defaults['checked']);
484
		}
485
 
486
		return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
487
	}
488
}
489
 
490
// ------------------------------------------------------------------------
491
 
492
if ( ! function_exists('form_radio'))
493
{
494
	/**
495
	 * Radio Button
496
	 *
497
	 * @param	mixed
498
	 * @param	string
499
	 * @param	bool
500
	 * @param	mixed
501
	 * @return	string
502
	 */
503
	function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
504
	{
505
		is_array($data) OR $data = array('name' => $data);
506
		$data['type'] = 'radio';
507
 
508
		return form_checkbox($data, $value, $checked, $extra);
509
	}
510
}
511
 
512
// ------------------------------------------------------------------------
513
 
514
if ( ! function_exists('form_submit'))
515
{
516
	/**
517
	 * Submit Button
518
	 *
519
	 * @param	mixed
520
	 * @param	string
521
	 * @param	mixed
522
	 * @return	string
523
	 */
524
	function form_submit($data = '', $value = '', $extra = '')
525
	{
526
		$defaults = array(
527
			'type' => 'submit',
528
			'name' => is_array($data) ? '' : $data,
529
			'value' => $value
530
		);
531
 
532
		return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
533
	}
534
}
535
 
536
// ------------------------------------------------------------------------
537
 
538
if ( ! function_exists('form_reset'))
539
{
540
	/**
541
	 * Reset Button
542
	 *
543
	 * @param	mixed
544
	 * @param	string
545
	 * @param	mixed
546
	 * @return	string
547
	 */
548
	function form_reset($data = '', $value = '', $extra = '')
549
	{
550
		$defaults = array(
551
			'type' => 'reset',
552
			'name' => is_array($data) ? '' : $data,
553
			'value' => $value
554
		);
555
 
556
		return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
557
	}
558
}
559
 
560
// ------------------------------------------------------------------------
561
 
562
if ( ! function_exists('form_button'))
563
{
564
	/**
565
	 * Form Button
566
	 *
567
	 * @param	mixed
568
	 * @param	string
569
	 * @param	mixed
570
	 * @return	string
571
	 */
572
	function form_button($data = '', $content = '', $extra = '')
573
	{
574
		$defaults = array(
575
			'name' => is_array($data) ? '' : $data,
576
			'type' => 'button'
577
		);
578
 
579
		if (is_array($data) && isset($data['content']))
580
		{
581
			$content = $data['content'];
582
			unset($data['content']); // content is not an attribute
583
		}
584
 
585
		return '<button '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
586
			.$content
587
			."</button>\n";
588
	}
589
}
590
 
591
// ------------------------------------------------------------------------
592
 
593
if ( ! function_exists('form_label'))
594
{
595
	/**
596
	 * Form Label Tag
597
	 *
598
	 * @param	string	The text to appear onscreen
599
	 * @param	string	The id the label applies to
2107 lars 600
	 * @param	mixed	Additional attributes
68 lars 601
	 * @return	string
602
	 */
603
	function form_label($label_text = '', $id = '', $attributes = array())
604
	{
605
 
606
		$label = '<label';
607
 
608
		if ($id !== '')
609
		{
610
			$label .= ' for="'.$id.'"';
611
		}
612
 
2107 lars 613
		$label .= _attributes_to_string($attributes);
68 lars 614
 
615
		return $label.'>'.$label_text.'</label>';
616
	}
617
}
618
 
619
// ------------------------------------------------------------------------
620
 
621
if ( ! function_exists('form_fieldset'))
622
{
623
	/**
624
	 * Fieldset Tag
625
	 *
626
	 * Used to produce <fieldset><legend>text</legend>.  To close fieldset
627
	 * use form_fieldset_close()
628
	 *
629
	 * @param	string	The legend text
630
	 * @param	array	Additional attributes
631
	 * @return	string
632
	 */
633
	function form_fieldset($legend_text = '', $attributes = array())
634
	{
635
		$fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
636
		if ($legend_text !== '')
637
		{
638
			return $fieldset.'<legend>'.$legend_text."</legend>\n";
639
		}
640
 
641
		return $fieldset;
642
	}
643
}
644
 
645
// ------------------------------------------------------------------------
646
 
647
if ( ! function_exists('form_fieldset_close'))
648
{
649
	/**
650
	 * Fieldset Close Tag
651
	 *
652
	 * @param	string
653
	 * @return	string
654
	 */
655
	function form_fieldset_close($extra = '')
656
	{
657
		return '</fieldset>'.$extra;
658
	}
659
}
660
 
661
// ------------------------------------------------------------------------
662
 
663
if ( ! function_exists('form_close'))
664
{
665
	/**
666
	 * Form Close Tag
667
	 *
668
	 * @param	string
669
	 * @return	string
670
	 */
671
	function form_close($extra = '')
672
	{
673
		return '</form>'.$extra;
674
	}
675
}
676
 
677
// ------------------------------------------------------------------------
678
 
679
if ( ! function_exists('form_prep'))
680
{
681
	/**
682
	 * Form Prep
683
	 *
684
	 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
685
	 *
686
	 * @deprecated	3.0.0	An alias for html_escape()
687
	 * @param	string|string[]	$str		Value to escape
688
	 * @return	string|string[]	Escaped values
689
	 */
690
	function form_prep($str)
691
	{
692
		return html_escape($str, TRUE);
693
	}
694
}
695
 
696
// ------------------------------------------------------------------------
697
 
698
if ( ! function_exists('set_value'))
699
{
700
	/**
701
	 * Form Value
702
	 *
703
	 * Grabs a value from the POST array for the specified field so you can
704
	 * re-populate an input field or textarea. If Form Validation
705
	 * is active it retrieves the info from the validation class
706
	 *
707
	 * @param	string	$field		Field name
708
	 * @param	string	$default	Default value
709
	 * @param	bool	$html_escape	Whether to escape HTML special characters or not
710
	 * @return	string
711
	 */
712
	function set_value($field, $default = '', $html_escape = TRUE)
713
	{
714
		$CI =& get_instance();
715
 
716
		$value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
717
			? $CI->form_validation->set_value($field, $default)
718
			: $CI->input->post($field, FALSE);
719
 
720
		isset($value) OR $value = $default;
721
		return ($html_escape) ? html_escape($value) : $value;
722
	}
723
}
724
 
725
// ------------------------------------------------------------------------
726
 
727
if ( ! function_exists('set_select'))
728
{
729
	/**
730
	 * Set Select
731
	 *
732
	 * Let's you set the selected value of a <select> menu via data in the POST array.
733
	 * If Form Validation is active it retrieves the info from the validation class
734
	 *
735
	 * @param	string
736
	 * @param	string
737
	 * @param	bool
738
	 * @return	string
739
	 */
740
	function set_select($field, $value = '', $default = FALSE)
741
	{
742
		$CI =& get_instance();
743
 
744
		if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
745
		{
746
			return $CI->form_validation->set_select($field, $value, $default);
747
		}
748
		elseif (($input = $CI->input->post($field, FALSE)) === NULL)
749
		{
750
			return ($default === TRUE) ? ' selected="selected"' : '';
751
		}
752
 
753
		$value = (string) $value;
754
		if (is_array($input))
755
		{
756
			// Note: in_array('', array(0)) returns TRUE, do not use it
757
			foreach ($input as &$v)
758
			{
759
				if ($value === $v)
760
				{
761
					return ' selected="selected"';
762
				}
763
			}
764
 
765
			return '';
766
		}
767
 
768
		return ($input === $value) ? ' selected="selected"' : '';
769
	}
770
}
771
 
772
// ------------------------------------------------------------------------
773
 
774
if ( ! function_exists('set_checkbox'))
775
{
776
	/**
777
	 * Set Checkbox
778
	 *
779
	 * Let's you set the selected value of a checkbox via the value in the POST array.
780
	 * If Form Validation is active it retrieves the info from the validation class
781
	 *
782
	 * @param	string
783
	 * @param	string
784
	 * @param	bool
785
	 * @return	string
786
	 */
787
	function set_checkbox($field, $value = '', $default = FALSE)
788
	{
789
		$CI =& get_instance();
790
 
791
		if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
792
		{
793
			return $CI->form_validation->set_checkbox($field, $value, $default);
794
		}
795
 
796
		// Form inputs are always strings ...
797
		$value = (string) $value;
798
		$input = $CI->input->post($field, FALSE);
799
 
800
		if (is_array($input))
801
		{
802
			// Note: in_array('', array(0)) returns TRUE, do not use it
803
			foreach ($input as &$v)
804
			{
805
				if ($value === $v)
806
				{
807
					return ' checked="checked"';
808
				}
809
			}
810
 
811
			return '';
812
		}
813
 
814
		// Unchecked checkbox and radio inputs are not even submitted by browsers ...
815
		if ($CI->input->method() === 'post')
816
		{
817
			return ($input === $value) ? ' checked="checked"' : '';
818
		}
819
 
820
		return ($default === TRUE) ? ' checked="checked"' : '';
821
	}
822
}
823
 
824
// ------------------------------------------------------------------------
825
 
826
if ( ! function_exists('set_radio'))
827
{
828
	/**
829
	 * Set Radio
830
	 *
831
	 * Let's you set the selected value of a radio field via info in the POST array.
832
	 * If Form Validation is active it retrieves the info from the validation class
833
	 *
834
	 * @param	string	$field
835
	 * @param	string	$value
836
	 * @param	bool	$default
837
	 * @return	string
838
	 */
839
	function set_radio($field, $value = '', $default = FALSE)
840
	{
841
		$CI =& get_instance();
842
 
843
		if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
844
		{
845
			return $CI->form_validation->set_radio($field, $value, $default);
846
		}
847
 
848
		// Form inputs are always strings ...
849
		$value = (string) $value;
850
		$input = $CI->input->post($field, FALSE);
851
 
852
		if (is_array($input))
853
		{
854
			// Note: in_array('', array(0)) returns TRUE, do not use it
855
			foreach ($input as &$v)
856
			{
857
				if ($value === $v)
858
				{
859
					return ' checked="checked"';
860
				}
861
			}
862
 
863
			return '';
864
		}
865
 
866
		// Unchecked checkbox and radio inputs are not even submitted by browsers ...
867
		if ($CI->input->method() === 'post')
868
		{
869
			return ($input === $value) ? ' checked="checked"' : '';
870
		}
871
 
872
		return ($default === TRUE) ? ' checked="checked"' : '';
873
	}
874
}
875
 
876
// ------------------------------------------------------------------------
877
 
878
if ( ! function_exists('form_error'))
879
{
880
	/**
881
	 * Form Error
882
	 *
883
	 * Returns the error for a specific form field. This is a helper for the
884
	 * form validation class.
885
	 *
886
	 * @param	string
887
	 * @param	string
888
	 * @param	string
889
	 * @return	string
890
	 */
891
	function form_error($field = '', $prefix = '', $suffix = '')
892
	{
893
		if (FALSE === ($OBJ =& _get_validation_object()))
894
		{
895
			return '';
896
		}
897
 
898
		return $OBJ->error($field, $prefix, $suffix);
899
	}
900
}
901
 
902
// ------------------------------------------------------------------------
903
 
904
if ( ! function_exists('validation_errors'))
905
{
906
	/**
907
	 * Validation Error String
908
	 *
909
	 * Returns all the errors associated with a form submission. This is a helper
910
	 * function for the form validation class.
911
	 *
912
	 * @param	string
913
	 * @param	string
914
	 * @return	string
915
	 */
916
	function validation_errors($prefix = '', $suffix = '')
917
	{
918
		if (FALSE === ($OBJ =& _get_validation_object()))
919
		{
920
			return '';
921
		}
922
 
923
		return $OBJ->error_string($prefix, $suffix);
924
	}
925
}
926
 
927
// ------------------------------------------------------------------------
928
 
929
if ( ! function_exists('_parse_form_attributes'))
930
{
931
	/**
932
	 * Parse the form attributes
933
	 *
934
	 * Helper function used by some of the form helpers
935
	 *
936
	 * @param	array	$attributes	List of attributes
937
	 * @param	array	$default	Default values
938
	 * @return	string
939
	 */
940
	function _parse_form_attributes($attributes, $default)
941
	{
942
		if (is_array($attributes))
943
		{
944
			foreach ($default as $key => $val)
945
			{
946
				if (isset($attributes[$key]))
947
				{
948
					$default[$key] = $attributes[$key];
949
					unset($attributes[$key]);
950
				}
951
			}
952
 
953
			if (count($attributes) > 0)
954
			{
955
				$default = array_merge($default, $attributes);
956
			}
957
		}
958
 
959
		$att = '';
960
 
961
		foreach ($default as $key => $val)
962
		{
963
			if ($key === 'value')
964
			{
965
				$val = html_escape($val);
966
			}
967
			elseif ($key === 'name' && ! strlen($default['name']))
968
			{
969
				continue;
970
			}
971
 
972
			$att .= $key.'="'.$val.'" ';
973
		}
974
 
975
		return $att;
976
	}
977
}
978
 
979
// ------------------------------------------------------------------------
980
 
981
if ( ! function_exists('_attributes_to_string'))
982
{
983
	/**
984
	 * Attributes To String
985
	 *
986
	 * Helper function used by some of the form helpers
987
	 *
988
	 * @param	mixed
989
	 * @return	string
990
	 */
991
	function _attributes_to_string($attributes)
992
	{
993
		if (empty($attributes))
994
		{
995
			return '';
996
		}
997
 
998
		if (is_object($attributes))
999
		{
1000
			$attributes = (array) $attributes;
1001
		}
1002
 
1003
		if (is_array($attributes))
1004
		{
1005
			$atts = '';
1006
 
1007
			foreach ($attributes as $key => $val)
1008
			{
1009
				$atts .= ' '.$key.'="'.$val.'"';
1010
			}
1011
 
1012
			return $atts;
1013
		}
1014
 
1015
		if (is_string($attributes))
1016
		{
1017
			return ' '.$attributes;
1018
		}
1019
 
1020
		return FALSE;
1021
	}
1022
}
1023
 
1024
// ------------------------------------------------------------------------
1025
 
1026
if ( ! function_exists('_get_validation_object'))
1027
{
1028
	/**
1029
	 * Validation Object
1030
	 *
1031
	 * Determines what the form validation class was instantiated as, fetches
1032
	 * the object and returns it.
1033
	 *
1034
	 * @return	mixed
1035
	 */
1036
	function &_get_validation_object()
1037
	{
1038
		$CI =& get_instance();
1039
 
1040
		// We set this as a variable since we're returning by reference.
1041
		$return = FALSE;
1042
 
1043
		if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
1044
		{
1045
			if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1046
			{
1047
				return $return;
1048
			}
1049
 
1050
			return $CI->$object;
1051
		}
1052
 
1053
		return $return;
1054
	}
1055
}