* @author Timo Hund */ class FrequentlySearchedController extends AbstractWidgetController implements LoggerAwareInterface { use LoggerAwareTrait; /** * Initializes the cache for this command. * * @return FrontendInterface|null */ protected function getInitializedCache(): ?FrontendInterface { $cacheIdentifier = 'tx_meilisearch'; /* @var FrontendInterface $cacheInstance */ try { $cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache($cacheIdentifier); } catch (NoSuchCacheException $exception) { $this->logger->error('Getting cache failed: ' . $exception->getMessage()); return null; } return $cacheInstance; } /** * Last searches */ public function indexAction() { $tsfe = $GLOBALS['TSFE']; $cache = $this->getInitializedCache(); $configuration = $this->controllerContext->getTypoScriptConfiguration(); /* @var FrequentSearchesService $frequentSearchesService */ $frequentSearchesService = GeneralUtility::makeInstance( FrequentSearchesService::class, /** @scrutinizer ignore-type */ $configuration, /** @scrutinizer ignore-type */ $cache, /** @scrutinizer ignore-type */ $tsfe ); $frequentSearches = $frequentSearchesService->getFrequentSearchTerms(); $minimumSize = $configuration->getSearchFrequentSearchesMinSize(); $maximumSize = $configuration->getSearchFrequentSearchesMaxSize(); $this->view->assign( 'contentArguments', [ 'frequentSearches' => $this->enrichFrequentSearchesInfo($frequentSearches, $minimumSize, $maximumSize) ] ); } /** * Enrich the frequentSearches * * @param array Frequent search terms as array with terms as keys and hits as the value * @param int $minimumSize * @param int $maximumSize * @return array An array with content for the frequent terms markers */ protected function enrichFrequentSearchesInfo(array $frequentSearchTerms, int $minimumSize, int $maximumSize): array { $frequentSearches = []; if (count($frequentSearchTerms)) { $maximumHits = max(array_values($frequentSearchTerms)); $minimumHits = min(array_values($frequentSearchTerms)); $spread = $maximumHits - $minimumHits; $step = ($spread == 0) ? 1 : ($maximumSize - $minimumSize) / $spread; foreach ($frequentSearchTerms as $term => $hits) { $size = round($minimumSize + (($hits - $minimumHits) * $step)); $frequentSearches[] = [ 'q' => htmlspecialchars_decode($term), 'hits' => $hits, 'style' => 'font-size: ' . $size . 'px', 'class' => 'tx-meilisearch-frequent-term-' . $size, 'size' => $size ]; } } return $frequentSearches; } }