From 819bcb4abe188e7b061089b3a59e65c9c4764cea Mon Sep 17 00:00:00 2001 From: Jacob O'Mara Date: Wed, 5 Nov 2025 21:34:28 +0000 Subject: [PATCH] Bug 38050: move sortfield validation to method for reuse Call method instead of repeating the validation logic --- Koha/Virtualshelf.pm | 25 +++++++++++++++++++++++++ virtualshelves/shelves.pl | 13 ++----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Koha/Virtualshelf.pm b/Koha/Virtualshelf.pm index 339351b6226..9d4665422c0 100644 --- a/Koha/Virtualshelf.pm +++ b/Koha/Virtualshelf.pm @@ -70,6 +70,9 @@ sub store { $self->created_on(dt_from_string) unless defined $self->created_on; + $self->sortfield( $self->_validate_sortfield( $self->sortfield ) ) + if $self->sortfield; + return $self->SUPER::store($self); } @@ -547,6 +550,28 @@ sub public_read_list { =head2 Internal methods +=head3 _validate_sortfield + +Validates and normalizes the sortfield value. +Defaults to 'title' if invalid, and converts 'copyrightdate' to 'publicationyear' for UNIMARC. + +=cut + +sub _validate_sortfield { + my ( $self, $sortfield ) = @_; + + # Default to 'title' if invalid + $sortfield = 'title' + unless grep { $_ eq $sortfield } qw( title author copyrightdate publicationyear itemcallnumber dateadded ); + + # Convert copyrightdate to publicationyear for UNIMARC + if ( $sortfield eq 'copyrightdate' && C4::Context->preference('marcflavour') eq 'UNIMARC' ) { + $sortfield = 'publicationyear'; + } + + return $sortfield; +} + =head3 _type =cut diff --git a/virtualshelves/shelves.pl b/virtualshelves/shelves.pl index f64caf75428..15770e07ea8 100755 --- a/virtualshelves/shelves.pl +++ b/virtualshelves/shelves.pl @@ -123,11 +123,7 @@ if ( $op eq 'add_form' ) { if ($shelf) { $op = $referer; my $sortfield = $query->param('sortfield'); - $sortfield = 'title' - unless grep { $_ eq $sortfield } qw( title author copyrightdate publicationyear itemcallnumber dateadded ); - if ( $sortfield eq 'copyrightdate' and C4::Context->preference('marcflavour') eq 'UNIMARC' ) { - $sortfield = 'publicationyear'; - } + $sortfield = $shelf->_validate_sortfield($sortfield); if ( $shelf->can_be_managed($loggedinuser) ) { $shelf->shelfname( scalar $query->param('shelfname') ); $shelf->sortfield($sortfield); @@ -294,12 +290,7 @@ if ( $op eq 'view' ) { $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting - $sortfield = 'title' - unless grep { $_ eq $sortfield } - qw( title author copyrightdate publicationyear itemcallnumber dateadded ); - if ( $sortfield eq 'copyrightdate' and C4::Context->preference('marcflavour') eq 'UNIMARC' ) { - $sortfield = 'publicationyear'; - } + $sortfield = $shelf->_validate_sortfield($sortfield); my $direction = $query->param('direction') || 'asc'; $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc'; my $rows; -- 2.39.5