summaryrefslogtreecommitdiff
path: root/modules/forge/libraries/Form_Checkbox.php
blob: 23b423b4b21ddb3fdd22d2d139cee371d6a64f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
 * FORGE checkbox input library.
 *
 * $Id$
 *
 * @package    Forge
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class Form_Checkbox_Core extends Form_Input {

	protected $data = array
	(
		'type' => 'checkbox',
		'class' => 'checkbox',
		'value' => '1',
		'checked' => FALSE,
	);

	protected $protect = array('type');

	public function __get($key)
	{
		if ($key == 'value')
		{
			// Return the value if the checkbox is checked
			return $this->data['checked'] ? $this->data['value'] : NULL;
		}

		return parent::__get($key);
	}

	public function label($val = NULL)
	{
		if ($val === NULL)
		{
			// Do not display labels for checkboxes, labels wrap checkboxes
			return '';
		}
		else
		{
			$this->data['label'] = ($val === TRUE) ? utf8::ucwords(inflector::humanize($this->name)) : $val;
			return $this;
		}
	}

	protected function html_element()
	{
		// Import the data
		$data = $this->data;

		if (empty($data['checked']))
		{
			// Not checked
			unset($data['checked']);
		}
		else
		{
			// Is checked
			$data['checked'] = 'checked';
		}

		if ($label = arr::remove('label', $data))
		{
			// There must be one space before the text
			$label = ' '.ltrim($label);
		}

		return '<label>'.$label.'</label>'.form::input($data);
	}

	protected function load_value()
	{
		if (is_bool($this->valid))
			return;

		// Makes the box checked if the value from POST is the same as the current value
		$this->data['checked'] = ($this->input_value($this->name) == $this->data['value']);
	}

} // End Form Checkbox