Overview

Namespaces

  • Budovy
  • Kdyby
    • BootstrapFormRenderer
      • DI
      • Latte
  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • Nextras
    • Datagrid
  • None
  • PHP
  • Tester
    • CodeCoverage
    • Runner
      • Output
  • Vodacek
    • Forms
      • Controls
  • WebLoader
    • Filter
    • Nette

Classes

  • BaseControl
  • Button
  • Checkbox
  • CheckboxList
  • ChoiceControl
  • CsrfProtection
  • HiddenField
  • ImageButton
  • MultiChoiceControl
  • MultiSelectBox
  • RadioList
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput
  • UploadControl
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Forms\Controls;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Choice control that allows multiple items selection.
 15:  *
 16:  * @author     David Grudl
 17:  *
 18:  * @property   array $items
 19:  * @property-read array $selectedItems
 20:  * @property-read array $rawValue
 21:  */
 22: abstract class MultiChoiceControl extends BaseControl
 23: {
 24:     /** @var array */
 25:     private $items = array();
 26: 
 27: 
 28:     public function __construct($label = NULL, array $items = NULL)
 29:     {
 30:         parent::__construct($label);
 31:         if ($items !== NULL) {
 32:             $this->setItems($items);
 33:         }
 34:     }
 35: 
 36: 
 37:     /**
 38:      * Loads HTTP data.
 39:      * @return void
 40:      */
 41:     public function loadHttpData()
 42:     {
 43:         $this->value = array_keys(array_flip($this->getHttpData(Nette\Forms\Form::DATA_TEXT)));
 44:         if (is_array($this->disabled)) {
 45:             $this->value = array_diff($this->value, array_keys($this->disabled));
 46:         }
 47:     }
 48: 
 49: 
 50:     /**
 51:      * Sets selected items (by keys).
 52:      * @param  array
 53:      * @return self
 54:      */
 55:     public function setValue($values)
 56:     {
 57:         if (is_scalar($values) || $values === NULL) {
 58:             $values = (array) $values;
 59:         } elseif (!is_array($values)) {
 60:             throw new Nette\InvalidArgumentException('Value must be array or NULL, ' . gettype($values) . " given in field '{$this->name}'.");
 61:         }
 62:         $flip = array();
 63:         foreach ($values as $value) {
 64:             if (!is_scalar($value) && !method_exists($value, '__toString')) {
 65:                 throw new Nette\InvalidArgumentException('Values must be scalar, ' . gettype($value) . " given in field '{$this->name}'.");
 66:             }
 67:             $flip[(string) $value] = TRUE;
 68:         }
 69:         $values = array_keys($flip);
 70:         if ($diff = array_diff($values, array_keys($this->items))) {
 71:             throw new Nette\InvalidArgumentException("Values '" . implode("', '", $diff) . "' are out of allowed range in field '{$this->name}'.");
 72:         }
 73:         $this->value = $values;
 74:         return $this;
 75:     }
 76: 
 77: 
 78:     /**
 79:      * Returns selected keys.
 80:      * @return array
 81:      */
 82:     public function getValue()
 83:     {
 84:         return array_values(array_intersect($this->value, array_keys($this->items)));
 85:     }
 86: 
 87: 
 88:     /**
 89:      * Returns selected keys (not checked).
 90:      * @return array
 91:      */
 92:     public function getRawValue()
 93:     {
 94:         return $this->value;
 95:     }
 96: 
 97: 
 98:     /**
 99:      * Is any item selected?
100:      * @return bool
101:      */
102:     public function isFilled()
103:     {
104:         return $this->getValue() !== array();
105:     }
106: 
107: 
108:     /**
109:      * Sets items from which to choose.
110:      * @param  array
111:      * @param  bool
112:      * @return self
113:      */
114:     public function setItems(array $items, $useKeys = TRUE)
115:     {
116:         $this->items = $useKeys ? $items : array_combine($items, $items);
117:         return $this;
118:     }
119: 
120: 
121:     /**
122:      * Returns items from which to choose.
123:      * @return array
124:      */
125:     public function getItems()
126:     {
127:         return $this->items;
128:     }
129: 
130: 
131:     /**
132:      * Returns selected values.
133:      * @return array
134:      */
135:     public function getSelectedItems()
136:     {
137:         return array_intersect_key($this->items, array_flip($this->value));
138:     }
139: 
140: 
141:     /**
142:      * Disables or enables control or items.
143:      * @param  bool|array
144:      * @return self
145:      */
146:     public function setDisabled($value = TRUE)
147:     {
148:         if (!is_array($value)) {
149:             return parent::setDisabled($value);
150:         }
151: 
152:         parent::setDisabled(FALSE);
153:         $this->disabled = array_fill_keys($value, TRUE);
154:         $this->value = array_diff($this->value, $value);
155:         return $this;
156:     }
157: 
158: 
159:     /**
160:      * Returns HTML name of control.
161:      * @return string
162:      */
163:     public function getHtmlName()
164:     {
165:         return parent::getHtmlName() . '[]';
166:     }
167: 
168: }
169: 
API documentation generated by ApiGen 2.8.0