Skip to content

Commit aad45e5

Browse files
committed
Add the ability the sort holdings in search results statuses
1 parent d5bcc43 commit aad45e5

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

config/vufind/config.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,6 +2275,14 @@ includeSchemaOrgMetadata = true
22752275
; If set to "numerical", it will sort numerically by MARC tag.
22762276
;marcSubjectHeadingsSort = "numerical"
22772277

2278+
; Sorting for holdings when getting record statuses in the search results page (default: "false").
2279+
; Available values are holdings array keys
2280+
; Unless reversed is specified, elements will be sorted by php defaults for operator <=> (a < b, false < true, 1 < 2)
2281+
; The keys at the top have priority over the following keys
2282+
;getStatusesSorting = "false"
2283+
;getStatusesSorting['availability'] = "reversed"
2284+
;getStatusesSorting['location'] = "reversed"
2285+
22782286
; The following two sections control the Alphabetic Browse module.
22792287
[AlphaBrowse]
22802288
; This setting controls how many headings are displayed on each page of results:

module/VuFind/src/VuFind/AjaxHandler/GetItemStatuses.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,31 @@ protected function renderFullStatus($record, $simpleStatus, array $values = [])
470470
return $this->renderer->render('ajax/status-full.phtml', $values);
471471
}
472472

473+
/**
474+
* Sort statuses according to given config (by default it come from config.ini)
475+
*
476+
* @param array[] $holdings The holdings to sort
477+
* @param array[] $sortingFields Config on how to sort the fields (first values are prioritized for sorting)
478+
*
479+
* @return void
480+
*/
481+
protected function sortStatuses(array &$holdings, array $sortingFields): void
482+
{
483+
usort($holdings, function ($a, $b) use ($sortingFields) {
484+
foreach ($sortingFields as $field => $order) {
485+
if (!isset($a[$field], $b[$field])) {
486+
continue;
487+
}
488+
$result = $a[$field] <=> $b[$field];
489+
if ($result === 0) {
490+
continue;
491+
}
492+
return $order !== "reversed" ? $result : -$result;
493+
}
494+
return 0;
495+
});
496+
}
497+
473498
/**
474499
* Handle a request.
475500
*
@@ -524,6 +549,9 @@ public function handleRequest(Params $params)
524549

525550
// Skip empty records:
526551
if (count($record)) {
552+
if (($this->config->Record->getStatusesSorting ?? "false") !== "false") {
553+
$this->sortStatuses($record, current($this->config->Record->getStatusesSorting));
554+
}
527555
// Check for errors
528556
if (!empty($record[0]['error'])) {
529557
$unknownStatus = $this->availabilityStatusManager->createAvailabilityStatus(

0 commit comments

Comments
 (0)