Source for file RadioList.php

Documentation is available at RadioList.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:  * Set of radio button controls.
  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   array $items 
  34. 34:  * @property-read Html $separatorPrototype 
  35. 35:  * @property-read Html $containerPrototype 
  36. 36:  */
  37. 37: class RadioList extends FormControl
  38. 38: {
  39. 39:     /** @var Html  separator element template */
  40. 40:     protected $separator;
  41. 41:  
  42. 42:     /** @var Html  container element template */
  43. 43:     protected $container;
  44. 44:  
  45. 45:     /** @var array */
  46. 46:     protected $items = array();
  47. 47:  
  48. 48:  
  49. 49:  
  50. 50:     /**
  51. 51:      * @param  string  label
  52. 52:      * @param  array   options from which to choose
  53. 53:      */
  54. 54:     public function __construct($label NULLarray $items NULL)
  55. 55:     {
  56. 56:         parent::__construct($label);
  57. 57:         $this->control->type 'radio';
  58. 58:         $this->container = Html::el();
  59. 59:         $this->separator = Html::el('br');
  60. 60:         if ($items !== NULL$this->setItems($items);
  61. 61:     }
  62. 62:  
  63. 63:  
  64. 64:  
  65. 65:     /**
  66. 66:      * Returns selected radio value.
  67. 67:      * @param  bool 
  68. 68:      * @return mixed 
  69. 69:      */
  70. 70:     public function getValue($raw FALSE)
  71. 71:     {
  72. 72:         return is_scalar($this->value&& ($raw || isset($this->items[$this->value])) $this->value NULL;
  73. 73:     }
  74. 74:  
  75. 75:  
  76. 76:  
  77. 77:     /**
  78. 78:      * Sets options from which to choose.
  79. 79:      * @param  array 
  80. 80:      * @return RadioList  provides a fluent interface
  81. 81:      */
  82. 82:     public function setItems(array $items)
  83. 83:     {
  84. 84:         $this->items $items;
  85. 85:         return $this;
  86. 86:     }
  87. 87:  
  88. 88:  
  89. 89:  
  90. 90:     /**
  91. 77: /**
  92. 78:      * Returns options from which to choose.
  93. 79:      * @return array 
  94. 93:      */
  95. 94:     final public function getItems()
  96. 95:     {
  97. 96:         return $this->items;
  98. 97:     }
  99. 98:  
  100. 99:  
  101. 100:  
  102. 101:     /**
  103. 102:      * Returns separator HTML element template.
  104. 103:      * @return Html 
  105. 104:      */
  106. 105:     final public function getSeparatorPrototype()
  107. 106:     {
  108. 107:         return $this->separator;
  109. 108:     }
  110. 109:  
  111. 110:  
  112. 111:  
  113. 112:     /**
  114. 113:      * Returns container HTML element template.
  115. 114:      * @return Html 
  116. 115:      */
  117. 116:     final public function getContainerPrototype()
  118. 117:     {
  119. 118:         return $this->container;
  120. 119:     }
  121. 120:  
  122. 121:  
  123. 122:  
  124. 123:     /**
  125. 124:      * Generates control's HTML element.
  126. 125:      * @param  mixed 
  127. 126:      * @return Html 
  128. 127:      */
  129. 128:     public function getControl($key NULL)
  130. 129:     {
  131. 130:         if ($key === NULL{
  132. 131:             $container clone $this->container;
  133. 132:             $separator = (string) $this->separator;
  134. 133:  
  135. 134:         elseif (!isset($this->items[$key])) {
  136. 135:             return NULL;
  137. 136:         }
  138. 137:  
  139. 138:         $control parent::getControl();
  140. 139:         $id $control->id;
  141. 140:         $counter = -1;
  142. 141:         $value $this->value === NULL NULL : (string) $this->getValue();
  143. 142:         $label Html::el('label');
  144. 143:  
  145. 144:         foreach ($this->items as $k => $val{
  146. 145:             $counter++;
  147. 146:             if ($key !== NULL && $key != $kcontinue// intentionally ==
  148. 147:  
  149. 148:             $control->id $label->for $id '-' $counter;
  150. 149:             $control->checked = (string) $k === $value;
  151. 150:             $control->value $k;
  152. 151:  
  153. 152:             if ($val instanceof Html{
  154. 153:                 $label->setHtml($val);
  155. 154:             else {
  156. 155:                 $label->setText($this->translate($val));
  157. 156:             }
  158. 157:  
  159. 158:             if ($key !== NULL{
  160. 159:                 return (string) $control . (string) $label;
  161. 160:             }
  162. 161:  
  163. 162:             $container->add((string) $control . (string) $label $separator);
  164. 163:             // TODO: separator after last item?
  165. 164:         }
  166. 165:  
  167. 166:         return $container;
  168. 167:     }
  169. 168:  
  170. 169:  
  171. 170:  
  172. 171:     /**
  173. 172:      * Generates label's HTML element.
  174. 173:      * @param  string 
  175. 174:      * @return void 
  176. 175:      */
  177. 176:     public function getLabel($caption NULL)
  178. 177:     {
  179. 178:         $label parent::getLabel($caption);
  180. 179:         $label->for NULL;
  181. 180:         return $label;
  182. 181:     }
  183. 182:  
  184. 183:  
  185. 184:  
  186. 185:     /**
  187. 186:      * Filled validator: has been any radio button selected?
  188. 187:      * @param  IFormControl 
  189. 188:      * @return bool 
  190. 189:      */
  191. 190:     public static function validateFilled(IFormControl $control)
  192. 191:     {
  193. 192:         return $control->getValue(!== NULL;
  194. 193:     }
  195. 194: