1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: *
6: * Copyright (c) 2004 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 multiple item selection.
20: *
21: * @author David Grudl
22: */
23: class MultiSelectBox extends SelectBox
24: {
25:
26:
27: /**
28: * Returns selected keys.
29: * @return array
30: */
31: public function getValue()
32: {
33: $allowed = array_keys($this->allowed);
34: if ($this->isFirstSkipped()) {
35: unset($allowed[0]);
36: }
37: return array_intersect($this->getRawValue(), $allowed);
38: }
39:
40:
41:
42: /**
43: * Returns selected keys (not checked).
44: * @return array
45: */
46: public function getRawValue()
47: {
48: if (is_scalar($this->value)) {
49: $value = array($this->value);
50:
51: } elseif (!is_array($this->value)) {
52: $value = array();
53:
54: } else {
55: $value = $this->value;
56: }
57:
58: $res = array();
59: foreach ($value as $val) {
60: if (is_scalar($val)) {
61: $res[] = $val;
62: }
63: }
64: return $res;
65: }
66:
67:
68:
69: /**
70: * Returns selected values.
71: * @return array
72: */
73: public function getSelectedItem()
74: {
75: if (!$this->areKeysUsed()) {
76: return $this->getValue();
77:
78: } else {
79: $res = array();
80: foreach ($this->getValue() as $value) {
81: $res[$value] = $this->allowed[$value];
82: }
83: return $res;
84: }
85: }
86:
87:
88:
89: /**
90: * Returns name of control within a Form & INamingContainer scope.
91: * @return string
92: */
93: public function getHtmlName()
94: {
95: return parent::getHtmlName() . '[]';
96: }
97:
98:
99:
100: /**
101: * Generates control's HTML element.
102: * @return Nette\Web\Html
103: */
104: public function getControl()
105: {
106: $control = parent::getControl();
107: $control->multiple = TRUE;
108: return $control;
109: }
110:
111: }
112: