From ff6e763cc3e646473e871806f32b348331fd6efb Mon Sep 17 00:00:00 2001
From: Baptiste Wojtkowski <baptiste.wojtkowski@biblibre.com>
Date: Mon, 17 Feb 2025 17:05:55 +0100
Subject: [PATCH] Bug 39148: Correct sort by publicationdate in OPAC lists

Test plan (UNIMARC):
1 - Create a list with different publicationdates, chose to sort it by
  copyright gate
2 - Go to the list in the OPAC -> it is not correctly sorted
3 - Pick sort order: copyright date -> not correctly sorted
4 - Apply patch
5 - Retry 2&3 and see it works
---
 opac/opac-shelves.pl | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/opac/opac-shelves.pl b/opac/opac-shelves.pl
index e7dfc65c..5f008cb0 100755
--- a/opac/opac-shelves.pl
+++ b/opac/opac-shelves.pl
@@ -155,6 +155,9 @@ if ( $op eq 'add_form' ) {
         $op = $referer;
         my $sortfield = $query->param('sortfield');
         $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
+        if ( $sortfield eq 'copyrightdate' and C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
+            $sortfield = 'publicationyear';
+        }
         if ( $shelf->can_be_managed($loggedinuser) ) {
             $shelf->shelfname( scalar $query->param('shelfname') );
             $shelf->sortfield($sortfield);
@@ -328,6 +331,9 @@ if ( $op eq 'view' ) {
             $sortfield = 'title'
                 if !$sortfield
                 or !grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
+            if ( $sortfield eq 'copyrightdate' and C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
+                $sortfield = 'publicationyear';
+            }
 
             my $rows;
             if ( $query->param('print') or $query->param('rss') ) {
-- 
2.30.2