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