From 7066dc79eaf8be626ac1da7bcb892956298f65db Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 27 Oct 2025 05:06:00 +0000 Subject: [PATCH] Bug 18148: Make list of lists in OPAC sortable This patch adds sorting to lists of lists in the OPAC. The mechanism used here for op=list is similar to the one used in op=view, so it's stylistically and functionally consistent with the rest of the Lists module in the OPAC. Test plan: 0. Apply the patch and restart Plack (ie koha-plack --restart kohadev) 1. Create 20+ public lists and 20+ private lists for a particular user you control (try to use names that range throughout the alphabet) 2. Using SQL, update the Modification date for at least 1 public list and 1 private list 3. As an anonymous (ie not logged in user), go to /cgi-bin/koha/opac-shelves.pl?op=list&public=0 4. Note you cannot see any lists 5. Go to http://localhost:8080/cgi-bin/koha/opac-shelves.pl?op=list&public=1 6, Note you can see public lists with a toolbar with the button "Sort" below the alert "Log in to create a new list" 7. Using the "Sort" option, try out all the different sort options for default, "List name", and "Modification date" 8. After sorting, try paging through the list of lists. You should note that the sorting is retained through the paging. 9. prove t/db_dependent/Virtualshelves.t 10. Celebrate! --- Koha/Virtualshelves.pm | 35 +++++++++++++++++-- .../bootstrap/en/modules/opac-shelves.tt | 24 +++++++++++-- opac/opac-shelves.pl | 14 ++++++-- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/Koha/Virtualshelves.pm b/Koha/Virtualshelves.pm index f3da2c7099..8546d0ac1f 100644 --- a/Koha/Virtualshelves.pm +++ b/Koha/Virtualshelves.pm @@ -87,6 +87,9 @@ sub get_private_shelves { my $rows = $params->{rows}; my $borrowernumber = $params->{borrowernumber} || 0; + my $sort_by = $params->{sort_by}; + my $order_by = _prepare_order_by_for_shelves( { sort_by => $sort_by } ); + $self->search( { public => 0, @@ -98,7 +101,7 @@ sub get_private_shelves { { join => ['virtualshelfshares'], distinct => 'shelfnumber', - order_by => 'shelfname', + order_by => $order_by, ( ( $page and $rows ) ? ( page => $page, rows => $rows ) : () ), } ); @@ -113,18 +116,46 @@ sub get_public_shelves { my $page = $params->{page}; my $rows = $params->{rows}; + my $sort_by = $params->{sort_by}; + my $order_by = _prepare_order_by_for_shelves( { sort_by => $sort_by } ); + $self->search( { public => 1, }, { distinct => 'shelfnumber', - order_by => 'shelfname', + order_by => $order_by, ( ( $page and $rows ) ? ( page => $page, rows => $rows ) : () ), } ); } +=head3 _prepare_order_by_for_shelves + +Create an "order_by" statement when sorting lists of lists + +=cut + +sub _prepare_order_by_for_shelves { + my ($args) = @_; + my $sort_by = $args->{sort_by}; + my $order_by_dir = '-asc'; + my $order_by_col = 'shelfname'; + if ($sort_by) { + my $sortfield = $sort_by->{sortfield}; + my $direction = $sort_by->{direction}; + if ( $direction eq 'asc' || $direction eq 'desc' ) { + $order_by_dir = '-' . $direction; + } + if ( $sortfield eq 'shelfname' || $sortfield eq 'lastmodified' ) { + $order_by_col = $sortfield; + } + } + my $order_by = { $order_by_dir => $order_by_col }; + return $order_by; +} + =head3 get_some_shelves =cut diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt index 875a88da23..60e609a856 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt @@ -751,11 +751,10 @@
[% END %] + [% SET show_new_list = 0 %] [% IF !public || Koha.Preference('OpacAllowPublicListCreation') %] [% IF loggedinusername %] -
New list
+ [% show_new_list = 1 %] [% ELSE %] [% IF Koha.Preference( 'opacuserlogin' ) == 1 %]
Log in to create a new list
@@ -764,6 +763,25 @@ [% END %] [% IF shelves.count %] +
+ [% IF ( show_new_list ) %] + New list | + [% END %] + +
[% IF !public %] diff --git a/opac/opac-shelves.pl b/opac/opac-shelves.pl index 77f7ffb034..1f999d827c 100755 --- a/opac/opac-shelves.pl +++ b/opac/opac-shelves.pl @@ -518,13 +518,17 @@ if ( $op eq 'view' ) { push @messages, { type => 'error', code => 'does_not_exist' }; } } elsif ( $op eq 'list' ) { + my $direction = $query->param('direction') || 'asc'; + my $sortfield = $query->param('sortfield') || 'shelfname'; + my $sort_by = { sortfield => $sortfield, direction => $direction }; + my $shelves; my ($rows) = (20); if ( !$public ) { $shelves = Koha::Virtualshelves->get_private_shelves( - { page => $page, rows => $rows, borrowernumber => $loggedinuser, } ); + { page => $page, rows => $rows, borrowernumber => $loggedinuser, sort_by => $sort_by, } ); } else { - $shelves = Koha::Virtualshelves->get_public_shelves( { page => $page, rows => $rows, } ); + $shelves = Koha::Virtualshelves->get_public_shelves( { page => $page, rows => $rows, sort_by => $sort_by, } ); } my $pager = $shelves->pager; @@ -532,7 +536,11 @@ if ( $op eq 'view' ) { shelves => $shelves, pagination_bar => pagination_bar( q||, $pager->last_page - $pager->first_page + 1, - $page, "page", { op => 'list', public => $public, } + $page, "page", + { + op => 'list', public => $public, sortfield => $sortfield, + direction => $direction, + } ), ); } -- 2.39.5
Your lists