YiiWheels
  • Package
  • Class
  • Tree

Packages

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

Classes

  • WhMultiSelect
 1 <?php
 2 /**
 3  * WhMultiSelect 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.multiselect
 9  * @uses YiiStrap.helpers.TbArray
10  */
11 
12 Yii::import('bootstrap.helpers.TbArray');
13 
14 class WhMultiSelect extends CInputWidget
15 {
16 
17     /**
18      * @var array @param data for generating the list options (value=>display)
19      */
20     public $data = array();
21 
22     /**
23      * @var string[] the JavaScript event handlers.
24      */
25     public $events = array();
26 
27     /**
28      * @var array the plugin options
29      * @see http://davidstutz.github.com/bootstrap-multiselect/
30      */
31     public $pluginOptions;
32 
33     /**
34      * Initializes the widget.
35      */
36     public function init()
37     {
38         if (empty($this->data)) {
39             throw new CException(Yii::t('zii', '"data" attribute cannot be blank'));
40         }
41 
42         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
43     }
44 
45     /**
46      * Runs the widget.
47      */
48     public function run()
49     {
50         $this->renderField();
51         $this->registerClientScript();
52     }
53 
54     /**
55      * Renders the multiselect field
56      */
57     public function renderField()
58     {
59         list($name, $id) = $this->resolveNameID();
60 
61         TbArray::defaultValue('id', $id, $this->htmlOptions);
62         TbArray::defaultValue('name', $name, $this->htmlOptions);
63 
64         // fixes #32: 'multiple' will be forced later in jQuery plugin
65         $this->htmlOptions['multiple'] = 'multiple';
66 
67         if ($this->hasModel()) {
68             echo CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
69         } else {
70             echo CHtml::dropDownList($this->name, $this->value, $this->data, $this->htmlOptions);
71         }
72     }
73 
74     /**
75      * Registers required client script for bootstrap multiselect. It is not used through bootstrap->registerPlugin
76      * in order to attach events if any
77      */
78     public function registerClientScript()
79     {
80         /* publish assets dir */
81         $path      = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
82         $assetsUrl = $this->getAssetsUrl($path);
83 
84         /* @var $cs CClientScript */
85         $cs = Yii::app()->getClientScript();
86 
87         $cs->registerCssFile($assetsUrl . '/css/bootstrap-multiselect.css');
88         $cs->registerScriptFile($assetsUrl . '/js/bootstrap-multiselect.js');
89 
90         /* initialize plugin */
91         $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
92 
93         $this->getApi()->registerPlugin('multiselect', $selector, $this->pluginOptions);
94         $this->getApi()->registerEvents($selector, $this->events);
95     }
96 }
YiiWheels API documentation generated by ApiGen 2.8.0