1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Grido (https://github.com/o5/grido) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz) |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the file LICENSE.md that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Grido\Components\Filters; |
13
|
|
|
|
14
|
|
|
use Nette\Utils\Strings; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Date-range input filter. |
18
|
|
|
* |
19
|
|
|
* @package Grido |
20
|
|
|
* @subpackage Components\Filters |
21
|
|
|
* @author Petr Bugyík |
22
|
|
|
* |
23
|
|
|
* @property string $mask |
24
|
|
|
*/ |
25
|
|
|
class DateRange extends Date |
26
|
1 |
|
{ |
27
|
|
|
/** @var string */ |
28
|
|
|
protected $condition = 'BETWEEN ? AND ?'; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
protected $mask = '/(.*)\s?-\s?(.*)/'; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
protected $dateFormatOutput = ['Y-m-d', 'Y-m-d G:i:s']; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $formatFrom |
38
|
|
|
* @param string $formatTo |
39
|
|
|
* @return \Grido\Components\Filters\DateRange |
40
|
|
|
*/ |
41
|
|
|
public function setDateFormatOutput($formatFrom, $formatTo = NULL) |
42
|
|
|
{ |
43
|
|
|
$formatTo = $formatTo === NULL |
44
|
1 |
|
? $formatFrom |
45
|
1 |
|
: $formatTo; |
46
|
|
|
|
47
|
1 |
|
$this->dateFormatOutput = [$formatFrom, $formatTo]; |
48
|
1 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets mask by regular expression. |
53
|
|
|
* @param string $mask |
54
|
|
|
* @return DateRange |
55
|
|
|
*/ |
56
|
|
|
public function setMask($mask) |
57
|
|
|
{ |
58
|
1 |
|
$this->mask = $mask; |
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getMask() |
66
|
|
|
{ |
67
|
1 |
|
return $this->mask; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return \Nette\Forms\Controls\TextInput |
72
|
|
|
*/ |
73
|
|
|
protected function getFormControl() |
74
|
|
|
{ |
75
|
1 |
|
$control = parent::getFormControl(); |
76
|
|
|
|
77
|
1 |
|
$prototype = $control->getControlPrototype(); |
78
|
1 |
|
array_pop($prototype->class); //remove "date" class |
79
|
1 |
|
$prototype->class[] = 'daterange'; |
80
|
|
|
|
81
|
1 |
|
return $control; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $value |
86
|
|
|
* @return Condition|bool |
87
|
|
|
* @throws \Exception |
88
|
|
|
* @internal |
89
|
|
|
*/ |
90
|
|
|
public function __getCondition($value) |
91
|
|
|
{ |
92
|
1 |
|
if ($this->where === NULL && is_string($this->condition)) { |
93
|
|
|
|
94
|
1 |
|
list (, $from, $to) = \Nette\Utils\Strings::match($value, $this->mask); |
95
|
1 |
|
$from = \DateTime::createFromFormat($this->dateFormatInput, trim($from)); |
96
|
1 |
|
$to = \DateTime::createFromFormat($this->dateFormatInput, trim($to)); |
97
|
|
|
|
98
|
1 |
|
if ($to && !Strings::match($this->dateFormatInput, '/G|H/i')) { //input format haven't got hour option |
99
|
1 |
|
Strings::contains($this->dateFormatOutput[1], 'G') || Strings::contains($this->dateFormatOutput[1], 'H') |
100
|
1 |
|
? $to->setTime(23, 59, 59) |
101
|
1 |
|
: $to->setTime(11, 59, 59); |
102
|
1 |
|
} |
103
|
|
|
|
104
|
1 |
|
$values = $from && $to |
105
|
1 |
|
? [$from->format($this->dateFormatOutput[0]), $to->format($this->dateFormatOutput[1])] |
106
|
1 |
|
: NULL; |
107
|
|
|
|
108
|
|
|
return $values |
109
|
1 |
|
? Condition::setup($this->getColumn(), $this->condition, $values) |
110
|
1 |
|
: Condition::setupEmpty(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return parent::__getCondition($value); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|