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