-
Notifications
You must be signed in to change notification settings - Fork 6
/
DateRangePicker.php
218 lines (193 loc) · 6.46 KB
/
DateRangePicker.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace webvimark\extensions\DateRangePicker;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use Yii;
/**
* DateRangePicker
*
* Wrapper for https://github.com/dangrossman/bootstrap-daterangepicker
*
* @author vi mark <[email protected]>
* @license MIT
*/
class DateRangePicker extends Widget
{
/**
* @var string
*/
public $selector;
/**
* For example: can be "PostSearch" as string or $searchModel for PostSearch class
*
* @var mixed
*/
public $model;
/**
* @var string
*/
public $attribute;
/**
* Event listener attached to the body to be sure that plugin works even after AJAX refresh
* To improve performance you can set some container ID that will be not changed during AJAX refresh
*
* @var string
*/
public $domContainer = 'body';
/**
* Datepicker params
*
* @var array
*/
public $pluginOptions = [];
public $applyCallback = '';
protected $_selector;
protected $_params = [
'opens' => 'left',
'format' => 'YYYY-MM-DD H:mm',
'showDropdowns' => true,
'timePicker' => true,
'timePicker12Hour' => false,
'timePickerIncrement' => 5,
];
/**
* @param string $category
* @param string $message
* @param array $params
* @param null $language
*
* @return string
*/
public static function t($category, $message, $params = [], $language = null)
{
if ( !isset(Yii::$app->i18n->translations['widgets/DateRangePicker/*']) )
{
Yii::$app->i18n->translations['widgets/DateRangePicker/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => __DIR__ . '/messages',
'fileMap' => [
'widgets/DateRangePicker/app' => 'app.php',
],
];
}
return Yii::t('widgets/DateRangePicker/' . $category, $message, $params, $language);
}
/**
* init
*/
public function run()
{
// If $searchModel has been passed instead of string
if ( is_object($this->model) )
{
$this->model = (new \ReflectionClass($this->model))->getShortName();
}
if ( $this->selector )
{
$this->_selector = $this->selector;
}
elseif ( $this->model AND $this->attribute )
{
$this->_selector = 'input[name="' . $this->model .'[' . $this->attribute . ']"]';
}
if ( ! $this->_selector )
throw new InvalidConfigException('Define selector or model + attributes');
DateRangePickerAsset::register($this->view);
// If applyCallback not set, then we try to update given grid
if ( ! $this->applyCallback AND $this->model AND $this->attribute AND $this->selector )
{
$this->applyCallback = "$('input[name=\"{$this->model}[{$this->attribute}]\"]').val(picker.startDate.format('{$this->_params['format']}') + ' - ' + picker.endDate.format('{$this->_params['format']}'));";
$this->applyCallback .= "$('input[name=\"{$this->model}[{$this->attribute}]\"]').trigger('change');";
}
$this->view->registerJs(<<<JS
var container = $('$this->domContainer');
container.off('focus', '$this->_selector').on('focus', '$this->_selector', function(){
$(this).daterangepicker({$this->_mergeParams()});
});
container.off('apply.daterangepicker', '$this->_selector').on('apply.daterangepicker', '$this->_selector', function(ev, picker) {
$('$this->_selector').trigger('change');
$this->applyCallback;
});
JS
);
}
/**
* _mergeParams
*
* @return string json array
*/
protected function _mergeParams()
{
$this->_params['locale'] = [
'firstDay' => 1,
'fromLabel' => DateRangePicker::t("app", "FROM"),
'toLabel' => DateRangePicker::t("app", "TO"),
'applyLabel' => DateRangePicker::t("app", "Apply"),
'cancelLabel' => DateRangePicker::t("app", "Cancel"),
'customRangeLabel' => DateRangePicker::t("app", "Custom range"),
'daysOfWeek' => [
DateRangePicker::t("app", 'Su'),
DateRangePicker::t("app", 'Mo'),
DateRangePicker::t("app", 'Tu'),
DateRangePicker::t("app", 'We'),
DateRangePicker::t("app", 'Th'),
DateRangePicker::t("app", 'Fr'),
DateRangePicker::t("app", 'Sa'),
],
'monthNames' => [
DateRangePicker::t("app", 'Jan'),
DateRangePicker::t("app", 'Feb'),
DateRangePicker::t("app", 'Mar'),
DateRangePicker::t("app", 'Apr'),
DateRangePicker::t("app", 'May'),
DateRangePicker::t("app", 'Jun'),
DateRangePicker::t("app", 'Jul'),
DateRangePicker::t("app", 'Aug'),
DateRangePicker::t("app", 'Sep'),
DateRangePicker::t("app", 'Oct'),
DateRangePicker::t("app", 'Nov'),
DateRangePicker::t("app", 'Dec'),
],
];
if ( !isset($this->pluginOptions['ranges']) )
{
$this->_params['ranges'] = [
DateRangePicker::t("app","Yesterday") => array(
date('Y-m-d', strtotime('-1 day')),
date('Y-m-d', time())
),
DateRangePicker::t("app","Today") => array(
date('Y-m-d', time()),
date('Y-m-d', strtotime('tomorrow'))
),
DateRangePicker::t("app","7 days") => array(
date('Y-m-d', strtotime('-7 days 0:00')),
date('Y-m-d', strtotime('tomorrow'))
),
DateRangePicker::t("app","Previous week") => array(
date('Y-m-d', strtotime('Monday previous week 0:00')),
date('Y-m-d', strtotime('Monday this week 0:00'))
),
DateRangePicker::t("app","Current week") => array(
date('Y-m-d', strtotime('Monday this week 0:00')),
date('Y-m-d', strtotime('tomorrow'))
),
DateRangePicker::t("app","30 days") => array(
date('Y-m-d', strtotime('-1 month')),
date('Y-m-d', strtotime('tomorrow'))
),
DateRangePicker::t("app","Previous month") => array(
date('Y-m-d', strtotime('first day of previous month')),
date('Y-m-d', strtotime('first day of this month'))
),
DateRangePicker::t("app","Current month") => array(
date('Y-m-d', strtotime('first day of this month')),
date('Y-m-d', strtotime('first day of next month'))
),
];
}
return json_encode(ArrayHelper::merge($this->_params, $this->pluginOptions));
}
}