forked from zhuravljov/yii2-datetime-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateRangePicker.php
109 lines (104 loc) · 3.34 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
<?php
namespace mitalcoi\widgets;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
use yii\web\JsExpression;
/**
* Class DateRangePicker
*
*/
class DateRangePicker extends InputWidget
{
public $ranges = [];
public $format='DD.MM.YYYY';
public $separator=' - ';
public $locale = [
'applyLabel' => 'Apply',
'cancelLabel' => 'Reset',
'fromLabel' => 'From',
'toLabel' => 'To',
'weekLabel' => 'W',
'customRangeLabel' => 'Custom Range',
'daysOfWeek' => ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
'monthNames' => [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
];
/**
* @var array the HTML attributes for the input tag.
*/
public $options = [
'class' => 'form-control'
];
/**
* @var array options for daterangepicker
*/
public $clientOptions = [];
public function init()
{
parent::init();
if (!isset($this->options['id'])) {
$this->options['id'] = $this->hasModel() ? Html::getInputId($this->model,
$this->attribute) : $this->getId();
}
$this->clientOptions['locale']=$this->locale;
$this->clientOptions['format']=$this->format;
$this->clientOptions['separator']=$this->separator;
$this->clientOptions['firstDay']=0;
if (!$this->ranges) {
$this->clientOptions['ranges'] = [
'Today' => [
new JsExpression('moment()'),
new JsExpression('moment()'),
],
'Yesterday' => [
new JsExpression('moment().subtract("days", 1)'),
new JsExpression('moment().subtract("days", 1)'),
],
'Last 7 days' => [
new JsExpression('moment().subtract("days", 7)'),
new JsExpression('moment().subtract("days", 1)'),
],
'Last 30 days' => [
new JsExpression('moment().subtract("days", 30)'),
new JsExpression('moment().subtract("days", 1)'),
],
'Last 365 days' => [
new JsExpression('moment().subtract("days", 360)'),
new JsExpression('moment().subtract("days", 1)'),
]
];
}else{
$this->clientOptions['ranges']=$this->ranges;
}
if (!isset($this->clientOptions['startDate'])) {
$this->clientOptions['startDate'] = (new \DateTime())->modify('-2 weeks')->format("d.m.Y");
}
}
public function run()
{
if ($this->hasModel()) {
echo Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
echo Html::textInput($this->name, $this->value, $this->options);
}
/** @var \yii\web\View $view */
$view = $this->getView();
DateRangePickerAsset::register($view);
$id = $this->options['id'];
$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
$view->registerJs("jQuery('#$id').daterangepicker($options);");
}
}