FoundationRadioGroup.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. class Staple_Form_FoundationRadioGroup extends Staple_Form_Element
  3. {
  4. const SORT_VALUES = 1;
  5. const SORT_LABELS_ALPHA = 2;
  6. const SORT_LABELS_REVERSE = 3;
  7. const SORT_USER = 4;
  8. /**
  9. * An array that holds the options list for the select box. The keys represent the values of the options,
  10. * and the values of the array are the labels for the options.
  11. * @var array
  12. */
  13. protected $buttons = array();
  14. /**
  15. * Boolean whether or not any button has been checked. Corrects for a "0" value radio box.
  16. * @var unknown_type
  17. */
  18. protected $checked = false;
  19. /**
  20. * Add a single option to the select list.
  21. *
  22. * @param mixed $value
  23. * @param string $label
  24. * @throws Exception
  25. */
  26. public function addButton($value,$label = NULL)
  27. {
  28. if(is_array($value) || is_resource($value))
  29. {
  30. throw new Exception('Select values must be strings or integers.', Staple_Error::APPLICATION_ERROR);
  31. }
  32. else
  33. {
  34. if(isset($label))
  35. {
  36. $this->buttons[$value] = $label;
  37. }
  38. else
  39. {
  40. $this->buttons[$value] = $value;
  41. }
  42. }
  43. return $this;
  44. }
  45. /**
  46. * Add an array of values to the select list. Keys of the array become values of the options and the values
  47. * become the labels for the options. The second option allows the use of the labels as the values for the
  48. * options.
  49. *
  50. * @param array $options
  51. * @param boolean $labelvalues
  52. * @throws Exception
  53. */
  54. public function addButtonsArray(array $options, $labelvalues = FALSE)
  55. {
  56. foreach($options as $value=>$label)
  57. {
  58. if(is_array($value) || is_resource($value))
  59. {
  60. throw new Exception('Select values must be strings or integers.', Staple_Error::APPLICATION_ERROR);
  61. }
  62. else
  63. {
  64. if($labelvalues === true)
  65. {
  66. $this->buttons[$label] = $label;
  67. }
  68. else
  69. {
  70. $this->buttons[$value] = $label;
  71. }
  72. }
  73. }
  74. return $this;
  75. }
  76. /**
  77. * Returns the options array.
  78. * @return array
  79. */
  80. public function getButtons()
  81. {
  82. return $this->buttons;
  83. }
  84. /**
  85. * Removes a button by its value.
  86. * @param mixed $value
  87. * @return Staple_Form_RadioGroup
  88. */
  89. public function removeButtonByValue($value)
  90. {
  91. unset($this->buttons[$value]);
  92. return $this;
  93. }
  94. /**
  95. * Removes a button by searching for the button name.
  96. * @param string $name
  97. * @return boolean
  98. */
  99. public function removeButtonByName($name)
  100. {
  101. if(($key = array_search($name, $this->buttons)) !== false)
  102. {
  103. unset($this->buttons[$key]);
  104. return true;
  105. }
  106. return false;
  107. }
  108. /**
  109. * Sorts the options list based on a set of preset sorts.
  110. * @param int $how
  111. * @param callback $sortFunction
  112. */
  113. public function sortOptions($how, $sortFunction = NULL)
  114. {
  115. switch($how)
  116. {
  117. case self::SORT_VALUES :
  118. ksort($this->buttons);
  119. break;
  120. case self::SORT_LABELS_ALPHA :
  121. asort($this->buttons);
  122. break;
  123. case self::SORT_LABELS_REVERSE :
  124. arsort($this->buttons);
  125. break;
  126. case self::SORT_USER:
  127. usort($this->buttons, $sortFunction);
  128. break;
  129. }
  130. return $this;
  131. }
  132. public function setValue($insert)
  133. {
  134. $this->checked = true;
  135. return parent::setValue($insert);
  136. }
  137. /* (non-PHPdoc)
  138. * @see Staple_Form_Element::field()
  139. */
  140. public function field()
  141. {
  142. $buf = '';
  143. foreach($this->buttons as $value=>$label)
  144. {
  145. $check = '';
  146. if($this->value == $value && $this->checked === true)
  147. {
  148. $check = ' checked';
  149. }
  150. $buf .= "<div class=\"row\">";
  151. $buf .= "<div class=\"small-12 columns\">";
  152. $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";
  153. $buf .= "<label for=\"".$this->escape($this->id)."_".$this->escape($value)."\">".$this->escape($label)."</label>\n";
  154. $buf .= "</div>";
  155. $buf .= "</div>";
  156. }
  157. return $buf;
  158. }
  159. /* (non-PHPdoc)
  160. * @see Staple_Form_Element::label()
  161. */
  162. public function label()
  163. {
  164. if(count($this->errors) != 0)
  165. {
  166. $buf = "<label for=\"".$this->escape($this->id)."\" class=\"error\">";
  167. }
  168. else
  169. {
  170. $buf = "<label for=\"".$this->escape($this->id)."\">";
  171. }
  172. if($this->required == 1)
  173. {
  174. $buf .= "<b>";
  175. $buf .= $this->label;
  176. $buf .= "</b> <small>(<i>Required</i>)</small>";
  177. }
  178. else
  179. {
  180. $buf .= $this->label;
  181. }
  182. $buf .= "</label>\n";
  183. return $buf;
  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/FoundationRadioGroupElement.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. $classes = $this->getClassString();
  204. $buf .= "<div class=\"row\">\n"; //Row Start
  205. $buf .= "<div class=\"small-12 columns\">\n"; //Label Start
  206. $buf .= $this->label();
  207. $buf .= "</div>\n"; //Label End
  208. $buf .= "<div class=\"small-12 columns\">\n"; //Field Start
  209. if(count($this->errors) != 0)
  210. {
  211. $buf .= "<label class=\"error\">";
  212. }
  213. $buf .= $this->field();
  214. if(count($this->errors) != 0)
  215. {
  216. $buf .= "</label>";
  217. $buf .= "<small class=\"error\">";
  218. foreach($this->errors as $error)
  219. {
  220. foreach($error as $message)
  221. {
  222. $buf .= "- $message<br>\n";
  223. }
  224. }
  225. $buf .= "</small>";
  226. }
  227. $buf .= "</div>\n"; //Field End
  228. $buf .= "</div>\n"; //Row end
  229. }
  230. return $buf;
  231. }
  232. }