SolrSearch.php 4.67 KB
Newer Older
Thomas Löffler's avatar
Thomas Löffler committed
1
<?php
Thomas Löffler's avatar
Thomas Löffler committed
2
declare(strict_types = 1);
Thomas Löffler's avatar
Thomas Löffler committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
namespace T3o\T3orgLayout\Middleware;

/*
 * This file is part of a TYPO3 extension.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use ApacheSolrForTypo3\Solr\ConnectionManager;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService;
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestBuilder;
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
use ApacheSolrForTypo3\Solr\Search;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\View\StandaloneView;
Thomas Löffler's avatar
Thomas Löffler committed
33
34
35

class SolrSearch implements MiddlewareInterface
{
36
37
38
39
40
41
42
    protected $allowedTypes = [
        'pages',
        'tx_news_domain_model_news',
        'tx_sfeventmgt_domain_model_event',
        'tx_terfe2_domain_model_extension'
    ];

Thomas Löffler's avatar
Thomas Löffler committed
43
44
45
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = $handler->handle($request);
46
47
        $queryParams = $request->getQueryParams();
        if (!$this->executeAjaxSearch($queryParams)) {
Thomas Löffler's avatar
Thomas Löffler committed
48
49
50
            return $response;
        }

51
52
53
54
55
        if ($queryParams['type']) {
            $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_solr.']['search.']['query.']['filter.']['typeFilter'] = 'type:' . $queryParams['type'];
        }
        if ($queryParams['additional']) {
            $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_solr.']['search.']['query.']['filter.']['additionalFilter'] = urldecode($queryParams['additional']);
Thomas Löffler's avatar
Thomas Löffler committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        }

        $typoscriptConfiguration = GeneralUtility::makeInstance(
            TypoScriptConfiguration::class,
            $GLOBALS['TSFE']->tmpl->setup,
            $request->getAttribute('site')->getRootpageId()
        );

        $searchRequestBuilder = new SearchRequestBuilder($typoscriptConfiguration);
        $queryParams = $request->getQueryParams();
        $queryParams['q'] .= '*';
        $searchRequest = $searchRequestBuilder->buildForSearch(
            $queryParams,
            $request->getAttribute('site')->getRootpageId(),
            $request->getAttribute('language')->getLanguageId()
        );
72
        $searchRequest->setResultsPerPage(5);
Thomas Löffler's avatar
Thomas Löffler committed
73
74
75
76
77
78
79
80
81
82
83
84
85

        try {
            $solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId(
                $request->getAttribute('site')->getRootpageId(),
                $request->getAttribute('language')->getLanguageId()
            );
            $search = GeneralUtility::makeInstance(Search::class, $solrConnection);
        } catch (NoSolrConnectionFoundException $e) {
            $response->getBody()->write('No Solr connection available.');

            return $response;
        }
        $searchResultSetService = GeneralUtility::makeInstance(SearchResultSetService::class, $typoscriptConfiguration, $search);
86
        $searchResultSetService->injectObjectManager(GeneralUtility::makeInstance(ObjectManager::class));
Thomas Löffler's avatar
Thomas Löffler committed
87
88
        $searchResultSet = $searchResultSetService->search($searchRequest);

89
        if ($queryParams['ajaxsearch'] === 'raw') {
Thomas Löffler's avatar
Thomas Löffler committed
90
91
92
93
94
95
96
97
98
            $results = [];
            foreach ($searchResultSet->getSearchResults()->getArrayCopy() as $result) {
                /** @var \ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\SearchResult $result */
                $results[] = $result->getFields();
            }

            return new JsonResponse($results);
        }

99
        $template = GeneralUtility::makeInstance(StandaloneView::class);
Thomas Löffler's avatar
Thomas Löffler committed
100
101
        $template->setTemplatePathAndFilename('EXT:t3org_layout/Resources/Private/Templates/Solr/SolrResults.html');
        $template->assign('documents', $searchResultSet->getSearchResults()->getArrayCopy());
102
        $template->assign('type', $request->getQueryParams()['type']);
Thomas Löffler's avatar
Thomas Löffler committed
103
104
105

        return new HtmlResponse($template->render());
    }
106
107
108
109
110
111
112
113

    protected function executeAjaxSearch(array $queryParams): bool
    {
        $paramsSet = isset($queryParams['ajaxsearch'], $queryParams['q'], $queryParams['type']);
        $correctType = in_array($queryParams['type'], $this->allowedTypes);

        return $paramsSet && $correctType;
    }
Thomas Löffler's avatar
Thomas Löffler committed
114
}