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