Commit 9830fd83 authored by Thomas Löffler's avatar Thomas Löffler
Browse files

[TASK] Change delay ms, check type parameter and add additional filter

parent 121ce1d9
Pipeline #20139 failed with stages
in 1 minute and 46 seconds
......@@ -30,18 +30,32 @@ use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\View\StandaloneView;
class SolrSearch implements MiddlewareInterface
{
protected $allowedTypes = [
'pages',
'tx_news_domain_model_news',
'tx_sfeventmgt_domain_model_event',
'tx_terfe2_domain_model_extension'
];
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
if (!isset($request->getQueryParams()['ajaxsearch'], $request->getQueryParams()['q'])) {
$queryParams = $request->getQueryParams();
if (!$this->executeAjaxSearch($queryParams)) {
return $response;
}
if ($request->getQueryParams()['type']) {
$GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_solr.']['search.']['query.']['filter.']['typeFilter'] = 'type:' . $request->getQueryParams()['type'];
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']);
}
$typoscriptConfiguration = GeneralUtility::makeInstance(
......@@ -72,10 +86,10 @@ class SolrSearch implements MiddlewareInterface
return $response;
}
$searchResultSetService = GeneralUtility::makeInstance(SearchResultSetService::class, $typoscriptConfiguration, $search);
$searchResultSetService->injectObjectManager(GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class));
$searchResultSetService->injectObjectManager(GeneralUtility::makeInstance(ObjectManager::class));
$searchResultSet = $searchResultSetService->search($searchRequest);
if ($request->getQueryParams()['ajaxsearch'] === 'raw') {
if ($queryParams['ajaxsearch'] === 'raw') {
$results = [];
foreach ($searchResultSet->getSearchResults()->getArrayCopy() as $result) {
/** @var \ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\SearchResult $result */
......@@ -85,11 +99,19 @@ class SolrSearch implements MiddlewareInterface
return new JsonResponse($results);
}
$template = GeneralUtility::makeInstance(\TYPO3\CMS\Fluid\View\StandaloneView::class);
$template = GeneralUtility::makeInstance(StandaloneView::class);
$template->setTemplatePathAndFilename('EXT:t3org_layout/Resources/Private/Templates/Solr/SolrResults.html');
$template->assign('documents', $searchResultSet->getSearchResults()->getArrayCopy());
$template->assign('type', $request->getQueryParams()['type']);
return new HtmlResponse($template->render());
}
protected function executeAjaxSearch(array $queryParams): bool
{
$paramsSet = isset($queryParams['ajaxsearch'], $queryParams['q'], $queryParams['type']);
$correctType = in_array($queryParams['type'], $this->allowedTypes);
return $paramsSet && $correctType;
}
}
......@@ -2,24 +2,24 @@
<f:render section="{type}" arguments="{_all}" />
<f:section name="pages">
<h3>Top 5 pages</h3>
<h3>Pages</h3>
<f:if condition="{documents}">
<f:then>
<f:for each="{documents}" as="document">
<a class="small" href="{document.fields.url}">
<p class="mb-3">
{document.fields.title} &mdash; {document.fields.content -> f:format.crop(maxCharacters: 40)}
<f:format.crop maxCharacters="65">{document.fields.title} &mdash; {document.fields.content}</f:format.crop>
</p>
</a>
</f:for>
</f:then>
<f:else>
No results found.
<f:render section="nothingfound" />
</f:else>
</f:if>
</f:section>
<f:section name="tx_news_domain_model_news">
<h3>Top 5 news</h3>
<h3>News</h3>
<f:if condition="{documents}">
<f:then>
<f:for each="{documents}" as="document">
......@@ -31,12 +31,12 @@
</f:for>
</f:then>
<f:else>
No results found.
<f:render section="nothingfound" />
</f:else>
</f:if>
</f:section>
<f:section name="tx_sfeventmgt_domain_model_event">
<h3>Top 5 events</h3>
<h3>Events</h3>
<f:if condition="{documents}">
<f:then>
<f:for each="{documents}" as="document">
......@@ -48,12 +48,12 @@
</f:for>
</f:then>
<f:else>
No results found.
<f:render section="nothingfound" />
</f:else>
</f:if>
</f:section>
<f:section name="tx_terfe2_domain_model_extension">
<h3>Top 5 extensions</h3>
<h3>Extensions</h3>
<f:if condition="{documents}">
<f:then>
<f:for each="{documents}" as="document">
......@@ -65,8 +65,14 @@
</f:for>
</f:then>
<f:else>
No results found.
<f:render section="nothingfound" />
</f:else>
</f:if>
</f:section>
<f:section name="nothingfound">
<p>
<small>Nothing found.</small>
</p>
</f:section>
</html>
......@@ -47,7 +47,9 @@ function doSearch(_searchQuery) {
data: {
"q": _searchQuery,
"ajaxsearch": 1,
"type": "tx_sfeventmgt_domain_model_event"
"type": "tx_sfeventmgt_domain_model_event",
"additional": encodeURI("enddate_dateS:[NOW TO *]"),
"no_cache": 1
},
success: function (response) {
$('.results-list-insert-events').html(response);
......@@ -115,5 +117,5 @@ $(document).ready(function () {
$('.results-list-insert-events').html('');
$('.results-list-insert-extensions').html('');
}
}, 500));
}, 300));
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment