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: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;
|
|
|
|
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
|
2021-01-07 08:47:15 +01:00
|
|
|
use TYPO3\CMS\Extbase\Persistence\Repository;
|
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-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();
|
|
|
|
$constraints = [];
|
|
|
|
$categories = $demand->getCategories();
|
|
|
|
|
|
|
|
if ($categories) {
|
|
|
|
$categoryConstraints = $this->createCategoryConstraint($query, $categories, $demand->getIncludeSubCategories());
|
|
|
|
if ($demand->getCategoryCombination() === 'or') {
|
|
|
|
$constraints['categories'] = $query->logicalOr($categoryConstraints);
|
|
|
|
} else {
|
|
|
|
$constraints['categories'] = $query->logicalAnd($categoryConstraints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-09-07 15:56:22 +02:00
|
|
|
if ($demand->getStart() !== null) {
|
2021-03-16 11:14:25 +01:00
|
|
|
$constraints['starts'] = $query->greaterThanOrEqual('start', $demand->getStart());
|
|
|
|
}
|
2021-09-07 15:56:22 +02:00
|
|
|
if ($demand->getEnd() != null) {
|
2021-03-16 11:14:25 +01:00
|
|
|
$constraints['ends'] = $query->lessThanOrEqual('end', $demand->getEnd());
|
|
|
|
}
|
|
|
|
|
2021-09-07 15:56:22 +02:00
|
|
|
if ($demand->getStart() === null && $demand->getEnd() === null) {
|
2019-10-11 15:12:07 +02:00
|
|
|
$now = new \DateTime('now', new \DateTimeZone('Europe/Berlin'));
|
|
|
|
$constraints['untilnow'] = $query->greaterThanOrEqual('start', $now);
|
2019-08-14 17:22:01 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 13:44:19 +02:00
|
|
|
if ($demand->getLimit() !== '') {
|
|
|
|
$query->setLimit((int) $demand->getLimit());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($constraints)) {
|
|
|
|
$query->matching($query->logicalAnd($constraints));
|
|
|
|
}
|
|
|
|
|
2019-08-14 17:22:01 +02:00
|
|
|
$query->setOrderings([$demand->getSortBy() => $demand->getSortOrder()]);
|
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',
|
2021-07-13 12:11:47 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$wordsToSearch = $demand->getSynonymsForSearchword();
|
|
|
|
$wordsToSearch[] = $demand->getSearchword();
|
|
|
|
$constraints = [];
|
|
|
|
|
|
|
|
$queryBuilder = $this->objectManager->get(ConnectionPool::class)
|
|
|
|
->getQueryBuilderForTable('tx_events_domain_model_date');
|
|
|
|
|
|
|
|
foreach ($wordsToSearch as $word) {
|
|
|
|
foreach ($fieldsToSearch as $field) {
|
|
|
|
$constraints[] = $query->like($field, '%' . $queryBuilder->escapeLikeWildcards($word) . '%');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->logicalOr($constraints);
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:52:14 +02:00
|
|
|
protected function createCategoryConstraint(QueryInterface $query, string $categories, bool $includeSubCategories = false): array
|
2019-07-18 13:44:19 +02:00
|
|
|
{
|
|
|
|
$constraints = [];
|
|
|
|
|
|
|
|
if ($includeSubCategories) {
|
|
|
|
$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) {
|
2019-07-18 13:44:19 +02:00
|
|
|
$constraints[] = $query->contains('event.categories', $category);
|
|
|
|
}
|
|
|
|
return $constraints;
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:52:14 +02:00
|
|
|
public function findSearchWord(string $search): array
|
2019-08-14 17:22:01 +02:00
|
|
|
{
|
2019-07-18 13:44:19 +02:00
|
|
|
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
|
2019-08-14 17:22:01 +02:00
|
|
|
->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',
|
2019-08-14 17:22:01 +02: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
|
|
|
}
|
2021-07-13 12:11:47 +02:00
|
|
|
}
|