Exemplo n.º 1
0
 protected function getSortingLinks()
 {
     $sortHelper = GeneralUtility::makeInstance('Tx_Solr_Sorting', $this->configuration['search.']['sorting.']['options.']);
     $query = $this->search->getQuery();
     $queryLinkBuilder = GeneralUtility::makeInstance('Tx_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;
 }
Exemplo n.º 2
0
 /**
  * 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
     $userQuery = $this->parentPlugin->getRawUserQuery();
     if (!is_null($userQuery) && !$this->configuration['search.']['query.']['allowEmptyQuery'] && empty($userQuery)) {
         $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 = t3lib_div::getUserObj($classReference);
             if ($errorDetector instanceof Tx_Solr_ErrorDetector) {
                 if ($errorDetector instanceof Tx_Solr_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 Tx_Solr_ErrorDetector.', 1359156192);
             }
         }
     }
     return $errors;
 }
 /**
  * Return the number of results for a Solr query respecting the allowed sites
  *
  * @return int
  */
 public function getNumberOfResults()
 {
     $solrConfiguration = Tx_Solr_Util::getSolrConfiguration();
     $allowedSites = Tx_Solr_Util::resolveSiteHashAllowedSites(GeneralUtility::_GP('id'), $solrConfiguration['search.']['query.']['allowedSites']);
     $q = GeneralUtility::_GP('q');
     /** @var \Tx_Solr_Query $query */
     $query = GeneralUtility::makeInstance('\\Tx_Solr_Query', $q);
     $query->setFieldList(array('title', 'url', 'teaser', 'score'));
     $query->setUserAccessGroups(explode(',', $GLOBALS['TSFE']->gr_list));
     $query->setSiteHashFilter($allowedSites);
     $this->searcher->getSearch()->search($query);
     $response = $this->searcher->getSearch()->getResponse();
     if (1 > $response->response->numFound) {
         $query->useRawQueryString(true);
         $response = $this->searcher->getSearch()->search($query);
     }
     return (int) $response->response->numFound;
 }
Exemplo n.º 4
0
 /**
  * @return string
  */
 protected function getPageBrowserRange()
 {
     $label = '';
     $resultsFrom = $this->search->getResponseBody()->start + 1;
     $resultsTo = $resultsFrom + count($this->search->getResultDocuments()) - 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 = GeneralUtility::intExplode(',', $this->configuration['search.']['results.']['resultsPerPageSwitchOptions'], TRUE);
     $currentNumberOfResultsShown = $this->parentPlugin->getNumberOfResultsPerPage();
     $queryLinkBuilder = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Query\\LinkBuilder', $this->parentPlugin->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;
 }
Exemplo n.º 6
0
    /**
     * Adds the JavaScript necessary for some of the facetting 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-factingExpansionLabels', $expansionLabels);
        $javascriptManager->loadFile('faceting.limitExpansion');
    }
Exemplo n.º 7
0
 /**
  * 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('Tx_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('Tx_Solr_PiResults_ResultsCommand', $plugin);
         $commandVariables = $resultsCommand->execute();
         $suggestionResults = $plugin->renderCommand('results', $commandVariables);
     }
     return $suggestionResults;
 }
 /**
  * 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->configuration['search.']['frequentSearches.']['minSize'];
     $maximumSize = $this->configuration['search.']['frequentSearches.']['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' => Tx_Solr_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;
 }
Exemplo n.º 9
0
 /**
  * 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' => Tx_Solr_Template::escapeMarkers($keywords), 'parameters' => '&q=' . html_entity_decode($keywords, ENT_NOQUOTES, 'UTF-8'), 'pid' => $this->parentPlugin->getLinkTargetPageId());
     }
     return $lastSearches;
 }