2019-07-18 13:44:19 +02:00
|
|
|
<?php
|
2021-01-07 08:50:43 +01:00
|
|
|
|
2019-07-18 13:44:19 +02:00
|
|
|
namespace Wrm\Events\Domain\Repository;
|
|
|
|
|
2023-05-22 09:59:45 +02:00
|
|
|
use DateTimeImmutable;
|
|
|
|
use DateTimeZone;
|
2021-12-07 15:26:25 +01:00
|
|
|
use TYPO3\CMS\Core\Context\Context;
|
2024-07-24 10:38:01 +02:00
|
|
|
use TYPO3\CMS\Core\Database\Connection;
|
2021-01-07 08:58:03 +01:00
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool;
|
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
2021-07-13 12:11:47 +02:00
|
|
|
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface;
|
2021-09-07 09:52:14 +02:00
|
|
|
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
|
2021-01-07 08:58:03 +01:00
|
|
|
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
|
2021-01-07 08:47:15 +01:00
|
|
|
use TYPO3\CMS\Extbase\Persistence\Repository;
|
2023-05-22 09:59:45 +02:00
|
|
|
use UnexpectedValueException;
|
2019-07-18 13:44:19 +02:00
|
|
|
use Wrm\Events\Domain\Model\Dto\DateDemand;
|
|
|
|
use Wrm\Events\Service\CategoryService;
|
|
|
|
|
2021-01-07 08:47:15 +01:00
|
|
|
class DateRepository extends Repository
|
2019-07-18 13:44:19 +02:00
|
|
|
{
|
2021-12-07 15:26:25 +01:00
|
|
|
/**
|
|
|
|
* @var Context
|
|
|
|
*/
|
|
|
|
protected $context;
|
|
|
|
|
2024-07-24 10:38:01 +02:00
|
|
|
/**
|
|
|
|
* @var ConnectionPool
|
|
|
|
*/
|
|
|
|
protected $connectionPool;
|
|
|
|
|
2021-12-07 15:26:25 +01:00
|
|
|
public function injectContext(Context $context): void
|
|
|
|
{
|
|
|
|
$this->context = $context;
|
|
|
|
}
|
|
|
|
|
2024-07-24 10:38:01 +02:00
|
|
|
public function injectConnectionPool(ConnectionPool $connectionPool): void
|
|
|
|
{
|
|
|
|
$this->connectionPool = $connectionPool;
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:52:14 +02:00
|
|
|
public function findByUids(string $uids): QueryResult
|
2019-07-18 13:44:19 +02:00
|
|
|
{
|
|
|
|
$uids = explode(',', $uids);
|
|
|
|
$query = $this->createQuery();
|
|
|
|
$query->matching(
|
|
|
|
$query->in('uid', $uids)
|
|
|
|
);
|
|
|
|
return $query->execute();
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:52:14 +02:00
|
|
|
public function findByDemand(DateDemand $demand): QueryResult
|
2019-07-18 13:44:19 +02:00
|
|
|
{
|
|
|
|
$query = $this->createDemandQuery($demand);
|
2019-08-14 17:22:01 +02:00
|
|
|
return $query->execute();
|
2019-07-18 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function createDemandQuery(DateDemand $demand): QueryInterface
|
|
|
|
{
|
|
|
|
$query = $this->createQuery();
|
2022-09-09 10:50:24 +02:00
|
|
|
$constraints = [
|
|
|
|
$this->createEventConstraint($query),
|
|
|
|
];
|
2019-07-18 13:44:19 +02:00
|
|
|
|
2022-07-12 07:22:48 +02:00
|
|
|
$categoriesConstraint = $this->createCategoryConstraint($query, $demand);
|
|
|
|
if ($categoriesConstraint instanceof ConstraintInterface) {
|
|
|
|
$constraints['categories'] = $categoriesConstraint;
|
2019-07-18 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 14:08:14 +02:00
|
|
|
if ($demand->getFeatures() !== []) {
|
|
|
|
$constraints['features'] = $this->createFeaturesConstraint($query, $demand);
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:56:18 +02:00
|
|
|
if ($demand->getLocations() !== []) {
|
2024-07-24 10:38:01 +02:00
|
|
|
$constraints['locations'] = $this->createLocationConstraint($query, $demand);
|
2022-08-02 15:56:18 +02:00
|
|
|
}
|
|
|
|
|
2023-04-03 08:54:48 +02:00
|
|
|
if ($demand->getOrganizers() !== []) {
|
|
|
|
$constraints['organizer'] = $query->in('event.organizer', $demand->getOrganizers());
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:44:19 +02:00
|
|
|
if ($demand->getRegion() !== '') {
|
|
|
|
$constraints['region'] = $query->equals('event.region', $demand->getRegion());
|
|
|
|
}
|
|
|
|
|
2021-01-07 08:50:43 +01:00
|
|
|
if ($demand->getHighlight() !== false) {
|
2019-07-18 13:44:19 +02:00
|
|
|
$constraints['highlight'] = $query->equals('event.highlight', $demand->getHighlight());
|
|
|
|
}
|
|
|
|
|
2019-08-14 17:22:01 +02:00
|
|
|
if ($demand->getSearchword() !== '') {
|
2021-07-13 12:11:47 +02:00
|
|
|
$constraints['searchword'] = $this->getSearchwordConstraint($query, $demand);
|
2019-08-14 17:22:01 +02:00
|
|
|
}
|
|
|
|
|
2021-07-13 15:50:14 +02:00
|
|
|
if ($demand->getUserCategories() !== []) {
|
|
|
|
$constraints['userCategories'] = $query->in('event.categories.uid', $demand->getUserCategories());
|
|
|
|
}
|
|
|
|
|
2023-02-16 15:33:19 +01:00
|
|
|
$timingConstraint = $this->createTimingConstraint($query, $demand);
|
|
|
|
if ($timingConstraint instanceof ConstraintInterface) {
|
|
|
|
$constraints['timing'] = $timingConstraint;
|
2021-03-16 11:14:25 +01:00
|
|
|
}
|
|
|
|
|
2022-05-16 13:24:39 +02:00
|
|
|
if ($demand->shouldShowFromNow() || $demand->shouldShowFromMidnight()) {
|
2023-05-22 09:59:45 +02:00
|
|
|
$now = $this->getNow();
|
2022-05-16 13:24:39 +02:00
|
|
|
|
|
|
|
if ($demand->shouldShowFromMidnight()) {
|
|
|
|
$now = $now->modify('midnight');
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:15:47 +01:00
|
|
|
$constraints['nowAndFuture'] = $query->logicalOr([
|
|
|
|
$query->greaterThanOrEqual('start', $now),
|
2023-06-07 08:56:42 +02:00
|
|
|
$query->greaterThanOrEqual('end', $now),
|
2021-12-06 12:15:47 +01:00
|
|
|
]);
|
2023-05-22 09:59:45 +02:00
|
|
|
} elseif ($demand->shouldShowUpcoming()) {
|
|
|
|
$now = $this->getNow();
|
|
|
|
|
|
|
|
$constraints['future'] = $query->logicalAnd([
|
|
|
|
$query->greaterThan('start', $now),
|
|
|
|
$query->logicalOr([
|
|
|
|
$query->equals('end', 0),
|
2023-06-07 08:56:42 +02:00
|
|
|
$query->greaterThan('end', $now),
|
|
|
|
]),
|
2023-05-22 09:59:45 +02:00
|
|
|
]);
|
2019-08-14 17:22:01 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 13:44:19 +02:00
|
|
|
if ($demand->getLimit() !== '') {
|
2023-06-07 08:56:42 +02:00
|
|
|
$query->setLimit((int)$demand->getLimit());
|
2019-07-18 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
2022-09-09 10:50:24 +02:00
|
|
|
$query->matching($query->logicalAnd($constraints));
|
2019-07-18 13:44:19 +02:00
|
|
|
|
2023-02-16 07:54:53 +01:00
|
|
|
if ($demand->getSortBy() && $demand->getSortOrder()) {
|
|
|
|
$query->setOrderings([$demand->getSortBy() => $demand->getSortOrder()]);
|
|
|
|
}
|
2019-07-18 13:44:19 +02:00
|
|
|
|
2021-12-07 15:26:25 +01:00
|
|
|
$callback = $demand->getQueryCalback();
|
|
|
|
if ($callback !== '') {
|
|
|
|
$params = ['query' => &$query];
|
|
|
|
GeneralUtility::callUserFunction($callback, $params, $this);
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:44:19 +02:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2021-07-13 12:11:47 +02:00
|
|
|
private function getSearchwordConstraint(
|
|
|
|
QueryInterface $query,
|
|
|
|
DateDemand $demand
|
|
|
|
): ConstraintInterface {
|
|
|
|
$fieldsToSearch = [
|
|
|
|
'event.title',
|
|
|
|
'event.teaser',
|
2021-07-13 14:04:56 +02:00
|
|
|
'event.categories.title',
|
2023-02-16 07:54:03 +01:00
|
|
|
'event.location.name',
|
2022-08-02 15:22:37 +02:00
|
|
|
'event.organizer.name',
|
2021-07-13 12:11:47 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$wordsToSearch = $demand->getSynonymsForSearchword();
|
|
|
|
$wordsToSearch[] = $demand->getSearchword();
|
|
|
|
$constraints = [];
|
|
|
|
|
2024-07-24 10:38:01 +02:00
|
|
|
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('tx_events_domain_model_date');
|
2021-07-13 12:11:47 +02:00
|
|
|
|
|
|
|
foreach ($wordsToSearch as $word) {
|
|
|
|
foreach ($fieldsToSearch as $field) {
|
|
|
|
$constraints[] = $query->like($field, '%' . $queryBuilder->escapeLikeWildcards($word) . '%');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->logicalOr($constraints);
|
|
|
|
}
|
|
|
|
|
2021-12-13 09:09:13 +01:00
|
|
|
protected function createCategoryConstraint(
|
|
|
|
QueryInterface $query,
|
2022-07-12 07:22:48 +02:00
|
|
|
DateDemand $demand
|
|
|
|
): ?ConstraintInterface {
|
|
|
|
$categories = $demand->getCategories();
|
2022-08-03 11:59:07 +02:00
|
|
|
if ($categories === '') {
|
|
|
|
return null;
|
|
|
|
}
|
2019-07-18 13:44:19 +02:00
|
|
|
$constraints = [];
|
|
|
|
|
2022-07-12 07:22:48 +02:00
|
|
|
if ($demand->getIncludeSubCategories()) {
|
|
|
|
$categories = GeneralUtility::makeInstance(CategoryService::class)
|
|
|
|
->getChildrenCategories($categories);
|
2019-07-18 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 09:52:14 +02:00
|
|
|
$categories = GeneralUtility::intExplode(',', $categories, true);
|
|
|
|
foreach ($categories as $category) {
|
2019-07-18 13:44:19 +02:00
|
|
|
$constraints[] = $query->contains('event.categories', $category);
|
|
|
|
}
|
2022-07-12 07:22:48 +02:00
|
|
|
|
|
|
|
if ($constraints === []) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($demand->getCategoryCombination() === 'or') {
|
|
|
|
return $query->logicalOr($constraints);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->logicalAnd($constraints);
|
2019-07-18 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
2023-02-16 15:33:19 +01:00
|
|
|
private function createTimingConstraint(
|
|
|
|
QueryInterface $query,
|
|
|
|
DateDemand $demand
|
|
|
|
): ?ConstraintInterface {
|
|
|
|
// Dates might have end of 0 if only start exists.
|
|
|
|
|
|
|
|
if ($demand->getStartObject() !== null && $demand->getEndObject() === null) {
|
|
|
|
return $query->logicalOr([
|
|
|
|
$query->greaterThanOrEqual('start', $demand->getStartObject()),
|
|
|
|
$query->greaterThanOrEqual('end', $demand->getStartObject()),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($demand->getStartObject() === null && $demand->getEndObject() !== null) {
|
|
|
|
return $query->logicalOr([
|
|
|
|
$query->logicalAnd([
|
|
|
|
$query->lessThanOrEqual('end', $demand->getEndObject()),
|
|
|
|
$query->greaterThan('end', 0),
|
|
|
|
]),
|
|
|
|
$query->lessThanOrEqual('start', $demand->getEndObject()),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($demand->getStartObject() !== null && $demand->getEndObject() !== null) {
|
|
|
|
return $query->logicalOr([
|
|
|
|
$query->logicalAnd([
|
|
|
|
$query->logicalOr([
|
|
|
|
$query->greaterThanOrEqual('start', $demand->getStartObject()),
|
|
|
|
$query->greaterThanOrEqual('end', $demand->getStartObject()),
|
|
|
|
]),
|
|
|
|
$query->logicalOr([
|
|
|
|
$query->lessThanOrEqual('start', $demand->getEndObject()),
|
|
|
|
$query->logicalAnd([
|
|
|
|
$query->lessThanOrEqual('end', $demand->getEndObject()),
|
|
|
|
$query->greaterThan('end', 0),
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
$query->logicalAnd([
|
|
|
|
$query->lessThanOrEqual('start', $demand->getStartObject()),
|
|
|
|
$query->greaterThanOrEqual('end', $demand->getEndObject()),
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:08:14 +02:00
|
|
|
private function createFeaturesConstraint(
|
|
|
|
QueryInterface $query,
|
|
|
|
DateDemand $demand
|
|
|
|
): ConstraintInterface {
|
|
|
|
$constraints = [];
|
|
|
|
|
|
|
|
foreach ($demand->getFeatures() as $feature) {
|
|
|
|
$constraints[] = $query->contains('event.features', $feature);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->logicalAnd($constraints);
|
|
|
|
}
|
|
|
|
|
2024-07-24 10:38:01 +02:00
|
|
|
private function createLocationConstraint(
|
|
|
|
QueryInterface $query,
|
|
|
|
DateDemand $demand
|
|
|
|
): ConstraintInterface {
|
|
|
|
$locations = $demand->getLocations();
|
|
|
|
$uidsToResolve = $locations;
|
|
|
|
|
|
|
|
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('tx_events_domain_model_location');
|
|
|
|
$queryBuilder->select('children');
|
|
|
|
$queryBuilder->from('tx_events_domain_model_location');
|
|
|
|
|
|
|
|
// Loop as resolved uids might have further children which need to be resolved as well.
|
|
|
|
do {
|
|
|
|
$concreteQueryBuilder = clone $queryBuilder;
|
|
|
|
$concreteQueryBuilder->where($concreteQueryBuilder->expr()->in(
|
|
|
|
'uid',
|
|
|
|
$concreteQueryBuilder->createNamedParameter($uidsToResolve, Connection::PARAM_INT_ARRAY)
|
|
|
|
));
|
|
|
|
|
|
|
|
foreach ($concreteQueryBuilder->execute()->fetchFirstColumn() as $newUids) {
|
|
|
|
if (is_string($newUids) === false) {
|
|
|
|
$newUids = '';
|
|
|
|
}
|
|
|
|
$newUids = GeneralUtility::intExplode(',', $newUids, true);
|
|
|
|
$uidsToResolve = array_diff($newUids, $locations);
|
|
|
|
$locations = array_merge($locations, $uidsToResolve);
|
|
|
|
}
|
|
|
|
} while ($uidsToResolve !== []);
|
|
|
|
|
|
|
|
return $query->in('event.location', $locations);
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:52:14 +02:00
|
|
|
public function findSearchWord(string $search): array
|
2019-08-14 17:22:01 +02:00
|
|
|
{
|
2024-07-24 10:38:01 +02:00
|
|
|
$connection = $this->connectionPool->getConnectionForTable('tx_events_domain_model_date');
|
2019-07-18 13:44:19 +02:00
|
|
|
|
|
|
|
$queryBuilder = $connection->createQueryBuilder();
|
|
|
|
|
|
|
|
$statement = $queryBuilder
|
2019-08-14 17:22:01 +02:00
|
|
|
->select('*')
|
|
|
|
->from('tx_events_domain_model_date')
|
2019-07-18 13:44:19 +02:00
|
|
|
->join(
|
2019-08-14 17:22:01 +02:00
|
|
|
'tx_events_domain_model_date',
|
|
|
|
'tx_events_domain_model_event',
|
2019-07-18 13:44:19 +02:00
|
|
|
'event',
|
2021-12-13 09:09:13 +01:00
|
|
|
$queryBuilder->expr()->eq(
|
|
|
|
'tx_events_domain_model_date.event',
|
|
|
|
$queryBuilder->quoteIdentifier('event.uid')
|
|
|
|
)
|
2019-07-18 13:44:19 +02:00
|
|
|
)->where(
|
2019-08-14 17:22:01 +02:00
|
|
|
$queryBuilder->expr()->like('event.title', $queryBuilder->createNamedParameter('%' . $search . '%'))
|
|
|
|
)->orderBy('tx_events_domain_model_date.start');
|
2019-07-18 13:44:19 +02:00
|
|
|
|
2019-08-14 17:22:01 +02:00
|
|
|
return $statement->execute()->fetchAll();
|
2019-07-18 13:44:19 +02:00
|
|
|
}
|
2022-09-09 10:50:24 +02:00
|
|
|
|
|
|
|
private function createEventConstraint(
|
|
|
|
QueryInterface $query
|
|
|
|
): ConstraintInterface {
|
|
|
|
return $query->logicalAnd(
|
|
|
|
// Use sub property to trigger join and pulling in event table constraints (hidden)
|
|
|
|
$query->logicalNot($query->equals('event.uid', null))
|
|
|
|
);
|
|
|
|
}
|
2023-05-22 09:59:45 +02:00
|
|
|
|
|
|
|
private function getNow(): DateTimeImmutable
|
|
|
|
{
|
|
|
|
$now = $this->context->getPropertyFromAspect(
|
|
|
|
'date',
|
|
|
|
'full',
|
|
|
|
new DateTimeImmutable()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$now instanceof DateTimeImmutable) {
|
|
|
|
throw new UnexpectedValueException(
|
|
|
|
'Could not retrieve now as DateTimeImmutable, got "' . gettype($now) . '".',
|
|
|
|
1639382648
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$now = $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
|
|
|
|
|
|
|
return $now;
|
|
|
|
}
|
2021-07-13 12:11:47 +02:00
|
|
|
}
|