Subversion-Projekte lars-tiefland.ci

Revision

Revision 1257 | Revision 2107 | Zur aktuellen Revision | 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
 *
2049 lars 9
 * Copyright (c) 2014 - 2017, 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/)
2049 lars 32
 * @copyright	Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
68 lars 33
 * @license	http://opensource.org/licenses/MIT	MIT License
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
600
	 * @param	array	Additional attributes
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
 
613
		if (is_array($attributes) && count($attributes) > 0)
614
		{
615
			foreach ($attributes as $key => $val)
616
			{
617
				$label .= ' '.$key.'="'.$val.'"';
618
			}
619
		}
620
 
621
		return $label.'>'.$label_text.'</label>';
622
	}
623
}
624
 
625
// ------------------------------------------------------------------------
626
 
627
if ( ! function_exists('form_fieldset'))
628
{
629
	/**
630
	 * Fieldset Tag
631
	 *
632
	 * Used to produce <fieldset><legend>text</legend>.  To close fieldset
633
	 * use form_fieldset_close()
634
	 *
635
	 * @param	string	The legend text
636
	 * @param	array	Additional attributes
637
	 * @return	string
638
	 */
639
	function form_fieldset($legend_text = '', $attributes = array())
640
	{
641
		$fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
642
		if ($legend_text !== '')
643
		{
644
			return $fieldset.'<legend>'.$legend_text."</legend>\n";
645
		}
646
 
647
		return $fieldset;
648
	}
649
}
650
 
651
// ------------------------------------------------------------------------
652
 
653
if ( ! function_exists('form_fieldset_close'))
654
{
655
	/**
656
	 * Fieldset Close Tag
657
	 *
658
	 * @param	string
659
	 * @return	string
660
	 */
661
	function form_fieldset_close($extra = '')
662
	{
663
		return '</fieldset>'.$extra;
664
	}
665
}
666
 
667
// ------------------------------------------------------------------------
668
 
669
if ( ! function_exists('form_close'))
670
{
671
	/**
672
	 * Form Close Tag
673
	 *
674
	 * @param	string
675
	 * @return	string
676
	 */
677
	function form_close($extra = '')
678
	{
679
		return '</form>'.$extra;
680
	}
681
}
682
 
683
// ------------------------------------------------------------------------
684
 
685
if ( ! function_exists('form_prep'))
686
{
687
	/**
688
	 * Form Prep
689
	 *
690
	 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
691
	 *
692
	 * @deprecated	3.0.0	An alias for html_escape()
693
	 * @param	string|string[]	$str		Value to escape
694
	 * @return	string|string[]	Escaped values
695
	 */
696
	function form_prep($str)
697
	{
698
		return html_escape($str, TRUE);
699
	}
700
}
701
 
702
// ------------------------------------------------------------------------
703
 
704
if ( ! function_exists('set_value'))
705
{
706
	/**
707
	 * Form Value
708
	 *
709
	 * Grabs a value from the POST array for the specified field so you can
710
	 * re-populate an input field or textarea. If Form Validation
711
	 * is active it retrieves the info from the validation class
712
	 *
713
	 * @param	string	$field		Field name
714
	 * @param	string	$default	Default value
715
	 * @param	bool	$html_escape	Whether to escape HTML special characters or not
716
	 * @return	string
717
	 */
718
	function set_value($field, $default = '', $html_escape = TRUE)
719
	{
720
		$CI =& get_instance();
721
 
722
		$value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
723
			? $CI->form_validation->set_value($field, $default)
724
			: $CI->input->post($field, FALSE);
725
 
726
		isset($value) OR $value = $default;
727
		return ($html_escape) ? html_escape($value) : $value;
728
	}
729
}
730
 
731
// ------------------------------------------------------------------------
732
 
