/
JuiDatetimePicker.php
128 lines (111 loc) · 4.54 KB
/
JuiDatetimePicker.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
<?php
namespace mackiavelly\juidatetimepicker;
use yii\helpers\Html,
yii\widgets\InputWidget,
yii\base\InvalidParamException,
yii\web\JsExpression,
yii\helpers\Json,
Yii;
class JuiDatetimePicker extends InputWidget
{
const SEPARATOR = ' ';
public $options = ['class' => 'form-control'];
public $dateFormat = null;
public $timeFormat = null;
public $altDateFormat = null;
public $altTimeFormat = null;
public $numberOfMonths = 1;
public $showButtonPanel = true;
public $clientOptions = [];
public $ignoreReadonly = false;
public $disableAlt = false;
public function init()
{
$formatter = Yii::$app->getFormatter();
if (is_null($this->dateFormat)) {
$this->dateFormat = $formatter->dateFormat;
if (is_null($this->dateFormat)) {
$this->dateFormat = 'medium';
}
}
if (is_null($this->timeFormat)) {
$this->timeFormat = $formatter->timeFormat;
if (is_null($this->timeFormat)) {
$this->timeFormat = 'medium';
}
}
if (is_null($this->altDateFormat)) {
$this->altDateFormat = $this->dateFormat;
}
if (is_null($this->altTimeFormat)) {
$this->altTimeFormat = $this->timeFormat;
}
parent::init();
}
public function run()
{
$inputId = $this->options['id'];
$altInputId = $inputId . '-alt';
$hasModel = $this->hasModel();
if (array_key_exists('value', $this->options)) {
$value = $this->options['value'];
} elseif ($hasModel) {
$value = Html::getAttributeValue($this->model, $this->attribute);
} else {
$value = $this->value;
}
$altOptions = [
'id' => $altInputId,
'name' => $altInputId,
];
if (!is_null($value) && ($value !== '')) {
$formatter = Yii::$app->getFormatter();
try {
$this->options['value'] = $formatter->asDate($value, $this->dateFormat) . self::SEPARATOR . $formatter->asTime($value, $this->timeFormat);
$altOptions['value'] = $formatter->asDate($value, $this->altDateFormat) . self::SEPARATOR . $formatter->asTime($value, $this->altTimeFormat);
} catch (InvalidParamException $e) {
// ignore exception and keep original value if it is not a valid date
}
}
if ($hasModel) {
$output = Html::activeTextInput($this->model, $this->attribute, $this->options)
.((!$this->disableAlt) ? Html::activeHiddenInput($this->model, $this->attribute, $altOptions) : null);
} else {
$output = Html::textInput($this->name, $this->value, $this->options)
.((!$this->disableAlt) ? Html::hiddenInput($this->name, $this->value, $altOptions) : null);
}
$this->clientOptions = array_merge([
'numberOfMonths' => $this->numberOfMonths,
'showButtonPanel' => $this->showButtonPanel
], $this->clientOptions, [
'dateFormat' => FormatConverter::convertDatePhpOrIcuToJui($this->dateFormat),
'separator' => self::SEPARATOR,
'timeFormat' => FormatConverter::convertTimePhpOrIcuToJui($this->timeFormat),
'altFormat' => FormatConverter::convertDatePhpOrIcuToJui($this->altDateFormat),
'altSeparator' => self::SEPARATOR,
'altTimeFormat' => FormatConverter::convertTimePhpOrIcuToJui($this->altTimeFormat),
'altField' => '#' . $altInputId,
'altFieldTimeOnly' => false
]);
if (!$this->ignoreReadonly && array_key_exists('readonly', $this->options) && $this->options['readonly']) {
$this->clientOptions['beforeShow'] = new JsExpression('function (input, inst) { return false; }');
}
if ($this->disableAlt) {
foreach ($this->clientOptions as $keyCO => $valueCO) {
if (strrpos($keyCO, 'alt') !== false) {
unset($this->clientOptions[$keyCO]);
}
}
}
$js = 'jQuery(\'#' . $inputId . '\').datetimepicker(' . Json::htmlEncode($this->clientOptions) . ');';
if (Yii::$app->getRequest()->getIsAjax()) {
$output .= Html::script($js);
} else {
$view = $this->getView();
JuiDatetimePickerAsset::register($view);
JuiDatetimePickerLanguageAsset::register($view);
$view->registerJs($js);
}
return $output;
}
}