1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: * @package Nette\Forms
11: */
12:
13:
14:
15: /**
16: * Set of radio button controls.
17: *
18: * @author David Grudl
19: *
20: * @property array $items
21: * @property-read NHtml $separatorPrototype
22: * @property-read NHtml $containerPrototype
23: * @package Nette\Forms
24: */
25: class NRadioList extends NFormControl
26: {
27: /** @var NHtml separator element template */
28: protected $separator;
29:
30: /** @var NHtml container element template */
31: protected $container;
32:
33: /** @var array */
34: protected $items = array();
35:
36:
37:
38: /**
39: * @param string label
40: * @param array options from which to choose
41: */
42: public function __construct($label = NULL, array $items = NULL)
43: {
44: parent::__construct($label);
45: $this->control->type = 'radio';
46: $this->container = NHtml::el();
47: $this->separator = NHtml::el('br');
48: if ($items !== NULL) $this->setItems($items);
49: }
50:
51:
52:
53: /**
54: * Returns selected radio value.
55: * @param bool
56: * @return mixed
57: */
58: public function getValue($raw = FALSE)
59: {
60: return is_scalar($this->value) && ($raw || isset($this->items[$this->value])) ? $this->value : NULL;
61: }
62:
63:
64:
65: /**
66: * Sets options from which to choose.
67: * @param array
68: * @return NRadioList provides a fluent interface
69: */
70: public function setItems(array $items)
71: {
72: $this->items = $items;
73: return $this;
74: }
75:
76:
77:
78: /**
79: * Returns options from which to choose.
80: * @return array
81: */
82: final public function getItems()
83: {
84: return $this->items;
85: }
86:
87:
88:
89: /**
90: * Returns separator HTML element template.
91: * @return NHtml
92: */
93: final public function getSeparatorPrototype()
94: {
95: return $this->separator;
96: }
97:
98:
99:
100: /**
101: * Returns container HTML element template.
102: * @return NHtml
103: */
104: final public function getContainerPrototype()
105: {
106: return $this->container;
107: }
108:
109:
110:
111: /**
112: * Generates control's HTML element.
113: * @param mixed
114: * @return NHtml
115: */
116: public function getControl($key = NULL)
117: {
118: if ($key === NULL) {
119: $container = clone $this->container;
120: $separator = (string) $this->separator;
121:
122: } elseif (!isset($this->items[$key])) {
123: return NULL;
124: }
125:
126: $control = parent::getControl();
127: $id = $control->id;
128: $counter = -1;
129: $value = $this->value === NULL ? NULL : (string) $this->getValue();
130: $label = NHtml::el('label');
131:
132: foreach ($this->items as $k => $val) {
133: $counter++;
134: if ($key !== NULL && $key != $k) continue; // intentionally ==
135:
136: $control->id = $label->for = $id . '-' . $counter;
137: $control->checked = (string) $k === $value;
138: $control->value = $k;
139:
140: if ($val instanceof NHtml) {
141: $label->setHtml($val);
142: } else {
143: $label->setText($this->translate($val));
144: }
145:
146: if ($key !== NULL) {
147: return (string) $control . (string) $label;
148: }
149:
150: $container->add((string) $control . (string) $label . $separator);
151: // TODO: separator after last item?
152: }
153:
154: return $container;
155: }
156:
157:
158:
159: /**
160: * Generates label's HTML element.
161: * @param string
162: * @return void
163: */
164: public function getLabel($caption = NULL)
165: {
166: $label = parent::getLabel($caption);
167: $label->for = NULL;
168: return $label;
169: }
170:
171:
172:
173: /**
174: * Filled validator: has been any radio button selected?
175: * @param IFormControl
176: * @return bool
177: */
178: public static function validateFilled(IFormControl $control)
179: {
180: return $control->getValue() !== NULL;
181: }
182:
183: }
184: