Add query callback to date demand

This allows 3rd party to alter the query of dates when demand is used.
E.g. allow more complex filters specific for some situations like CEs.

Relates: #9505
This commit is contained in:
Daniel Siepmann 2021-12-07 15:26:25 +01:00
parent 30f030aab6
commit ce3863619e
3 changed files with 43 additions and 1 deletions

View file

@ -80,6 +80,11 @@ class DateDemand
*/ */
protected $considerDate = false; protected $considerDate = false;
/**
* @var string
*/
protected $queryCallback = '';
public static function createFromRequestValues( public static function createFromRequestValues(
array $submittedValues, array $submittedValues,
array $settings array $settings
@ -110,6 +115,7 @@ class DateDemand
$instance->setSortBy($settings['sortByDate'] ?? ''); $instance->setSortBy($settings['sortByDate'] ?? '');
$instance->setSortOrder($settings['sortOrder'] ?? ''); $instance->setSortOrder($settings['sortOrder'] ?? '');
$instance->setQueryCallback($settings['queryCallback'] ?? '');
if (!empty($settings['limit'])) { if (!empty($settings['limit'])) {
$instance->setLimit($settings['limit']); $instance->setLimit($settings['limit']);
@ -275,4 +281,14 @@ class DateDemand
{ {
$this->considerDate = $considerDate; $this->considerDate = $considerDate;
} }
public function getQueryCalback(): string
{
return $this->queryCallback;
}
public function setQueryCallback(string $queryCallback): void
{
$this->queryCallback = $queryCallback;
}
} }

View file

@ -47,6 +47,7 @@ class DateDemandFactory
): void { ): void {
$this->contentObjectRenderer = $contentObjectRenderer; $this->contentObjectRenderer = $contentObjectRenderer;
} }
public function fromSettings(array $settings): DateDemand public function fromSettings(array $settings): DateDemand
{ {
$demand = new DateDemand(); $demand = new DateDemand();
@ -75,6 +76,9 @@ class DateDemandFactory
if (!empty($settings['limit'])) { if (!empty($settings['limit'])) {
$demand->setLimit($settings['limit']); $demand->setLimit($settings['limit']);
} }
if (!empty($settings['queryCallback'])) {
$demand->setQueryCallback($settings['queryCallback']);
}
return $demand; return $demand;
} }

View file

@ -2,6 +2,7 @@
namespace Wrm\Events\Domain\Repository; namespace Wrm\Events\Domain\Repository;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface; use TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface;
@ -14,6 +15,16 @@ use Wrm\Events\Service\CategoryService;
class DateRepository extends Repository class DateRepository extends Repository
{ {
/**
* @var Context
*/
protected $context;
public function injectContext(Context $context): void
{
$this->context = $context;
}
public function findByUids(string $uids): QueryResult public function findByUids(string $uids): QueryResult
{ {
$uids = explode(',', $uids); $uids = explode(',', $uids);
@ -80,7 +91,12 @@ class DateRepository extends Repository
} }
if ($demand->getStart() === null && $demand->getEnd() === null) { if ($demand->getStart() === null && $demand->getEnd() === null) {
$now = new \DateTime('now', new \DateTimeZone('Europe/Berlin')); $now = $this->context->getPropertyFromAspect(
'date',
'full',
new \DateTimeImmutable()
);
$now = $now->modify('midnight');
$constraints['nowAndFuture'] = $query->logicalOr([ $constraints['nowAndFuture'] = $query->logicalOr([
$query->greaterThanOrEqual('start', $now), $query->greaterThanOrEqual('start', $now),
$query->greaterThanOrEqual('end', $now) $query->greaterThanOrEqual('end', $now)
@ -97,6 +113,12 @@ class DateRepository extends Repository
$query->setOrderings([$demand->getSortBy() => $demand->getSortOrder()]); $query->setOrderings([$demand->getSortBy() => $demand->getSortOrder()]);
$callback = $demand->getQueryCalback();
if ($callback !== '') {
$params = ['query' => &$query];
GeneralUtility::callUserFunction($callback, $params, $this);
}
return $query; return $query;
} }