Source for file SelectBox.php

Documentation is available at SelectBox.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Forms
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../../Forms/Controls/FormControl.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * Select box control that allows single item selection.
  29. 29:  *
  30. 30:  * @author     David Grudl
  31. 31:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  32. 32:  * @package    Nette\Forms
  33. 33:  */
  34. 34: class SelectBox extends FormControl
  35. 35: {
  36. 36:     /** @var array */
  37. 37:     private $items array();
  38. 38:  
  39. 39:     /** @var array */
  40. 40:     protected $allowed = array();
  41. 41:  
  42. 42:     /** @var bool */
  43. 43:     private $skipFirst FALSE;
  44. 44:  
  45. 45:     /** @var bool */
  46. 46:     private $useKeys TRUE;
  47. 47:  
  48. 48:  
  49. 49:  
  50. 50:     /**
  51. 51:      * @param  string  label
  52. 52:      * @param  array   items from which to choose
  53. 53:      * @param  int     number of rows that should be visible
  54. 54:      */
  55. 55:     public function __construct($labelarray $items NULL$size NULL)
  56. 56:     {
  57. 57:         parent::__construct($label);
  58. 58:         $this->control->setName('select');
  59. 59:         $this->control->size $size ? (int) $size NULL;
  60. 60:         $this->control->onfocus 'this.onmousewheel=function(){return false}';  // prevents accidental change in IE
  61. 61:         $this->label->onclick 'return false';  // prevents deselect in IE 5 - 6
  62. 62:         if ($items !== NULL{
  63. 63:             $this->setItems($items);
  64. 64:         }
  65. 65:     }
  66. 66:  
  67. 67:  
  68. 68:  
  69. 69:     /**
  70. 70:      * Returns selected item key.
  71. 71:      * @return mixed 
  72. 72:      */
  73. 73:     public function getValue()
  74. 74:     {
  75. 75:         $allowed $this->allowed;
  76. 76:         if ($this->skipFirst{
  77. 77:             $allowed array_slice($allowed1count($allowed)TRUE);
  78. 78:         }
  79. 79:  
  80. 80:         return is_scalar($this->value&& isset($allowed[$this->value]$this->value NULL;
  81. 81:     }
  82. 82:  
  83. 83:  
  84. 84:  
  85. 85:     /**
  86. 86:      * Returns selected item key (not checked).
  87. 87:      * @return mixed 
  88. 88:      */
  89. 89:     public function getRawValue()
  90. 90:     {
  91. 91:         return is_scalar($this->value$this->value NULL;
  92. 92:     }
  93. 93:  
  94. 94:  
  95. 95:  
  96. 96:     /**
  97. 97:      * Ignores the first item in select box.
  98. 98:      * @param  bool 
  99. 99:      * @return SelectBox  provides a fluent interface
  100. 100:      */
  101. 101:     public function skipFirst($value TRUE)
  102. 102:     {
  103. 103:         $this->skipFirst = (bool) $value;
  104. 104:         return $this;
  105. 105:     }
  106. 106:  
  107. 107:  
  108. 108:  
  109. 109:     /**
  110. 110:      * Is first item in select box ignored?
  111. 111:      * @return bool 
  112. 112:      */
  113. 113:     final public function isFirstSkipped()
  114. 114:     {
  115. 115:         return $this->skipFirst;
  116. 116:     }
  117. 117:  
  118. 118:  
  119. 119:  
  120. 120:     /**
  121. 121:      * Are the keys used?
  122. 122:      * @return bool 
  123. 123:      */
  124. 124:     final public function areKeysUsed()
  125. 125:     {
  126. 126:         return $this->useKeys;
  127. 127:     }
  128. 128:  
  129. 129:  
  130. 130:  
  131. 131:     /**
  132. 132:      * Sets items from which to choose.
  133. 133:      * @param  array 
  134. 134:      * @return SelectBox  provides a fluent interface
  135. 135:      */
  136. 136:     public function setItems(array $items$useKeys TRUE)
  137. 137:     {
  138. 138:         $this->items $items;
  139. 139:         $this->allowed array();
  140. 140:         $this->useKeys = (bool) $useKeys;
  141. 141:  
  142. 142:         foreach ($items as $key => $value{
  143. 143:             if (!is_array($value)) {
  144. 144:                 $value array($key => $value);
  145. 145:             }
  146. 146:  
  147. 147:             foreach ($value as $key2 => $value2{
  148. 148:                 if (!$this->useKeys{
  149. 149:                     if (!is_scalar($value2)) {
  150. 150:                         throw new InvalidArgumentException("All items must be scalars.");
  151. 151:                     }
  152. 152:                     $key2 $value2;
  153. 153:                 }
  154. 154:  
  155. 155:                 if (isset($this->allowed[$key2])) {
  156. 156:                     throw new InvalidArgumentException("Items contain duplication for key '$key2'.");
  157. 157:                 }
  158. 158:  
  159. 159:                 $this->allowed[$key2$value2;
  160. 160:             }
  161. 161:         }
  162. 162:         return $this;
  163. 163:     }
  164. 164:  
  165. 165:  
  166. 166:  
  167. 167:     /**
  168. 168:      * Returns items from which to choose.
  169. 169:      * @return array 
  170. 170:      */
  171. 171:     final public function getItems()
  172. 172:     {
  173. 173:         return $this->items;
  174. 174:     }
  175. 175:  
  176. 176:  
  177. 177:  
  178. 178:     /**
  179. 179:      * Returns selected value.
  180. 180:      * @return string 
  181. 181:      */
  182. 182:     public function getSelectedItem()
  183. 183:     {
  184. 184:         if (!$this->useKeys{
  185. 185:             return $this->getValue();
  186. 186:  
  187. 187:         else {
  188. 188:             $value $this->getValue();
  189. 189:             return $value === NULL NULL $this->allowed[$value];
  190. 190:         }
  191. 191:     }
  192. 192:  
  193. 193:  
  194. 194:  
  195. 195:     /**
  196. 196:      * Generates control's HTML element.
  197. 197:      * @return Html 
  198. 198:      */
  199. 199:     public function getControl()
  200. 200:     {
  201. 201:         $control parent::getControl();
  202. 202:         $selected $this->getValue();
  203. 203:         $selected is_array($selectedarray_flip($selectedarray($selected => TRUE);
  204. 204:         $option Html::el('option');
  205. 205:  
  206. 206:         foreach ($this->items as $key => $value{
  207. 207:             if (!is_array($value)) {
  208. 208:                 $value array($key => $value);
  209. 209:                 $dest $control;
  210. 210:  
  211. 211:             else {
  212. 212:                 $dest $control->create('optgroup')->label($key);
  213. 213:             }
  214. 214:  
  215. 215:             foreach ($value as $key2 => $value2{
  216. 216:                 if ($value2 instanceof Html{
  217. 217:                     $dest->add((string) $value2->selected(isset($selected[$key2])));
  218. 218:  
  219. 219:                 elseif ($this->useKeys{
  220. 220:                     $dest->add((string) $option->value($key2)->selected(isset($selected[$key2]))->setText($this->translate($value2)));
  221. 221:  
  222. 222:                 else {
  223. 223:                     $dest->add((string) $option->selected(isset($selected[$value2]))->setText($this->translate($value2)));
  224. 224:                 }
  225. 225:             }
  226. 226:         }
  227. 227:         return $control;
  228. 228:     }
  229. 229:  
  230. 230:  
  231. 231:  
  232. 232:     /**
  233. 233:      * Filled validator: has been any item selected?
  234. 234:      * @param  IFormControl 
  235. 235:      * @return bool 
  236. 236:      */
  237. 237:     public static function validateFilled(IFormControl $control)
  238. 238:     {
  239. 239:         $value $control->getValue();
  240. 240:         return is_array($valuecount($value$value !== NULL;
  241. 241:     }
  242. 242: