Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions wcfsetup/install/files/lib/system/gridView/filter/DateFilter.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace wcf\system\gridView\filter;

use wcf\data\DatabaseObjectList;
use wcf\system\form\builder\field\AbstractFormField;
use wcf\system\form\builder\field\DateRangeFormField;
use wcf\system\WCF;

/**
* Filter for columns that contain unix timestamps.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*/
class DateFilter extends AbstractFilter
{
#[\Override]
public function getFormField(string $id, string $label): AbstractFormField
{
return DateRangeFormField::create($id)
->label($label)
->nullable();
}

#[\Override]
public function applyFilter(DatabaseObjectList $list, string $id, string $value): void
{
$columnName = $this->getDatabaseColumnName($list, $id);
$timestamps = $this->getTimestamps($value);

if (!$timestamps['from'] && !$timestamps['to']) {
return;
}

if (!$timestamps['to']) {
$list->getConditionBuilder()->add("{$columnName} >= ?", [$timestamps['from']]);
} else {
$list->getConditionBuilder()->add("{$columnName} BETWEEN ? AND ?", [$timestamps['from'], $timestamps['to']]);
}
}

#[\Override]
public function renderValue(string $value): string
{
$values = explode(';', $value);
if (\count($values) !== 2) {
return '';
}

$locale = WCF::getLanguage()->getLocale();
$fromString = $toString = '';
if ($values[0] !== '') {
$fromDateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d',
$values[0],
WCF::getUser()->getTimeZone()
);
if ($fromDateTime !== false) {
$fromString = \IntlDateFormatter::formatObject(
$fromDateTime,
[
\IntlDateFormatter::LONG,
\IntlDateFormatter::NONE,
],
$locale
);
}
}
if ($values[1] !== '') {
$toDateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d',
$values[1],
WCF::getUser()->getTimeZone()
);
if ($toDateTime !== false) {
$toString = \IntlDateFormatter::formatObject(
$toDateTime,
[
\IntlDateFormatter::LONG,
\IntlDateFormatter::NONE,
],
$locale
);
}
}

if ($fromString && $toString) {
return $fromString . ' ‐ ' . $toString;
} else if ($fromString) {
return '>= ' . $fromString;
} else if ($toString) {
return '<= ' . $toString;
}

return '';
}

/**
* @return array{from: int, to: int}
*/
private function getTimestamps(string $value): array
{
$from = 0;
$to = 0;

$values = explode(';', $value);
if (\count($values) === 2) {
$fromDateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d',
$values[0]
);
if ($fromDateTime !== false) {
$fromDateTime = $fromDateTime->setTime(0, 0);
$from = $fromDateTime->getTimestamp();
}

$toDateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d',
$values[1]
);
if ($toDateTime !== false) {
$toDateTime = $toDateTime->setTime(23, 59, 59);
$to = $toDateTime->getTimestamp();
}
}

