forked from oat-sa/tao-core
/
class.Search.php
124 lines (113 loc) · 4.01 KB
/
class.Search.php
1
2
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2014-2020 (original work) Open Assessment Technologies SA;
*
*/
declare(strict_types=1);
use oat\generis\model\OntologyAwareTrait;
use oat\tao\model\http\HttpJsonResponseTrait;
use oat\tao\model\Lists\Business\Domain\ClassMetadataSearchRequest;
use oat\tao\model\search\index\OntologyIndexService;
use oat\tao\model\search\SearchProxy;
/**
* Controller for indexed searches
*
* @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php
* @package tao
* phpcs:disable Squiz.Classes.ValidClassName
*/
class tao_actions_Search extends tao_actions_CommonModule
{
use OntologyAwareTrait;
use HttpJsonResponseTrait;
/**
* Search parameters endpoints.
* The response provides parameters to create a datatable.
*/
public function searchParams(): void
{
$queryParams = $this->getPsrRequest()->getQueryParams();
$parsedBody = $this->getPsrRequest()->getParsedBody();
if (
!isset(
$parsedBody['structure'],
$parsedBody['query'],
$queryParams['rootNode'],
$parsedBody['parentNode']
)
) {
$this->setErrorJsonResponse('Request is missing required params');
}
$this->setSuccessJsonResponse([
'url' => _url('search'),
'params' => [
'query' => $parsedBody['query'],
'rootNode' => $queryParams['rootNode'],
'parentNode' => $parsedBody['parentNode'],
'structure' => $parsedBody['structure'],
],
'settings' => $this->getSearchProxy()->getSearchSettingsService()->getSettingsByClassMetadataSearchRequest(
(new ClassMetadataSearchRequest())->setStructure($parsedBody['structure'])
->setClassUri($parsedBody['parentNode'])
),
'filter' => [],
'result' => true
]);
}
/**
* Search results
* The search is paginated and initiated by the datatable component.
*/
public function search(): void
{
try {
$this->returnJson(
$this->getSearchProxy()->search(
$this->getPsrRequest()
)
);
} catch (Exception $exception) {
$this->setErrorJsonResponse(
$exception->getMessage()
);
}
}
public function getIndexes(): void
{
if ($this->hasRequestParameter('rootNode') === true) {
$rootNodeUri = $this->getRequestParameter('rootNode');
$indexes = OntologyIndexService::getIndexesByClass($this->getClass($rootNodeUri));
$json = [];
foreach ($indexes as $propertyUri => $index) {
foreach ($index as $i) {
$json[] = [
'identifier' => $i->getIdentifier(),
'fuzzyMatching' => $i->isFuzzyMatching(),
'propertyId' => $propertyUri,
];
}
}
$this->setSuccessJsonResponse($json, 200);
} else {
$this->returnJson("The 'rootNode' parameter is missing.", 500);
}
}
private function getSearchProxy(): SearchProxy
{
return $this->getServiceLocator()->get(SearchProxy::SERVICE_ID);
}
}