RadioGroup.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * A class to create radio button element groups.
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. class Staple_Form_RadioGroup extends Staple_Form_Element
  24. {
  25. const SORT_VALUES = 1;
  26. const SORT_LABELS_ALPHA = 2;
  27. const SORT_LABELS_REVERSE = 3;
  28. const SORT_USER = 4;
  29. /**
  30. * An array that holds the options list for the select box. The keys represent the values of the options,
  31. * and the values of the array are the labels for the options.
  32. * @var array
  33. */
  34. protected $buttons = array();
  35. /**
  36. * Boolean whether or not any button has been checked. Corrects for a "0" value radio box.
  37. * @var unknown_type
  38. */
  39. protected $checked = false;
  40. /**
  41. * Add a single option to the select list.
  42. *
  43. * @param mixed $value
  44. * @param string $label
  45. * @throws Exception
  46. */
  47. public function addButton($value,$label = NULL)
  48. {
  49. if(is_array($value) || is_resource($value))
  50. {
  51. throw new Exception('Select values must be strings or integers.', Staple_Error::APPLICATION_ERROR);
  52. }
  53. else
  54. {
  55. if(isset($label))
  56. {
  57. $this->buttons[$value] = $label;
  58. }
  59. else
  60. {
  61. $this->buttons[$value] = $value;
  62. }
  63. }
  64. return $this;
  65. }
  66. /**
  67. * Add an array of values to the select list. Keys of the array become values of the options and the values
  68. * become the labels for the options. The second option allows the use of the labels as the values for the
  69. * options.
  70. *
  71. * @param array $options
  72. * @param boolean $labelvalues
  73. * @throws Exception
  74. */
  75. public function addButtonsArray(array $options, $labelvalues = FALSE)
  76. {
  77. foreach($options as $value=>$label)
  78. {
  79. if(is_array($value) || is_resource($value))
  80. {
  81. throw new Exception('Select values must be strings or integers.', Staple_Error::APPLICATION_ERROR);
  82. }
  83. else
  84. {
  85. if($labelvalues === true)
  86. {
  87. $this->buttons[$label] = $label;
  88. }
  89. else
  90. {
  91. $this->buttons[$value] = $label;
  92. }
  93. }
  94. }
  95. return $this;
  96. }
  97. /**
  98. * Returns the options array.
  99. * @return array
  100. */
  101. public function getButtons()
  102. {
  103. return $this->buttons;
  104. }
  105. /**
  106. * Removes a button by its value.
  107. * @param mixed $value
  108. * @return Staple_Form_RadioGroup
  109. */
  110. public function removeButtonByValue($value)
  111. {
  112. unset($this->buttons[$value]);
  113. return $this;
  114. }
  115. /**
  116. * Removes a button by searching for the button name.
  117. * @param string $name
  118. * @return boolean
  119. */
  120. public function removeButtonByName($name)
  121. {
  122. if(($key = array_search($name, $this->buttons)) !== false)
  123. {
  124. unset($this->buttons[$key]);
  125. return true;
  126. }
  127. return false;
  128. }
  129. /**
  130. * Sorts the options list based on a set of preset sorts.
  131. * @param int $how
  132. * @param callback $sortFunction
  133. */
  134. public function sortOptions($how, $sortFunction = NULL)
  135. {
  136. switch($how)
  137. {
  138. case self::SORT_VALUES :
  139. ksort($this->buttons);
  140. break;
  141. case self::SORT_LABELS_ALPHA :
  142. asort($this->buttons);
  143. break;
  144. case self::SORT_LABELS_REVERSE :
  145. arsort($this->buttons);
  146. break;
  147. case self::SORT_USER:
  148. usort($this->buttons, $sortFunction);
  149. break;
  150. }
  151. return $this;
  152. }
  153. public function setValue($insert)
  154. {
  155. $this->checked = true;
  156. return parent::setValue($insert);
  157. }
  158. /* (non-PHPdoc)
  159. * @see Staple_Form_Element::field()
  160. */
  161. public function field()
  162. {
  163. $buf = '';
  164. foreach($this->buttons as $value=>$label)
  165. {
  166. $check = '';
  167. if($this->value == $value && $this->checked === true)
  168. {
  169. $check = ' checked';
  170. }
  171. $buf .= " <div class=\"form_radio\" id=\"".$this->escape($this->id)."_".$this->escape($value)."_div\">\n";
  172. $buf .= " <input type=\"radio\" name=\"".$this->escape($this->name)."\" id=\"".$this->escape($this->id)."_".$this->escape($value)."\" value=\"".$this->escape($value)."\"$check".$this->getAttribString().">\n";
  173. $buf .= " <label for=\"".$this->escape($this->id)."_".$this->escape($value)."\">".$this->escape($label)."</label>\n";
  174. $buf .= " </div>\n";
  175. }
  176. return $buf;
  177. }
  178. /* (non-PHPdoc)
  179. * @see Staple_Form_Element::label()
  180. */
  181. public function label()
  182. {
  183. return ' <label'.$this->getClassString().'>'.$this->label."</label>\n";
  184. }
  185. /**
  186. * Builds the select list form element.
  187. *
  188. * @see Staple_Form_Element::build()
  189. */
  190. public function build()
  191. {
  192. $buf = '';
  193. $view = FORMS_ROOT.'/fields/RadioGroup.phtml';
  194. if(file_exists($view))
  195. {
  196. ob_start();
  197. include $view;
  198. $buf = ob_get_contents();
  199. ob_end_clean();
  200. }
  201. else
  202. {
  203. $this->addClass('form_element');
  204. $this->addClass('element_radiogroup');
  205. $classes = $this->getClassString();
  206. $buf .= "<div$classes id=\"".$this->escape($this->id)."_element\">\n";
  207. $buf .= $this->label();
  208. $buf .= $this->field();
  209. $buf .= $this->instructions();
  210. $buf .= "</div>\n";
  211. }
  212. return $buf;
  213. }
  214. }