YiiWheels
  • Package
  • Class
  • Tree

Packages

  • yiiwheels
    • behaviors
    • widgets
    • widgets
      • ace
      • box
      • datepicker
      • daterangepicker
      • datetimepicker
      • detail
      • editable
      • fileupload
      • fileuploader
      • formhelpers
      • gallery
      • google
      • grid
        • behaviors
        • operations
      • highcharts
      • maskInput
      • maskmoney
      • modal
      • multiselect
      • rangeslider
      • redactor
      • select2
      • sparklines
      • switch
      • timeago
      • timepicker
      • toggle
      • typeahead

Classes

  • WhCountries
  • WhDatePickerHelper
  • WhFonts
  • WhFontSizes
  • WhGoogleFonts
  • WhLanguages
  • WhPhone
  • WhSelectBox
  • WhStates
  • WhTimePickerHelper
  • WhTimezones
  1 <?php
  2 /**
  3  * WhSelectBox widget class
  4  *
  5  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  6  * @copyright Copyright &copy; 2amigos.us 2013-
  7  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  8  * @package YiiWheels.widgets.formhelpers
  9  * @uses YiiStrap.helpers.TbArray
 10  * @uses YiiStrap.helpers.TbHtml
 11  */
 12 Yii::import('bootstrap.helpers.TbArray');
 13 Yii::import('bootstrap.helpers.TbHtml');
 14 
 15 class WhSelectBox extends CInputWidget
 16 {
 17 
 18     /**
 19      * @var array the data list to display
 20      */
 21     public $data = array();
 22 
 23     /**
 24      * @var string size. Valid values are:
 25      *
 26      * - input-mini
 27      * - input-small
 28      * - input-medium
 29      * - input-large
 30      * - input-xlarge
 31      * - input-xxlarge
 32      */
 33     public $size = 'input-medium';
 34 
 35     /**
 36      * @var bool whether to display filter or not
 37      */
 38     public $displayFilter = true;
 39 
 40     /**
 41      * @var array the htmlOptions of the wrapper layer
 42      */
 43     public $wrapperOptions = array();
 44 
 45     /**
 46      * Widget's initialization method
 47      * @throws CException
 48      */
 49     public function init()
 50     {
 51         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
 52     }
 53 
 54     /**
 55      * Runs the widget.
 56      */
 57     public function run()
 58     {
 59         $this->renderField();
 60         $this->registerClientScript();
 61     }
 62 
 63     /**
 64      * Renders the input file field
 65      */
 66     public function renderField()
 67     {
 68         list($name, $id) = $this->resolveNameID();
 69 
 70         TbArray::defaultValue('id', $id, $this->htmlOptions);
 71         TbArray::defaultValue('name', $name, $this->htmlOptions);
 72 
 73         TbHtml::addCssClass('bfh-selectbox', $this->wrapperOptions);
 74         echo CHtml::openTag('div', $this->wrapperOptions);
 75         if ($this->hasModel()) {
 76             echo CHtml::activeHiddenField($this->model, $this->attribute, $this->htmlOptions);
 77             $value = $this->model->{$this->attribute};
 78             $valueText = $value && isset($this->data[$value]) ? $this->data[$value] : '&nbsp;';
 79         } else {
 80             echo CHtml::hiddenField($name, $this->value, $this->htmlOptions);
 81             $value = $this->value;
 82             $valueText = $value && isset($this->data[$value]) ? $this->data[$value] : '&nbsp;';
 83         }
 84 
 85         echo CHtml::openTag('a', array('class' => 'bfh-selectbox-toggle', 'role' => 'button', 'data-toggle' => 'bfh-selectbox', 'href' => '#'));
 86             echo CHtml::tag('span', array('class' => 'bfh-selectbox-option ' . $this->size, 'data-option' => $value), $valueText);
 87             echo CHtml::tag('b', array('class' => 'caret'), '&nbsp;');
 88         echo CHtml::closeTag('a');
 89 
 90         echo CHtml::openTag('div', array('class' => 'bfh-selectbox-options'));
 91         if($this->displayFilter) {
 92             echo '<input type="text" class="bfh-selectbox-filter">';
 93         }
 94         $items = array();
 95         foreach($this->data as $key=>$item) {
 96             $items[] = CHtml::tag('a', array('tabindex' => '-1', 'href' => '#', 'data-option' => $key), $item);
 97         }
 98         echo CHtml::tag('ul', array('role'=>'options'), '<li>' . implode('</li><li>', $items) . '</i>');
 99         echo CHtml::closeTag('div');
100 
101         echo CHtml::closeTag('div');
102     }
103 
104     /**
105      * Registers client script
106      */
107     public function registerClientScript()
108     {
109         /* publish assets dir */
110         $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
111         $assetsUrl = $this->getAssetsUrl($path);
112 
113         /* @var $cs CClientScript */
114         $cs = Yii::app()->getClientScript();
115 
116         $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers.css');
117         $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-selectbox.js');
118 
119     }
120 }
YiiWheels API documentation generated by ApiGen 2.8.0