123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- /**
- * A class to create SELECT form elements.
- *
- * @author Ironpilot
- * @copyright Copywrite (c) 2011, STAPLE CODE
- *
- * This file is part of the STAPLE Framework.
- *
- * The STAPLE Framework is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or (at your option)
- * any later version.
- *
- * The STAPLE Framework is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
- */
- class Staple_Form_SelectElement extends Staple_Form_Element
- {
- const SORT_VALUES = 1;
- const SORT_LABELS_ALPHA = 2;
- const SORT_LABELS_REVERSE = 3;
-
- /**
- * An array that holds the options list for the select box. The keys represent the values of the options,
- * and the values of the array are the labels for the options.
- * @var array
- */
- protected $options = array();
- /**
- * Boolean to signify a selected element.
- * @var bool
- */
- private $selected = false;
- /**
- * Defines the size of the select element
- * @var int
- */
- protected $size = 1;
- /**
- * Boolean to signify that multiple options are selectable in the select element.
- * @var bool
- */
- protected $multiple = false;
-
- /**
- * Returns a boolean whether the checkbox is checked or not.
- * @return boolean
- */
- public function isSelected()
- {
- if($this->selected === true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /**
- * Returns a boolean whether the box is a multi-select or not
- * @return bool
- */
- public function isMultiple()
- {
- return $this->multiple;
- }
- /**
- * Set the multiple attribute of the select element.
- * @param bool $bool
- * @return $this
- */
- public function setMultiple($bool = true)
- {
- $this->multiple = (bool)$bool;
- return $this;
- }
-
- /**
- * Sets the value for the select box
- * @param boolean $val
- * @return Staple_Form_CheckboxElement
- */
- public function setValue($val)
- {
- $this->selected = true;
- return parent::setValue($val);
- }
- /**
- * Sets the size of the select element
- * @param int $size
- * @return $this
- */
- public function setSize($size)
- {
- $this->size = (int)$size;
- return $this;
- }
-
- /**
- * Returns the size of the select element
- * @return int
- */
- public function getSize()
- {
- return $this->size;
- }
- /**
- * Add a single option to the select list.
- *
- * @param mixed $value
- * @param string $label
- * @return $this
- * @throws Exception
- */
- public function addOption($value,$label = NULL)
- {
- if(is_array($value) || is_resource($value))
- {
- throw new Exception('Select values must be strings or integers.', Staple_Error::APPLICATION_ERROR);
- }
- else
- {
- if(isset($label))
- {
- $this->options[$value] = $label;
- }
- else
- {
- $this->options[$value] = $value;
- }
- }
- return $this;
- }
- /**
- * Add an array of values to the select list. Keys of the array become values of the options and the values
- * become the labels for the options. The second option allows the use of the labels as the values for the
- * options.
- *
- * @param array $options
- * @param boolean $labelvalues
- * @return $this
- * @throws Exception
- */
- public function addOptionsArray(array $options, $labelvalues = FALSE)
- {
- foreach($options as $value=>$label)
- {
- if(is_array($value) || is_resource($value))
- {
- throw new Exception('Select values must be strings or integers.', Staple_Error::APPLICATION_ERROR);
- }
- else
- {
- if($labelvalues === true)
- {
- $this->options[$label] = $label;
- }
- else
- {
- $this->options[$value] = $label;
- }
- }
- }
- return $this;
- }
-
- /**
- * Removes all the options from the select list.
- */
- public function clearOptionList()
- {
- $this->options = array();
- return $this;
- }
-
- /**
- * Returns the options array.
- * @return array
- */
- public function getOptions()
- {
- return $this->options;
- }
- /**
- * Sorts the options list based on a set of preset sorts.
- * @param int $how
- * @return $this
- */
- public function sortOptions($how)
- {
- switch($how)
- {
- case self::SORT_VALUES :
- ksort($this->options);
- break;
- case self::SORT_LABELS_ALPHA :
- asort($this->options);
- break;
- case self::SORT_LABELS_REVERSE :
- arsort($this->options);
- break;
- }
- return $this;
- }
-
- /* (non-PHPdoc)
- * @see Staple_Form_Element::field()
- */
- public function field()
- {
- $buf = '';
- $buf .= " <select name=\"".$this->escape($this->name)."\" id=\"".$this->escape($this->id)."\"";
- if($this->size > 1)
- {
- $buf .= ' size="'.(int)$this->getSize().'"';
- }
- if($this->multiple === true)
- {
- $buf .= ' multiple="multiple"';
- }
- $buf .= $this->getAttribString().">\n";
- foreach($this->options as $value=>$label)
- {
- $select = '';
- if($this->value == $value && $this->isSelected())
- {
- $select = ' selected';
- }
- $buf .= " <option value=\"".$this->escape($value)."\"$select>".$this->escape($label)."</option>\n";
- }
- $buf .= " </select>\n";
- return $buf;
- }
- /* (non-PHPdoc)
- * @see Staple_Form_Element::label()
- */
- public function label()
- {
- return ' <label for="'.$this->escape($this->id).'"'.$this->getClassString().'>'.$this->label."</label>\n";
- }
- /**
- * Builds the select list form element.
- *
- * @see Staple_Form_Element::build()
- */
- public function build()
- {
- $buf = '';
- $view = FORMS_ROOT.'/fields/SelectElement.phtml';
- if(file_exists($view))
- {
- ob_start();
- include $view;
- $buf = ob_get_contents();
- ob_end_clean();
- }
- else
- {
- $this->addClass('form_element');
- $this->addClass('element_select');
- $classes = $this->getClassString();
- $buf .= "<div$classes id=\"".$this->escape($this->id)."_element\">\n";
- $buf .= $this->label();
- $buf .= $this->field();
- $buf .= "</div>\n";
- }
- return $buf;
- }
- }
|