1: <?php
2:
3: /**
4: * This file is part of the Nette Framework.
5: *
6: * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
7: *
8: * This source file is subject to the "Nette license", and/or
9: * GPL license. For more information please see http://nette.org
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 NHtml $separatorPrototype
22: * @property-read NHtml $containerPrototype
23: */
24: class NRadioList extends NFormControl
25: {
26: /** @var NHtml separator element template */
27: protected $separator;
28:
29: /** @var NHtml 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 = NHtml::el();
46: $this->separator = NHtml::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: * Sets options from which to choose.
66: * @param array
67: * @return NRadioList provides a fluent interface
68: */
69: public function setItems(array $items)
70: {
71: $this->items = $items;
72: return $this;
73: }
74:
75:
76:
77: /**
78: * Returns options from which to choose.
79: * @return array
80: */
81: final public function getItems()
82: {
83: return $this->items;
84: }
85:
86:
87:
88: /**
89: * Returns separator HTML element template.
90: * @return NHtml
91: */
92: final public function getSeparatorPrototype()
93: {
94: return $this->separator;
95: }
96:
97:
98:
99: /**
100: * Returns container HTML element template.
101: * @return NHtml
102: */
103: final public function getContainerPrototype()
104: {
105: return $this->container;
106: }
107:
108:
109:
110: /**
111: * Generates control's HTML element.
112: * @param mixed
113: * @return NHtml
114: */
115: public function getControl($key = NULL)
116: {
117: if ($key === NULL) {
118: $container = clone $this->container;
119: $separator = (string) $this->separator;
120:
121: } elseif (!isset($this->items[$key])) {
122: return NULL;
123: }
124:
125: $control = parent::getControl();
126: $id = $control->id;
127: $counter = -1;
128: $value = $this->value === NULL ? NULL : (string) $this->getValue();
129: $label = NHtml::el('label');
130:
131: foreach ($this->items as $k => $val) {
132: $counter++;
133: if ($key !== NULL && $key != $k) continue; // intentionally ==
134:
135: $control->id = $label->for = $id . '-' . $counter;
136: $control->checked = (string) $k === $value;
137: $control->value = $k;
138:
139: if ($val instanceof NHtml) {
140: $label->setHtml($val);
141: } else {
142: $label->setText($this->translate($val));
143: }
144:
145: if ($key !== NULL) {
146: return (string) $control . (string) $label;
147: }
148:
149: $container->add((string) $control . (string) $label . $separator);
150: unset($control->data['nette-rules']);
151: // TODO: separator after last item?
152: }
153:
154: return $container;
155: }
156:
157:
158:
159: /**
160: * Generates label's HTML element.
161: * @param string
162: * @return void
163: */
164: public function getLabel($caption = NULL)
165: {
166: $label = parent::getLabel($caption);
167: $label->for = NULL;
168: return $label;
169: }
170:
171:
172:
173: /**
174: * Filled validator: has been any radio button selected?
175: * @param IFormControl
176: * @return bool
177: */
178: public static function validateFilled(IFormControl $control)
179: {
180: return $control->getValue() !== NULL;
181: }
182:
183: }
184: