From 3b4ecbe6ce0690049ae32c44e06d8edb4ec84d99 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 28 Jun 2022 11:38:55 +0200 Subject: [PATCH] Extend demand and provide search to templates --- Classes/Controller/DateController.php | 27 +++- Classes/Domain/Model/Date.php | 13 ++ Classes/Domain/Model/Dto/DateDemand.php | 39 +++++- Documentation/Changelog/2.5.0.rst | 47 +++++++ Tests/Unit/Domain/Model/DateTest.php | 93 +++++++++++++ .../Unit/Domain/Model/Dto/DateDemandTest.php | 128 ++++++++++++++++++ ext_emconf.php | 2 +- 7 files changed, 338 insertions(+), 11 deletions(-) create mode 100644 Documentation/Changelog/2.5.0.rst create mode 100644 Tests/Unit/Domain/Model/DateTest.php create mode 100644 Tests/Unit/Domain/Model/Dto/DateDemandTest.php diff --git a/Classes/Controller/DateController.php b/Classes/Controller/DateController.php index b357bbe..1ff0745 100644 --- a/Classes/Controller/DateController.php +++ b/Classes/Controller/DateController.php @@ -91,9 +91,14 @@ class DateController extends AbstractController ); } - public function listAction(): void + /** + * @param array $search + */ + public function listAction(array $search = []): void { - if ( + if ($search !== []) { + $demand = DateDemand::createFromRequestValues($search, $this->settings); + } elseif ( ($this->request->hasArgument('searchword') && $this->request->getArgument('searchword') != '') || ($this->request->hasArgument('region') && $this->request->getArgument('region') != '') || ($this->request->hasArgument('start') && $this->request->getArgument('start') != '') @@ -104,17 +109,24 @@ class DateController extends AbstractController } else { $demand = $this->demandFactory->fromSettings($this->settings); } - $dates = $this->dateRepository->findByDemand($demand); - $this->view->assign('dates', $this->dateRepository->findByDemand($demand)); + + $this->view->assignMultiple([ + 'search' => $search, + 'demand' => $demand, + 'dates' => $this->dateRepository->findByDemand($demand), + ]); } - public function searchAction(): void + /** + * @param array $search + */ + public function searchAction(array $search = []): void { - $arguments = GeneralUtility::_GET('tx_events_datelist') ?? []; + $arguments = GeneralUtility::_GET('tx_events_datelist') ?? $search; if (is_array($arguments) === false) { $arguments = []; } - if (isset($arguments['events_search'])) { + if (isset($arguments['events_search']) && is_array($arguments['events_search'])) { $arguments += $arguments['events_search']; unset($arguments['events_search']); } @@ -125,6 +137,7 @@ class DateController extends AbstractController 'start' => $arguments['start'] ?? '', 'end' => $arguments['end'] ?? '', 'considerDate' => $arguments['considerDate'] ?? '', + 'search' => $search, 'demand' => DateDemand::createFromRequestValues($arguments, $this->settings), 'regions' => $this->regionRepository->findAll(), 'categories' => $this->categoryRepository->findAllCurrentlyAssigned(), diff --git a/Classes/Domain/Model/Date.php b/Classes/Domain/Model/Date.php index 6c9ebe3..dadf50d 100644 --- a/Classes/Domain/Model/Date.php +++ b/Classes/Domain/Model/Date.php @@ -66,6 +66,11 @@ class Date extends AbstractEntity $this->start = $start; } + public function getHasUsefulStartTime(): bool + { + return $this->getStart()->format('H:i') !== '00:00'; + } + /** * @return \DateTime end */ @@ -83,7 +88,15 @@ class Date extends AbstractEntity $this->end = $end; } + public function getHasUsefulEndTime(): bool + { + return $this->getEnd()->format('H:i') !== '23:59'; + } + public function getEndsOnSameDay(): bool + { + return $this->getStart()->format('Y-m-d') === $this->getEnd()->format('Y-m-d'); + } /** * @return Event diff --git a/Classes/Domain/Model/Dto/DateDemand.php b/Classes/Domain/Model/Dto/DateDemand.php index e2c08b7..c7a9259 100644 --- a/Classes/Domain/Model/Dto/DateDemand.php +++ b/Classes/Domain/Model/Dto/DateDemand.php @@ -22,7 +22,7 @@ class DateDemand protected $categories = ''; /** - * @var array + * @var int[] */ protected $userCategories = []; @@ -38,9 +38,15 @@ class DateDemand /** * @var string + * Legacy, superceeded by regions which allows multi select / filter. */ protected $region = ''; + /** + * @var int[] + */ + protected $regions = []; + /** * @var bool */ @@ -99,6 +105,9 @@ class DateDemand $instance->setSynonyms($settings['synonyms'] ?? []); $instance->setRegion($submittedValues['region'] ?? ''); + if (isset($submittedValues['regions']) && is_array($submittedValues['regions'])) { + $instance->setRegions($submittedValues['regions']); + } if ($submittedValues['highlight'] ?? false) { $instance->setHighlight($settings['highlight'] ?? false); @@ -114,8 +123,8 @@ class DateDemand $instance->setConsiderDate((bool)$submittedValues['considerDate']); } - if (is_array($submittedValues['userCategories'])) { - $instance->userCategories = array_map('intval', $submittedValues['userCategories']); + if (isset($submittedValues['userCategories']) && is_array($submittedValues['userCategories'])) { + $instance->setUserCategories($submittedValues['userCategories']); } $instance->setSortBy($settings['sortByDate'] ?? ''); @@ -154,6 +163,17 @@ class DateDemand return $this->categories; } + /** + * @param int[] $categories + */ + public function setUserCategories(array $categories): void + { + $this->userCategories = array_map('intval', $categories); + } + + /** + * @return int[] + */ public function getUserCategories(): array { return $this->userCategories; @@ -194,6 +214,19 @@ class DateDemand $this->region = $region; } + /** + * @return int[] + */ + public function getRegions(): array + { + return $this->regions; + } + + public function setRegions(array $regions): void + { + $this->regions = array_map('intval', $regions); + } + public function getHighlight(): bool { return $this->highlight; diff --git a/Documentation/Changelog/2.5.0.rst b/Documentation/Changelog/2.5.0.rst new file mode 100644 index 0000000..c2be87c --- /dev/null +++ b/Documentation/Changelog/2.5.0.rst @@ -0,0 +1,47 @@ +2.5.0 +===== + +Breaking +-------- + +Nothing + +Features +-------- + +* Respect search in list and search action of date controller. + The argument prefix has to be ``search``. + The value is used to create the demand and filter the dates. + The value is also provided to the view. + + That uses Extbase native argument mapping by name. No need for hardcoded plugin + namespaces or to provide some arguments from request to the view. + + The search is a plain array and therefore allows to pass whatever the templates + provide. + +* Extend ``DateDemand``. It now supports filtering by request values for Regions. + +* Extend ``Date`` to provide helpful methods for templates. + New methods: + + * ``getHasUsefulStartTime()`` + + * ``getHasUsefulEndTime()`` + + * ``getEndsOnSameDay()`` + +Fixes +----- + +Nothing + +Tasks +----- + +Nothing + +Deprecation +----------- + +Nothing diff --git a/Tests/Unit/Domain/Model/DateTest.php b/Tests/Unit/Domain/Model/DateTest.php new file mode 100644 index 0000000..9688666 --- /dev/null +++ b/Tests/Unit/Domain/Model/DateTest.php @@ -0,0 +1,93 @@ +setStart(new \DateTime('2022-07-11T13:48:00')); + + self::assertTrue($subject->getHasUsefulStartTime()); + } + + /** + * @test + */ + public function returnsThatItDoesNotHaveUsefulStartTime(): void + { + $subject = new Date(); + $subject->setStart(new \DateTime('2022-07-11T00:00:00')); + + self::assertFalse($subject->getHasUsefulStartTime()); + } + + /** + * @test + */ + public function returnsThatItHasUsefulEndTime(): void + { + $subject = new Date(); + $subject->setEnd(new \DateTime('2022-07-11T00:00:00')); + + self::assertTrue($subject->getHasUsefulEndTime()); + } + + /** + * @test + */ + public function returnsThatItDoesNotHaveUsefulEndTime(): void + { + $subject = new Date(); + $subject->setEnd(new \DateTime('2022-07-11T23:59:00')); + + self::assertFalse($subject->getHasUsefulEndTime()); + } + + /** + * @test + */ + public function returnsThatItEndsOnSameDay(): void + { + $subject = new Date(); + $subject->setStart(new \DateTime('2022-07-11T14:00:00')); + $subject->setEnd(new \DateTime('2022-07-11T22:00:00')); + + self::assertTrue($subject->getEndsOnSameDay()); + } + + /** + * @test + */ + public function returnsThatItDoesNotEndOnSameDay(): void + { + $subject = new Date(); + $subject->setStart(new \DateTime('2022-07-11T14:00:00')); + $subject->setEnd(new \DateTime('2022-07-13T22:00:00')); + + self::assertFalse($subject->getEndsOnSameDay()); + } +} diff --git a/Tests/Unit/Domain/Model/Dto/DateDemandTest.php b/Tests/Unit/Domain/Model/Dto/DateDemandTest.php new file mode 100644 index 0000000..c116c10 --- /dev/null +++ b/Tests/Unit/Domain/Model/Dto/DateDemandTest.php @@ -0,0 +1,128 @@ + 'This is the search word', + ], + [ + ] + ); + + self::assertSame( + 'This is the search word', + $result->getSearchword() + ); + } + + /** + * @test + */ + public function synonymsAreSetBySettings(): void + { + $result = DateDemand::createFromRequestValues( + [ + 'searchword' => 'synonym1', + ], + [ + 'synonyms' => [ + [ + 'word' => 'Word1', + 'synonyms' => 'synonym1, synonym2', + ], + [ + 'word' => 'Word2', + 'synonyms' => 'synonym3, synonym4', + ], + [ + 'word' => 'Word3', + 'synonyms' => 'synonym1', + ], + ], + ] + ); + + self::assertSame( + [ + 'Word1', + 'Word3', + ], + $result->getSynonymsForSearchword() + ); + } + + /** + * @test + */ + public function categoriesAreSetByRequest(): void + { + $result = DateDemand::createFromRequestValues( + [ + 'userCategories' => [ + '10', '20', + ], + ], + [ + ] + ); + + self::assertSame( + [ + 10, + 20, + ], + $result->getUserCategories() + ); + } + + /** + * @test + */ + public function regionsAreSetByRequest(): void + { + $result = DateDemand::createFromRequestValues( + [ + 'regions' => [ + '10', '20', + ], + ], + [ + ] + ); + + self::assertSame( + [ + 10, + 20, + ], + $result->getRegions() + ); + } +} diff --git a/ext_emconf.php b/ext_emconf.php index 973f150..4f43794 100644 --- a/ext_emconf.php +++ b/ext_emconf.php @@ -9,7 +9,7 @@ $EM_CONF['events'] = [ 'state' => 'alpha', 'createDirs' => '', 'clearCacheOnLoad' => 0, - 'version' => '2.4.4', + 'version' => '2.5.0', 'constraints' => [ 'depends' => [ 'typo3' => '10.4.00-11.5.99',