Skip to content

Commit d6e8d4e

Browse files
[BUGFIX] Rewrite CollectionController, ListViewController and SearchController (kitodo#1527)
1 parent 2993a5b commit d6e8d4e

File tree

3 files changed

+230
-173
lines changed

3 files changed

+230
-173
lines changed

Classes/Controller/CollectionController.php

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
use Kitodo\Dlf\Common\SolrPaginator;
1515
use Kitodo\Dlf\Common\Solr\Solr;
1616
use Kitodo\Dlf\Domain\Model\Collection;
17+
use Kitodo\Dlf\Domain\Repository\CollectionRepository;
18+
use Kitodo\Dlf\Domain\Repository\MetadataRepository;
1719
use TYPO3\CMS\Core\Pagination\SimplePagination;
1820
use TYPO3\CMS\Core\Utility\GeneralUtility;
1921
use TYPO3\CMS\Core\Utility\MathUtility;
20-
use Kitodo\Dlf\Domain\Repository\CollectionRepository;
21-
use Kitodo\Dlf\Domain\Repository\MetadataRepository;
2222

2323
/**
2424
* Controller class for the plugin 'Collection'.
@@ -66,6 +66,12 @@ public function injectMetadataRepository(MetadataRepository $metadataRepository)
6666
$this->metadataRepository = $metadataRepository;
6767
}
6868

69+
/**
70+
* @access protected
71+
* @var array The current search parameter
72+
*/
73+
protected $searchParams = [];
74+
6975
/**
7076
* Show a list of collections
7177
*
@@ -75,10 +81,9 @@ public function injectMetadataRepository(MetadataRepository $metadataRepository)
7581
*/
7682
public function listAction(): void
7783
{
78-
$solr = Solr::getInstance($this->settings['solrcore']);
79-
80-
if (!$solr->ready) {
81-
$this->logger->error('Apache Solr not available');
84+
// Quit without doing anything if required variables are not set.
85+
if (empty($this->settings['solrcore'])) {
86+
$this->logger->warning('Incomplete plugin configuration for SOLR. Please check the plugin settings for UID of SOLR core.');
8287
return;
8388
}
8489

@@ -103,7 +108,7 @@ public function listAction(): void
103108
}
104109
}
105110

106-
$processedCollections = $this->processCollections($collections, $solr);
111+
$processedCollections = $this->processCollections($collections);
107112

108113
// Randomize sorting?
109114
if (!empty($this->settings['randomize'])) {
@@ -118,68 +123,71 @@ public function listAction(): void
118123
*
119124
* @access protected
120125
*
121-
* @param Collection $collection The collection object
126+
* @param ?Collection $collection The collection object
122127
*
123128
* @return void
124129
*/
125-
public function showAction(Collection $collection): void
130+
public function showAction(?Collection $collection = null): void
126131
{
127-
$searchParams = $this->getParametersSafely('searchParameter');
128-
129-
// Instantiate the Solr. Without Solr present, we can't do anything.
130-
$solr = Solr::getInstance($this->settings['solrcore']);
131-
if (!$solr->ready) {
132-
$this->logger->error('Apache Solr not available');
132+
// Quit without doing anything if required variables are not set.
133+
if (empty($this->settings['solrcore'])) {
134+
$this->logger->warning('Incomplete plugin configuration for SOLR. Please check the plugin settings for UID of SOLR core.');
133135
return;
134136
}
135137

136-
// Pagination of Results: Pass the currentPage to the fluid template to calculate current index of search result.
137-
$currentPage = $this->getParametersSafely('page');
138-
if (empty($currentPage)) {
139-
$currentPage = 1;
138+
$this->searchParams = $this->getParametersSafely('searchParameter');
139+
$searchRequestData = GeneralUtility::_GPmerged('tx_dlf_search');
140+
141+
if (isset($searchRequestData['searchParameter']) && is_array($searchRequestData['searchParameter'])) {
142+
$this->searchParams = array_merge($this->searchParams ?: [], $searchRequestData['searchParameter']);
143+
$this->request->getAttribute('frontend.user')->setKey('ses', 'search', $this->searchParams);
144+
}
145+
146+
// Get current page from request data because the parameter is shared between plugins
147+
$currentPage = $this->requestData['page'] ?? 1;
148+
149+
if (!isset($collection)) {
150+
$collection = $this->collectionRepository->findByUid($this->searchParams['collection']);
151+
} else {
152+
$this->searchParams['collection'] = $collection->getUid();
140153
}
141154

142-
$searchParams['collection'] = $collection->getUid();
143-
// If a targetPid is given, the results will be shown by ListView on the target page.
155+
// If a targetPid is given, the results will be shown by Collection on the target page.
144156
if (!empty($this->settings['targetPid'])) {
145-
$this->redirect('main', 'ListView', null,
157+
$this->redirect(
158+
'show',
159+
'Collection',
160+
null,
146161
[
147-
'searchParameter' => $searchParams,
148-
'page' => $currentPage
149-
], $this->settings['targetPid']
162+
'collection' => $collection
163+
],
164+
$this->settings['targetPid']
150165
);
151166
}
152167

153168
// get all metadata records to be shown in results
154169
$listedMetadata = $this->metadataRepository->findByIsListed(true);
155170

156-
// get all indexed metadata fields
157-
$indexedMetadata = $this->metadataRepository->findByIndexIndexed(true);
158-
159171
// get all sortable metadata records
160172
$sortableMetadata = $this->metadataRepository->findByIsSortable(true);
161173

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);
174+
$solrResults = $this->documentRepository->findSolrByCollection($collection, $this->settings, $this->searchParams, $listedMetadata);
175+
$numResults = $solrResults->getNumFound();
166176

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);
177+
$itemsPerPage = $this->settings['list']['paginate']['itemsPerPage'] ?? 25;
173178

174-
$pagination = $this->buildSimplePagination($simplePagination, $solrPaginator);
175-
$this->view->assignMultiple([ 'pagination' => $pagination, 'paginator' => $solrPaginator ]);
176-
}
179+
$solrPaginator = new SolrPaginator($solrResults, $currentPage, $itemsPerPage);
180+
$simplePagination = new SimplePagination($solrPaginator);
181+
182+
$pagination = $this->buildSimplePagination($simplePagination, $solrPaginator);
183+
$this->view->assignMultiple([ 'pagination' => $pagination, 'paginator' => $solrPaginator ]);
177184

178185
$this->view->assign('viewData', $this->viewData);
179-
$this->view->assign('documents', $solrResults);
186+
$this->view->assign('documents', !empty($solrResults) ? $solrResults : []);
187+
$this->view->assign('numResults', $numResults);
180188
$this->view->assign('collection', $collection);
181189
$this->view->assign('page', $currentPage);
182-
$this->view->assign('lastSearch', $searchParams);
190+
$this->view->assign('lastSearch', $this->searchParams);
183191
$this->view->assign('sortableMetadata', $sortableMetadata);
184192
$this->view->assign('listedMetadata', $listedMetadata);
185193
}
@@ -202,7 +210,7 @@ public function showSortedAction(): void
202210
}
203211

