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