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: */
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: * Ignores the first item in select box.
90: * @param string
91: * @return SelectBox provides a fluent interface
92: */
93: public function skipFirst($item = NULL)
94: {
95: if (is_bool($item)) {
96: $this->skipFirst = $item;
97: } else {
98: $this->skipFirst = TRUE;
99: if ($item !== NULL) {
100: $this->items = array('' => $item) + $this->items;
101: $this->allowed = array('' => '') + $this->allowed;
102: }
103: }
104: return $this;
105: }
106:
107:
108:
109: /**
110: * Is first item in select box ignored?
111: * @return bool
112: */
113: final public function isFirstSkipped()
114: {
115: return $this->skipFirst;
116: }
117:
118:
119:
120: /**
121: * Are the keys used?
122: * @return bool
123: */
124: final public function areKeysUsed()
125: {
126: return $this->useKeys;
127: }
128:
129:
130:
131: /**
132: * Sets items from which to choose.
133: * @param array
134: * @return SelectBox provides a fluent interface
135: */
136: public function setItems(array $items, $useKeys = TRUE)
137: {
138: $this->items = $items;
139: $this->allowed = array();
140: $this->useKeys = (bool) $useKeys;
141:
142: foreach ($items as $key => $value) {
143: if (!is_array($value)) {
144: $value = array($key => $value);
145: }
146:
147: foreach ($value as $key2 => $value2) {
148: if (!$this->useKeys) {
149: if (!is_scalar($value2)) {
150: throw new \InvalidArgumentException("All items must be scalar.");
151: }
152: $key2 = $value2;
153: }
154:
155: if (isset($this->allowed[$key2])) {
156: throw new \InvalidArgumentException("Items contain duplication for key '$key2'.");
157: }
158:
159: $this->allowed[$key2] = $value2;
160: }
161: }
162: return $this;
163: }
164:
165:
166:
167: /**
168: * Returns items from which to choose.
169: * @return array
170: */
171: final public function getItems()
172: {
173: return $this->items;
174: }
175:
176:
177:
178: /**
179: * Returns selected value.
180: * @return string
181: */
182: public function getSelectedItem()
183: {
184: if (!$this->useKeys) {
185: return $this->getValue();
186:
187: } else {
188: $value = $this->getValue();
189: return $value === NULL ? NULL : $this->allowed[$value];
190: }
191: }
192:
193:
194:
195: /**
196: * Generates control's HTML element.
197: * @return Nette\Web\Html
198: */
199: public function getControl()
200: {
201: $control = parent::getControl();
202: if ($this->skipFirst) {
203: reset($this->items);
204: $control->data['nette-empty-value'] = $this->useKeys ? key($this->items) : current($this->items);
205: }
206: $selected = $this->getValue();
207: $selected = is_array($selected) ? array_flip($selected) : array($selected => TRUE);
208: $option = Nette\Web\Html::el('option');
209:
210: foreach ($this->items as $key => $value) {
211: if (!is_array($value)) {
212: $value = array($key => $value);
213: $dest = $control;
214:
215: } else {
216: $dest = $control->create('optgroup')->label($key);
217: }
218:
219: foreach ($value as $key2 => $value2) {
220: if ($value2 instanceof Nette\Web\Html) {
221: $dest->add((string) $value2->selected(isset($selected[$key2])));
222:
223: } else {
224: $key2 = $this->useKeys ? $key2 : $value2;
225: $value2 = $this->translate($value2);
226: $dest->add((string) $option->value($key2 === $value2 ? NULL : $key2)->selected(isset($selected[$key2]))->setText($value2));
227: }
228: }
229: }
230: return $control;
231: }
232:
233:
234:
235: /**
236: * Filled validator: has been any item selected?
237: * @param IFormControl
238: * @return bool
239: */
240: public static function validateFilled(IFormControl $control)
241: {
242: $value = $control->getValue();
243: return is_array($value) ? count($value) > 0 : $value !== NULL;
244: }
245:
246: }
247: