From 7c68aa61a565a33ba7e3f5fd27c9fc77589e2883 Mon Sep 17 00:00:00 2001 From: Alexander Kellner Date: Sat, 24 Feb 2024 12:44:50 +0100 Subject: [PATCH] [FEATURE] Update of Analysis/Search/Detail view --- Classes/Backend/Units/AbstractUnit.php | 10 ++-- Classes/Controller/AbstractController.php | 11 +--- Classes/Controller/AnalysisController.php | 12 ++-- Classes/Domain/Model/Transfer/FilterDto.php | 35 ++++++++++-- .../Domain/Repository/SearchRepository.php | 19 +++++++ Classes/Utility/BackendUtility.php | 40 ++++++++----- Classes/Utility/StringUtility.php | 2 +- .../Analysis/SearchDetailPageAjax.html | 57 ++++++++++--------- 8 files changed, 123 insertions(+), 63 deletions(-) diff --git a/Classes/Backend/Units/AbstractUnit.php b/Classes/Backend/Units/AbstractUnit.php index 33d51c7b..991624d5 100644 --- a/Classes/Backend/Units/AbstractUnit.php +++ b/Classes/Backend/Units/AbstractUnit.php @@ -10,7 +10,6 @@ use In2code\Lux\Utility\ObjectUtility; use In2code\Lux\Utility\StringUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Extbase\Property\PropertyMapper; use TYPO3\CMS\Fluid\View\StandaloneView; abstract class AbstractUnit @@ -71,10 +70,11 @@ protected function initializeFilter(): void { $filter = ObjectUtility::getFilterDto(); if ($this->filterClass !== '' && $this->filterFunction !== '') { - $filterArray = BackendUtility::getSessionValue('filter', $this->filterFunction, $this->filterClass); - $filterArray = array_merge(['timePeriod' => FilterDto::PERIOD_LAST3MONTH], $filterArray); - $propertyMapper = GeneralUtility::makeInstance(PropertyMapper::class); - $filter = $propertyMapper->convert($filterArray, FilterDto::class); + $filter = BackendUtility::getFilterFromSession( + $this->filterFunction, + $this->filterClass, + ['timePeriod' => FilterDto::PERIOD_LAST3MONTH] + ); } $this->filter = $filter; } diff --git a/Classes/Controller/AbstractController.php b/Classes/Controller/AbstractController.php index 0fd8e33c..0fa40c46 100644 --- a/Classes/Controller/AbstractController.php +++ b/Classes/Controller/AbstractController.php @@ -148,21 +148,12 @@ protected function setFilter(int $timePeriod = FilterDto::PERIOD_DEFAULT): void // Save to session if ($this->request->hasArgument('filter') === false) { - $filter = BackendUtility::getSessionValue('filter', $this->getActionName(), $this->getControllerName()); - $filter = array_merge(['timePeriod' => $timePeriod], $filter); + $filter = BackendUtility::getFilterArrayFromSession($this->getActionName(), $this->getControllerName()); } else { $filter = (array)$this->request->getArgument('filter'); BackendUtility::saveValueToSession('filter', $this->getActionName(), $this->getControllerName(), $filter); } - if (array_key_exists('categoryScoring', $filter) - && (is_array($filter['categoryScoring']) || $filter['categoryScoring'] === '')) { - $filter['categoryScoring'] = 0; - } - if (array_key_exists('branchCode', $filter) - && (is_array($filter['branchCode']) || $filter['branchCode'] === '')) { - $filter['branchCode'] = 0; - } if (isset($filter['identified']) && $filter['identified'] === '') { $filter['identified'] = FilterDto::IDENTIFIED_ALL; } diff --git a/Classes/Controller/AnalysisController.php b/Classes/Controller/AnalysisController.php index 157bb09a..d65a28d6 100644 --- a/Classes/Controller/AnalysisController.php +++ b/Classes/Controller/AnalysisController.php @@ -25,6 +25,7 @@ use In2code\Lux\Domain\Model\News; use In2code\Lux\Domain\Model\Page; use In2code\Lux\Domain\Model\Transfer\FilterDto; +use In2code\Lux\Utility\BackendUtility; use In2code\Lux\Utility\FileUtility; use In2code\Lux\Utility\LocalizationUtility; use In2code\Lux\Utility\ObjectUtility; @@ -401,7 +402,7 @@ public function detailSearchAction(string $searchterm): ResponseInterface $this->view->assignMultiple([ 'searchterm' => $searchterm, 'searchData' => GeneralUtility::makeInstance(SearchDataProvider::class, $filter), - 'searches' => $this->searchRepository->findBySearchterm(urldecode($searchterm)), + 'searches' => $this->searchRepository->findBySearchterm($filter), ]); $this->addDocumentHeaderForCurrentController(); @@ -494,10 +495,13 @@ public function detailSearchAjaxPage(ServerRequestInterface $request): ResponseI 'EXT:lux/Resources/Private/Templates/Analysis/SearchDetailPageAjax.html' )); $standaloneView->setPartialRootPaths(['EXT:lux/Resources/Private/Partials/']); + $filter = BackendUtility::getFilterFromSession( + 'search', + 'Analysis', + ['searchterm' => urldecode($request->getQueryParams()['searchterm']), 'limit' => 10] + ); $standaloneView->assignMultiple([ - 'searches' => $this->searchRepository->findBySearchterm( - urldecode($request->getQueryParams()['searchterm']) - ), + 'searches' => $this->searchRepository->findBySearchterm($filter), 'searchterm' => $request->getQueryParams()['searchterm'], ]); $response = GeneralUtility::makeInstance(JsonResponse::class); diff --git a/Classes/Domain/Model/Transfer/FilterDto.php b/Classes/Domain/Model/Transfer/FilterDto.php index 8dcf57ea..a014e9bb 100644 --- a/Classes/Domain/Model/Transfer/FilterDto.php +++ b/Classes/Domain/Model/Transfer/FilterDto.php @@ -52,6 +52,7 @@ class FilterDto */ protected string $timeTo = ''; + protected int $limit = 0; protected int $scoring = 0; protected int $timePeriod = self::PERIOD_DEFAULT; protected int $identified = self::IDENTIFIED_ALL; @@ -94,6 +95,16 @@ public function getSearchterm(): string return StringUtility::sanitizeString($this->searchterm); } + /** + * Without sanitize function + * + * @return string + */ + public function getSearchtermRaw(): string + { + return $this->searchterm; + } + public function isSearchtermSet(): bool { return $this->getSearchterm() !== ''; @@ -261,9 +272,9 @@ public function isCategoryScoringSet(): bool return $this->getCategoryScoring() !== null; } - public function setCategoryScoring(int $categoryUid): self + public function setCategoryScoring(?int $categoryUid): self { - if ($categoryUid > 0) { + if ((int)$categoryUid > 0) { $categoryRepository = GeneralUtility::makeInstance(CategoryRepository::class); $category = $categoryRepository->findByUid((int)$categoryUid); if ($category !== null) { @@ -416,9 +427,9 @@ public function isBranchCodeSet(): bool return $this->getBranchCode() > 0; } - public function setBranchCode(int $branchCode): self + public function setBranchCode(?int $branchCode): self { - $this->branchCode = $branchCode; + $this->branchCode = (int)$branchCode; return $this; } @@ -486,6 +497,22 @@ public function setCompany(?Company $company): self return $this; } + public function getLimit(): int + { + return $this->limit; + } + + public function isLimitSet(): bool + { + return $this->getLimit() > 0; + } + + public function setLimit(int $limit): self + { + $this->limit = $limit; + return $this; + } + /** * Calculated values from here */ diff --git a/Classes/Domain/Repository/SearchRepository.php b/Classes/Domain/Repository/SearchRepository.php index 9670a50b..28f125fc 100644 --- a/Classes/Domain/Repository/SearchRepository.php +++ b/Classes/Domain/Repository/SearchRepository.php @@ -14,6 +14,7 @@ use In2code\Lux\Utility\DatabaseUtility; use TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException; use TYPO3\CMS\Extbase\Persistence\QueryInterface; +use TYPO3\CMS\Extbase\Persistence\QueryResultInterface; class SearchRepository extends AbstractRepository { @@ -45,6 +46,24 @@ public function findCombinedBySearchIdentifier(FilterDto $filter): array return $connection->executeQuery($sql)->fetchAllAssociative(); } + public function findBySearchterm(FilterDto $filter): QueryResultInterface + { + $sql = 'select s.* from ' . Search::TABLE_NAME . ' s' + . ' left join ' . Pagevisit::TABLE_NAME . ' pv on s.pagevisit = pv.uid' + . ' left join ' . Visitor::TABLE_NAME . ' v on s.visitor = v.uid' + . ' left join ' . Categoryscoring::TABLE_NAME . ' cs on cs.visitor = v.uid' + . ' where s.searchterm = "' . $filter->getSearchterm() . '"' + . $this->extendWhereClauseWithFilterTime($filter, true, 's') + . $this->extendWhereClauseWithFilterScoring($filter, 'v') + . $this->extendWhereClauseWithFilterCategoryScoring($filter, 'cs') + . $this->extendWhereClauseWithFilterSite($filter, 'pv') + . ' order by s.crdate desc' + . ' limit ' . ($filter->isLimitSet() ? $filter->getLimit() : 750); + $query = $this->createQuery(); + $query = $query->statement($sql); + return $query->execute(); + } + /** * Todo: This function can be removed (without replacement) if there are some values in * tx_lux_domain_model_search.pagevisit (introduced with version 35.0.0) diff --git a/Classes/Utility/BackendUtility.php b/Classes/Utility/BackendUtility.php index 9470b087..bd19fbfd 100644 --- a/Classes/Utility/BackendUtility.php +++ b/Classes/Utility/BackendUtility.php @@ -3,7 +3,11 @@ declare(strict_types=1); namespace In2code\Lux\Utility; +use In2code\Lux\Domain\Model\Transfer\FilterDto; +use Throwable; use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Property\PropertyMapper; class BackendUtility { @@ -32,19 +36,33 @@ public static function isAdministrator(): bool return false; } - /** - * @param string $key - * @param string $action - * @param string $controller - * @param array $data - * @return void - * @codeCoverageIgnore - */ - public static function saveValueToSession(string $key, string $action, string $controller, array $data) + public static function saveValueToSession(string $key, string $action, string $controller, array $data): void { self::getBackendUserAuthentication()->setAndSaveSessionData($key . $action . $controller . '_lux', $data); } + public static function getFilterFromSession( + string $actionName, + string $controllerName, + array $propertyOverlay = [] + ): ?FilterDto { + $filterArray = self::getFilterArrayFromSession($actionName, $controllerName, $propertyOverlay); + try { + return GeneralUtility::makeInstance(PropertyMapper::class)->convert($filterArray, FilterDto::class); + } catch (Throwable $exception) { + return null; + } + } + + public static function getFilterArrayFromSession( + string $actionName, + string $controllerName, + array $propertyOverlay = [] + ): array { + $filter = BackendUtility::getSessionValue('filter', $actionName, $controllerName); + return array_merge($filter, $propertyOverlay); + } + public static function getSessionValue(string $key, string $action, string $controller): array { $value = self::getBackendUserAuthentication()->getSessionData($key . $action . $controller . '_lux'); @@ -54,10 +72,6 @@ public static function getSessionValue(string $key, string $action, string $cont return []; } - /** - * @return ?BackendUserAuthentication - * @SuppressWarnings(PHPMD.Superglobals) - */ public static function getBackendUserAuthentication(): ?BackendUserAuthentication { return $GLOBALS['BE_USER'] ?? null; diff --git a/Classes/Utility/StringUtility.php b/Classes/Utility/StringUtility.php index 121e5046..61e39614 100644 --- a/Classes/Utility/StringUtility.php +++ b/Classes/Utility/StringUtility.php @@ -71,7 +71,7 @@ public static function cleanString(string $string, bool $toLower = false, string */ public static function sanitizeString(string $string): string { - return preg_replace('/[\/\\*#?$%&!=\'"`´<>{}\[\]()]|--/', '', $string); + return preg_replace('/[\/\\#?$%&!=\'"`´<>{}\[\]()]|--/', '', $string); } public static function getRandomString(int $length = 32, bool $lowerAndUpperCase = true): string diff --git a/Resources/Private/Templates/Analysis/SearchDetailPageAjax.html b/Resources/Private/Templates/Analysis/SearchDetailPageAjax.html index ff7b4020..bd1c0c1c 100644 --- a/Resources/Private/Templates/Analysis/SearchDetailPageAjax.html +++ b/Resources/Private/Templates/Analysis/SearchDetailPageAjax.html @@ -7,34 +7,39 @@

- - - - - - - - - - - - - - - - + +
SearchtermLeadCompanyTime
{search.searchterm} - {search.visitor.fullName} - {search.visitor.company} - - {search.crdate} - -
+ + + + + + - - -
SearchtermLeadCompanyTime
+ + + + + {search.searchterm} + + {search.visitor.fullName} + + {search.visitor.company} + + + {search.crdate} + + + + + + - + + + + +