Skip to content

Commit

Permalink
[FEATURE] Respect configuration.site for list view and add a filter f…
Browse files Browse the repository at this point in the history
…or configuration
  • Loading branch information
einpraegsam committed Mar 1, 2024
1 parent ffa7639 commit d8bf863
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 105 deletions.
10 changes: 5 additions & 5 deletions Classes/Controller/AbstractNewsletterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ protected function setFilter(): void
$filter
);
}
if (isset($filter['usergroup']['__identity']) && $filter['usergroup']['__identity'] === '0') {
unset($filter['usergroup']);
}
if (array_key_exists('category', $filter) && (is_array($filter['category']) || $filter['category'] === '')) {
$filter['category'] = 0;
$clearFields = ['usergroup', 'configuration', 'category'];
foreach ($clearFields as $clearField) {
if (($filter[$clearField] ?? '0') === '0' || is_array($filter[$clearField])) {
unset($filter[$clearField]);
}
}
$this->request = $this->request->withArgument('filter', $filter);
}
Expand Down
4 changes: 2 additions & 2 deletions Classes/Controller/NewsletterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public function initializeListAction(): void
public function listAction(Filter $filter): ResponseInterface
{
$this->view->assignMultiple([
'newsletters' => $this->newsletterRepository->findAll(),
'newsletters' => $this->newsletterRepository->findAllAuthorized(),
'newslettersGrouped' => $this->newsletterRepository->findAllGroupedByCategories($filter),
'configurations' => $this->configurationRepository->findAll(),
'configurations' => $this->configurationRepository->findAllAuthorized(),
'categories' => $this->categoryRepository->findAllLuxletterCategories(),
'filter' => $filter,
]);
Expand Down
131 changes: 57 additions & 74 deletions Classes/Domain/Model/Dto/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,121 +4,114 @@
namespace In2code\Luxletter\Domain\Model\Dto;

use DateTime;
use In2code\Luxletter\Domain\Model\Category;
use In2code\Luxletter\Domain\Model\Configuration;
use In2code\Luxletter\Domain\Model\Usergroup;
use In2code\Luxletter\Utility\LocalizationUtility;

/**
* Class Filter
*/
class Filter
{
const TIME_DEFAULT = 0;
const TIME_1_WEEK = 10;
const TIME_1_MONTH = 20;
const TIME_3_MONTHS = 30;
const TIME_6_MONTHS = 40;
const TIME_1_YEAR = 50;
public const TIME_DEFAULT = 0;
public const TIME_1_WEEK = 10;
public const TIME_1_MONTH = 20;
public const TIME_3_MONTHS = 30;
public const TIME_6_MONTHS = 40;
public const TIME_1_YEAR = 50;

/**
* @var string
*/
protected $searchterm = '';
protected string $searchterm = '';

/**
* @var Usergroup
*/
protected $usergroup = null;
protected ?Usergroup $usergroup = null;
protected ?Category $category = null;
protected ?Configuration $configuration = null;

/**
* @var \In2code\Luxletter\Domain\Model\Category|null
*/
protected $category = null;

/**
* @var int
*/
protected $time = self::TIME_DEFAULT;
protected int $time = self::TIME_DEFAULT;

/**
* This is just a dummy property, that helps to recognize if a filter is set and helps to save this to the session
*
* @var bool
*/
protected $reset = false;
protected bool $reset = false;

/**
* @return string
*/
public function getSearchterm(): string
{
return $this->searchterm;
}

/**
* @return string[]
*/
public function isSearchtermSet(): bool
{
return $this->getSearchterm() !== '';
}

public function getSearchterms(): array
{
return explode(' ', $this->getSearchterm());
}

/**
* @param string $searchterm
* @return Filter
*/
public function setSearchterm(string $searchterm): self
{
$this->searchterm = $searchterm;
return $this;
}

/**
* @return Usergroup
*/
public function getUsergroup(): ?Usergroup
{
return $this->usergroup;
}

/**
* @param Usergroup|null $usergroup
* @return Filter
*/
public function isUsergroupSet(): bool
{
return $this->getUsergroup() !== null;
}

public function setUsergroup(?Usergroup $usergroup): self
{
$this->usergroup = $usergroup;
return $this;
}

/**
* @return \In2code\Luxletter\Domain\Model\Category|null
*/
public function getCategory(): ?\In2code\Luxletter\Domain\Model\Category
public function getCategory(): ?Category
{
return $this->category;
}

/**
* @param \In2code\Luxletter\Domain\Model\Category|null $category
* @return Filter
*/
public function setCategory(?\In2code\Luxletter\Domain\Model\Category $category): Filter
public function isCategorySet(): bool
{
return $this->getCategory() !== null;
}

public function setCategory(?Category $category): Filter
{
$this->category = $category;
return $this;
}

/**
* @return int
*/
public function getConfiguration(): ?Configuration
{
return $this->configuration;
}

public function isConfigurationSet(): bool
{
return $this->getConfiguration() !== null;
}

public function setConfiguration(?Configuration $configuration): self
{
$this->configuration = $configuration;
return $this;
}

public function getTime(): int
{
return $this->time;
}

/**
* @return DateTime
*/
public function isTimeSet(): bool
{
return $this->getTime() !== self::TIME_DEFAULT;
}

public function getTimeDateStart(): DateTime
{
$date = new DateTime();
Expand All @@ -142,40 +135,30 @@ public function getTimeDateStart(): DateTime
return $date;
}

/**
* @param int $time
* @return Filter
*/
public function setTime(int $time): Filter
{
$this->time = $time;
return $this;
}

/**
* @return bool
*/
public function isReset(): bool
{
return $this->reset;
}

/**
* @param bool $reset
* @return Filter
*/
public function setReset(bool $reset): self
{
$this->reset = $reset;
return $this;
}

/**
* @return bool
*/
public function isSet(): bool
{
return $this->searchterm !== '' || $this->usergroup !== null || $this->category !== null || $this->time > 0;
return $this->isSearchtermSet()
|| $this->isConfigurationSet()
|| $this->isUsergroupSet()
|| $this->isCategorySet()
|| $this->isTimeSet();
}

public function getTimeOptions(): array
Expand Down
24 changes: 23 additions & 1 deletion Classes/Domain/Repository/NewsletterRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@
use In2code\Luxletter\Domain\Model\Log;
use In2code\Luxletter\Domain\Model\Newsletter;
use In2code\Luxletter\Domain\Model\Queue;
use In2code\Luxletter\Domain\Service\SiteService;
use In2code\Luxletter\Utility\BackendUserUtility;
use In2code\Luxletter\Utility\DatabaseUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

class NewsletterRepository extends AbstractRepository
{
public function findAllAuthorized(): QueryResultInterface
{
$query = $this->createQuery();
if (BackendUserUtility::isAdministrator() === false) {
$siteService = GeneralUtility::makeInstance(SiteService::class);
$query->matching($query->in('configuration.site', array_keys($siteService->getAllowedSites())));
}
return $query->execute();
}

public function findLatestNewsletter(): ?Newsletter
{
$query = $this->createQuery();
Expand Down Expand Up @@ -60,8 +73,8 @@ public function findAllGroupedByCategories(Filter $filter): array
protected function findAllByFilter(Filter $filter): ?QueryResultInterface
{
$query = $this->createQuery();
$logicalAnd = [];
if ($filter->isSet()) {
$logicalAnd = [$query->greaterThan('uid', 0)];
if ($filter->getSearchterm() !== '') {
$logicalOr = [];
foreach ($filter->getSearchterms() as $searchterm) {
Expand All @@ -77,6 +90,15 @@ protected function findAllByFilter(Filter $filter): ?QueryResultInterface
if ($filter->getTime() > 0) {
$logicalAnd[] = $query->greaterThanOrEqual('crdate', $filter->getTimeDateStart());
}
if ($filter->isConfigurationSet()) {
$logicalAnd[] = $query->equals('configuration', $filter->getConfiguration());
}
}
if (BackendUserUtility::isAdministrator() === false) {
$siteService = GeneralUtility::makeInstance(SiteService::class);
$logicalAnd[] = $query->in('configuration.site', array_keys($siteService->getAllowedSites()));
}
if ($logicalAnd !== []) {
$query->matching($query->logicalAnd(...$logicalAnd));
}
return $query->execute();
Expand Down
55 changes: 32 additions & 23 deletions Resources/Private/Partials/Filter/NewsletterList.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h3 class="panel-title">Filter</h3>
<div class="panel-body">
<f:form name="filter" class="form" object="{filter}" action="list">
<div class="row">
<div class="col-md-3">
<div class="col-xl-4 col-md-6 mb-3">
<h5><f:translate key="module.newsletter.list.filter.search.label">search</f:translate></h5>

<div class="input-group form-group">
Expand All @@ -24,33 +24,26 @@ <h5><f:translate key="module.newsletter.list.filter.search.label">search</f:tran
<core:icon identifier="actions-search" />
</button>
</div>
</div>


<div class="col-md-3">
<h5><f:translate key="module.newsletter.list.filter.category.label">Category</f:translate></h5>

<div class="input-group form-group">
<label for="categoryScoring" class="hidden">
<f:translate key="module.newsletter.list.filter.category.label">Category</f:translate>
</label>
<f:form.select
property="category"
options="{categories}"
optionLabelField="title"
optionValueField="uid"
prependOptionValue=""
prependOptionLabel=""
id="configuration"
class="form-control form-select"
id="categoryScoring"/>
<button type="button" class="btn btn-default" data-global-event="click" data-action-focus="#categoryScoring">
<core:icon identifier="actions-tag" />
property="configuration"
options="{configurations}"
prependOptionLabel=""
prependOptionValue="0"
optionValueField="uid"
optionLabelField="title"
value="{filter.configuration}" />
<button type="button" class="btn btn-default" data-global-event="click" data-action-focus="#configuration">
<core:icon identifier="actions-user" size="small" />
</button>
</div>
</div>


<div class="col-md-3">
<div class="col-xl-4 col-md-6 mb-3">
<h5><f:translate key="module.newsletter.list.filter.time.label">Time</f:translate></h5>

<div class="input-group form-group">
Expand All @@ -66,13 +59,29 @@ <h5><f:translate key="module.newsletter.list.filter.time.label">Time</f:translat
<core:icon identifier="actions-calendar-alternative" />
</button>
</div>
</div>

<div class="input-group form-group">
<label for="categoryScoring" class="hidden">
<f:translate key="module.newsletter.list.filter.category.label">Category</f:translate>
</label>
<f:form.select
property="category"
options="{categories}"
optionLabelField="title"
optionValueField="uid"
prependOptionValue="0"
prependOptionLabel=""
class="form-control form-select"
id="categoryScoring"/>
<button type="button" class="btn btn-default" data-global-event="click" data-action-focus="#categoryScoring">
<core:icon identifier="actions-tag" />
</button>
</div>
</div>

<div class="col-md-3">
<h5 style="visibility: hidden;">Submit</h5>

<div style="display: flex; flex-direction: initial;">
<div class="col-xl-4 col-md-6 mb-3" style="display: flex; flex-direction: column; justify-content: flex-end;">
<div style="display: flex; justify-content: space-between;">
<f:comment>Clear filter</f:comment>
<f:if condition="{filter.set}">
<f:link.action class="btn btn-default" style="flex: 50%; height: 32px; margin-top: auto; margin-right: 10px;" action="resetFilter" arguments="{redirectAction:view.action}">
Expand Down

0 comments on commit d8bf863

Please sign in to comment.