1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004, 2011 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;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * Select box control that allows single item selection.
20: *
21: * @author David Grudl
22: *
23: * @property-read mixed $rawValue
24: * @property array $items
25: * @property-read mixed $selectedItem
26: * @property-read bool $firstSkipped
27: */
28: class SelectBox extends FormControl
29: {
30: /** @var array */
31: private $items = array();
32:
33: /** @var array */
34: protected $allowed = array();
35:
36: /** @var bool */
37: private $skipFirst = FALSE;
38:
39: /** @var bool */
40: private $useKeys = TRUE;
41:
42:
43:
44: /**
45: * @param string label
46: * @param array items from which to choose
47: * @param int number of rows that should be visible
48: */
49: public function __construct($label = NULL, array $items = NULL, $size = NULL)
50: {
51: parent::__construct($label);
52: $this->control->setName('select');
53: $this->control->size = $size > 1 ? (int) $size : NULL;
54: if ($items !== NULL) {
55: $this->setItems($items);
56: }
57: }
58:
59:
60:
61: /**
62: * Returns selected item key.
63: * @return mixed
64: */
65: public function getValue()
66: {
67: $allowed = $this->allowed;
68: if ($this->skipFirst) {
69: $allowed = array_slice($allowed, 1, count($allowed), TRUE);
70: }
71:
72: return is_scalar($this->value) && isset($allowed[$this->value]) ? $this->value : NULL;
73: }
74:
75:
76:
77: /**
78: * Returns selected item key (not checked).
79: * @return mixed
80: */
81: public function getRawValue()
82: {
83: return is_scalar($this->value) ? $this->value : NULL;
84: }
85:
86:
87:
88: /**
89: * Has been any item selected?
90: * @return bool
91: */
92: public function isFilled()
93: {
94: $value = $this->getValue();
95: return is_array($value) ? count($value) > 0 : $value !== NULL;
96: }
97:
98:
99:
100: /**
101: * Ignores the first item in select box.
102: * @param string
103: * @return SelectBox provides a fluent interface
104: */
105: public function skipFirst($item = NULL)
106: {
107: if (is_bool($item)) {
108: $this->skipFirst = $item;
109: } else {
110: $this->skipFirst = TRUE;
111: if ($item !== NULL) {
112: $this->items = array('' => $item) + $this->items;
113: $this->allowed = array('' => '') + $this->allowed;
114: }
115: }
116: return $this;
117: }
118:
119:
120:
121: /**
122: * Is first item in select box ignored?
123: * @return bool
124: */
125: final public function isFirstSkipped()
126: {
127: return $this->skipFirst;
128: }
129:
130:
131:
132: /**
133: * Are the keys used?
134: * @return bool
135: */
136: final public function areKeysUsed()
137: {
138: return $this->useKeys;
139: }
140:
141:
142:
143: /**
144: * Sets items from which to choose.
145: * @param array
146: * @return SelectBox provides a fluent interface
147: */
148: public function setItems(array $items, $useKeys = TRUE)
149: {
150: $this->items = $items;
151: $this->allowed = array();
152: $this->useKeys = (bool) $useKeys;
153:
154: foreach ($items as $key => $value) {
155: if (!is_array($value)) {
156: $value = array($key => $value);
157: }
158:
159: foreach ($value as $key2 => $value2) {
160: if (!$this->useKeys) {
161: if (!is_scalar($value2)) {
162: throw new \InvalidArgumentException("All items must be scalar.");
163: }
164: $key2 = $value2;
165: }
166:
167: if (isset($this->allowed[$key2])) {
168: throw new \InvalidArgumentException("Items contain duplication for key '$key2'.");
169: }
170:
171: $this->allowed[$key2] = $value2;
172: }
173: }
174: return $this;
175: }
176:
177:
178:
179: /**
180: * Returns items from which to choose.
181: * @return array
182: */
183: final public function getItems()
184: {
185: return $this->items;
186: }
187:
188:
189:
190: /**
191: * Returns selected value.
192: * @return string
193: */
194: public function getSelectedItem()
195: {
196: if (!$this->useKeys) {
197: return $this->getValue();
198:
199: } else {
200: $value = $this->getValue();
201: return $value === NULL ? NULL : $this->allowed[$value];
202: }
203: }
204:
205:
206:
207: /**
208: * Generates control's HTML element.
209: * @return Nette\Web\Html
210: */
211: public function getControl()
212: {
213: $control = parent::getControl();
214: if ($this->skipFirst) {
215: reset($this->items);
216: $control->data('nette-empty-value', $this->useKeys ? key($this->items) : current($this->items));
217: }
218: $selected = $this->getValue();
219: $selected = is_array($selected) ? array_flip($selected) : array($selected => TRUE);
220: $option = Nette\Web\Html::el('option');
221:
222: foreach ($this->items as $key => $value) {
223: if (!is_array($value)) {
224: $value = array($key => $value);
225: $dest = $control;
226:
227: } else {
228: $dest = $control->create('optgroup')->label($key);
229: }
230:
231: foreach ($value as $key2 => $value2) {
232: if ($value2 instanceof Nette\Web\Html) {
233: $dest->add((string) $value2->selected(isset($selected[$key2])));
234:
235: } else {
236: $key2 = $this->useKeys ? $key2 : $value2;
237: $value2 = $this->translate((string) $value2);
238: $dest->add((string) $option->value($key2 === $value2 ? NULL : $key2)
239: ->selected(isset($selected[$key2]))
240: ->setText($value2));
241: }
242: }
243: }
244: return $control;
245: }
246:
247: }
248: