FoundationSelectElement.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. class Staple_Form_FoundationSelectElement extends Staple_Form_Element
  3. {
  4. const SORT_VALUES = 1;
  5. const SORT_LABELS_ALPHA = 2;
  6. const SORT_LABELS_REVERSE = 3;
  7. /**
  8. * An array that holds the options list for the select box. The keys represent the values of the options,
  9. * and the values of the array are the labels for the options.
  10. * @var array
  11. */
  12. protected $options = array();
  13. /**
  14. * Boolean to signify a selected element.
  15. * @var bool
  16. */
  17. private $selected = false;
  18. /**
  19. * Defines the size of the select element
  20. * @var int
  21. */
  22. protected $size = 1;
  23. /**
  24. * Boolean to signify that multiple options are selectable in the select element.
  25. * @var bool
  26. */
  27. protected $multiple = false;
  28. /**
  29. * Returns a boolean whether the checkbox is checked or not.
  30. * @return boolean
  31. */
  32. public function isSelected()
  33. {
  34. if($this->selected === true)
  35. {
  36. return true;
  37. }
  38. else
  39. {
  40. return false;
  41. }
  42. }
  43. /**
  44. * Returns a boolean whether the box is a multi-select or not
  45. * @return bool
  46. */
  47. public function isMultiple()
  48. {
  49. return $this->multiple;
  50. }
  51. /**
  52. * Set the multiple attribute of the select element.
  53. * @param bool $bool
  54. * @return $this
  55. */
  56. public function setMultiple($bool = true)
  57. {
  58. $this->multiple = (bool)$bool;
  59. return $this;
  60. }
  61. /**
  62. * Sets the value for the select box
  63. * @param boolean $val
  64. * @return Staple_Form_CheckboxElement
  65. */
  66. public function setValue($val)
  67. {
  68. $this->selected = true;
  69. return parent::setValue($val);
  70. }
  71. /**
  72. * Sets the size of the select element
  73. * @param int $size
  74. * @return $this
  75. */
  76. public function setSize($size)
  77. {
  78. $this->size = (int)$size;
  79. return $this;
  80. }
  81. /**
  82. * Returns the size of the select element
  83. * @return int
  84. */
  85. public function getSize()
  86. {
  87. return $this->size;
  88. }
  89. /**
  90. * Add a single option to the select list.
  91. *
  92. * @param mixed $value
  93. * @param string $label
  94. * @return $this
  95. * @throws Exception
  96. */
  97. public function addOption($value,$label = NULL)
  98. {
  99. if(is_array($value) || is_resource($value))
  100. {
  101. throw new Exception('Select values must be strings or integers.', Staple_Error::APPLICATION_ERROR);
  102. }
  103. else
  104. {
  105. if(isset($label))
  106. {
  107. $this->options[$value] = $label;
  108. }
  109. else
  110. {
  111. $this->options[$value] = $value;
  112. }
  113. }
  114. return $this;
  115. }
  116. /**
  117. * Add an array of values to the select list. Keys of the array become values of the options and the values
  118. * become the labels for the options. The second option allows the use of the labels as the values for the
  119. * options.
  120. *
  121. * @param array $options
  122. * @param boolean $labelvalues
  123. * @return $this
  124. * @throws Exception
  125. */
  126. public function addOptionsArray(array $options, $labelvalues = FALSE)
  127. {
  128. foreach($options as $value=>$label)
  129. {
  130. if(is_array($value) || is_resource($value))
  131. {
  132. throw new Exception('Select values must be strings or integers.', Staple_Error::APPLICATION_ERROR);
  133. }
  134. else
  135. {
  136. if($labelvalues === true)
  137. {
  138. $this->options[$label] = $label;
  139. }
  140. else
  141. {
  142. $this->options[$value] = $label;
  143. }
  144. }
  145. }
  146. return $this;
  147. }
  148. /**
  149. * Removes all the options from the select list.
  150. */
  151. public function clearOptionList()
  152. {
  153. $this->options = array();
  154. return $this;
  155. }
  156. /**
  157. * Returns the options array.
  158. * @return array
  159. */
  160. public function getOptions()
  161. {
  162. return $this->options;
  163. }
  164. /**
  165. * Sorts the options list based on a set of preset sorts.
  166. * @param int $how
  167. * @return $this
  168. */
  169. public function sortOptions($how)
  170. {
  171. switch($how)
  172. {
  173. case self::SORT_VALUES :
  174. ksort($this->options);
  175. break;
  176. case self::SORT_LABELS_ALPHA :
  177. asort($this->options);
  178. break;
  179. case self::SORT_LABELS_REVERSE :
  180. arsort($this->options);
  181. break;
  182. }
  183. return $this;
  184. }
  185. /* (non-PHPdoc)
  186. * @see Staple_Form_Element::field()
  187. */
  188. public function field()
  189. {
  190. $buf = '';
  191. $buf .= " <select name=\"".$this->escape($this->name)."\" id=\"".$this->escape($this->id)."\"";
  192. if($this->size > 1)
  193. {
  194. $buf .= ' size="'.(int)$this->getSize().'"';
  195. }
  196. if($this->multiple === true)
  197. {
  198. $buf .= ' multiple="multiple"';
  199. }
  200. $buf .= $this->getAttribString().">\n";
  201. foreach($this->options as $value=>$label)
  202. {
  203. $select = '';
  204. if($this->value == $value && $this->isSelected())
  205. {
  206. $select = ' selected';
  207. }
  208. $buf .= " <option value=\"".$this->escape($value)."\"$select>".$this->escape($label)."</option>\n";
  209. }
  210. $buf .= " </select>\n";
  211. return $buf;
  212. }
  213. /* (non-PHPdoc)
  214. * @see Staple_Form_Element::label()
  215. */
  216. public function label()
  217. {
  218. if(count($this->errors) != 0)
  219. {
  220. $buf = "<label for=\"".$this->escape($this->id)."\" class=\"error\">";
  221. }
  222. else
  223. {
  224. $buf = "<label for=\"".$this->escape($this->id)."\">";
  225. }
  226. if($this->required == 1)
  227. {
  228. $buf .= "<b>";
  229. $buf .= $this->label;
  230. $buf .= "</b> <small>(<i>Required</i>)</small>";
  231. }
  232. else
  233. {
  234. $buf .= $this->label;
  235. }
  236. $buf .= "</label>\n";
  237. return $buf;
  238. }
  239. /**
  240. * Builds the select list form element.
  241. *
  242. * @see Staple_Form_Element::build()
  243. */
  244. public function build()
  245. {
  246. $buf = '';
  247. $view = FORMS_ROOT.'/fields/FoundationSelectElement.phtml';
  248. if(file_exists($view))
  249. {
  250. ob_start();
  251. include $view;
  252. $buf = ob_get_contents();
  253. ob_end_clean();
  254. }
  255. else
  256. {
  257. $classes = $this->getClassString();
  258. $buf .= "<div class=\"row\">\n"; //Row Start
  259. $buf .= "<div class=\"small-12 columns\">\n"; //Label Start
  260. $buf .= $this->label();
  261. $buf .= "</div>\n"; //Label End
  262. $buf .= "<div class=\"small-12 columns\">\n"; //Field Start
  263. if(count($this->errors) != 0)
  264. {
  265. $buf .= "<label class=\"error\">";
  266. }
  267. $buf .= $this->field();
  268. if(count($this->errors) != 0)
  269. {
  270. $buf .= "</label>";
  271. $buf .= "<small class=\"error\">";
  272. foreach($this->errors as $error)
  273. {
  274. foreach($error as $message)
  275. {
  276. $buf .= "- $message<br>\n";
  277. }
  278. }
  279. $buf .= "</small>";
  280. }
  281. $buf .= "</div>\n"; //Field End
  282. $buf .= "</div>\n"; //Row end
  283. }
  284. return $buf;
  285. }
  286. }