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