1 <?php
2 3 4 5 6 7 8 9 10 11 12 13
14 Yii::import('bootstrap.helpers.TbArray');
15 Yii::import('bootstrap.helpers.TbHtml');
16
17 class WhSwitch extends CInputWidget
18 {
19 20 21
22 public $inputType = 'checkbox';
23
24 25 26
27 public $size = 'small';
28
29 30 31
32 public $onColor = 'primary';
33
34 35 36
37 public $offColor = 'warning';
38
39 40 41
42 public $animated = true;
43
44 45 46
47 public $onLabel;
48
49 50 51
52 public $offLabel;
53
54 55 56
57 public $textLabel;
58
59 60 61
62 public $events = array();
63
64 65 66
67 public $debugMode = false;
68
69 70 71
72 protected $pluginOptions = array();
73
74
75 76 77 78
79 public function init()
80 {
81 if (!in_array($this->inputType, array('radio', 'checkbox'))) {
82 throw new CException(Yii::t('zii', '"inputType" attribute must be of type "radio" or "checkbox"'));
83 }
84 if (!in_array($this->size, array('mini', 'small', 'large'))) {
85 throw new CException(Yii::t('zii', 'Unknown value for attribute "size".'));
86 }
87 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
88
89 TbHtml::addCssClass('make-switch', $this->pluginOptions);
90 TbHtml::addCssClass('switch-' . $this->size, $this->pluginOptions);
91 if(!$this->animated) {
92 $this->pluginOptions['data-animated'] = 'false';
93 }
94 $this->pluginOptions['data-on-label'] = $this->onLabel;
95 $this->pluginOptions['data-off-label'] = $this->offLabel;
96 $this->pluginOptions['data-text-label'] = $this->textLabel;
97 $this->pluginOptions['data-on'] = $this->onColor;
98 $this->pluginOptions['data-off'] = $this->offColor;
99 }
100
101 102 103
104 public function run()
105 {
106 $this->renderField();
107 $this->registerClientScript();
108 }
109
110 111 112
113 public function renderField()
114 {
115 list($name, $id) = $this->resolveNameID();
116
117 TbArray::defaultValue('id', $id, $this->htmlOptions);
118 TbArray::defaultValue('name', $name, $this->htmlOptions);
119
120 $this->pluginOptions['id'] = $this->htmlOptions['id'] . '_switch';
121 echo CHtml::openTag('div', $this->pluginOptions);
122 if ($this->hasModel()) {
123 echo $this->inputType == 'radio'
124 ? CHtml::activeRadioButton($this->model, $this->attribute, $this->htmlOptions)
125 : CHtml::activeCheckBox($this->model, $this->attribute, $this->htmlOptions);
126 } else {
127 echo $this->inputType == 'radio'
128 ? CHtml::radioButton($this->name, $this->value, $this->htmlOptions)
129 : CHtml::checkBox($this->name, $this->value, $this->htmlOptions);
130 }
131 echo CHtml::closeTag('div');
132 }
133
134 135 136 137
138 public function registerClientScript()
139 {
140
141 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
142 $assetsUrl = $this->getAssetsUrl($path);
143
144
145 $cs = Yii::app()->getClientScript();
146
147 $min = $this->debugMode
148 ? '.min'
149 : '';
150
151 $cs->registerCssFile($assetsUrl . '/css/bootstrap-switch.css');
152 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-switch' . $min . '.js', CClientScript::POS_END);
153
154 if($this->events)
155 {
156
157 $selector = '#' . TbArray::getValue('id', $this->pluginOptions);
158
159 $this->getApi()->registerEvents($selector, $this->events);
160 }
161 }
162 }