|
| 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 | +} |
0 commit comments