FoundationCheckboxGroup.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * @author Ironpilot
  4. * @copyright Copywrite (c) 2011, STAPLE CODE
  5. *
  6. * This file is part of the STAPLE Framework.
  7. *
  8. * The STAPLE Framework is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by the
  10. * Free Software Foundation, either version 3 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * The STAPLE Framework is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. class Staple_Form_FoundationCheckboxGroup extends Staple_Form_Element
  22. {
  23. /**
  24. * An array that holds the Checkbox elements.
  25. * @var array[Staple_Form_CheckboxElement]
  26. */
  27. protected $boxes = array();
  28. public function addCheckbox(Staple_Form_FoundationCheckboxElement $box)
  29. {
  30. $this->boxes[] = $box;
  31. return $this;
  32. }
  33. /**
  34. * Adds multiple checkboxes to the array of checkboxes;
  35. * @param array $boxes
  36. */
  37. public function addCheckboxArray(array $boxes)
  38. {
  39. foreach($boxes as $box)
  40. {
  41. if($box instanceof Staple_Form_FoundationCheckboxElement)
  42. {
  43. $this->addCheckbox($box);
  44. }
  45. }
  46. return $this;
  47. }
  48. /**
  49. * Return all the checkbox objects
  50. * @return array[Staple_Form_CheckboxElement]
  51. */
  52. public function getBoxes()
  53. {
  54. return $this->boxes;
  55. }
  56. /**
  57. * Sorts the boxes by Label
  58. */
  59. public function sortBoxesByLabel()
  60. {
  61. usort($this->boxes, array($this, 'sortCmpLabel'));
  62. return $this;
  63. }
  64. /**
  65. * Sorts the boxes by Name
  66. */
  67. public function sortBoxesByName()
  68. {
  69. usort($this->boxes, array($this, 'sortCmpName'));
  70. return $this;
  71. }
  72. /**
  73. * This simple function resets the $boxes array.
  74. */
  75. public function clearBoxes()
  76. {
  77. $this->boxes = array();
  78. return $this;
  79. }
  80. /**
  81. * Returns an associative array of
  82. * @return array
  83. */
  84. public function getValue()
  85. {
  86. $values = array();
  87. foreach($this->boxes as $key=>$value)
  88. {
  89. $values[$value->getName()] = $value->getValue();
  90. }
  91. return $values;
  92. }
  93. /**
  94. * This function requires an associative array composed of the form field names, followed by their values.
  95. * @return Staple_Form_CheckboxGroup
  96. */
  97. public function setValue(array $inserts)
  98. {
  99. foreach($this->boxes as $key=>$value)
  100. {
  101. if(array_key_exists($value->getName(), $inserts))
  102. {
  103. $this->boxes[$key]->setValue($inserts[$value->getName()]);
  104. }
  105. }
  106. return $this;
  107. }
  108. /**
  109. * Comparison function for sorting by names
  110. * @param Staple_Form_CheckboxElement $a
  111. * @param Staple_Form_CheckboxElement $b
  112. */
  113. private function sortCmpName(Staple_Form_FoundationCheckboxElement $a, Staple_Form_FoundationCheckboxElement $b)
  114. {
  115. return strcmp($a->getName(), $b->getName());
  116. }
  117. /**
  118. * Comparison function for sorting by labels
  119. * @param Staple_Form_CheckboxElement $a
  120. * @param Staple_Form_CheckboxElement $b
  121. */
  122. private function sortCmpLabel(Staple_Form_FoundationCheckboxElement $a, Staple_Form_FoundationCheckboxElement $b)
  123. {
  124. return strcmp($a->getLabel(), $b->getLabel());
  125. }
  126. //-------------------------------------------------BUILDERS-------------------------------------------------
  127. /**
  128. *
  129. * @see Staple_Form_Element::field()
  130. */
  131. public function field()
  132. {
  133. $buff = '<div class="form_checkboxes">';
  134. foreach ($this->boxes as $box)
  135. {
  136. $buff .= $box->build();
  137. }
  138. $buff .= '</div>';
  139. return $buff;
  140. }
  141. public function errors()
  142. {
  143. $errors = implode(" ",$this->getErrors());
  144. return $errors;
  145. }
  146. /**
  147. *
  148. * @see Staple_Form_Element::label()
  149. */
  150. public function label()
  151. {
  152. return "<label".$this->getClassString().">".$this->escape($this->getLabel())."</label>";
  153. }
  154. /**
  155. *
  156. * @see Staple_Form_Element::build()
  157. */
  158. public function build()
  159. {
  160. $buf = '';
  161. $view = FORMS_ROOT.'/fields/CheckboxGroup.phtml';
  162. if(file_exists($view))
  163. {
  164. ob_start();
  165. include $view;
  166. $buf = ob_get_contents();
  167. ob_end_clean();
  168. }
  169. else
  170. {
  171. $buf .= "<div class=\"row\">\n"; //Row Start
  172. $buf .= "<div class=\"small-12 columns\">\n"; //Label Start
  173. $buf .= $this->label();
  174. $buf .= "</div>\n"; //Label End
  175. $buf .= "<div class=\"small-12 columns\">\n"; //Field Start
  176. if(count($this->errors) != 0)
  177. {
  178. $buf .= "<label class=\"error\">";
  179. }
  180. $buf .= $this->field();
  181. if(count($this->errors) != 0)
  182. {
  183. $buf .= "</label>";
  184. $buf .= "<small class=\"error\">";
  185. foreach($this->errors as $error)
  186. {
  187. foreach($error as $message)
  188. {
  189. $buf .= "- $message<br>\n";
  190. }
  191. }
  192. $buf .= "</small>";
  193. }
  194. $buf .= "</div>\n"; //Field End
  195. $buf .= "</div>\n"; //Row end
  196. return $buf;
  197. }
  198. }
  199. }
  200. ?>