/** * Gets errors that may have been found with the user's query. * * @return array An array of errors, each error is an array itself and has a message and a code key. */ protected function getErrors() { $errors = array(); // detect empty user queries $resultService = $this->parentPlugin->getSearchResultSetService(); $searchWasTriggeredWithEmptyQuery = $resultService->getLastSearchWasExecutedWithEmptyQueryString(); if ($searchWasTriggeredWithEmptyQuery && !$this->configuration->getSearchQueryAllowEmptyQuery()) { $errors[] = array('message' => '###LLL:error_emptyQuery###', 'code' => 1300893669); } // hook to provide additional error messages if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['addSearchErrors'])) { foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['addSearchErrors'] as $classReference) { $errorDetector = GeneralUtility::getUserObj($classReference); if ($errorDetector instanceof ErrorDetector) { if ($errorDetector instanceof CommandPluginAware) { $errorDetector->setParentPlugin($this->parentPlugin); } $additionalErrors = $errorDetector->getErrors(); if (is_array($additionalErrors)) { $errors = array_merge($errors, $additionalErrors); } else { throw new \UnexpectedValueException($classReference . ' must return an array', 1359156111); } } else { throw new \InvalidArgumentException('Error detector "' . $classReference . '" must implement interface ApacheSolrForTypo3\\Solr\\Plugin\\Results\\ErrorDetector.', 1359156192); } } } return $errors; }
protected function getSortingLinks() { $sortHelper = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Sorting', $this->configuration['search.']['sorting.']['options.']); $query = $this->search->getQuery(); $queryLinkBuilder = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query\\LinkBuilder', $query); $queryLinkBuilder->setLinkTargetPageId($this->parentPlugin->getLinkTargetPageId()); $sortOptions = array(); $urlParameters = GeneralUtility::_GP('tx_solr'); $urlSortParameters = GeneralUtility::trimExplode(',', $urlParameters['sort']); $configuredSortOptions = $sortHelper->getSortOptions(); foreach ($configuredSortOptions as $sortOptionName => $sortOption) { $sortDirection = $this->configuration['search.']['sorting.']['defaultOrder']; if (!empty($this->configuration['search.']['sorting.']['options.'][$sortOptionName . '.']['defaultOrder'])) { $sortDirection = $this->configuration['search.']['sorting.']['options.'][$sortOptionName . '.']['defaultOrder']; } $sortIndicator = $sortDirection; $currentSortOption = ''; $currentSortDirection = ''; foreach ($urlSortParameters as $urlSortParameter) { $explodedUrlSortParameter = explode(' ', $urlSortParameter); if ($explodedUrlSortParameter[0] == $sortOptionName) { list($currentSortOption, $currentSortDirection) = $explodedUrlSortParameter; break; } } // toggle sorting direction for the current sorting field if ($currentSortOption == $sortOptionName) { switch ($currentSortDirection) { case 'asc': $sortDirection = 'desc'; $sortIndicator = 'asc'; break; case 'desc': $sortDirection = 'asc'; $sortIndicator = 'desc'; break; } } if (!empty($this->configuration['search.']['sorting.']['options.'][$sortOptionName . '.']['fixedOrder'])) { $sortDirection = $this->configuration['search.']['sorting.']['options.'][$sortOptionName . '.']['fixedOrder']; } $sortParameter = $sortOptionName . ' ' . $sortDirection; $temp = array('link' => $queryLinkBuilder->getQueryLink($sortOption['label'], array('sort' => $sortParameter)), 'url' => $queryLinkBuilder->getQueryUrl(array('sort' => $sortParameter)), 'optionName' => $sortOptionName, 'field' => $sortOption['field'], 'label' => $sortOption['label'], 'is_current' => '0', 'direction' => $sortDirection, 'indicator' => $sortIndicator, 'current_direction' => ' '); // set sort indicator for the current sorting field if ($currentSortOption == $sortOptionName) { $temp['selected'] = 'selected="selected"'; $temp['current'] = 'current'; $temp['is_current'] = '1'; $temp['current_direction'] = $sortIndicator; } // special case relevance: just reset the search to normal behavior if ($sortOptionName == 'relevance') { $temp['link'] = $queryLinkBuilder->getQueryLink($sortOption['label'], array('sort' => NULL)); $temp['url'] = $queryLinkBuilder->getQueryUrl(array('sort' => NULL)); unset($temp['direction'], $temp['indicator']); } $sortOptions[] = $temp; } return $sortOptions; }
/** * @return string */ protected function getPageBrowserRange() { $label = ''; $resultsFrom = $this->search->getResponseBody()->start + 1; $resultsTo = $resultsFrom + count($this->search->getResultDocumentsEscaped()) - 1; $resultsTotal = $this->search->getNumberOfResults(); $label = strtr($this->parentPlugin->pi_getLL('results_range'), array('@resultsFrom' => $resultsFrom, '@resultsTo' => $resultsTo, '@resultsTotal' => $resultsTotal)); return $label; }
/** * Generates the options for the results per page switch. * * @return array Array of results per page switch options. */ public function getResultsPerPageOptions() { $resultsPerPageOptions = array(); $resultsPerPageSwitchOptions = $this->configuration->getSearchResultsPerPageSwitchOptionsAsArray(); $currentNumberOfResultsShown = $this->parentPlugin->getSearchResultSetService()->getLastResultSet()->getResultsPerPage(); $queryLinkBuilder = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query\\LinkBuilder', $this->parentPlugin->getSearchResultSetService()->getSearch()->getQuery()); $queryLinkBuilder->removeUnwantedUrlParameter('resultsPerPage'); $queryLinkBuilder->setLinkTargetPageId($this->parentPlugin->getLinkTargetPageId()); foreach ($resultsPerPageSwitchOptions as $option) { $selected = ''; $selectedClass = ''; if ($option == $currentNumberOfResultsShown) { $selected = ' selected="selected"'; $selectedClass = ' class="currentNumberOfResults"'; } $resultsPerPageOptions[] = array('value' => $option, 'selected' => $selected, 'selectedClass' => $selectedClass, 'url' => $queryLinkBuilder->getQueryUrl(array('resultsPerPage' => $option))); } return $resultsPerPageOptions; }
/** * Adds the JavaScript necessary for some of the faceting features; * folding/unfolding a list of facet options that exceed the configured * limit of visible options * * @return void */ protected function addFacetingJavascript() { $javascriptManager = $this->parentPlugin->getJavascriptManager(); $expansionLabels = ' var tx_solr_facetLabels = { \'showMore\' : \'' . $this->parentPlugin->pi_getLL('faceting_showMore') . '\', \'showFewer\' : \'' . $this->parentPlugin->pi_getLL('faceting_showFewer') . '\' }; '; $javascriptManager->addJavascript('tx_solr-facetingExpansionLabels', $expansionLabels); $javascriptManager->loadFile('faceting.limitExpansion'); }
/** * Prepares the content for the last search markers * * @return array An array with content for the last search markers */ protected function getLastSearches() { /** @var $lastSearchesService \ApacheSolrForTypo3\Solr\Domain\Search\LastSearches\LastSearchesService */ $lastSearchesService = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Domain\\Search\\LastSearches\\LastSearchesService', $this->configuration, $GLOBALS['TSFE'], $GLOBALS['TYPO3_DB']); // fill array for output $lastSearches = array(); $lastSearchesKeywords = $lastSearchesService->getLastSearches(); foreach ($lastSearchesKeywords as $keywords) { $keywords = stripslashes($keywords); $lastSearches[] = array('q' => Template::escapeMarkers($keywords), 'parameters' => '&q=' . html_entity_decode($keywords, ENT_NOQUOTES, 'UTF-8'), 'pid' => $this->parentPlugin->getLinkTargetPageId()); } return $lastSearches; }
/** * Builds the properties for the frequent search term markers. * * @param array $frequentSearchTerms Frequent search terms as array with terms as keys and hits as the value * @return array An array with content for the frequent terms markers */ protected function getSearchTermMarkerProperties(array $frequentSearchTerms) { $frequentSearches = array(); $minimumSize = $this->frequentSearchConfiguration['minSize']; $maximumSize = $this->frequentSearchConfiguration['maxSize']; 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[] = array('term' => Template::escapeMarkers($term), 'hits' => $hits, 'style' => 'font-size: ' . $size . 'px', 'class' => 'tx-solr-frequent-term-' . $size, 'parameters' => '&q=' . html_entity_decode($term, ENT_NOQUOTES, 'UTF-8'), 'pid' => $this->parentPlugin->getLinkTargetPageId()); } } return $frequentSearches; }
/** * Gets the results for the suggested keywords. * * Conducts a new search using the suggested keywords and uses that search * to render the regular results command. * * @return string The rendered results command for the results of the suggested keywords. */ protected function getSuggestionResults() { $spellChecker = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\SpellChecker'); $suggestedKeywords = $spellChecker->getCollatedSuggestion(); $suggestionResults = ''; if (!empty($suggestedKeywords)) { $plugin = $this->parentPlugin; $search = $this->parentPlugin->getSearch(); $query = clone $search->getQuery(); $query->setKeywords($suggestedKeywords); $search->search($query); $resultsCommand = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Plugin\\Results\\ResultsCommand', $plugin); $commandVariables = $resultsCommand->execute(); $suggestionResults = $plugin->renderCommand('results', $commandVariables); } return $suggestionResults; }
/** * Gets the results for the suggested keywords. * * Conducts a new search using the suggested keywords and uses that search * to render the regular results command. * * @return string The rendered results command for the results of the suggested keywords. */ protected function getSuggestionResults() { $spellChecker = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\SpellChecker'); $suggestedKeywords = $spellChecker->getCollatedSuggestion(); $suggestionResults = ''; if (empty($suggestedKeywords)) { return $suggestionResults; } $plugin = $this->parentPlugin; $searchResultSetService = $this->parentPlugin->getSearchResultSetService(); // repeat the last search with suggestion as query string $usedRequest = $searchResultSetService->getLastResultSet()->getUsedSearchRequest(); $usedRequest->setRawQueryString($suggestedKeywords); $searchResultSetService->search($usedRequest); $resultsCommand = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Plugin\\Results\\ResultsCommand', $plugin); $commandVariables = $resultsCommand->execute(); $suggestionResults = $plugin->renderCommand('results', $commandVariables); return $suggestionResults; }
/** * Prepares the content for the last search markers * * @return array An array with content for the last search markers */ protected function getLastSearches() { $lastSearchesKeywords = array(); switch ($this->configuration['search.']['lastSearches.']['mode']) { case 'user': $lastSearchesKeywords = $this->getLastSearchesFromSession(); break; case 'global': $lastSearchesKeywords = $this->getLastSearchesFromDatabase($this->configuration['search.']['lastSearches.']['limit']); break; } // fill array for output $i = 0; $lastSearches = array(); foreach ($lastSearchesKeywords as $keywords) { if (++$i > $this->configuration['search.']['lastSearches.']['limit']) { break; } $keywords = stripslashes($keywords); $lastSearches[] = array('q' => Template::escapeMarkers($keywords), 'parameters' => '&q=' . html_entity_decode($keywords, ENT_NOQUOTES, 'UTF-8'), 'pid' => $this->parentPlugin->getLinkTargetPageId()); } return $lastSearches; }