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