1 <?php
2 3 4 5 6 7 8 9 10
11 Yii::import('bootstrap.helpers.TbArray');
12
13 class WhTimezones extends CInputWidget
14 {
15 16 17 18 19 20
21 public $pluginOptions = array();
22
23 24 25
26 public $useHelperSelectBox = false;
27
28 29 30
31 public $helperOptions = array();
32
33
34 35 36 37
38 public function init()
39 {
40
41 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
42
43 TbHtml::addCssClass('bfh-timezones', $this->htmlOptions);
44 }
45
46 47 48
49 public function run()
50 {
51 $this->renderField();
52 $this->registerClientScript();
53 }
54
55 56 57
58 public function renderField()
59 {
60 list($name, $id) = $this->resolveNameID();
61
62 TbArray::defaultValue('id', $id, $this->htmlOptions);
63 TbArray::defaultValue('name', $name, $this->htmlOptions);
64
65 if ($this->useHelperSelectBox) {
66 $select = Yii::createComponent(CMap::mergeArray($this->helperOptions, array(
67 'class' => 'yiiwheels.widgets.formhelpers.WhSelectBox',
68 'htmlOptions' => $this->htmlOptions,
69 'model' => $this->model,
70 'attribute' => $this->attribute,
71 'name' => $this->name,
72 'value' => $this->value,
73 'wrapperOptions' => array(
74 'class' => 'bfh-timezones',
75 'data-country' => $this->hasModel() ? $this->model->{$this->attribute} : $this->value,
76 'data-timezone' => isset($this->pluginOptions['timezone'])
77 ? $this->pluginOptions['timezone']
78 : null
79 )
80 )));
81 $select->init();
82 $select->run();
83 } else {
84 $this->htmlOptions['data-country'] = $this->hasModel()
85 ? $this->model->{$this->attribute}
86 : $this->value;
87 $this->htmlOptions['data-timezone'] = isset($this->pluginOptions['timezone'])
88 ? $this->pluginOptions['timezone']
89 : null;
90 if ($this->hasModel()) {
91 echo CHtml::activeDropDownList($this->model, $this->attribute, array(), $this->htmlOptions);
92 } else {
93 echo CHtml::dropDownList($name, $this->value, array(), $this->htmlOptions);
94 }
95 }
96 }
97
98 99 100
101 public function registerClientScript()
102 {
103
104 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
105 $assetsUrl = $this->getAssetsUrl($path);
106
107
108 $cs = Yii::app()->getClientScript();
109
110 $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers.css');
111
112 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-timezones.codes.js');
113 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-timezones.js');
114
115
116 if(!$this->useHelperSelectBox)
117 {
118 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
119 $this->getApi()->registerPlugin('bfhtimezones', $selector, $this->pluginOptions);
120 }
121
122 }
123 }