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-12-07 15:26:25 +01:00
|
|
|
use TYPO3\CMS\Core\Context\Context;
|
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-12-07 15:26:25 +01:00
|
|
|
/**
|
|
|
|
* @var Context
|
|
|
|
*/
|
|
|
|
protected $context;
|
|
|
|
|
|
|
|
public function injectContext(Context $context): void
|
|
|
|
{
|
|
|
|
$this->context = $context;
|
|
|
|
}
|
|
|
|
|
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 = [];
|
|
|
|
|
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() !== []) {
|
|
|
|
$constraints['locations'] = $query->in('event.location', $demand->getLocations());
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
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-09-07 15:58:10 +02:00
|
|
|
// Dates might have end of 0 if only start exists.
|
|
|
|
// This is respected to take start as end date.
|
|
|
|
$constraints['ends'] = $query->logicalOr([
|
|
|
|
$query->logicalAnd([
|
|
|
|
$query->lessThanOrEqual('end', $demand->getEnd()),
|
|
|
|
$query->greaterThan('end', 0)
|
|
|
|
]),
|
|
|
|
$query->logicalAnd([
|
|
|
|
$query->equals('end', 0),
|
|
|
|
$query->lessThanOrEqual('start', $demand->getEnd())
|
|
|
|
]),
|
|
|
|
]);
|
2021-03-16 11:14:25 +01:00
|
|
|
}
|
|
|
|
|
2022-05-16 13:24:39 +02:00
|
|
|
if ($demand->shouldShowFromNow() || $demand->shouldShowFromMidnight()) {
|
2021-12-07 15:26:25 +01:00
|
|
|
$now = $this->context->getPropertyFromAspect(
|
|
|
|
'date',
|
|
|
|
'full',
|
|
|
|
new \DateTimeImmutable()
|
|
|
|
);
|
2021-12-13 09:09:13 +01:00
|
|
|
if (!$now instanceof \DateTimeImmutable) {
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Could not retrieve now as DateTimeImmutable, got "' . gettype($now) . '".',
|
|
|
|
1639382648
|
|
|
|
);
|
|
|
|
}
|
2022-05-16 13:24:39 +02:00
|
|
|
|
|
|
|
$now = $now->setTimezone(new \DateTimeZone(date_default_timezone_get()));
|
|
|
|
|
|
|
|
if ($demand->shouldShowFromMidnight()) {
|
|
|
|
$now = $now->modify('midnight');
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:15:47 +01:00
|
|
|
$constraints['nowAndFuture'] = $query->logicalOr([
|
|
|
|
$query->greaterThanOrEqual('start', $now),
|
|
|
|
$query->greaterThanOrEqual('end', $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
|
|
|
|
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',
|
2022-08-02 15:22:37 +02:00
|
|
|
'event.name', // Location name
|
|
|
|
'event.organizer.name',
|
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-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();
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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',
|
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
|
|
|
}
|
2021-07-13 12:11:47 +02:00
|
|
|
}
|