1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\Forms\Controls;
9:
10: use Nette;
11:
12:
13: /**
14: * Select box control that allows multiple items selection.
15: *
16: * @author David Grudl
17: */
18: class MultiSelectBox extends MultiChoiceControl
19: {
20: /** @var array of option / optgroup */
21: private $options = array();
22:
23:
24: /**
25: * Sets options and option groups from which to choose.
26: * @return self
27: */
28: public function setItems(array $items, $useKeys = TRUE)
29: {
30: if (!$useKeys) {
31: $res = array();
32: foreach ($items as $key => $value) {
33: unset($items[$key]);
34: if (is_array($value)) {
35: foreach ($value as $val) {
36: $res[$key][(string) $val] = $val;
37: }
38: } else {
39: $res[(string) $value] = $value;
40: }
41: }
42: $items = $res;
43: }
44: $this->options = $items;
45: return parent::setItems(Nette\Utils\Arrays::flatten($items, TRUE));
46: }
47:
48:
49: /**
50: * Generates control's HTML element.
51: * @return Nette\Utils\Html
52: */
53: public function getControl()
54: {
55: $items = array();
56: foreach ($this->options as $key => $value) {
57: $items[is_array($value) ? $this->translate($key) : $key] = $this->translate($value);
58: }
59:
60: return Nette\Forms\Helpers::createSelectBox(
61: $items,
62: array(
63: 'selected?' => $this->value,
64: 'disabled:' => is_array($this->disabled) ? $this->disabled : NULL,
65: )
66: )->addAttributes(parent::getControl()->attrs)->multiple(TRUE);
67: }
68:
69:
70: /** @deprecated */
71: function getSelectedItem()
72: {
73: trigger_error(__METHOD__ . '(TRUE) is deprecated; use getSelectedItems() instead.', E_USER_DEPRECATED);
74: return $this->getSelectedItems();
75: }
76:
77: }
78: