SelectElement.class.php 6.1 KB

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