|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_table\form_nette\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
|
|
|
/** |
|
13
|
|
|
* Class DateRange |
|
14
|
|
|
* @package kalanis\kw_table\form_nette\Controls |
|
15
|
|
|
* Set ranges of dates |
|
16
|
|
|
*/ |
|
17
|
|
|
class DateRange extends TextInput |
|
18
|
|
|
{ |
|
19
|
|
|
protected string $inputFormat = 'd.m.Y H:i:s'; |
|
20
|
|
|
protected string $searchFormat = 'Y-m-d H:i:s'; |
|
21
|
|
|
|
|
22
|
|
|
protected ?Html $start; |
|
23
|
|
|
protected ?Html $end; |
|
24
|
|
|
protected ?DateTime $startValue; |
|
25
|
|
|
protected ?DateTime $endValue; |
|
26
|
|
|
|
|
27
|
|
|
protected $started = false; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(string $name, $label = null, $maxLength = null) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct($label, $maxLength); |
|
32
|
|
|
$this->setControlHtml($name); |
|
33
|
|
|
$this->started = true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function setControlHtml(string $name) |
|
37
|
|
|
{ |
|
38
|
|
|
$span = Html::el('span'); |
|
39
|
|
|
$divSince = Html::el('div', ['class' => 'input-group dateTimePickerRange']); |
|
40
|
|
|
$divTo = clone $divSince; |
|
41
|
|
|
|
|
42
|
|
|
$start = $this->start = Html::el('input', [ |
|
43
|
|
|
'type' => 'text', |
|
44
|
|
|
'name' => $name . '[]', |
|
45
|
|
|
'placeholder' => _('From'), |
|
46
|
|
|
'id' => $name . 'StartId', |
|
47
|
|
|
'class' => 'form-control cleanable listingDateTimePicker', |
|
48
|
|
|
'aria-label' => _('Time from') |
|
49
|
|
|
]); |
|
50
|
|
|
$end = $this->end = Html::el('input', [ |
|
51
|
|
|
'type' => 'text', |
|
52
|
|
|
'name' => $name . '[]', |
|
53
|
|
|
'placeholder' => _('To'), |
|
54
|
|
|
'id' => $name . 'StartId', |
|
55
|
|
|
'class' => 'form-control cleanable listingDateTimePicker', |
|
56
|
|
|
'aria-label' => _('Time to') |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
$divSince->add($start); |
|
60
|
|
|
$divTo->add($end); |
|
61
|
|
|
$span->add($divSince); |
|
62
|
|
|
$span->add($divTo); |
|
63
|
|
|
$this->control = $span; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function setInputFormat(string $format) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->inputFormat = $format; |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function setSearchFormat(string $format) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->searchFormat = $format; |
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getValue() |
|
79
|
|
|
{ |
|
80
|
|
|
if (empty($this->startValue)) { |
|
81
|
|
|
$startValue = new \DateTime(); |
|
82
|
|
|
$startValue->setDate(1, 1, 1); |
|
83
|
|
|
} else { |
|
84
|
|
|
$startValue = $this->startValue; |
|
85
|
|
|
}; |
|
86
|
|
|
if (empty($this->endValue)) { |
|
87
|
|
|
$endValue = new \DateTime(); |
|
88
|
|
|
$endValue->modify('+1000year'); |
|
89
|
|
|
} else { |
|
90
|
|
|
$endValue = $this->endValue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return [ |
|
94
|
|
|
0 => $startValue->format($this->searchFormat), |
|
95
|
|
|
1 => $endValue->format($this->searchFormat) |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function setValue($value) |
|
100
|
|
|
{ |
|
101
|
|
|
$startValue = $this->startValue; |
|
102
|
|
|
$endValue = $this->endValue; |
|
103
|
|
|
|
|
104
|
|
|
if (is_array($value)) { |
|
105
|
|
|
if (isset($value[0])) { |
|
106
|
|
|
$startValue = $value[0]; |
|
107
|
|
|
} |
|
108
|
|
|
if (isset($value[1])) { |
|
109
|
|
|
$endValue = $value[1]; |
|
110
|
|
|
} |
|
111
|
|
|
} else { |
|
112
|
|
|
$startValue = $value; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (!empty($startValue) && !($startValue instanceof \DateTime)) { |
|
116
|
|
|
$startValue = new \DateTime($startValue); |
|
117
|
|
|
} |
|
118
|
|
|
if (!empty($endValue) && !($endValue instanceof \DateTime)) { |
|
119
|
|
|
$endValue = new \DateTime($endValue); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$this->startValue = $startValue; |
|
123
|
|
|
$this->endValue = $endValue; |
|
124
|
|
|
if ($this->started) { |
|
125
|
|
|
$this->start->addAttributes(['value' => (!empty($startValue) ? $startValue->format($this->inputFormat) : null)]); |
|
|
|
|
|
|
126
|
|
|
$this->end->addAttributes(['value' => (!empty($endValue) ? $endValue->format($this->inputFormat) : null)]); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public static function register(?string $inputFormat = null, ?string $searchFormat = null) |
|
131
|
|
|
{ |
|
132
|
|
|
Container::extensionMethod('addTbDateRange', function ($container, $name, $label = null, $maxLength = null) use ($inputFormat, $searchFormat) { |
|
133
|
|
|
$picker = $container[$name] = new DateRange($name, $label, $maxLength); |
|
134
|
|
|
|
|
135
|
|
|
if (null !== $inputFormat) |
|
136
|
|
|
$picker->setInputFormat($inputFormat); |
|
137
|
|
|
if (null !== $searchFormat) |
|
138
|
|
|
$picker->setSearchFormat($searchFormat); |
|
139
|
|
|
|
|
140
|
|
|
return $picker; |
|
141
|
|
|
}); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|