Skip to content

Commit

Permalink
Add multiple categories to search
Browse files Browse the repository at this point in the history
This allows to filter for multiple categories (OR).
<f:form.checkbox name="customSearch[categories][]" value="category id"/>
  • Loading branch information
okmiim committed May 17, 2022
1 parent c316397 commit bfdc722
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
35 changes: 32 additions & 3 deletions Classes/Domain/Model/Dto/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class Search
{
protected $fullText = '';

protected $category = 0;
/**
* @var int[]
*/
protected $categories = [];

public function getFullText(): string
{
Expand All @@ -20,14 +23,40 @@ public function setFullText(string $fullText): void
$this->fullText = $fullText;
}

/**
* @return int
*
* @deprecated Use categories instead!
*/
public function getCategory(): int
{
return $this->category;
return $this->categories[0] ?? 0;
}

/**
* @param int $category
*
* @deprecated Use categories instead!
*/
public function setCategory(int $category): void
{
$this->category = $category;
$this->categories = [$category];
}

/**
* @return int[]
*/
public function getCategories(): array
{
return $this->categories;
}

/**
* @param int[] $categories
*/
public function setCategories(array $categories): void
{
$this->categories = $categories;
}

public function isSearch(): bool
Expand Down
8 changes: 6 additions & 2 deletions Classes/Domain/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ public function findBySearch(Search $search)
$query->like('description', '%' . $search->getFullText() . '%'),
]);
}
if ($search->getCategory()) {
$constraints['categories'] = $query->contains('categories', $search->getCategory());
if ($search->getCategories()) {
$categories = [];
foreach ($search->getCategories() as $category) {
$categories[] = $query->contains('categories', $category);
}
$constraints['categories'] = $query->logicalOr($categories);
}
$query->matching($query->logicalAnd($constraints));
$rows = $query->execute(true);
Expand Down
11 changes: 9 additions & 2 deletions Classes/EventListener/DefaultEventSearchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use HDNET\Calendarize\Domain\Repository\EventRepository;
use HDNET\Calendarize\Event\IndexRepositoryFindBySearchEvent;
use HDNET\Calendarize\Register;
use TYPO3\CMS\Core\Utility\MathUtility;

class DefaultEventSearchListener
{
Expand Down Expand Up @@ -48,8 +49,14 @@ protected function getSearchDto(IndexRepositoryFindBySearchEvent $event): Search
$customSearch = $event->getCustomSearch();

$search = new Search();
$search->setFullText(trim((string)$customSearch['fullText'] ?? ''));
$search->setCategory((int)$customSearch['category'] ?? 0);
$search->setFullText(trim((string)($customSearch['fullText'] ?? '')));
if (\is_array($customSearch['categories'])) {
$categories = array_map('intval', $customSearch['categories']);
$search->setCategories($categories);
} elseif (MathUtility::canBeInterpretedAsInteger($customSearch['category'] ?? '')) {
// Fallback for previous mode
$search->setCategories([(int)$customSearch['category']]);
}

return $search;
}
Expand Down

0 comments on commit bfdc722

Please sign in to comment.