1 <?php
2 3 4 5 6 7 8 9 10 11 12
13 Yii::import('bootstrap.helpers.TbArray');
14
15 class WhDateTimePicker extends CInputWidget
16 {
17
18 19 20 21
22 public $selector;
23
24 25 26
27 public $format = 'dd/MM/yyyy hh:mm:ss';
28
29 30 31
32 public $iconTime = 'icon-time';
33
34 35 36
37 public $iconDate = 'icon-calendar';
38
39 40 41 42 43 44 45 46 47 48 49
50 public $pluginOptions = array();
51
52 53 54
55 public $events = array();
56
57 58 59
60 public function init()
61 {
62 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
63 $this->htmlOptions['id'] = TbArray::getValue('id', $this->htmlOptions, $this->getId());
64 $this->htmlOptions['data-format'] = $this->format;
65 }
66
67 68 69
70 public function run()
71 {
72 $this->renderField();
73 $this->registerClientScript();
74 }
75
76 77 78
79 public function renderField()
80 {
81 if (null === $this->selector) {
82 $options = array();
83
84 list($name, $id) = $this->resolveNameID();
85
86 $options['id'] = $id . '_datetimepicker';
87 TbHtml::addCssClass('input-append', $options);
88
89 echo TbHtml::openTag('div', $options);
90 if ($this->hasModel()) {
91 echo TbHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
92 } else {
93 echo TbHtml::textField($name, $this->value, $this->htmlOptions);
94 }
95 echo TbHtml::openTag('span', array('class' => 'add-on'));
96 echo '<i data-time-icon="' . $this->iconTime . '" data-date-icon="' . $this->iconDate . '"></i>';
97 echo TbHtml::closeTag('span');
98 echo TbHtml::closeTag('div');
99 }
100 }
101
102
103 104 105 106
107 public function registerClientScript()
108 {
109
110 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
111 $assetsUrl = $this->getAssetsUrl($path);
112
113
114 $cs = Yii::app()->getClientScript();
115
116 $cs->registerCssFile($assetsUrl . '/css/bootstrap-datetimepicker.min.css');
117 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-datetimepicker.min.js', CClientScript::POS_END);
118 if (isset($this->pluginOptions['language'])) {
119 $cs->registerScriptFile(
120 $assetsUrl . '/js/locales/bootstrap-datetimepicker.' . $this->pluginOptions['language'] . '.js'
121 , CClientScript::POS_END);
122 }
123
124
125 $selector = null === $this->selector
126 ? '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId()) . '_datetimepicker'
127 : $this->selector;
128
129 $this->getApi()->registerPlugin('datetimepicker', $selector, $this->pluginOptions);
130
131 if($this->events)
132 {
133
134 $this->getApi()->registerEvents($selector, $this->events);
135 }
136 }
137 }
138