/Nette/Forms/Controls/MultiSelectBox.php
PHP | 113 lines | 49 code | 24 blank | 40 comment | 6 complexity | 7b58fe343c2eb60d63d3f057d2a6705e MD5 | raw file
Possible License(s): BSD-3-Clause
- <?php
- /**
- * Nette Framework
- *
- * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "Nette license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://nettephp.com
- *
- * @copyright Copyright (c) 2004, 2009 David Grudl
- * @license http://nettephp.com/license Nette license
- * @link http://nettephp.com
- * @category Nette
- * @package Nette\Forms
- */
- /*namespace Nette\Forms;*/
- require_once dirname(__FILE__) . '/../../Forms/Controls/SelectBox.php';
- /**
- * Select box control that allows multiple item selection.
- *
- * @author David Grudl
- * @copyright Copyright (c) 2004, 2009 David Grudl
- * @package Nette\Forms
- */
- class MultiSelectBox extends SelectBox
- {
- /**
- * Returns selected keys.
- * @return array
- */
- public function getValue()
- {
- $allowed = array_keys($this->allowed);
- if ($this->isFirstSkipped()) {
- unset($allowed[0]);
- }
- return array_intersect($this->getRawValue(), $allowed);
- }
- /**
- * Returns selected keys (not checked).
- * @return array
- */
- public function getRawValue()
- {
- if (is_scalar($this->value)) {
- $value = array($this->value);
- } elseif (!is_array($this->value)) {
- $value = array();
- } else {
- $value = $this->value;
- }
- $res = array();
- foreach ($value as $val) {
- if (is_scalar($val)) {
- $res[] = $val;
- }
- }
- return $res;
- }
- /**
- * Returns selected values.
- * @return array
- */
- public function getSelectedItem()
- {
- if (!$this->useKeys) {
- return $this->getValue();
- } else {
- $res = array();
- foreach ($this->getValue() as $value) {
- $res[$value] = $this->allowed[$value];
- }
- return $res;
- }
- }
- /**
- * Generates control's HTML element.
- * @return Nette\Web\Html
- */
- public function getControl()
- {
- $control = parent::getControl();
- $control->name .= '[]';
- $control->multiple = TRUE;
- return $control;
- }
- }