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: * Set of radio button controls.
20: *
21: * @author David Grudl
22: *
23: * @property array $items
24: * @property-read Nette\Utils\Html $separatorPrototype
25: * @property-read Nette\Utils\Html $containerPrototype
26: */
27: class RadioList extends BaseControl
28: {
29: /** @var Nette\Utils\Html separator element template */
30: protected $separator;
31:
32: /** @var Nette\Utils\Html container element template */
33: protected $container;
34:
35: /** @var array */
36: protected $items = array();
37:
38:
39: /**
40: * @param string label
41: * @param array options from which to choose
42: */
43: public function __construct($label = NULL, array $items = NULL)
44: {
45: parent::__construct($label);
46: $this->control->type = 'radio';
47: $this->container = Html::el();
48: $this->separator = Html::el('br');
49: if ($items !== NULL) {
50: $this->setItems($items);
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: * Has been any radio button selected?
68: * @return bool
69: */
70: public function isFilled()
71: {
72: return $this->getValue() !== NULL;
73: }
74:
75:
76: /**
77: * Sets options from which to choose.
78: * @param array
79: * @return self
80: */
81: public function setItems(array $items)
82: {
83: $this->items = $items;
84: return $this;
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: * Returns separator HTML element template.
100: * @return Nette\Utils\Html
101: */
102: final public function getSeparatorPrototype()
103: {
104: return $this->separator;
105: }
106:
107:
108: /**
109: * Returns container HTML element template.
110: * @return Nette\Utils\Html
111: */
112: final public function getContainerPrototype()
113: {
114: return $this->container;
115: }
116:
117:
118: /**
119: * Generates control's HTML element.
120: * @param mixed
121: * @return Nette\Utils\Html
122: */
123: public function getControl($key = NULL)
124: {
125: if ($key === NULL) {
126: $container = clone $this->container;
127: $separator = (string) $this->separator;
128:
129: } elseif (!isset($this->items[$key])) {
130: return NULL;
131: }
132:
133: $control = parent::getControl();
134: $id = $control->id;
135: $counter = -1;
136: $value = $this->value === NULL ? NULL : (string) $this->getValue();
137: $label = Html::el('label');
138:
139: foreach ($this->items as $k => $val) {
140: $counter++;
141: if ($key !== NULL && (string) $key !== (string) $k) {
142: continue;
143: }
144:
145: $control->id = $label->for = $id . '-' . $counter;
146: $control->checked = (string) $k === $value;
147: $control->value = $k;
148:
149: if ($val instanceof Html) {
150: $label->setHtml($val);
151: } else {
152: $label->setText($this->translate((string) $val));
153: }
154:
155: if ($key !== NULL) {
156: return Html::el()->add($control)->add($label);
157: }
158:
159: $container->add((string) $control . (string) $label . $separator);
160: $control->data('nette-rules', NULL);
161: // TODO: separator after last item?
162: }
163:
164: return $container;
165: }
166:
167:
168: /**
169: * Generates label's HTML element.
170: * @param string
171: * @return void
172: */
173: public function getLabel($caption = NULL)
174: {
175: $label = parent::getLabel($caption);
176: $label->for = NULL;
177: return $label;
178: }
179:
180: }
181: