123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace Ppb\Form\Element;
- use Cube\Form\Element\Select,
- Cube\Controller\Front;
- class ChznSelect extends Select
- {
- const ELEMENT_CLASS = 'chzn-select';
- const SELECT_MULTIPLE_SIZE = '5';
-
- protected $_element = 'ChznSelect';
-
- protected $_baseUrl;
-
- public function __construct($name, $initialize = true)
- {
- parent::__construct($name);
- if ($initialize === true) {
- $this->_baseUrl = Front::getInstance()->getRequest()->getBaseUrl();
- $this->setHeaderCode('<link href="' . $this->_baseUrl . '/js/chosen/chosen.css" media="screen" rel="stylesheet" type="text/css">')
- ->setBodyCode('<script type="text/javascript" src="' . $this->_baseUrl . '/js/chosen/chosen.jquery.min.js"></script>')
- ->setBodyCode(
- "<script type=\"text/javascript\">" . "\n"
- . " $('." . self::ELEMENT_CLASS . "').chosen(); " . "\n"
- . "</script>");
- }
- $this->addAttribute('class', self::ELEMENT_CLASS);
- }
- public function render()
- {
- $output = null;
- $value = $this->getValue();
- if ($this->getMultiple() === true) {
- $this->_attributes['multiple'] = 'multiple';
- $brackets = '';
-
- if (isset($this->_customData['doubleBrackets'])) {
- if ($this->_customData['doubleBrackets'] == true) {
- $brackets = $this->getBrackets();
- $this->setBrackets($brackets . '[]');
- }
- }
- $output .= '<input type="hidden" name="' . $this->_name . $brackets . '" value=""'
- . $this->_endTag;
- }
- if (!isset($this->_attributes['size'])) {
- $this->_attributes['size'] = self::SELECT_MULTIPLE_SIZE;
- }
- $output .= '<select name="' . $this->_name . $this->getBrackets() . '" '
- . $this->renderAttributes() . '>';
- foreach ((array)$this->_multiOptions as $key => $option) {
- $selected = (in_array($key, (array)$value)) ? ' selected' : '';
- $output .= '<option value="' . $key . '"' . $selected . '>' . $option . '</option>';
- }
- $output .= '</select>';
- return $output;
- }
- }
|