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: * @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) {
48: $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: * 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: * Sets options from which to choose.
76: * @param array
77: * @return self
78: */
79: public function setItems(array $items)
80: {
81: $this->items = $items;
82: return $this;
83: }
84:
85:
86: /**
87: * Returns options from which to choose.
88: * @return array
89: */
90: final public function getItems()
91: {
92: return $this->items;
93: }
94:
95:
96: /**
97: * Returns separator HTML element template.
98: * @return Html
99: */
100: final public function getSeparatorPrototype()
101: {
102: return $this->separator;
103: }
104:
105:
106: /**
107: * Returns container HTML element template.
108: * @return Html
109: */
110: final public function getContainerPrototype()
111: {
112: return $this->container;
113: }
114:
115:
116: /**
117: * Generates control's HTML element.
118: * @param mixed
119: * @return Html
120: */
121: public function getControl($key = NULL)
122: {
123: if ($key === NULL) {
124: $container = clone $this->container;
125: $separator = (string) $this->separator;
126:
127: } elseif (!isset($this->items[$key])) {
128: return NULL;
129: }
130:
131: $control = parent::getControl();
132: $id = $control->id;
133: $counter = -1;
134: $value = $this->value === NULL ? NULL : (string) $this->getValue();
135: $label = Html::el('label');
136:
137: foreach ($this->items as $k => $val) {
138: $counter++;
139: if ($key !== NULL && (string) $key !== (string) $k) {
140: continue;
141: }
142:
143: $control->id = $label->for = $id . '-' . $counter;
144: $control->checked = (string) $k === $value;
145: $control->value = $k;
146:
147: if ($val instanceof Html) {
148: $label->setHtml($val);
149: } else {
150: $label->setText($this->translate((string) $val));
151: }
152:
153: if ($key !== NULL) {
154: return Html::el()->add($control)->add($label);
155: }
156:
157: $container->add((string) $control . (string) $label . $separator);
158: $control->data('nette-rules', NULL);
159: // TODO: separator after last item?
160: }
161:
162: return $container;
163: }
164:
165:
166: /**
167: * Generates label's HTML element.
168: * @param string
169: * @return void
170: */
171: public function getLabel($caption = NULL)
172: {
173: $label = parent::getLabel($caption);
174: $label->for = NULL;
175: return $label;
176: }
177:
178: }
179: