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