1 <?php
2 3 4 5 6 7 8 9 10
11 Yii::import('bootstrap.helpers.TbHtml');
12 Yii::import('bootstrap.helpers.TbArray');
13
14 class WhDatePicker extends CInputWidget
15 {
16 17 18
19 public $pluginOptions = array();
20
21 22 23
24 public $events = array();
25
26 27 28
29 public function init()
30 {
31 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
32
33 TbArray::defaultValue('autocomplete', 'off', $this->htmlOptions);
34 TbHtml::addCssClass('grd-white', $this->htmlOptions);
35
36 $this->initOptions();
37 }
38
39 40 41
42 public function initOptions()
43 {
44 TbArray::defaultValue('format', 'mm/dd/yyyy', $this->pluginOptions);
45 TbArray::defaultValue('autoclose', true, $this->pluginOptions);
46 }
47
48 49 50
51 public function run()
52 {
53 $this->renderField();
54 $this->registerClientScript();
55 }
56
57 58 59
60 public function renderField()
61 {
62 list($name, $id) = $this->resolveNameID();
63
64 TbArray::defaultValue('id', $id, $this->htmlOptions);
65 TbArray::defaultValue('name', $name, $this->htmlOptions);
66
67 if ($this->hasModel()) {
68 echo TbHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
69
70 } else {
71 echo TbHtml::textField($name, $this->value, $this->htmlOptions);
72 }
73 }
74
75 76 77
78 public function registerClientScript()
79 {
80
81 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
82 $assetsUrl = $this->getAssetsUrl($path);
83
84
85 $cs = Yii::app()->getClientScript();
86
87 $cs->registerCssFile($assetsUrl . '/css/datepicker.css');
88 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-datepicker.js', CClientScript::POS_END);
89
90 if ($language = TbArray::getValue('language', $this->pluginOptions)) {
91 $cs->registerScriptFile(
92 $assetsUrl . '/js/locales/bootstrap-datepicker.' . $language . '.js',
93 CClientScript::POS_END
94 );
95 }
96
97
98 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
99
100 $this->getApi()->registerPlugin('bdatepicker', $selector, $this->pluginOptions);
101 $this->getApi()->registerEvents($selector, $this->events);
102
103 }
104 }
105