1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Grido\Components\Filters;
13
14 use Nette\Utils\Strings;
15
16 17 18 19 20 21 22 23 24
25 class DateRange extends Date
26 {
27
28 protected $condition = 'BETWEEN ? AND ?';
29
30
31 protected $mask = '/(.*)\s?-\s?(.*)/';
32
33
34 protected $dateFormatOutput = array('Y-m-d', 'Y-m-d G:i:s');
35
36 37 38 39 40
41 public function setDateFormatOutput($formatFrom, $formatTo = NULL)
42 {
43 $formatTo = $formatTo === NULL
44 ? $formatFrom
45 : $formatTo;
46
47 $this->dateFormatOutput = array($formatFrom, $formatTo);
48 return $this;
49 }
50
51 52 53 54 55
56 public function setMask($mask)
57 {
58 $this->mask = $mask;
59 return $this;
60 }
61
62 63 64
65 public function getMask()
66 {
67 return $this->mask;
68 }
69
70 71 72
73 protected function getFormControl()
74 {
75 $control = parent::getFormControl();
76
77 $prototype = $control->getControlPrototype();
78 array_pop($prototype->class);
79 $prototype->class[] = 'daterange';
80
81 return $control;
82 }
83
84 85 86 87 88 89
90 public function __getCondition($value)
91 {
92 if ($this->where === NULL && is_string($this->condition)) {
93
94 list (, $from, $to) = \Nette\Utils\Strings::match($value, $this->mask);
95 $from = \DateTime::createFromFormat($this->dateFormatInput, trim($from));
96 $to = \DateTime::createFromFormat($this->dateFormatInput, trim($to));
97
98 if ($to && !Strings::match($this->dateFormatInput, '/G|H/i')) {
99 Strings::contains($this->dateFormatOutput[1], 'G') || Strings::contains($this->dateFormatOutput[1], 'H')
100 ? $to->setTime(23, 59, 59)
101 : $to->setTime(11, 59, 59);
102 }
103
104 $values = $from && $to
105 ? array($from->format($this->dateFormatOutput[0]), $to->format($this->dateFormatOutput[1]))
106 : NULL;
107
108 return $values
109 ? Condition::setup($this->getColumn(), $this->condition, $values)
110 : Condition::setupEmpty();
111 }
112
113 return parent::__getCondition($value);
114 }
115 }
116