This repository was archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDatePicker.php
88 lines (75 loc) · 2.71 KB
/
DatePicker.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
<?php
namespace mrserg161\airdatepicker;
use Yii;
use yii\base\InvalidParamException;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* Class AirDatePicker
* Manual http://t1m0n.name/air-datepicker/docs/index-ru.html
* GitHub https://github.com/t1m0n/air-datepicker
* @package mrserg161\airdatepicker
* @author Malovichko Sergey <[email protected]>
*/
class DatePicker extends InputWidget
{
public $template = '{input}';
public $options = [
'class' => 'form-control',
];
public $clientOptions = [];
public $clientEvents = [];
public $dateFormat;
public function init()
{
parent::init();
if (!isset($this->options['id'])) {
$this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
}
if ($this->dateFormat === null) {
$this->dateFormat = Yii::$app->formatter->datetimeFormat;
}
}
public function run()
{
$asset = DatePickerAsset::register($this->view);
if (isset($this->clientOptions['language'])) {
$lang = $this->clientOptions['language'];
$this->view->registerJsFile($asset->baseUrl . "/js/i18n/datepicker.$lang.js", [
'depends' => DatePickerAsset::class,
]);
}
// get formatted date value
if ($this->hasModel()) {
$value = Html::getAttributeValue($this->model, $this->attribute);
} else {
$value = $this->value;
}
if ($value !== null && $value !== '') {
try {
if (is_int($value)) {
$value = Yii::$app->formatter->asDatetime($value, $this->dateFormat);
}
} catch (InvalidParamException $e) {
// ignore exception and keep original value if it is not a valid date
}
}
$id = $this->options['id'];
$clientOptions = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
$js = "jQuery('#$id').datepicker($clientOptions)";
if ($value) {
$this->clientEvents = array_merge($this->clientEvents, ['selectDate' => 'new Date(' . strtotime($value) * 1000 . ')']);
}
foreach ($this->clientEvents as $event => $handler) {
$js .= ".data('datepicker').$event($handler)";
}
$this->view->registerJs($js . ';');
$this->options['value'] = $value;
return strtr($this->template, [
'{input}' => $this->hasModel()
? Html::activeTextInput($this->model, $this->attribute, $this->options)
: Html::textInput($this->name, $value, $this->options),
]);
}
}