return [
'from' => $from,
'to' => $to,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SelectFilter extends AbstractFilter
public function __construct(
private readonly array $options,
string $databaseColumn = '',
private readonly bool $labelLanguageItems = true
protected readonly bool $labelLanguageItems = true
) {
parent::__construct($databaseColumn);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* Filter for columns that contain unix timestamps.
* In contrast to `DateFilter`, this filter also allows filtering by a specific time.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
Expand Down Expand Up @@ -51,10 +52,10 @@ public function renderValue(string $value): string
return '';
}

$locale = WCF::getLanguage()->getLocale();;
$locale = WCF::getLanguage()->getLocale();
$fromString = $toString = '';
if ($values[0] !== '') {
$fromDateTime = \DateTime::createFromFormat(
$fromDateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d\TH:i:sP',
$values[0],
WCF::getUser()->getTimeZone()
Expand All @@ -71,7 +72,7 @@ public function renderValue(string $value): string
}
}
if ($values[1] !== '') {
$toDateTime = \DateTime::createFromFormat(
$toDateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d\TH:i:sP',
$values[1],
WCF::getUser()->getTimeZone()
Expand Down Expand Up @@ -121,7 +122,7 @@ private function getTimestamps(string $value): array

private function getTimestamp(string $date): int
{
$dateTime = \DateTime::createFromFormat(
$dateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d\TH:i:sP',
$date
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace wcf\system\listView\filter;

use wcf\data\DatabaseObjectList;
use wcf\system\category\CategoryHandler;
use wcf\system\form\builder\field\AbstractFormField;
use wcf\system\form\builder\field\SelectFormField;

/**
* Allows a column to be filtered on the basis of a select category.
*
* @author Olaf Braun
* @copyright 2001-2025 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*/
class CategoryFilter extends AbstractFilter
{
/**
* @param \Traversable<mixed> $options
*/
public function __construct(
private readonly \Traversable $options,
string $id,
string $languageItem = 'wcf.global.category',
string $databaseColumn = ''

) {
parent::__construct($id, $languageItem, $databaseColumn);
}

#[\Override]
public function getFormField(): AbstractFormField
{
return SelectFormField::create($this->id)
->label($this->languageItem)
->options($this->options, true);
}

#[\Override]
public function applyFilter(DatabaseObjectList $list, string $value): void
{
$columnName = $this->getDatabaseColumnName($list);

$list->getConditionBuilder()->add("{$columnName} = ?", [$value]);
}

#[\Override]
public function renderValue(string $value): string
{
return CategoryHandler::getInstance()->getCategory((int)$value)->getTitle();
}
}
135 changes: 135 additions & 0 deletions wcfsetup/install/files/lib/system/listView/filter/DateFilter.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace wcf\system\listView\filter;

use wcf\data\DatabaseObjectList;
use wcf\system\form\builder\field\AbstractFormField;
use wcf\system\form\builder\field\DateRangeFormField;
use wcf\system\WCF;

/**
* Filter for columns that contain unix timestamps.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*/
class DateFilter extends AbstractFilter
{
#[\Override]
public function getFormField(): AbstractFormField
{
return DateRangeFormField::create($this->id)
->label($this->languageItem)
->nullable();
}

#[\Override]
public function applyFilter(DatabaseObjectList $list, string $value): void
{
$columnName = $this->getDatabaseColumnName($list);
$timestamps = $this->getTimestamps($value);

if (!$timestamps['from'] && !$timestamps['to']) {
return;
}

if (!$timestamps['to']) {
$list->getConditionBuilder()->add("{$columnName} >= ?", [$timestamps['from']]);
} else {
$list->getConditionBuilder()->add("{$columnName} BETWEEN ? AND ?", [$timestamps['from'], $timestamps['to']]);
}
}

#[\Override]
public function renderValue(string $value): string
{
$values = explode(';', $value);
if (\count($values) !== 2) {
return '';
}

$locale = WCF::getLanguage()->getLocale();
$fromString = $toString = '';
if ($values[0] !== '') {
$fromDateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d',
$values[0],
WCF::getUser()->getTimeZone()
);
if ($fromDateTime !== false) {
$fromString = \IntlDateFormatter::formatObject(
$fromDateTime,
[
\IntlDateFormatter::LONG,
\IntlDateFormatter::NONE,
],
$locale
);
}
}
if ($values[1] !== '') {
$toDateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d',
$values[1],
WCF::getUser()->getTimeZone()
);
if ($toDateTime !== false) {
$toString = \IntlDateFormatter::formatObject(
$toDateTime,
[
\IntlDateFormatter::LONG,
\IntlDateFormatter::NONE,
],
$locale
);
}
}

if ($fromString && $toString) {
return $fromString . ' ‐ ' . $toString;
} else if ($fromString) {
return '>= ' . $fromString;
} else if ($toString) {
return '<= ' . $toString;
}

return '';
}

/**
* @return array{from: int, to: int}
*/
private function getTimestamps(string $value): array
{
$from = 0;
$to = 0;

$values = explode(';', $value);
if (\count($values) === 2) {
$fromDateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d',
$values[0]
);
if ($fromDateTime !== false) {
$fromDateTime = $fromDateTime->setTime(0, 0);
$from = $fromDateTime->getTimestamp();
}

$toDateTime = \DateTimeImmutable::createFromFormat(
'Y-m-d',
$values[1]
);
if ($toDateTime !== false) {
$toDateTime = $toDateTime->setTime(23, 59, 59);
$to = $toDateTime->getTimestamp();
}
}

return [
'from' => $from,
'to' => $to,
];
}
}
Loading