733
if ( ! function_exists('set_select'))
734
{
735
	/**
736
	 * Set Select
737
	 *
738
	 * Let's you set the selected value of a <select> menu via data in the POST array.
739
	 * If Form Validation is active it retrieves the info from the validation class
740
	 *
741
	 * @param	string
742
	 * @param	string
743
	 * @param	bool
744
	 * @return	string
745
	 */
746
	function set_select($field, $value = '', $default = FALSE)
747
	{
748
		$CI =& get_instance();
749
 
750
		if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
751
		{
752
			return $CI->form_validation->set_select($field, $value, $default);
753
		}
754
		elseif (($input = $CI->input->post($field, FALSE)) === NULL)
755
		{
756
			return ($default === TRUE) ? ' selected="selected"' : '';
757
		}
758
 
759
		$value = (string) $value;
760
		if (is_array($input))
761
		{
762
			// Note: in_array('', array(0)) returns TRUE, do not use it
763
			foreach ($input as &$v)
764
			{
765
				if ($value === $v)
766
				{
767
					return ' selected="selected"';
768
				}
769
			}
770
 
771
			return '';
772
		}
773
 
774
		return ($input === $value) ? ' selected="selected"' : '';
775
	}
776
}
777
 
778
// ------------------------------------------------------------------------
779
 
780
if ( ! function_exists('set_checkbox'))
781
{
782
	/**
783
	 * Set Checkbox
784
	 *
785
	 * Let's you set the selected value of a checkbox via the value in the POST array.
786
	 * If Form Validation is active it retrieves the info from the validation class
787
	 *
788
	 * @param	string
789
	 * @param	string
790
	 * @param	bool
791
	 * @return	string
792
	 */
793
	function set_checkbox($field, $value = '', $default = FALSE)
794
	{
795
		$CI =& get_instance();
796
 
797
		if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
798
		{
799
			return $CI->form_validation->set_checkbox($field, $value, $default);
800
		}
801
 
802
		// Form inputs are always strings ...
803
		$value = (string) $value;
804
		$input = $CI->input->post($field, FALSE);
805
 
806
		if (is_array($input))
807
		{
808
			// Note: in_array('', array(0)) returns TRUE, do not use it
809
			foreach ($input as &$v)
810
			{
811
				if ($value === $v)
812
				{
813
					return ' checked="checked"';
814
				}
815
			}
816
 
817
			return '';
818
		}
819
 
820
		// Unchecked checkbox and radio inputs are not even submitted by browsers ...
821
		if ($CI->input->method() === 'post')
822
		{
823
			return ($input === $value) ? ' checked="checked"' : '';
824
		}
825
 
826
		return ($default === TRUE) ? ' checked="checked"' : '';
827
	}
828
}
829
 
830
// ------------------------------------------------------------------------
831
 
832
if ( ! function_exists('set_radio'))
833
{
834
	/**
835
	 * Set Radio
836
	 *
837
	 * Let's you set the selected value of a radio field via info in the POST array.
838
	 * If Form Validation is active it retrieves the info from the validation class
839
	 *
840
	 * @param	string	$field
841
	 * @param	string	$value
842
	 * @param	bool	$default
843
	 * @return	string
844
	 */
845
	function set_radio($field, $value = '', $default = FALSE)
846
	{
847
		$CI =& get_instance();
848
 
849
		if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
850
		{
851
			return $CI->form_validation->set_radio($field, $value, $default);
852
		}
853
 
854
		// Form inputs are always strings ...
855
		$value = (string) $value;
856
		$input = $CI->input->post($field, FALSE);
857
 
858
		if (is_array($input))
859
		{
860
			// Note: in_array('', array(0)) returns TRUE, do not use it
861
			foreach ($input as &$v)
862
			{
863
				if ($value === $v)
864
				{
865
					return ' checked="checked"';
866
				}
867
			}
868
 
869
			return '';
870
		}
871
 
872
		// Unchecked checkbox and radio inputs are not even submitted by browsers ...
873
		if ($CI->input->method() === 'post')
874
		{
875
			return ($input === $value) ? ' checked="checked"' : '';
876
		}
877
 
878
		return ($default === TRUE) ? ' checked="checked"' : '';
879
	}
880
}
881
 
882
// ------------------------------------------------------------------------
883
 
