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