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;
|
|
|
|
|
2021-01-07 08:54:42 +01:00
|
|
|
/*
|
2019-07-18 13:44:19 +02:00
|
|
|
* This file is part of the TYPO3 CMS project.
|
|
|
|
*
|
|
|
|
* It is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License, either version 2
|
|
|
|
* of the License, or any later version.
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please read the
|
|
|
|
* LICENSE.txt file that was distributed with this source code.
|
|
|
|
*
|
|
|
|
* The TYPO3 project - inspiring people to share!
|
|
|
|
*/
|
|
|
|
|
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
2021-01-12 14:34:13 +01:00
|
|
|
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface;
|
2021-09-07 09:52:14 +02:00
|
|
|
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
|
2019-07-18 13:44:19 +02:00
|
|
|
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
|
2021-01-07 08:54:42 +01:00
|
|
|
use TYPO3\CMS\Extbase\Persistence\Repository;
|
|
|
|
use Wrm\Events\Domain\Model\Dto\EventDemand;
|
2021-01-12 14:12:58 +01:00
|
|
|
use Wrm\Events\Domain\Model\Event;
|
2021-01-07 08:54:42 +01:00
|
|
|
use Wrm\Events\Service\CategoryService;
|
2019-07-18 13:44:19 +02:00
|
|
|
|
2021-01-07 08:47:15 +01:00
|
|
|
class EventRepository extends Repository
|
2019-07-18 13:44:19 +02:00
|
|
|
{
|
2021-09-07 09:52:14 +02:00
|
|
|
public function findByUids(string $uids): QueryResult
|
2019-07-18 13:44:19 +02:00
|
|
|
{
|
|
|
|
$query = $this->createQuery();
|
2021-01-12 14:34:13 +01:00
|
|
|
$query->matching($query->in('uid', GeneralUtility::intExplode(',', $uids)));
|
2019-07-18 13:44:19 +02:00
|
|
|
|
|
|
|
return $query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-07 09:52:14 +02:00
|
|
|
* @return QueryResult|array
|
2019-07-18 13:44:19 +02:00
|
|
|
*/
|
|
|
|
public function findByDemand(EventDemand $demand)
|
|
|
|
{
|
|
|
|
$query = $this->createDemandQuery($demand);
|
2021-01-12 14:12:58 +01:00
|
|
|
|
|
|
|
if ($demand->getRecordUids() !== [] && $demand->getSortBy() === 'default') {
|
|
|
|
return $this->sortByDemand($query, $demand);
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:44:19 +02:00
|
|
|
return $query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createDemandQuery(EventDemand $demand): QueryInterface
|
|
|
|
{
|
|
|
|
$query = $this->createQuery();
|
2021-01-12 14:34:13 +01:00
|
|
|
$query = $this->setOrderings($query, $demand);
|
|
|
|
|
|
|
|
$constraints = $this->getConstraints($query, $demand);
|
|
|
|
if (!empty($constraints)) {
|
|
|
|
$query->matching($query->logicalAnd($constraints));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($demand->getLimit() !== '') {
|
|
|
|
$query->setLimit((int) $demand->getLimit());
|
|
|
|
}
|
2019-07-18 13:44:19 +02:00
|
|
|
|
2021-01-12 14:34:13 +01:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setOrderings(QueryInterface $query, EventDemand $demand): QueryInterface
|
|
|
|
{
|
2019-07-18 13:44:19 +02:00
|
|
|
$sortBy = $demand->getSortBy();
|
2021-01-12 14:12:58 +01:00
|
|
|
$sortingsToIgnore = ['singleSelection', 'default'];
|
2021-01-12 14:34:13 +01:00
|
|
|
|
|
|
|
if (!$sortBy || in_array($sortBy, $sortingsToIgnore)) {
|
|
|
|
return $query;
|
2019-07-18 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:34:13 +01:00
|
|
|
$order = QueryInterface::ORDER_ASCENDING;
|
|
|
|
if (strtolower($demand->getSortOrder()) === 'desc') {
|
|
|
|
$order = QueryInterface::ORDER_DESCENDING;
|
|
|
|
}
|
2019-07-18 13:44:19 +02:00
|
|
|
|
2021-01-12 14:34:13 +01:00
|
|
|
$query->setOrderings([$sortBy => $order]);
|
2019-07-18 13:44:19 +02:00
|
|
|
|
2021-01-12 14:34:13 +01:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function sortByDemand(QueryInterface $query, EventDemand $demand): array
|
|
|
|
{
|
|
|
|
$result = $query->execute()->toArray();
|
|
|
|
$expectedSorting = $demand->getRecordUids();
|
|
|
|
|
|
|
|
usort($result, function (Event $eventA, Event $eventB) use ($expectedSorting) {
|
|
|
|
$positionOfA = array_search($eventA->getUid(), $expectedSorting);
|
2021-03-16 08:10:46 +01:00
|
|
|
if ($positionOfA === false) {
|
|
|
|
$positionOfA = array_search($eventA->getLocalizedUid(), $expectedSorting);
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:34:13 +01:00
|
|
|
$positionOfB = array_search($eventB->getUid(), $expectedSorting);
|
2021-03-16 08:10:46 +01:00
|
|
|
if ($positionOfB === false) {
|
|
|
|
$positionOfB = array_search($eventB->getLocalizedUid(), $expectedSorting);
|
|
|
|
}
|
2021-01-12 14:34:13 +01:00
|
|
|
|
|
|
|
return $positionOfA <=> $positionOfB;
|
|
|
|
});
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2021-12-13 09:09:13 +01:00
|
|
|
/**
|
|
|
|
* @return ConstraintInterface[]
|
|
|
|
*/
|
2021-01-12 14:34:13 +01:00
|
|
|
private function getConstraints(QueryInterface $query, EventDemand $demand): array
|
|
|
|
{
|
|
|
|
$constraints = [];
|
|
|
|
|
|
|
|
if ($demand->getCategories()) {
|
|
|
|
$constraints['categories'] = $this->createCategoryConstraint($query, $demand);
|
2019-07-18 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:12:58 +01:00
|
|
|
if ($demand->getRecordUids() !== []) {
|
|
|
|
$constraints['recordUids'] = $query->in('uid', $demand->getRecordUids());
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:44:19 +02:00
|
|
|
if ($demand->getRegion() !== '') {
|
|
|
|
$constraints['region'] = $query->equals('region', $demand->getRegion());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($demand->getHighlight()) {
|
|
|
|
$constraints['highlight'] = $query->equals('highlight', $demand->getHighlight());
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:34:13 +01:00
|
|
|
return $constraints;
|
2019-07-18 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:34:13 +01:00
|
|
|
protected function createCategoryConstraint(QueryInterface $query, EventDemand $demand): ConstraintInterface
|
2019-07-18 13:44:19 +02:00
|
|
|
{
|
|
|
|
$constraints = [];
|
|
|
|
|
2021-09-07 09:52:14 +02:00
|
|
|
$categories = $demand->getCategories();
|
2021-02-24 10:46:53 +01:00
|
|
|
if ($demand->getIncludeSubCategories()) {
|
2019-07-18 13:44:19 +02:00
|
|
|
$categoryService = GeneralUtility::makeInstance(CategoryService::class);
|
2021-09-07 09:52:14 +02:00
|
|
|
$categories = $categoryService->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) {
|
2021-02-24 10:46:53 +01:00
|
|
|
$constraints[] = $query->contains('categories', $category);
|
2019-07-18 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:34:13 +01:00
|
|
|
if ($demand->getCategoryCombination() === 'or') {
|
|
|
|
return $query->logicalOr($constraints);
|
|
|
|
}
|
|
|
|
return $query->logicalAnd($constraints);
|
2021-01-12 14:12:58 +01:00
|
|
|
}
|
2019-08-14 17:22:01 +02:00
|
|
|
|
2021-09-07 09:52:14 +02:00
|
|
|
public function findSearchWord(string $search): QueryResult
|
2019-08-14 17:22:01 +02:00
|
|
|
{
|
|
|
|
$query = $this->createQuery();
|
2021-01-12 14:34:13 +01:00
|
|
|
$query->matching($query->like('title', '%' . $search . '%'));
|
2019-08-14 17:22:01 +02:00
|
|
|
$query->setOrderings(['title' => QueryInterface::ORDER_ASCENDING]);
|
|
|
|
$query->setLimit(20);
|
|
|
|
return $query->execute();
|
|
|
|
}
|
2021-01-07 08:50:43 +01:00
|
|
|
}
|