1 <?php
 2  3  4  5  6  7 
 8 namespace foundation\widgets;
 9 
10 use foundation\helpers\Html;
11 use foundation\enum\Enum;
12 
13 14 15 16 17 18 19 20 
21 class SwitchButton extends base\Input
22 {
23     24 25 
26     public $size = Enum::SIZE_SMALL;
27     28 29 
30     public $radius = ENum::RADIUS_ROUND;
31     32 33 
34     public $onLabel = 'On';
35     36 37 
38     public $offLabel = 'Off';
39 
40 
41     42 43 
44     public function init()
45     {
46         Html::addCssClass($this->htmlOptions, Enum::SWITCH_BUTTON);
47         if ($this->size) {
48             Html::addCssClass($this->htmlOptions, $this->size);
49         }
50         if ($this->radius) {
51             Html::addCssClass($this->htmlOptions, $this->radius);
52         }
53         parent::init();
54     }
55 
56     57 58 
59     public function run()
60     {
61         echo $this->renderButtons();
62     }
63 
64     65 66 67 
68     public function renderButtons()
69     {
70         list($name, $id) = $this->resolveNameID();
71         $buttons   = array();
72         $checked   = $this->hasModel() ? (!$this->model->{$this->attribute}) : true;
73         $buttons[] = \CHtml::radioButton($name, $checked, array('id' => $id . '_on', 'value' => 1));
74         $buttons[] = \CHtml::label($this->offLabel, $id . '_on');
75         $checked   = $checked ? false : true;
76         $buttons[] = \CHtml::radioButton($name, $checked, array('id' => $id . '_off', 'value' => 1));
77         $buttons[] = \CHtml::label($this->onLabel, $id . '_off');
78         $buttons[] = '<span></span>';
79 
80         return \CHtml::tag('div', $this->htmlOptions, implode("\n", $buttons));
81     }
82 }