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: * @package Nette\Forms
11: */
12:
13:
14:
15: /**
16: * Select box control that allows multiple item selection.
17: *
18: * @author David Grudl
19: */
20: class MultiSelectBox extends SelectBox
21: {
22:
23:
24: /**
25: * Returns selected keys.
26: * @return array
27: */
28: public function getValue()
29: {
30: $allowed = array_keys($this->allowed);
31: if ($this->isFirstSkipped()) {
32: unset($allowed[0]);
33: }
34: return array_intersect($this->getRawValue(), $allowed);
35: }
36:
37:
38:
39: /**
40: * Returns selected keys (not checked).
41: * @return array
42: */
43: public function getRawValue()
44: {
45: if (is_scalar($this->value)) {
46: $value = array($this->value);
47:
48: } elseif (!is_array($this->value)) {
49: $value = array();
50:
51: } else {
52: $value = $this->value;
53: }
54:
55: $res = array();
56: foreach ($value as $val) {
57: if (is_scalar($val)) {
58: $res[] = $val;
59: }
60: }
61: return $res;
62: }
63:
64:
65:
66: /**
67: * Returns selected values.
68: * @return array
69: */
70: public function getSelectedItem()
71: {
72: if (!$this->areKeysUsed()) {
73: return $this->getValue();
74:
75: } else {
76: $res = array();
77: foreach ($this->getValue() as $value) {
78: $res[$value] = $this->allowed[$value];
79: }
80: return $res;
81: }
82: }
83:
84:
85:
86: /**
87: * Returns HTML name of control.
88: * @return string
89: */
90: public function getHtmlName()
91: {
92: return parent::getHtmlName() . '[]';
93: }
94:
95:
96:
97: /**
98: * Generates control's HTML element.
99: * @return Html
100: */
101: public function getControl()
102: {
103: $control = parent::getControl();
104: $control->multiple = TRUE;
105: return $control;
106: }
107:
108: }
109: