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!

Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Signed-off-by: Laura_Escamilla <laura.escamilla@bywatersolutions.com>
Signed-off-by: Laura Escamilla <laura.escamilla@bywatersolutions.com>
This commit is contained in:
David Cook 2025-10-27 05:06:00 +00:00 committed by Laura Escamilla
parent 09860c5816
commit c4cd2d02a2
No known key found for this signature in database
GPG Key ID: B5DEB5FD2FCDA5D8
3 changed files with 65 additions and 8 deletions

View File

@ -84,6 +84,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,
@ -95,7 +98,7 @@ sub get_private_shelves {
{
join => ['virtualshelfshares'],
distinct => 'shelfnumber',
order_by => 'shelfname',
order_by => $order_by,
( ( $page and $rows ) ? ( page => $page, rows => $rows ) : () ),
}
);
@ -110,18 +113,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

View File

@ -677,11 +677,10 @@
<div id="publicshelves" class="tab-pane active" role="tabpanel" aria-labelledby="public_lists"> </div>
[% END %]
[% SET show_new_list = 0 %]
[% IF !public || Koha.Preference('OpacAllowPublicListCreation') %]
[% IF loggedinusername %]
<div id="toolbar" class="toolbar"
><a class="btn btn-link newshelf" href="/cgi-bin/koha/opac-shelves.pl?op=add_form"><i class="fa fa-plus" aria-hidden="true"></i> New list</a></div
>
[% show_new_list = 1 %]
[% ELSE %]
[% IF Koha.Preference( 'opacuserlogin' ) == 1 %]
<div class="alert alert-info"><a href="/cgi-bin/koha/opac-shelves.pl?op=add_form">Log in to create a new list</a></div>
@ -690,6 +689,25 @@
[% END %]
[% IF shelves.count %]
<div id="toolbar" class="toolbar">
[% IF ( show_new_list ) %]
<a class="btn btn-link newshelf" href="/cgi-bin/koha/opac-shelves.pl?op=add_form"><i class="fa fa-plus" aria-hidden="true"></i> New list</a> <span class="sep">|</span>
[% END %]
<div class="btn-group dropdown">
<button type="button" class="btn btn-link dropdown-toggle" data-bs-toggle="dropdown" id="sortmenu" aria-haspopup="true" aria-expanded="false"
><i class="fa fa-sort" aria-hidden="true"></i> Sort</button
>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="sortmenu">
<a class="dropdown-item" href="/cgi-bin/koha/opac-shelves.pl?op=list&public=[% public | html %]&sortfield=">Default sorting</a>
<span class="dropdown-header">List name</span>
<a class="dropdown-item" href="/cgi-bin/koha/opac-shelves.pl?op=list&public=[% public | html %]&sortfield=shelfname&direction=asc">List name (A-Z)</a>
<a class="dropdown-item" href="/cgi-bin/koha/opac-shelves.pl?op=list&public=[% public | html %]&sortfield=shelfname&direction=desc">List name (Z-A)</a>
<span class="dropdown-header">Modification date</span>
<a class="dropdown-item" href="/cgi-bin/koha/opac-shelves.pl?op=list&public=[% public | html %]&sortfield=lastmodified&direction=desc">Modification date (newest to oldest)</a>
<a class="dropdown-item" href="/cgi-bin/koha/opac-shelves.pl?op=list&public=[% public | html %]&sortfield=lastmodified&direction=asc">Modification date (oldest to newest)</a>
</div>
</div>
</div>
<table class="table">
[% IF !public %]
<caption class="sr-only">Your lists</caption>

View File

@ -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,
}
),
);
}