884
if ( ! function_exists('form_error'))
885
{
886
	/**
887
	 * Form Error
888
	 *
889
	 * Returns the error for a specific form field. This is a helper for the
890
	 * form validation class.
891
	 *
892
	 * @param	string
893
	 * @param	string
894
	 * @param	string
895
	 * @return	string
896
	 */
897
	function form_error($field = '', $prefix = '', $suffix = '')
898
	{
899
		if (FALSE === ($OBJ =& _get_validation_object()))
900
		{
901
			return '';
902
		}
903
 
904
		return $OBJ->error($field, $prefix, $suffix);
905
	}
906
}
907
 
908
// ------------------------------------------------------------------------
909
 
910
if ( ! function_exists('validation_errors'))
911
{
912
	/**
913
	 * Validation Error String
914
	 *
915
	 * Returns all the errors associated with a form submission. This is a helper
916
	 * function for the form validation class.
917
	 *
918
	 * @param	string
919
	 * @param	string
920
	 * @return	string
921
	 */
922
	function validation_errors($prefix = '', $suffix = '')
923
	{
924
		if (FALSE === ($OBJ =& _get_validation_object()))
925
		{
926
			return '';
927
		}
928
 
929
		return $OBJ->error_string($prefix, $suffix);
930
	}
931
}
932
 
933
// ------------------------------------------------------------------------
934
 
935
if ( ! function_exists('_parse_form_attributes'))
936
{
937
	/**
938
	 * Parse the form attributes
939
	 *
940
	 * Helper function used by some of the form helpers
941
	 *
942
	 * @param	array	$attributes	List of attributes
943
	 * @param	array	$default	Default values
944
	 * @return	string
945
	 */
946
	function _parse_form_attributes($attributes, $default)
947
	{
948
		if (is_array($attributes))
949
		{
950
			foreach ($default as $key => $val)
951
			{
952
				if (isset($attributes[$key]))
953
				{
954
					$default[$key] = $attributes[$key];
955
					unset($attributes[$key]);
956
				}
957
			}
958
 
959
			if (count($attributes) > 0)
960
			{
961
				$default = array_merge($default, $attributes);
962
			}
963
		}
964
 
965
		$att = '';
966
 
967
		foreach ($default as $key => $val)
968
		{
969
			if ($key === 'value')
970
			{
971
				$val = html_escape($val);
972
			}
973
			elseif ($key === 'name' && ! strlen($default['name']))
974
			{
975
				continue;
976
			}
977
 
978
			$att .= $key.'="'.$val.'" ';
979
		}
980
 
981
		return $att;
982
	}
983
}
984
 
985
// ------------------------------------------------------------------------
986
 
987
if ( ! function_exists('_attributes_to_string'))
988
{
989
	/**
990
	 * Attributes To String
991
	 *
992
	 * Helper function used by some of the form helpers
993
	 *
994
	 * @param	mixed
995
	 * @return	string
996
	 */
997
	function _attributes_to_string($attributes)
998
	{
999
		if (empty($attributes))
1000
		{
1001
			return '';
1002
		}
1003
 
1004
		if (is_object($attributes))
1005
		{
1006
			$attributes = (array) $attributes;
1007
		}
1008
 
1009
		if (is_array($attributes))
1010
		{
1011
			$atts = '';
1012
 
1013
			foreach ($attributes as $key => $val)
1014
			{
1015
				$atts .= ' '.$key.'="'.$val.'"';
1016
			}
1017
 
1018
			return $atts;
1019
		}
1020
 
1021
		if (is_string($attributes))
1022
		{
1023
			return ' '.$attributes;
1024
		}
1025
 
1026
		return FALSE;
1027
	}
1028
}
1029
 
1030
// ------------------------------------------------------------------------
1031
 
1032
if ( ! function_exists('_get_validation_object'))
1033
{
1034
	/**
1035
	 * Validation Object
1036
	 *
1037
	 * Determines what the form validation class was instantiated as, fetches
1038
	 * the object and returns it.
1039
	 *
1040
	 * @return	mixed
1041
	 */
1042
	function &_get_validation_object()
1043
	{
1044
		$CI =& get_instance();
1045
 
1046
		// We set this as a variable since we're returning by reference.
1047
		$return = FALSE;
1048
 
1049
		if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
1050
		{
1051
			if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1052
			{
1053
				return $return;
1054
			}
1055
 
1056
			return $CI->$object;
1057
		}
1058
 
1059
		return $return;
1060
	}
1061
}