* @author Timo Hund */ class SearchController extends AbstractBaseController { /** * @var TemplateView */ protected $view; /** * Provide search query in extbase arguments. */ protected function initializeAction() { parent::initializeAction(); $this->mapGlobalQueryStringWhenEnabled(); } /** * @return void */ protected function mapGlobalQueryStringWhenEnabled() { $query = GeneralUtility::_GET('q'); $useGlobalQueryString = $query !== null && !$this->typoScriptConfiguration->getSearchIgnoreGlobalQParameter(); if ($useGlobalQueryString) { $this->request->setArgument('q', $query); } } /** * @param ViewInterface $view */ public function initializeView(ViewInterface $view) { if($view instanceof TemplateView) { $customTemplate = $this->getCustomTemplateFromConfiguration(); if($customTemplate === '') { return; } if(strpos($customTemplate, 'EXT:') !== false) { $view->setTemplatePathAndFilename($customTemplate); } else { $view->setTemplate($customTemplate); } } } /** * @return string */ protected function getCustomTemplateFromConfiguration() { $templateKey = str_replace('Action', '', $this->actionMethodName); $customTemplate = $this->typoScriptConfiguration->getViewTemplateByFileKey($templateKey); return $customTemplate; } /** * Results */ public function resultsAction() { try { $arguments = (array)$this->request->getArguments(); $pageId = $this->typoScriptFrontendController->getRequestedId(); $languageId = Util::getLanguageUid(); $searchRequest = $this->getSearchRequestBuilder()->buildForSearch($arguments, $pageId, $languageId); $searchResultSet = $this->searchService->search($searchRequest); // we pass the search result set to the controller context, to have the possibility // to access it without passing it from partial to partial $this->controllerContext->setSearchResultSet($searchResultSet); $values = [ 'additionalFilters' => $this->getAdditionalFilters(), 'resultSet' => $searchResultSet, 'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace(), 'arguments' => $arguments ]; $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]); $this->view->assignMultiple($values); } catch (MeilisearchUnavailableException $e) { $this->handleMeilisearchUnavailable(); } } /** * Form */ public function formAction() { $values = [ 'search' => $this->searchService->getSearch(), 'additionalFilters' => $this->getAdditionalFilters(), 'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace() ]; $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]); $this->view->assignMultiple($values); } /** * Frequently Searched */ public function frequentlySearchedAction() { /** @var $searchResultSet SearchResultSet */ $searchResultSet = GeneralUtility::makeInstance(SearchResultSet::class); $pageId = $this->typoScriptFrontendController->getRequestedId(); $languageId = Util::getLanguageUid(); $searchRequest = $this->getSearchRequestBuilder()->buildForFrequentSearches($pageId, $languageId); $searchResultSet->setUsedSearchRequest($searchRequest); $this->controllerContext->setSearchResultSet($searchResultSet); $values = [ 'additionalFilters' => $this->getAdditionalFilters(), 'resultSet' => $searchResultSet ]; $values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]); $this->view->assignMultiple($values); } /** * This action allows to render a detailView with data from meilisearch. * * @param string $documentId */ public function detailAction($documentId = '') { try { $document = $this->searchService->getDocumentById($documentId); $this->view->assign('document', $document); } catch (MeilisearchUnavailableException $e) { $this->handleMeilisearchUnavailable(); } } /** * Rendered when no search is available. * @return string */ public function meilisearchNotAvailableAction() { if ($this->response instanceof Response) { $this->response->setStatus(503); } } /** * Called when the meilisearch server is unavailable. * * @return void */ protected function handleMeilisearchUnavailable() { parent::handleMeilisearchUnavailable(); $this->forward('meilisearchNotAvailable'); } /** * This method can be overwritten to add additionalFilters for the autosuggest. * By default the suggest controller will apply the configured filters from the typoscript configuration. * * @return array */ protected function getAdditionalFilters() { return []; } }