|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_table\form_nette_lte\Controls; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Nette\Forms\Container; |
|
7
|
|
|
use Nette\Forms\Controls\TextInput; |
|
8
|
|
|
use Nette\Utils\Html; |
|
9
|
|
|
use Nette\Utils\DateTime; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class DateTimeRange extends TextInput |
|
13
|
|
|
{ |
|
14
|
|
|
protected string $uniqueId = ''; |
|
15
|
|
|
protected string $searchFormat = 'Y-m-d H:i:s'; |
|
16
|
|
|
|
|
17
|
|
|
protected ?Html $input; |
|
18
|
|
|
protected ?DateTime $startValue; |
|
19
|
|
|
protected ?DateTime $endValue; |
|
20
|
|
|
|
|
21
|
|
|
/** @var bool */ |
|
22
|
|
|
protected $started = false; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(string $name, $label = null, $maxLength = null, $searchFormat = null, \DateTime $startTime = null, \DateTime $endTime = null) |
|
25
|
|
|
{ |
|
26
|
|
|
parent::__construct($label, $maxLength); |
|
27
|
|
|
$this->uniqueId = (!$this->uniqueId) ? uniqid('adminLteDateTimeRange') : $this->uniqueId; |
|
28
|
|
|
if ($searchFormat) { |
|
29
|
|
|
$this->setSearchFormat($searchFormat); |
|
30
|
|
|
} |
|
31
|
|
|
$this->setControlHtml($name); |
|
32
|
|
|
$this->startValue = $startTime; |
|
33
|
|
|
$this->endValue = $endTime; |
|
34
|
|
|
$this->started = true; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function setControlHtml(string $name) |
|
38
|
|
|
{ |
|
39
|
|
|
$divGroup = Html::el('div', ['class' => "input-group dateTimeRangeDiv"]); |
|
40
|
|
|
$divGroupAddonClock = Html::el('div', ['class' => "input-group-addon", 'id' => $this->uniqueId . 'Focus']); |
|
41
|
|
|
$divGroupAddonTimes = Html::el('div', ['class' => "input-group-addon", 'id' => $this->uniqueId . 'Clear']); |
|
42
|
|
|
$iClock = Html::el('i', ['class' => "fa fa-clock-o"]); |
|
43
|
|
|
$iTimes = Html::el('i', ['class' => "fa fa-times"]); |
|
44
|
|
|
$input = $this->input = Html::el('input', [ |
|
45
|
|
|
'type' => 'text', |
|
46
|
|
|
'name' => $name, |
|
47
|
|
|
'id' => $this->uniqueId, |
|
48
|
|
|
'class' => 'form-control pull-right active', |
|
49
|
|
|
'aria-label' => _('Time from - to') |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
// http://www.daterangepicker.com/ |
|
53
|
|
|
$scriptText = 'document.addEventListener(\'DOMContentLoaded\', function() { |
|
54
|
|
|
moment.locale(\'cs\'); |
|
55
|
|
|
$("#' . $this->uniqueId . '").daterangepicker({ |
|
56
|
|
|
autoUpdateInput: false, |
|
57
|
|
|
timePicker: true, |
|
58
|
|
|
timePicker24Hour: true, |
|
59
|
|
|
timePickerIncrement: 15, |
|
60
|
|
|
locale: { |
|
61
|
|
|
"format": "DD.MM.YYYY H:mm", // TODO because processing until it will be defined internationalization |
|
62
|
|
|
customRangeLabel: "' . t('Own range') . '", |
|
|
|
|
|
|
63
|
|
|
applyLabel: "' . t('Use') . '", |
|
64
|
|
|
cancelLabel: "' . t('Cancel') . '", |
|
65
|
|
|
}, |
|
66
|
|
|
}, |
|
67
|
|
|
function (start, end) { |
|
68
|
|
|
$(\'#' . $this->uniqueId . '\') |
|
69
|
|
|
.val(start.format(\'DD.MM.YYYY H:mm\') + \' - \' + end.format(\'DD.MM.YYYY H:mm\')) |
|
70
|
|
|
.trigger(\'change\'); |
|
71
|
|
|
} |
|
72
|
|
|
); |
|
73
|
|
|
$(\'.dateTimeRangeDiv input\').change(function() { |
|
74
|
|
|
$(this).parents(\'form\').submit(); |
|
75
|
|
|
}); |
|
76
|
|
|
$(\'#' . $this->uniqueId . 'Focus' . '\').click(function(){ |
|
77
|
|
|
$(' . $this->uniqueId . ').focus(); |
|
78
|
|
|
}); |
|
79
|
|
|
$(\'#' . $this->uniqueId . 'Clear' . '\').click(function(){ |
|
80
|
|
|
$(' . $this->uniqueId . ').val(\'\'); |
|
81
|
|
|
$(this).parents(\'form\').submit(); |
|
82
|
|
|
}); |
|
83
|
|
|
}, false);'; |
|
84
|
|
|
$script = Html::el('script')->add($scriptText); |
|
85
|
|
|
|
|
86
|
|
|
$divGroupAddonClock->add($iClock); |
|
87
|
|
|
$divGroupAddonTimes->add($iTimes); |
|
88
|
|
|
$divGroup->add($divGroupAddonClock); |
|
89
|
|
|
$divGroup->add($divGroupAddonTimes); |
|
90
|
|
|
$divGroup->add($input); |
|
91
|
|
|
$divGroup->add($script); |
|
92
|
|
|
$this->control = $divGroup; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public static function register(?string $searchFormat = null, ?\DateTime $startTime = null, ?\DateTime $endTime = null) |
|
96
|
|
|
{ |
|
97
|
|
|
Container::extensionMethod('addAdminLteDateTimeRange', function ($container, $name, $label = null, $maxLength = null) use ($searchFormat, $startTime, $endTime) { |
|
98
|
|
|
$picker = $container[$name] = new DateTimeRange($name, $label, $maxLength, $searchFormat, $startTime, $endTime); |
|
99
|
|
|
return $picker; |
|
100
|
|
|
}); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function setSearchFormat(string $format) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->searchFormat = $format; |
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getValue() |
|
110
|
|
|
{ |
|
111
|
|
|
$startValue = $endValue = null; |
|
112
|
|
|
if (!empty($this->startValue)) { |
|
113
|
|
|
$startValue = $this->startValue->format($this->searchFormat); |
|
114
|
|
|
}; |
|
115
|
|
|
if (!empty($this->endValue)) { |
|
116
|
|
|
$endValue = $this->endValue->format($this->searchFormat); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return [ |
|
120
|
|
|
0 => $startValue, |
|
121
|
|
|
1 => $endValue |
|
122
|
|
|
]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function setValue($value) |
|
126
|
|
|
{ |
|
127
|
|
|
$exploded = explode('-', $value, 2); |
|
128
|
|
|
if (1 < count($exploded)) { |
|
129
|
|
|
$this->startValue = new \DateTime(trim($exploded[0])); |
|
130
|
|
|
$this->endValue = new \DateTime(trim($exploded[1])); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if ($this->started) { |
|
134
|
|
|
$this->input->addAttributes(['value' => $value]); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|