Skip to content

Commit

Permalink
[bugfix] detail page paging should work with activated grouping now
Browse files Browse the repository at this point in the history
  • Loading branch information
cradeck committed Jul 19, 2024
1 parent 837cd04 commit 8028fa4
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 31 deletions.
126 changes: 95 additions & 31 deletions Classes/Service/SolrServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function getDocumentById(string $id): array
$arguments = $this->getRequestArguments();

$assignments = [];
if ($this->settings['paging']['detailPagePaging'] && array_key_exists('underlyingQuery', $arguments) && !$arguments["underlyingQuery"]["group"]) {
if ($this->settings['paging']['detailPagePaging'] && array_key_exists('underlyingQuery', $arguments)) {
// If underlying query has been sent, fetch more data to enable paging arrows.
$underlyingQueryInfo = $arguments['underlyingQuery'];

Expand All @@ -197,8 +197,10 @@ public function getDocumentById(string $id): array
}

$this->createQueryForArguments($arguments);
$this->query->setStart($index['previousIndex']);
$this->query->setRows($index['nextIndex'] - $index['previousIndex'] + 1);
if (!$arguments["underlyingQuery"]["group"]) {
$this->query->setStart($index['previousIndex']);
$this->query->setRows($index['nextIndex'] - $index['previousIndex'] + 1);
}

$this->signalSlotDispatcher->dispatch('Subugoe\Find\Controller\SearchController', 'detailActionBeforePagingSelect', array(&$this->query, $arguments['underlyingQuery']));

Expand Down Expand Up @@ -983,7 +985,7 @@ protected function getCount(?array $arguments = null): int
$count = (int) $this->settings['paging']['perPage'];

if (array_key_exists('count', $arguments)) {
$count = (int) $this->requestArguments['count'];
$count = (int) $arguments['count'];
}

$maxCount = (int) $this->settings['paging']['maximumPerPage'];
Expand Down Expand Up @@ -1109,36 +1111,98 @@ protected function getRecordsWithUnderlyingQuery(array $assignments, array $inde
/** @var \Solarium\QueryType\Select\Result\Result $selectResults */
$selectResults = $connection->execute($this->query);

if ($selectResults->getNumFound() > 0) {
$assignments['results'] = $selectResults;
$resultSet = $selectResults->getDocuments();
if ($arguments['group']) {
// special handling for grouped results required

$field = null;
$groupSetting = $this->settings['grouping'];
if ($arguments["groupfield"]){
$field = $arguments["groupfield"];
} else{
$field = $groupSetting["field"];
}

// the actual result is at position 0 (for the first document) or 1 (otherwise).
$document = $resultSet[$index['resultIndexOffset']];
if ($document['id'] === $id) {
$assignments['document'] = $document;
if (0 !== $index['resultIndexOffset']) {
$assignments['document-previous'] = $resultSet[0];
$assignments['document-previous-number'] = $index['previousIndex'] + 1;
}
$groups = $selectResults->getGrouping();
$group = $groups->getGroup($field);
$numFound = $group->getMatches();
$valueGroups = $group->getValueGroups();
$numValueGroups = count($valueGroups);

$document = null;
$docIdx = (int)$arguments["position"];

$idx = 0;
$vidx = 0;

foreach ($valueGroups as $valueGroup) {
$docs = $valueGroup->getDocuments();
$numDocsInGroup = count($docs);

for ($i=0; $i < $numDocsInGroup; ++$i) {
$doc = $docs[$i];
if ($doc["id"] == $id) {
$document = $doc;

$assignments["document-previous"] = $docs[$i-1];
$assignments['document-previous-number'] = $idx;
$assignments["document-next"] = $docs[$i+1];
$assignments['document-next-number'] = $idx+2;

if ($i == 0 ) { // first doc in group
if ($vidx > 0) {
$prevVGdocs = $valueGroups[$vidx-1]->getDocuments();
$assignments["document-previous"] = $prevVGdocs[count($prevVGdocs)-1];
}
}
if ($i == $numDocsInGroup - 1) { // last doc in current group
if ($vidx != $numValueGroups) {
$nextVGdocs = $valueGroups[$vidx+1]->getDocuments();
$assignments["document-next"] = $nextVGdocs[0];
}
}
break;
}
++$idx;
}
if ($document){
break;
}
++$vidx;
}
$assignments['document'] = $document;
$assignments['results'] = ["numfound" => $numFound];
} else {
if ($selectResults->getNumFound() > 0) {
$assignments['results'] = $selectResults;
$resultSet = $selectResults->getDocuments();

// the actual result is at position 0 (for the first document) or 1 (otherwise).
$document = $resultSet[$index['resultIndexOffset']];
if ($document['id'] === $id) {
$assignments['document'] = $document;
if (0 !== $index['resultIndexOffset']) {
$assignments['document-previous'] = $resultSet[0];
$assignments['document-previous-number'] = $index['previousIndex'] + 1;
}

$nextResultIndex = 1 + $index['resultIndexOffset'];
if (count($resultSet) > $nextResultIndex) {
$assignments['document-next'] = $resultSet[$nextResultIndex];
$assignments['document-next-number'] = $index['nextIndex'] + 1;
$nextResultIndex = 1 + $index['resultIndexOffset'];
if (count($resultSet) > $nextResultIndex) {
$assignments['document-next'] = $resultSet[$nextResultIndex];
$assignments['document-next-number'] = $index['nextIndex'] + 1;
}
} else {
$message = sprintf('»detail« action query with underlying query could not retrieve record id »%s«.', $id);
$this->logger->error(
$message,
['arguments' => $arguments]
);
$assignments["error"] = ["solr" => $message];
}
} else {
$message = sprintf('»detail« action query with underlying query could not retrieve record id »%s«.', $id);
$this->logger->error(
$message,
['arguments' => $arguments]
);
$message = '»detail« action query with underlying query returned no results.';
$this->logger->error($message, ['arguments' => $arguments]);
$assignments["error"] = ["solr" => $message];
}
} else {
$message = '»detail« action query with underlying query returned no results.';
$this->logger->error($message, ['arguments' => $arguments]);
$assignments["error"] = ["solr" => $message];
}
} catch (HttpException $httpException) {
$this->logger->error(
Expand Down Expand Up @@ -1457,11 +1521,11 @@ protected function setSortOrder(array $arguments): void
* @param array $arguments request arguments
*/
private function addGrouping ($arguments) {
$limit = -1;
$field = null;

if (!empty($arguments["group"]) && $arguments["group"]){
if (!empty($this->settings['grouping'])) {
$limit = -1;
$field = null;

$groupSetting = $this->settings['grouping'];

if ($arguments["groupfield"]){
Expand Down
3 changes: 3 additions & 0 deletions Classes/Utility/FrontendUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public static function addQueryInformationAsJavaScript($query, array $settings,

if ($arguments['group']) {
$underlyingQuery['group'] = $arguments['group'];
if ($arguments['grouplimit']) {
$underlyingQuery['grouplimit'] = $arguments['grouplimit'];
}
}

GeneralUtility::makeInstance(AssetCollector::class)->addInlineJavaScript('find_underlyingQuery', 'const underlyingQuery = ' . json_encode($underlyingQuery) . ';');
Expand Down

0 comments on commit 8028fa4

Please sign in to comment.