From 4923944175069efe0068759e8d256ce7bb088c5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Fromme?= Date: Tue, 26 Oct 2021 11:26:09 +0200 Subject: [PATCH] Further improve mobile search --- .../Controller/AjaxFilterPanelController.php | 26 +++-- .../Controller/AjaxSearchController.php | 1 - .../Classes/Controller/SearchController.php | 15 --- .../Configuration/TypoScript/setup.typoscript | 3 +- .../ext/ep_products/ext_localconf.php | 12 +- .../ViewHelpers/StimulusActionViewHelper.php | 83 ++++++++++++++ .../StimulusControllerViewHelper.php | 108 ++++++++++++++++++ .../controllers/mobile_search_controller.js | 6 +- .../Private/Partials/Search/MobileSearch.html | 4 +- .../{Options.html => Index.html} | 2 +- 10 files changed, 214 insertions(+), 46 deletions(-) create mode 100644 public/typo3conf/ext/ep_theme/Classes/ViewHelpers/StimulusActionViewHelper.php create mode 100644 public/typo3conf/ext/ep_theme/Classes/ViewHelpers/StimulusControllerViewHelper.php rename public/typo3conf/ext/ep_theme/Resources/Private/Templates/AjaxFilterPanel/{Options.html => Index.html} (94%) diff --git a/public/typo3conf/ext/ep_products/Classes/Controller/AjaxFilterPanelController.php b/public/typo3conf/ext/ep_products/Classes/Controller/AjaxFilterPanelController.php index 84f35333..85c5030d 100644 --- a/public/typo3conf/ext/ep_products/Classes/Controller/AjaxFilterPanelController.php +++ b/public/typo3conf/ext/ep_products/Classes/Controller/AjaxFilterPanelController.php @@ -28,6 +28,7 @@ namespace EP\EpProducts\Controller; ***************************************************************/ use EP\EpProducts\Service\FilterService; +use EP\EpProducts\Service\SearchResultService; use EP\EpProducts\Traits\RequestArgumentTypeConversionTrait; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; @@ -42,12 +43,18 @@ class AjaxFilterPanelController extends ActionController */ protected $filterService; + /** + * @var SearchResultService + */ + private $searchResultService; + /** * @param FilterService $filterService */ - public function __construct(FilterService $filterService) + public function __construct(FilterService $filterService, SearchResultService $searchResultService) { $this->filterService = $filterService; + $this->searchResultService = $searchResultService; } public function initializeAction() @@ -59,20 +66,15 @@ class AjaxFilterPanelController extends ActionController /** * @param array $filterSettings */ - public function optionsAction(array $filterSettings) + public function indexAction(array $filterSettings) { - $forcedConceptUids = GeneralUtility::trimExplode(',', $this->settings['forcedConceptUids'], true); $excludedConceptUids = GeneralUtility::trimExplode(',', $this->settings['excludedConceptUids']); - $excludedRegionUids = GeneralUtility::trimExplode(',', $this->settings['excludedRegionUids']); - $filterOptions = $this - ->filterService - ->getFilterOptions( - $filterSettings, - $excludedConceptUids, - $excludedRegionUids, - $forcedConceptUids - ); + $searchResult = $this + ->searchResultService + ->getResult($filterSettings, $excludedConceptUids); + + $filterOptions = $this->filterService->collectFilterOptionsFromResult($searchResult); $resultsUri = $this->uriBuilder ->reset() diff --git a/public/typo3conf/ext/ep_products/Classes/Controller/AjaxSearchController.php b/public/typo3conf/ext/ep_products/Classes/Controller/AjaxSearchController.php index 5448c451..598ab606 100644 --- a/public/typo3conf/ext/ep_products/Classes/Controller/AjaxSearchController.php +++ b/public/typo3conf/ext/ep_products/Classes/Controller/AjaxSearchController.php @@ -74,7 +74,6 @@ class AjaxSearchController extends ActionController $organic = true; $forcedConceptUids = GeneralUtility::trimExplode(',', $this->settings['forcedConceptUids'], true); $excludedConceptUids = GeneralUtility::trimExplode(',', $this->settings['excludedConceptUids']); - $excludedRegionUids = GeneralUtility::trimExplode(',', $this->settings['excludedRegionUids']); if (isset($filterSettings['dateFrom']) || isset($filterSettings['dateTo'])) { $dateSelected = true; diff --git a/public/typo3conf/ext/ep_products/Classes/Controller/SearchController.php b/public/typo3conf/ext/ep_products/Classes/Controller/SearchController.php index 462dec39..e49a82ae 100644 --- a/public/typo3conf/ext/ep_products/Classes/Controller/SearchController.php +++ b/public/typo3conf/ext/ep_products/Classes/Controller/SearchController.php @@ -59,21 +59,6 @@ class SearchController extends ActionController $this->searchResultService = $searchResultService; } - public function searchbarAction() - { - $fixed = (bool)$this->settings['fixed']; - $searchParams = $this->searchResultService->getNormalizedSearchParams(); - $filterSettings = $this->filterService->getNormalizedFilterSettings(); - $filterOptions = $this->filterService->getFilterOptions($filterSettings); - - $this->view->assignMultiple([ - 'filterSettings' => $filterSettings, - 'filterOptions' => $filterOptions, - 'searchParams' => $searchParams, - 'fixed' => $fixed, - ]); - } - public function initializeProcessSearchAction() { $this->processSearchParamsArgument(); diff --git a/public/typo3conf/ext/ep_products/Configuration/TypoScript/setup.typoscript b/public/typo3conf/ext/ep_products/Configuration/TypoScript/setup.typoscript index 24d31bb4..5a231150 100644 --- a/public/typo3conf/ext/ep_products/Configuration/TypoScript/setup.typoscript +++ b/public/typo3conf/ext/ep_products/Configuration/TypoScript/setup.typoscript @@ -149,8 +149,7 @@ tx_epproducts_ajax_json { 2 = searchresult } AjaxFilterPanel { - 1 = options - 2 = search + 1 = index } AjaxDate { 1 = dates diff --git a/public/typo3conf/ext/ep_products/ext_localconf.php b/public/typo3conf/ext/ep_products/ext_localconf.php index 351a0bda..682fbf07 100644 --- a/public/typo3conf/ext/ep_products/ext_localconf.php +++ b/public/typo3conf/ext/ep_products/ext_localconf.php @@ -51,14 +51,6 @@ $iconRegistry->registerIcon( ] ); -\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin( - 'EP.ep_products', - 'searchbox', - [ - 'Search' => 'searchbox', - ] -); - \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin( 'EP.ep_products', 'searchresult', @@ -295,7 +287,7 @@ $iconRegistry->registerIcon( 'ajax', [ 'AjaxSearch' => 'searchresult', - 'AjaxFilterPanel' => 'options,search', + 'AjaxFilterPanel' => 'index', 'AjaxDate' => 'dates', 'AjaxContingent' => 'list,rooms', 'AjaxTable' => 'datestable,pricetable,pricetableHtml,eventPricetable,daytripRooms', @@ -305,7 +297,7 @@ $iconRegistry->registerIcon( ], [ 'AjaxSearch' => 'searchresult', - 'AjaxFilterPanel' => 'options,search', + 'AjaxFilterPanel' => 'index', 'AjaxDate' => 'dates', 'AjaxContingent' => 'list,rooms', 'AjaxTable' => 'datestable,pricetable,pricetableHtml,eventPricetable,daytripRooms', diff --git a/public/typo3conf/ext/ep_theme/Classes/ViewHelpers/StimulusActionViewHelper.php b/public/typo3conf/ext/ep_theme/Classes/ViewHelpers/StimulusActionViewHelper.php new file mode 100644 index 00000000..f930c2b1 --- /dev/null +++ b/public/typo3conf/ext/ep_theme/Classes/ViewHelpers/StimulusActionViewHelper.php @@ -0,0 +1,83 @@ +, dreipunktnull + * + * All rights reserved + * + * This script is part of the TYPO3 project. The TYPO3 project is + * free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * The GNU General Public License can be found at + * http://www.gnu.org/copyleft/gpl.html. + * + * This script is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * This copyright notice MUST APPEAR in all copies of the script! + ***************************************************************/ + +use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; +use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; +use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic; + +class StimulusActionViewHelper extends AbstractViewHelper +{ + use CompileWithRenderStatic; + + protected $escapeOutput = false; + + public function initializeArguments() + { + parent::initializeArguments(); + + $this->registerArgument('controller', 'string', 'Controller name', true); + $this->registerArgument('action', 'string', 'Action name', true); + $this->registerArgument('event', 'string', 'Event name', false); + } + + /** + * @param array $arguments + * @param \Closure $renderChildrenClosure + * @param RenderingContextInterface $renderingContext + * @return string + */ + public static function renderStatic( + array $arguments, + \Closure $renderChildrenClosure, + RenderingContextInterface $renderingContext + ) + { + $controllerName = static::normalizeControllerName($arguments['controller']); + $action = $arguments['action']; + $event = $arguments['event']; + + if (!$event) { + return sprintf('data-action="%s#%s"', $controllerName, $action); + } + + return sprintf('data-action="%s->%s#%s"', $event, $controllerName, $action); + } + + /** + * Method taken from symfony/webpack-encore-bundle + * + * @param string $str + * @return string + */ + private static function normalizeControllerName(string $str): string + { + return preg_replace('/^@/', '', str_replace('_', '-', str_replace('/', '--', $str))); + } + +} diff --git a/public/typo3conf/ext/ep_theme/Classes/ViewHelpers/StimulusControllerViewHelper.php b/public/typo3conf/ext/ep_theme/Classes/ViewHelpers/StimulusControllerViewHelper.php new file mode 100644 index 00000000..02fa34ea --- /dev/null +++ b/public/typo3conf/ext/ep_theme/Classes/ViewHelpers/StimulusControllerViewHelper.php @@ -0,0 +1,108 @@ +, dreipunktnull + * + * All rights reserved + * + * This script is part of the TYPO3 project. The TYPO3 project is + * free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * The GNU General Public License can be found at + * http://www.gnu.org/copyleft/gpl.html. + * + * This script is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * This copyright notice MUST APPEAR in all copies of the script! + ***************************************************************/ + +use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; +use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; +use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic; + +class StimulusControllerViewHelper extends AbstractViewHelper +{ + use CompileWithRenderStatic; + + protected $escapeOutput = false; + + public function initializeArguments() + { + parent::initializeArguments(); + + $this->registerArgument('controller', 'string', 'Controller name', true); + $this->registerArgument('values', 'array', 'Controller values', false, []); + } + + /** + * @param array $arguments + * @param \Closure $renderChildrenClosure + * @param RenderingContextInterface $renderingContext + * @return string + */ + public static function renderStatic( + array $arguments, + \Closure $renderChildrenClosure, + RenderingContextInterface $renderingContext + ) + { + $controllerName = static::normalizeControllerName($arguments['controller']); + $attributes = [ + sprintf('data-controller="%s"', $controllerName), + ]; + + foreach ($arguments['values'] as $key => $value) { + if (!is_scalar($value)) { + $value = htmlspecialchars(json_encode($value), ENT_QUOTES); + } + + if (\is_bool($value)) { + $value = $value ? 'true' : 'false'; + } + + $key = static::normalizeKeyName($key); + + $attributes[] = sprintf('data-%s-%s-value="%s"', $controllerName, $key, $value); + } + + return implode(' ', $attributes); + } + + /** + * Method taken from symfony/webpack-encore-bundle + * + * @param string $str + * @return string + */ + private static function normalizeControllerName(string $str): string + { + return preg_replace('/^@/', '', str_replace('_', '-', str_replace('/', '--', $str))); + } + + /** + * Method taken from symfony/webpack-encore-bundle + * + * @param string $str + * @return string + */ + private static function normalizeKeyName(string $str): string + { + // Adapted from ByteString::camel + $str = ucfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $str)))); + + // Adapted from ByteString::snake + return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1-\2', $str)); + } + +} diff --git a/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/controllers/mobile_search_controller.js b/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/controllers/mobile_search_controller.js index a29537c8..974f8182 100644 --- a/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/controllers/mobile_search_controller.js +++ b/public/typo3conf/ext/ep_theme/Resources/Private/Assets/js/controllers/mobile_search_controller.js @@ -12,8 +12,8 @@ export default class extends Controller { ] static values = { - optionsUri: String, - initialOptionsUri: String, + uri: String, + initUri: String, loading: Boolean, loaded: Boolean, show: Boolean, @@ -48,7 +48,7 @@ export default class extends Controller { } load(options) { - let uri = this.loadedValue ? this.optionsUriValue : this.initialOptionsUriValue + let uri = this.loadedValue ? this.uriValue : this.initUriValue this.loadingValue = true fetch(uri, options) .then(this.checkStatus) diff --git a/public/typo3conf/ext/ep_theme/Resources/Private/Partials/Search/MobileSearch.html b/public/typo3conf/ext/ep_theme/Resources/Private/Partials/Search/MobileSearch.html index e0447da0..909cab43 100644 --- a/public/typo3conf/ext/ep_theme/Resources/Private/Partials/Search/MobileSearch.html +++ b/public/typo3conf/ext/ep_theme/Resources/Private/Partials/Search/MobileSearch.html @@ -4,8 +4,8 @@