204212
// output is done by show action
205-
$this->forward('show', null, null, ['searchParameter' => $searchParams, 'collection' => $collection]);
213+
$this->forward('show', null, null, ['collection' => $collection, 'searchParams' => $searchParams]);
206214

207215
}
208216

@@ -212,12 +220,13 @@ public function showSortedAction(): void
212220
* @access private
213221
*
214222
* @param QueryResultInterface|array|object $collections to be processed
215-
* @param Solr $solr for query
216223
*
217224
* @return array
218225
*/
219-
private function processCollections($collections, Solr $solr): array
226+
private function processCollections($collections): array
220227
{
228+
$solr = Solr::getInstance($this->settings['solrcore']);
229+
221230
$processedCollections = [];
222231

223232
// Process results.

Classes/Controller/ListViewController.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
namespace Kitodo\Dlf\Controller;
1313

1414
use Kitodo\Dlf\Common\SolrPaginator;
15-
use TYPO3\CMS\Core\Pagination\SimplePagination;
1615
use Kitodo\Dlf\Domain\Repository\MetadataRepository;
1716
use Kitodo\Dlf\Domain\Repository\CollectionRepository;
17+
use TYPO3\CMS\Core\Pagination\SimplePagination;
18+
use TYPO3\CMS\Core\Utility\GeneralUtility;
1819

1920
/**
2021
* Controller class for the plugin 'ListView'.
@@ -66,7 +67,7 @@ public function injectMetadataRepository(MetadataRepository $metadataRepository)
6667
* @access protected
6768
* @var array The current search parameter
6869
*/
69-
protected $searchParams;
70+
protected $searchParams = [];
7071

7172
/**
7273
* The main method of the plugin
@@ -77,16 +78,18 @@ public function injectMetadataRepository(MetadataRepository $metadataRepository)
7778
*/
7879
public function mainAction(): void
7980
{
81+
// Quit without doing anything if required variables are not set.
82+
if (empty($this->settings['solrcore'])) {
83+
$this->logger->warning('Incomplete plugin configuration for SOLR. Please check the plugin settings for UID of SOLR core.');
84+
return;
85+
}
86+
8087
$this->searchParams = $this->getParametersSafely('searchParameter');
88+
$searchRequestData = GeneralUtility::_GPmerged('tx_dlf_search');
8189

82-
// extract collection(s) from collection parameter
83-
$collections = [];
84-
if (is_array($this->searchParams) && array_key_exists('collection', $this->searchParams)) {
85-
foreach(explode(',', $this->searchParams['collection']) as $collectionEntry) {
86-
if (!empty($collectionEntry)) {
87-
$collections[] = $this->collectionRepository->findByUid((int) $collectionEntry);
88-
}
89-
}
90+
if (isset($searchRequestData['searchParameter']) && is_array($searchRequestData['searchParameter'])) {
91+
$this->searchParams = array_merge($this->searchParams ?: [], $searchRequestData['searchParameter']);
92+
$this->request->getAttribute('frontend.user')->setKey('ses', 'search', $this->searchParams);
9093
}
9194

9295
// Get current page from request data because the parameter is shared between plugins
@@ -98,19 +101,13 @@ public function mainAction(): void
98101
// get all metadata records to be shown in results
99102
$listedMetadata = $this->metadataRepository->findByIsListed(true);
100103

101-
// get all indexed metadata fields
102-
$indexedMetadata = $this->metadataRepository->findByIndexIndexed(true);
103-
104-
$solrResults = null;
105104
$numResults = 0;
106-
if (is_array($this->searchParams) && !empty($this->searchParams)) {
107-
$solrResults = $this->documentRepository->findSolrByCollections($collections, $this->settings, $this->searchParams, $listedMetadata, $indexedMetadata);
105+
if (!empty($this->searchParams)) {
106+
$solrResults = $this->documentRepository->findSolrWithoutCollection($this->settings, $this->searchParams, $listedMetadata);
108107
$numResults = $solrResults->getNumFound();
109108

110-
$itemsPerPage = $this->settings['list']['paginate']['itemsPerPage'];
111-
if (empty($itemsPerPage)) {
112-
$itemsPerPage = 25;
113-
}
109+
$itemsPerPage = $this->settings['list']['paginate']['itemsPerPage'] ?? 25;
110+
114111
$solrPaginator = new SolrPaginator($solrResults, $currentPage, $itemsPerPage);
115112
$simplePagination = new SimplePagination($solrPaginator);
116113

@@ -119,7 +116,7 @@ public function mainAction(): void
119116
}
120117

121118
$this->view->assign('viewData', $this->viewData);
122-
$this->view->assign('documents', $solrResults);
119+
$this->view->assign('documents', !empty($solrResults) ? $solrResults : []);
123120
$this->view->assign('numResults', $numResults);
124121
$this->view->assign('page', $currentPage);
125122
$this->view->assign('lastSearch', $this->searchParams);

0 commit comments

Comments
 (0)