1 <?php
2 3 4 5 6 7 8 9 10 11
12 Yii::import('bootstrap.helpers.TbArray');
13 Yii::import('bootstrap.helpers.TbHtml');
14
15 class WhDatePickerHelper extends CInputWidget
16 {
17
18 19 20
21 public $format = 'y-m-d';
22
23 24 25
26 public $inputOptions = array();
27
28 29 30
31 public $pluginOptions = 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-datepicker', $this->htmlOptions);
44 $this->htmlOptions['data-format'] = $this->format;
45 $this->htmlOptions['data-date'] = $this->hasModel()
46 ? $this->model->{$this->attribute}
47 : $this->value;
48
49 $this->inputOptions['readonly'] = true;
50 $this->pluginOptions['format'] = $this->format;
51 $this->pluginOptions['date'] = $this->htmlOptions['data-date'];
52
53 }
54
55 56 57
58 public function run()
59 {
60 $this->renderField();
61 $this->registerClientScript();
62 }
63
64 65 66
67 public function renderField()
68 {
69
70 list($name, $id) = $this->resolveNameID();
71
72 TbArray::defaultValue('id', $id, $this->htmlOptions);
73 TbArray::defaultValue('name', $name, $this->htmlOptions);
74
75 echo CHtml::openTag('div', $this->htmlOptions);
76 echo CHtml::openTag('div', array(
77 'class' => 'input-prepend bfh-datepicker-toggle',
78 'data-toggle' => 'bfh-datepicker'
79 ));
80 echo CHtml::tag('span', array('class' => 'add-on'), TbHtml::icon(TbHtml::ICON_CALENDAR));
81 if ($this->hasModel()) {
82 echo CHtml::activeTextField($this->model, $this->attribute, $this->inputOptions);
83 } else {
84 echo CHtml::textField($name, $this->value, $this->inputOptions);
85 }
86 echo CHtml::closeTag('div');
87
88 echo '<div class="bfh-datepicker-calendar">
89 <table class="calendar table table-bordered">
90 <thead>
91 <tr class="months-header">
92 <th class="month" colspan="4">
93 <a class="previous" href="#"><i class="icon-chevron-left"></i></a>
94 <span></span>
95 <a class="next" href="#"><i class="icon-chevron-right"></i></a>
96 </th>
97 <th class="year" colspan="3">
98 <a class="previous" href="#"><i class="icon-chevron-left"></i></a>
99 <span></span>
100 <a class="next" href="#"><i class="icon-chevron-right"></i></a>
101 </th>
102 </tr>
103 <tr class="days-header">
104 </tr>
105 </thead>
106 <tbody>
107 </tbody>
108 </table>
109 </div>';
110 echo CHtml::closeTag('div');
111 }
112
113 114 115
116 public function registerClientScript()
117 {
118
119 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
120 $assetsUrl = $this->getAssetsUrl($path);
121
122
123 $cs = Yii::app()->getClientScript();
124
125 $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers.css');
126 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-datepicker.en_US.js');
127 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-datepicker.js');
128
129
130
131
132
133 }
134 }