Created
July 17, 2013 08:26
-
-
Save tamboer/6018798 to your computer and use it in GitHub Desktop.
render html selectors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * html selectors | |
| * | |
| * set of html selectors on dashboard and ... | |
| * | |
| * TODO OPTIONS | |
| * 1. pass one big array and loop to create selectors | |
| * 2. add specific functionality | |
| * - example: switch off selectors if user has no permission | |
| * | |
| * | |
| * | |
| * @param array $selectors | |
| */ | |
| class Zend_View_Helper_RenderSelectors extends Zend_View_Helper_Abstract { | |
| protected $_sampleSelectors = array(); | |
| protected $_selectors = array(); | |
| protected $_params; | |
| function renderSelectors($params) | |
| { | |
| $this->_params = $params; | |
| //$this->newSelect($name, $label, $selected, $options = array()); | |
| $this->_defaultSelectors(); | |
| $this->_renderSelectors(); | |
| } | |
| protected function _defaultSelectors() | |
| { | |
| $name = 'name'; | |
| $label = 'label'; | |
| $selected = 2; | |
| $options = array( | |
| array('value' => 1, 'label' => 'one'), | |
| array('value' => 2, 'label' => 'two'), | |
| array('value' => 3, 'label' => 'three'), | |
| array('value' => 4, 'label' => 'four'), | |
| array('value' => 5, 'label' => 'five'), | |
| ); | |
| $this->createSelect($name, $label, $selected, $options); | |
| } | |
| public function createSelect($name, $label, $selected, $options = array()) | |
| { | |
| $opt_pairs = array(); | |
| foreach ($options as $key => $option) { | |
| $opt_pairs[$key]['value'] = $option['value']; | |
| $opt_pairs[$key]['label'] = $option['label']; | |
| } | |
| $select = array( | |
| 'name' => $name, | |
| 'label' => $label, | |
| 'selected' => $selected, | |
| 'options' => $opt_pairs, | |
| ); | |
| $this->_addSelectToArray($select); | |
| } | |
| protected function _addSelectToArray($select) | |
| { | |
| $this->_selectors[] = $select; | |
| } | |
| protected function _renderSelectors() | |
| { | |
| $selectors = $this->_selectors; | |
| foreach ($selectors as $key => $selector) { | |
| echo $this->_castSelect($selector['name'], $selector['selected'], $selector['options']); | |
| } | |
| } | |
| protected function _castSelect($name, $selected, $options = array()) | |
| { | |
| $str = '<select name="' . $name . '">'; | |
| foreach ($options as $key => $value) { | |
| if ($value['value'] == $selected) { | |
| $str .= '<option selected value="' . $value['value'] . '">' . $value['label'] . '</option>'; | |
| } else { | |
| $str .= '<option value="' . $value['value'] . '">' . $value['label'] . '</option>'; | |
| } | |
| } | |
| $str .= '</select>'; | |
| return $str; | |
| } | |
| public function newSelect($name, $label, $selected, $options = array()) | |
| { | |
| $this->createSelect($name, $label, $selected, $options); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment