Skip to content

Commit c4b417b

Browse files
committed
Add date filter
1 parent 1c97c33 commit c4b417b

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace wcf\system\gridView\filter;
4+
5+
use wcf\data\DatabaseObjectList;
6+
use wcf\system\form\builder\field\AbstractFormField;
7+
use wcf\system\form\builder\field\DateRangeFormField;
8+
use wcf\system\WCF;
9+
10+
/**
11+
* Filter for columns that contain unix timestamps.
12+
*
13+
* @author Marcel Werk
14+
* @copyright 2001-2024 WoltLab GmbH
15+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16+
* @since 6.2
17+
*/
18+
class DateFilter extends AbstractFilter
19+
{
20+
#[\Override]
21+
public function getFormField(string $id, string $label): AbstractFormField
22+
{
23+
return DateRangeFormField::create($id)
24+
->label($label)
25+
->nullable();
26+
}
27+
28+
#[\Override]
29+
public function applyFilter(DatabaseObjectList $list, string $id, string $value): void
30+
{
31+
$columnName = $this->getDatabaseColumnName($list, $id);
32+
$timestamps = $this->getTimestamps($value);
33+
34+
if (!$timestamps['from'] && !$timestamps['to']) {
35+
return;
36+
}
37+
38+
if (!$timestamps['to']) {
39+
$list->getConditionBuilder()->add("{$columnName} >= ?", [$timestamps['from']]);
40+
} else {
41+
$list->getConditionBuilder()->add("{$columnName} BETWEEN ? AND ?", [$timestamps['from'], $timestamps['to']]);
42+
}
43+
}
44+
45+
#[\Override]
46+
public function renderValue(string $value): string
47+
{
48+
$values = explode(';', $value);
49+
if (\count($values) !== 2) {
50+
return '';
51+
}
52+
53+
$locale = WCF::getLanguage()->getLocale();
54+
$fromString = $toString = '';
55+
if ($values[0] !== '') {
56+
$fromDateTime = \DateTime::createFromFormat(
57+
'Y-m-d',
58+
$values[0],
59+
WCF::getUser()->getTimeZone()
60+
);
61+
if ($fromDateTime !== false) {
62+
$fromString = \IntlDateFormatter::formatObject(
63+
$fromDateTime,
64+
[
65+
\IntlDateFormatter::LONG,
66+
\IntlDateFormatter::NONE,
67+
],
68+
$locale
69+
);
70+
}
71+
}
72+
if ($values[1] !== '') {
73+
$toDateTime = \DateTime::createFromFormat(
74+
'Y-m-d',
75+
$values[1],
76+
WCF::getUser()->getTimeZone()
77+
);
78+
if ($toDateTime !== false) {
79+
$toString = \IntlDateFormatter::formatObject(
80+
$toDateTime,
81+
[
82+
\IntlDateFormatter::LONG,
83+
\IntlDateFormatter::NONE,
84+
],
85+
$locale
86+
);
87+
}
88+
}
89+
90+
if ($fromString && $toString) {
91+
return $fromString . '' . $toString;
92+
} else if ($fromString) {
93+
return '>= ' . $fromString;
94+
} else if ($toString) {
95+
return '<= ' . $toString;
96+
}
97+
98+
return '';
99+
}
100+
101+
/**
102+
* @return array{from: int, to: int}
103+
*/
104+
private function getTimestamps(string $value): array
105+
{
106+
$from = 0;
107+
$to = 0;
108+
109+
$values = explode(';', $value);
110+
if (\count($values) === 2) {
111+
$fromDateTime = \DateTime::createFromFormat(
112+
'Y-m-d',
113+
$values[0]
114+
);
115+
if ($fromDateTime !== false) {
116+
$fromDateTime->setTime(0, 0);
117+
$from = $fromDateTime->getTimestamp();
118+
}
119+
120+
$toDateTime = \DateTime::createFromFormat(
121+
'Y-m-d',
122+
$values[1]
123+
);
124+
if ($toDateTime !== false) {
125+
$toDateTime->setTime(23, 59, 59);
126+
$to = $toDateTime->getTimestamp();
127+
}
128+
}
129+
130+
return [
131+
'from' => $from,
132+
'to' => $to,
133+
];
134+
}
135+
}

wcfsetup/install/files/lib/system/gridView/filter/TimeFilter.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/**
1111
* Filter for columns that contain unix timestamps.
12+
* In contrast to `DateFilter`, this filter also allows filtering by a specific time.
1213
*
1314
* @author Marcel Werk
1415
* @copyright 2001-2024 WoltLab GmbH
@@ -51,7 +52,7 @@ public function renderValue(string $value): string
5152
return '';
5253
}
5354

54-
$locale = WCF::getLanguage()->getLocale();;
55+
$locale = WCF::getLanguage()->getLocale();
5556
$fromString = $toString = '';
5657
if ($values[0] !== '') {
5758
$fromDateTime = \DateTime::createFromFormat(

0 commit comments

Comments
 (0)