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