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