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-07-13 12:11:47 +02:00
|
|
|
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface;
|
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;
|
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool;
|
|
|
|
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
|
|
|
|
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
|
|
|
|
|
2021-01-07 08:47:15 +01:00
|
|
|
class DateRepository extends Repository
|
2019-07-18 13:44:19 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2019-08-14 17:22:01 +02:00
|
|
|
* Find all dates based on selected uids
|
2019-07-18 13:44:19 +02:00
|
|
|
* @param string $uids
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function findByUids($uids)
|
|
|
|
{
|
|
|
|
$uids = explode(',', $uids);
|
|
|
|
$query = $this->createQuery();
|
|
|
|
$query->matching(
|
|
|
|
$query->in('uid', $uids)
|
|
|
|
);
|
|
|
|
return $query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DateDemand $demand
|
|
|
|
* @return QueryResultInterface
|
|
|
|
* @throws InvalidQueryException
|
|
|
|
*/
|
|
|
|
public function findByDemand(DateDemand $demand)
|
|
|
|
{
|
|
|
|
$query = $this->createDemandQuery($demand);
|
2019-08-14 17:22:01 +02:00
|
|
|
return $query->execute();
|
2019-07-18 13:44:19 +02:00
|
|
|
|
|
|
|
// For testing purposes
|
|
|
|
// $query = $this->createDemandQueryViaBuilder($demand);
|
2019-12-11 09:15:07 +01:00
|
|
|
// return $query->execute()->fetchAll();
|
2019-07-18 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DateDemand $demand
|
|
|
|
* @return QueryInterface
|
|
|
|
* @throws InvalidQueryException
|
|
|
|
*/
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-08-14 17:22:01 +02:00
|
|
|
if ($demand->getStart() !== '' && $demand->getEnd() != '') {
|
|
|
|
$constraints['daterange'] = $query->logicalAnd(
|
|
|
|
[
|
|
|
|
$query->greaterThanOrEqual('start', $demand->getStart()),
|
2019-11-13 10:46:21 +01:00
|
|
|
$query->lessThanOrEqual('end', $demand->getEnd())
|
2019-08-14 17:22:01 +02:00
|
|
|
]
|
|
|
|
);
|
2019-10-09 15:03:58 +02:00
|
|
|
} else {
|
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);
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:44:19 +02:00
|
|
|
/**
|
|
|
|
* @param QueryInterface $query
|
|
|
|
* @param string $categories
|
|
|
|
* @param bool $includeSubCategories
|
|
|
|
* @return array
|
|
|
|
* @throws InvalidQueryException
|
|
|
|
*/
|
|
|
|
protected function createCategoryConstraint(QueryInterface $query, $categories, bool $includeSubCategories = false): array
|
|
|
|
{
|
|
|
|
$constraints = [];
|
|
|
|
|
|
|
|
if ($includeSubCategories) {
|
|
|
|
$categoryService = GeneralUtility::makeInstance(CategoryService::class);
|
|
|
|
$allCategories = $categoryService->getChildrenCategories($categories);
|
|
|
|
if (!\is_array($allCategories)) {
|
|
|
|
$allCategories = GeneralUtility::intExplode(',', $allCategories, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$allCategories = GeneralUtility::intExplode(',', $categories, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($allCategories as $category) {
|
|
|
|
$constraints[] = $query->contains('event.categories', $category);
|
|
|
|
}
|
|
|
|
return $constraints;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-14 17:22:01 +02:00
|
|
|
* findSearchWord with Query Builder
|
|
|
|
* @param $search
|
2019-07-18 13:44:19 +02:00
|
|
|
*/
|
2019-08-14 17:22:01 +02:00
|
|
|
public function findSearchWord($search)
|
|
|
|
{
|
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
|
|
|
}
|