1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Search; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2012-2015 Ingo Renner <[email protected]> |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query; |
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Sorting\SortingHelper; |
29
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest; |
30
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestAware; |
31
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Sorting search component |
35
|
|
|
* |
36
|
|
|
* TODO maybe merge ApacheSolrForTypo3\Solr\Sorting into ApacheSolrForTypo3\Solr\Search\SortingComponent |
37
|
|
|
* |
38
|
|
|
* @author Ingo Renner <[email protected]> |
39
|
|
|
*/ |
40
|
|
|
class SortingComponent extends AbstractComponent implements QueryAware, SearchRequestAware |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Solr query |
45
|
|
|
* |
46
|
|
|
* @var Query |
47
|
|
|
*/ |
48
|
|
|
protected $query; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var SearchRequest |
52
|
|
|
*/ |
53
|
|
|
protected $searchRequest; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initializes the search component. |
57
|
|
|
* |
58
|
|
|
* Sets the sorting query parameters |
59
|
|
|
*/ |
60
|
40 |
|
public function initializeSearchComponent() |
61
|
|
|
{ |
62
|
40 |
|
if (!empty($this->searchConfiguration['query.']['sortBy'])) { |
63
|
1 |
|
$this->query->addQueryParameter('sort', $this->searchConfiguration['query.']['sortBy']); |
64
|
|
|
} |
65
|
|
|
|
66
|
40 |
|
$arguments = $this->searchRequest->getArguments(); |
67
|
|
|
|
68
|
40 |
|
if (!empty($this->searchConfiguration['sorting']) && $this->hasValidSorting($arguments)) { |
69
|
1 |
|
$sortHelper = GeneralUtility::makeInstance(SortingHelper::class, $this->searchConfiguration['sorting.']['options.']); |
70
|
1 |
|
$sortField = $sortHelper->getSortFieldFromUrlParameter($arguments['sort']); |
71
|
1 |
|
$this->query->setSorting($sortField); |
72
|
|
|
} |
73
|
40 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Checks if the arguments array has a valid sorting. |
77
|
|
|
* |
78
|
|
|
* @param array $arguments |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
33 |
|
protected function hasValidSorting(array $arguments) |
82
|
|
|
{ |
83
|
33 |
|
return !empty($arguments['sort']) && preg_match('/^([a-z0-9_]+ (asc|desc)[, ]*)*([a-z0-9_]+ (asc|desc))+$/i', $arguments['sort']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Provides the extension component with an instance of the current query. |
88
|
|
|
* |
89
|
|
|
* @param Query $query Current query |
90
|
|
|
*/ |
91
|
40 |
|
public function setQuery(Query $query) |
92
|
|
|
{ |
93
|
40 |
|
$this->query = $query; |
94
|
40 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param SearchRequest $searchRequest |
98
|
|
|
*/ |
99
|
40 |
|
public function setSearchRequest(SearchRequest $searchRequest) |
100
|
|
|
{ |
101
|
40 |
|
$this->searchRequest = $searchRequest; |
102
|
40 |
|
} |
103
|
|
|
} |
104
|
|
|
|