forked from bupy7/yii2-date-range-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidget.php
137 lines (126 loc) · 3.9 KB
/
Widget.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
<?php
namespace bupy7\drp;
use Yii;
use yii\widgets\InputWidget;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\web\View;
use yii\helpers\Html;
use bupy7\drp\traits\WidgetTrait;
use bupy7\drp\assets\DateRangePickerAsset;
use bupy7\drp\assets\MomentAsset;
/**
* Wrapper of bootstrap date range picker for Yii2.
*
* Plugin home page: http://www.daterangepicker.com/
*
* @author Belosludcev Vasilij <[email protected]>
* @since 1.0.0
*/
class Widget extends InputWidget
{
use WidgetTrait;
/**
* The key that identifies the JS code block.
*/
const JS_KEY = 'bupy7/drp/';
/**
* @var array Options of plugin.
* @see http://www.daterangepicker.com/#options
*/
public $pluginOptions = [];
/**
* @var array Events of plugin where `key` is event name and `value` is handler function.
* @see http://www.daterangepicker.com/#events
*/
public $pluginEvents = [];
/**
* @var string Language of plugin. If `null` then `\yii\base\Application::language` will be used.
* @see MomentAsset::registerAssetFiles()
* @see WidgetTrait::convertLanguage()
*/
public $language;
/**
* @var boolean Whether set `true` property `$pluginOptions['locale']['format']` will be converting to correct
* format of Moment.js library from PHP DateTime format.
* @see WidgetTrait::convertDateFormat()
*/
public $convertDateFormat = false;
/**
* @inheritdoc
*/
public $options = ['class' => 'form-control'];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->language = $this->convertLanguage($this->language, Yii::$app->language);
if ($this->convertDateFormat && isset($this->pluginOptions['locale']['format'])) {
$this->pluginOptions['locale']['format'] = $this->convertDateFormat($this->pluginOptions['locale']['format']);
}
}
/**
* @inheritdoc
*/
public function run()
{
$this->registerAssets();
$this->registerClientScripts();
$this->registerPluginEvents();
return $this->renderFieldInput();
}
/**
* Rendering field input of date range picker.
* @return string
*/
protected function renderFieldInput()
{
if ($this->hasModel()) {
return Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
return Html::textInput($this->name, $this->value, $this->options);
}
}
/**
* Registering of asset bundles for this plugin.
*/
protected function registerAssets()
{
$momentAsset = MomentAsset::register($this->view);
$momentAsset->language = $this->language;
DateRangePickerAsset::register($this->view);
}
/**
* Registering of client scripts for this plugin.
*/
protected function registerClientScripts()
{
if (isset($this->pluginOptions['ranges'])) {
$ranges = [];
foreach ($this->pluginOptions['ranges'] as $label => $value) {
$ranges[$label] = new JsExpression($value);
}
$this->pluginOptions['ranges'] = $ranges;
}
$pluginOptions = Json::encode($this->pluginOptions);
$this->view->registerJs("$('#{$this->options['id']}').daterangepicker({$pluginOptions});");
}
/**
* Registering of plugin events.
*/
protected function registerPluginEvents()
{
foreach ($this->pluginEvents as $event => $handler) {
if (!empty($event)) {
$handler = new JsExpression($handler);
$this->view->registerJs(
"$('#{$this->options['id']}').on('{$event}', {$handler});",
View::POS_READY,
self::JS_KEY . $this->options['id'] . '/events/' . $event
);
}
}
}
}