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  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Forms
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Select box control that allows single item selection.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Forms
  20. 20:  *
  21. 21:  * @property-read mixed $rawValue 
  22. 22:  * @property   array $items 
  23. 23:  * @property-read mixed $selectedItem 
  24. 24:  * @property-read bool $firstSkipped 
  25. 25:  */
  26. 26: class SelectBox extends FormControl
  27. 27: {
  28. 28:     /** @var array */
  29. 29:     private $items array();
  30. 30:  
  31. 31:     /** @var array */
  32. 32:     protected $allowed = array();
  33. 33:  
  34. 34:     /** @var bool */
  35. 35:     private $skipFirst FALSE;
  36. 36:  
  37. 37:     /** @var bool */
  38. 38:     private $useKeys TRUE;
  39. 39:  
  40. 40:  
  41. 41:  
  42. 42:     /**
  43. 43:      * @param  string  label
  44. 44:      * @param  array   items from which to choose
  45. 45:      * @param  int     number of rows that should be visible
  46. 46:      */
  47. 47:     public function __construct($label NULLarray $items NULL$size NULL)
  48. 48:     {
  49. 49:         parent::__construct($label);
  50. 50:         $this->control->setName('select');
  51. 51:         $this->control->size $size ? (int) $size NULL;
  52. 52:         $this->control->onfocus 'this.onmousewheel=function(){return false}';  // prevents accidental change in IE
  53. 53:         $this->label->onclick 'document.getElementById(this.htmlFor).focus();return false';  // prevents deselect in IE 5 - 6
  54. 54:         if ($items !== NULL{
  55. 55:             $this->setItems($items);
  56. 56:         }
  57. 57:     }
  58. 58:  
  59. 59:  
  60. 60:  
  61. 61:     /**
  62. 62:      * Returns selected item key.
  63. 63:      * @return mixed 
  64. 64:      */
  65. 65:     public function getValue()
  66. 66:     {
  67. 67:         $allowed $this->allowed;
  68. 68:         if ($this->skipFirst{
  69. 69:             $allowed array_slice($allowed1count($allowed)TRUE);
  70. 70:         }
  71. 71:  
  72. 72:         return is_scalar($this->value&& isset($allowed[$this->value]$this->value NULL;
  73. 73:     }
  74. 74:  
  75. 75:  
  76. 76:  
  77. 77:     /**
  78. 78:      * Returns selected item key (not checked).
  79. 79:      * @return mixed 
  80. 80:      */
  81. 81:     public function getRawValue()
  82. 82:     {
  83. 83:         return is_scalar($this->value$this->value NULL;
  84. 84:     }
  85. 85:  
  86. 86:  
  87. 87:  
  88. 88:     /**
  89. 89:      * Ignores the first item in select box.
  90. 90:      * @param  string 
  91. 91:      * @return SelectBox  provides a fluent interface
  92. 92:      */
  93. 93:     public function skipFirst($item NULL)
  94. 94:     {
  95. 95:         if (is_bool($item)) {
  96. 96:             $this->skipFirst = $item;
  97. 97:         else {
  98. 98:             $this->skipFirst = TRUE;
  99. 99:             if ($item !== NULL{
  100. 100:                 $this->items array('' => $item$this->items;
  101. 101:                 $this->allowed array('' => ''$this->allowed;
  102. 102:             }
  103. 103:         }
  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: