1 <?php
2 3 4 5 6 7 8 9 10 11 12
13 Yii::import('bootstrap.helpers.TbArray');
14
15 class WhDateRangePicker extends CInputWidget
16 {
17
18 19 20 21
22 public $selector;
23
24 25 26
27 public $callback;
28
29 30 31
32 public $pluginOptions = array();
33
34 35 36
37 public function init()
38 {
39 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
40 $this->htmlOptions['id'] = TbArray::getValue('id', $this->htmlOptions, $this->getId());
41 }
42
43 44 45
46 public function run()
47 {
48 $this->renderField();
49 $this->registerClientScript();
50 }
51
52 53 54
55 public function renderField()
56 {
57 if (null === $this->selector) {
58 list($name, $id) = $this->resolveNameID();
59
60 if ($this->hasModel()) {
61 echo TbHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
62 } else {
63 echo TbHtml::textField($name, $this->value, $this->htmlOptions);
64 }
65
66 $this->setLocaleSettings();
67 }
68 }
69
70 71 72 73 74 75 76 77
78 private function setLocaleSettings()
79 {
80 $this->setDaysOfWeekNames();
81 $this->setMonthNames();
82 }
83
84 85 86
87 private function setDaysOfWeekNames()
88 {
89 if (empty($this->pluginOptions['locale']['daysOfWeek'])) {
90 $this->pluginOptions['locale']['daysOfWeek'] = Yii::app()->locale->getWeekDayNames('narrow', true);
91 }
92 }
93
94 95 96
97 private function setMonthNames()
98 {
99 if (empty($this->pluginOptions['locale']['monthNames'])) {
100 $this->pluginOptions['locale']['monthNames'] = array_values(
101 Yii::app()->locale->getMonthNames('wide', true)
102 );
103 }
104 }
105
106 107 108 109
110 public function registerClientScript()
111 {
112
113 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
114 $assetsUrl = $this->getAssetsUrl($path);
115
116
117 $this->getYiiWheels()->registerAssetJs('moment.min.js');
118
119
120 $cs = Yii::app()->getClientScript();
121
122 $cs->registerCssFile($assetsUrl . '/css/daterangepicker.css');
123 $cs->registerScriptFile($assetsUrl . '/js/daterangepicker.js', CClientScript::POS_END);
124
125
126 $selector = null === $this->selector
127 ? '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId())
128 : $this->selector;
129
130 $callback = ($this->callback instanceof CJavaScriptExpression)
131 ? $this->callback
132 : ($this->callback === null ? '' : new CJavaScriptExpression($this->callback));
133
134 $cs->registerScript(
135 __CLASS__ . '#' . $this->getId(),
136 '$("' . $selector . '").daterangepicker(' .
137 CJavaScript::encode($this->pluginOptions) .
138 ($callback ? ', ' . CJavaScript::encode($callback) : '') .
139 ');'
140 );
141 }
142 }
143