Merge branch 'feature/9158-synonyms' into 'master'

Add synonym feature for date search

See merge request typo3/events!5
This commit is contained in:
Daniel Siepmann 2021-07-13 11:16:00 +00:00
commit 049e121183
3 changed files with 74 additions and 7 deletions

View file

@ -161,6 +161,8 @@ class DateController extends ActionController
if ($this->request->hasArgument('searchword') && $this->request->getArgument('searchword') != '')
$demand->setSearchword((string)$this->request->getArgument('searchword'));
$demand->setSynonyms($this->settings['synonyms'] ?? []);
if ($this->request->hasArgument('start') && $this->request->getArgument('start') != '')
$demand->setStart(strtotime($this->request->getArgument('start') . ' 00:00'));

View file

@ -2,6 +2,8 @@
namespace Wrm\Events\Domain\Model\Dto;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class DateDemand {
/**
@ -59,6 +61,15 @@ class DateDemand {
*/
protected $searchword = '';
/**
* @var array
*
* Synonym1 => ['word1', 'word2', ],
* Synonym2 => ['word3', 'word4', ],
*
*/
protected $synonyms = [];
/**
* @var bool
*/
@ -208,6 +219,39 @@ class DateDemand {
$this->searchword = $searchword;
}
/**
* @param array $synonyms
* [
* [
* 'word' => 'Word1',
* 'synonyms' => 'synonym1, synonym2',
* ],
* [
* 'word' => 'Word2',
* 'synonyms' => 'synonym3, synonym4',
* ],
*
* ]
*/
public function setSynonyms(array $synonyms): void
{
$this->synonyms = [];
foreach ($synonyms as $config) {
$synonymsForWord = GeneralUtility::trimExplode(',', $config['synonyms'], true);
foreach ($synonymsForWord as $synonym) {
$synonym = mb_strtolower($synonym);
$this->synonyms[$synonym][] = $config['word'];
$this->synonyms[$synonym] = array_unique($this->synonyms[$synonym]);
}
}
}
public function getSynonymsForSearchword(): array
{
$searchWord = mb_strtolower($this->getSearchword());
return $this->synonyms[$searchWord] ?? [];
}
/**
* @return string
*/

View file

@ -1,6 +1,7 @@
<?php
namespace Wrm\Events\Domain\Repository;
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface;
use Wrm\Events\Domain\Model\Dto\DateDemand;
use Wrm\Events\Service\CategoryService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
@ -70,12 +71,7 @@ class DateRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
}
if ($demand->getSearchword() !== '') {
$constraints['searchword'] = $query->logicalOr(
[
$query->like('event.title', '%' . $demand->getSearchword() . '%'),
$query->like('event.teaser', '%' . $demand->getSearchword() . '%')
]
);
$constraints['searchword'] = $this->getSearchwordConstraint($query, $demand);
}
if ($demand->getStart() !== '' && $demand->getEnd() != '') {
@ -103,6 +99,31 @@ class DateRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
return $query;
}
private function getSearchwordConstraint(
QueryInterface $query,
DateDemand $demand
): ConstraintInterface {
$fieldsToSearch = [
'event.title',
'event.teaser',
];
$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);
}
/**
* @param QueryInterface $query
* @param string $categories
@ -157,4 +178,4 @@ class DateRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
return $statement->execute()->fetchAll();
}
}
}