1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
6
|
|
|
* |
7
|
|
|
* @license GNU General Public License version 3 or later. |
8
|
|
|
* For the full copyright and license information, please read the |
9
|
|
|
* LICENSE.txt file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Kitodo\Dlf\Controller; |
13
|
|
|
|
14
|
|
|
use Kitodo\Dlf\Common\SolrPaginator; |
15
|
|
|
use Kitodo\Dlf\Common\Solr\Solr; |
16
|
|
|
use Kitodo\Dlf\Domain\Model\Collection; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use TYPO3\CMS\Core\Pagination\SimplePagination; |
19
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
20
|
|
|
use TYPO3\CMS\Core\Utility\MathUtility; |
21
|
|
|
use Kitodo\Dlf\Domain\Repository\CollectionRepository; |
22
|
|
|
use Kitodo\Dlf\Domain\Repository\MetadataRepository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Controller class for the plugin 'Collection'. |
26
|
|
|
* |
27
|
|
|
* @package TYPO3 |
28
|
|
|
* @subpackage dlf |
29
|
|
|
* |
30
|
|
|
* @access public |
31
|
|
|
*/ |
32
|
|
|
class CollectionController extends AbstractController |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @access protected |
36
|
|
|
* @var CollectionRepository |
37
|
|
|
*/ |
38
|
|
|
protected CollectionRepository $collectionRepository; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @access public |
42
|
|
|
* |
43
|
|
|
* @param CollectionRepository $collectionRepository |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function injectCollectionRepository(CollectionRepository $collectionRepository): void |
48
|
|
|
{ |
49
|
|
|
$this->collectionRepository = $collectionRepository; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @access protected |
54
|
|
|
* @var MetadataRepository |
55
|
|
|
*/ |
56
|
|
|
protected MetadataRepository $metadataRepository; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @access public |
60
|
|
|
* |
61
|
|
|
* @param MetadataRepository $metadataRepository |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function injectMetadataRepository(MetadataRepository $metadataRepository): void |
66
|
|
|
{ |
67
|
|
|
$this->metadataRepository = $metadataRepository; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Show a list of collections |
72
|
|
|
* |
73
|
|
|
* @access public |
74
|
|
|
* |
75
|
|
|
* @return ResponseInterface the response |
76
|
|
|
*/ |
77
|
|
|
public function listAction(): ResponseInterface |
78
|
|
|
{ |
79
|
|
|
$solr = Solr::getInstance($this->settings['solrcore']); |
80
|
|
|
|
81
|
|
|
if (!$solr->ready) { |
82
|
|
|
$this->logger->error('Apache Solr not available'); |
83
|
|
|
return $this->htmlResponse(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$collections = []; |
87
|
|
|
|
88
|
|
|
// Sort collections according to order in plugin flexform configuration |
89
|
|
|
if ($this->settings['collections']) { |
90
|
|
|
$sortedCollections = []; |
91
|
|
|
foreach (GeneralUtility::intExplode(',', $this->settings['collections']) as $uid) { |
92
|
|
|
$sortedCollections[$uid] = $this->collectionRepository->findByUid($uid); |
93
|
|
|
} |
94
|
|
|
$collections = $sortedCollections; |
95
|
|
|
} else { |
96
|
|
|
$collections = $this->collectionRepository->findAll(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (count($collections) == 1 && empty($this->settings['dont_show_single']) && is_array($collections)) { |
100
|
|
|
return $this->redirect('show', null, null, ['collection' => array_pop($collections)]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$processedCollections = $this->processCollections($collections, $solr); |
104
|
|
|
|
105
|
|
|
// Randomize sorting? |
106
|
|
|
if (!empty($this->settings['randomize'])) { |
107
|
|
|
ksort($processedCollections, SORT_NUMERIC); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->view->assign('collections', $processedCollections); |
111
|
|
|
|
112
|
|
|
return $this->htmlResponse(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Show a single collection with description and all its documents. |
117
|
|
|
* |
118
|
|
|
* @access protected |
119
|
|
|
* |
120
|
|
|
* @param Collection $collection The collection object |
121
|
|
|
* |
122
|
|
|
* @return ResponseInterface the response |
123
|
|
|
*/ |
124
|
|
|
public function showAction(Collection $collection): ResponseInterface |
125
|
|
|
{ |
126
|
|
|
$searchParams = $this->getParametersSafely('searchParameter'); |
127
|
|
|
|
128
|
|
|
// Instantiate the Solr. Without Solr present, we can't do anything. |
129
|
|
|
$solr = Solr::getInstance($this->settings['solrcore']); |
130
|
|
|
if (!$solr->ready) { |
131
|
|
|
$this->logger->error('Apache Solr not available'); |
132
|
|
|
return $this->htmlResponse(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Pagination of Results: Pass the currentPage to the fluid template to calculate current index of search result. |
136
|
|
|
$currentPage = $this->getParametersSafely('page'); |
137
|
|
|
if (empty($currentPage)) { |
138
|
|
|
$currentPage = 1; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$searchParams['collection'] = $collection; |
142
|
|
|
// If a targetPid is given, the results will be shown by ListView on the target page. |
143
|
|
|
if (!empty($this->settings['targetPid'])) { |
144
|
|
|
return $this->redirect( |
145
|
|
|
'main', 'ListView', null, |
146
|
|
|
[ |
147
|
|
|
'searchParameter' => $searchParams, |
148
|
|
|
'page' => $currentPage |
149
|
|
|
], $this->settings['targetPid'] |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// get all metadata records to be shown in results |
154
|
|
|
$listedMetadata = $this->metadataRepository->findByIsListed(true); |
|
|
|
|
155
|
|
|
|
156
|
|
|
// get all indexed metadata fields |
157
|
|
|
$indexedMetadata = $this->metadataRepository->findByIndexIndexed(true); |
|
|
|
|
158
|
|
|
|
159
|
|
|
// get all sortable metadata records |
160
|
|
|
$sortableMetadata = $this->metadataRepository->findByIsSortable(true); |
|
|
|
|
161
|
|
|
|
162
|
|
|
// get all documents of given collection |
163
|
|
|
$solrResults = null; |
164
|
|
|
if (is_array($searchParams) && !empty($searchParams)) { |
165
|
|
|
$solrResults = $this->documentRepository->findSolrByCollection($collection, $this->settings, $searchParams, $listedMetadata, $indexedMetadata); |
166
|
|
|
|
167
|
|
|
$itemsPerPage = $this->settings['list']['paginate']['itemsPerPage']; |
168
|
|
|
if (empty($itemsPerPage)) { |
169
|
|
|
$itemsPerPage = 25; |
170
|
|
|
} |
171
|
|
|
$solrPaginator = new SolrPaginator($solrResults, $currentPage, $itemsPerPage); |
172
|
|
|
$simplePagination = new SimplePagination($solrPaginator); |
173
|
|
|
|
174
|
|
|
$pagination = $this->buildSimplePagination($simplePagination, $solrPaginator); |
175
|
|
|
$this->view->assignMultiple([ 'pagination' => $pagination, 'paginator' => $solrPaginator ]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->view->assign('viewData', $this->viewData); |
179
|
|
|
$this->view->assign('documents', $solrResults); |
180
|
|
|
$this->view->assign('collection', $collection); |
181
|
|
|
$this->view->assign('page', $currentPage); |
182
|
|
|
$this->view->assign('lastSearch', $searchParams); |
183
|
|
|
$this->view->assign('sortableMetadata', $sortableMetadata); |
184
|
|
|
$this->view->assign('listedMetadata', $listedMetadata); |
185
|
|
|
|
186
|
|
|
return $this->htmlResponse(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* This is an uncached helper action to make sorting possible on collection single views. |
191
|
|
|
* |
192
|
|
|
* @access public |
193
|
|
|
* |
194
|
|
|
* @return ResponseInterface the response |
195
|
|
|
*/ |
196
|
|
|
public function showSortedAction(): ResponseInterface |
197
|
|
|
{ |
198
|
|
|
// if search was triggered, get search parameters from POST variables |
199
|
|
|
$searchParams = $this->getParametersSafely('searchParameter'); |
200
|
|
|
|
201
|
|
|
$collection = null; |
202
|
|
|
if ($searchParams['collection']['__identity'] && MathUtility::canBeInterpretedAsInteger($searchParams['collection']['__identity'])) { |
203
|
|
|
$collection = $this->collectionRepository->findByUid($searchParams['collection']['__identity']); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// output is done by show action |
207
|
|
|
return $this->redirect('show', null, null, ['searchParameter' => $searchParams, 'collection' => $collection]); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Processes collections for displaying in the frontend. |
212
|
|
|
* |
213
|
|
|
* @access private |
214
|
|
|
* |
215
|
|
|
* @param QueryResultInterface|array|object $collections to be processed |
|
|
|
|
216
|
|
|
* @param Solr $solr for query |
217
|
|
|
* |
218
|
|
|
* @return array |
219
|
|
|
*/ |
220
|
|
|
private function processCollections($collections, Solr $solr): array |
221
|
|
|
{ |
222
|
|
|
$processedCollections = []; |
223
|
|
|
|
224
|
|
|
// Process results. |
225
|
|
|
foreach ($collections as $collection) { |
226
|
|
|
$solrQuery = ''; |
227
|
|
|
if ($collection->getIndexSearch() != '') { |
228
|
|
|
$solrQuery .= '(' . $collection->getIndexSearch() . ')'; |
229
|
|
|
} else { |
230
|
|
|
$solrQuery .= 'collection:("' . Solr::escapeQuery($collection->getIndexName()) . '")'; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// We only care about the UID and partOf in the results and want them sorted |
234
|
|
|
$params = [ |
235
|
|
|
'fields' => 'uid,partof', |
236
|
|
|
'sort' => [ |
237
|
|
|
'uid' => 'asc' |
238
|
|
|
] |
239
|
|
|
]; |
240
|
|
|
// virtual collection might yield documents, that are not toplevel true or partof anything |
241
|
|
|
if ($collection->getIndexSearch()) { |
242
|
|
|
$params['query'] = $solrQuery; |
243
|
|
|
} else { |
244
|
|
|
$params['query'] = $solrQuery . ' AND partof:0 AND toplevel:true'; |
245
|
|
|
} |
246
|
|
|
$partOfNothing = $solr->searchRaw($params); |
247
|
|
|
|
248
|
|
|
$params['query'] = $solrQuery . ' AND NOT partof:0 AND toplevel:true'; |
249
|
|
|
$partOfSomething = $solr->searchRaw($params); |
250
|
|
|
|
251
|
|
|
$collectionInfo = []; |
252
|
|
|
// Titles are all documents that are "root" elements i.e. partof == 0 |
253
|
|
|
$collectionInfo['titles'] = []; |
254
|
|
|
foreach ($partOfNothing as $doc) { |
255
|
|
|
$collectionInfo['titles'][$doc->uid] = $doc->uid; |
256
|
|
|
} |
257
|
|
|
// Volumes are documents that are both |
258
|
|
|
// a) "leaf" elements i.e. partof != 0 |
259
|
|
|
// b) "root" elements that are not referenced by other documents ("root" elements that have no descendants) |
260
|
|
|
$collectionInfo['volumes'] = $collectionInfo['titles']; |
261
|
|
|
foreach ($partOfSomething as $doc) { |
262
|
|
|
$collectionInfo['volumes'][$doc->uid] = $doc->uid; |
263
|
|
|
// If a document is referenced via partof, it’s not a volume anymore. |
264
|
|
|
unset($collectionInfo['volumes'][$doc->partof]); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// Generate random but unique array key taking amount of documents into account. |
268
|
|
|
do { |
269
|
|
|
$key = ($collection->getPriority() * 1000) + random_int(0, 1000); |
270
|
|
|
} while (!empty($processedCollections[$key])); |
271
|
|
|
|
272
|
|
|
$processedCollections[$key]['collection'] = $collection; |
273
|
|
|
$processedCollections[$key]['info'] = $collectionInfo; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
// Randomize sorting? |
277
|
|
|
if (!empty($this->settings['randomize'])) { |
278
|
|
|
ksort($processedCollections, SORT_NUMERIC); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $processedCollections; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|