-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateTimeRangePicker.php
189 lines (161 loc) · 5.4 KB
/
DateTimeRangePicker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace maddoger\widgets;
use Yii;
use yii\helpers\FormatConverter;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* DateTimeEditor Widget For Yii2 class file.
*
* @property array $plugins
*
* @author Vitaliy Syrchikov <[email protected]>
*/
class DateTimeRangePicker extends InputWidget
{
/**
* @var array plugin options
*/
public $clientOptions = [];
/**
* @var array text field options
*/
public $options = [];
/**
* @var string format
*/
public $jsFormat;
/**
* @var string format
*/
public $phpFormat;
/**
* @var bool Reformat value from model to phpFormat
*/
public $reformatValue = false;
/**
* @var array
*/
public $clientOptions2;
/**
* @var array
*/
public $options2;
/**
* @var string name of textarea tag or name of attribute
*/
public $attribute2;
/**
* @var string value for text area (without model)
*/
public $value2;
/**
* @var string
*/
public $delimiter = '<span class="input-group-addon">-</span>';
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
parent::init();
if (!isset($this->options['class'])) {
$this->options['class'] = 'form-control';
}
if (!isset($this->options2['class'])) {
$this->options2['class'] = 'form-control';
}
}
/**
* Renders the widget.
*/
public function run()
{
if (!$this->phpFormat && $this->hasModel()) {
$attribute = $this->model->{$this->attribute};
if ($attribute instanceof DateTimeAttribute) {
if (isset($attribute->localFormat[1])) {
$this->phpFormat = $attribute->localFormat[1];
}
}
}
if (!$this->phpFormat) {
$this->phpFormat = Yii::$app->formatter->datetimeFormat;
}
$fieldName = $this->name ? $this->name : $this->attribute;
if (!is_null($this->model)) {
$fieldName = Html::getInputName($this->model, $this->attribute);
if (!array_key_exists('id', $this->options)) {
$this->options['id'] = Html::getInputId($this->model, $this->attribute);
}
if (!$this->value) {
$this->value = Html::getAttributeValue($this->model, $this->attribute);
if ($this->reformatValue && $this->value !== null) {
try
{
$this->value = Yii::$app->formatter->asDatetime($this->value, $this->phpFormat);
} catch (\Exception $e){}
}
}
}
$fieldName2 = $this->name ? $this->name : $this->attribute2;
if (!is_null($this->model)) {
$fieldName2 = Html::getInputName($this->model, $this->attribute2);
if (!array_key_exists('id', $this->options2)) {
$this->options2['id'] = Html::getInputId($this->model, $this->attribute2);
}
if (!$this->value2) {
$this->value2 = Html::getAttributeValue($this->model, $this->attribute2);
if ($this->reformatValue && $this->value2 !== null) {
try
{
$this->value2 = Yii::$app->formatter->asDatetime($this->value2, $this->phpFormat);
} catch (\Exception $e){}
}
}
}
$this->registerClientScript();
$res = '<div id="'.$this->getId().'" class="input-group">';
$res .= Html::textInput($fieldName, $this->value, $this->options);
$res .= $this->delimiter;
$res .= Html::textInput($fieldName2, $this->value2, $this->options2);
$res .= '</div>';
return $res;
}
/**
* Registers CKEditor JS
*/
protected function registerClientScript()
{
$view = $this->getView();
DateTimePickerAsset::register($view);
if (!$this->jsFormat) {
$this->jsFormat = DateTimePicker::convertPhpDateToMomentJs(FormatConverter::convertDateIcuToPhp($this->phpFormat));
}
/*
* locale fix
* @author <https://github.com/sim2github>
*/
if (!isset($this->clientOptions['locale'])) {
$applocale = strtolower(substr(Yii::$app->language , 0, 2)); //First 2 letters
$this->clientOptions['locale'] = $applocale;
}
if (!isset($this->clientOptions['format'])) {
$this->clientOptions['format'] = $this->jsFormat;
}
if (!isset($this->clientOptions2['locale'])) {
$applocale = strtolower(substr(Yii::$app->language , 0, 2)); //First 2 letters
$this->clientOptions2['locale'] = $applocale;
}
if (!isset($this->clientOptions2['format'])) {
$this->clientOptions2['format'] = $this->jsFormat;
}
$config = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
$config2 = empty($this->clientOptions2) ? '' : Json::encode($this->clientOptions2);
$js = "$('#" . $this->options['id'] . "').datetimepicker($config);";
$js .= "$('#" . $this->options2['id'] . "').datetimepicker($config2);";
$view->registerJs($js);
}
}