3 # Copyright 2023 Washington County School District
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
28 use Koha::List::Patron qw( GetPatronLists AddPatronsToList DelPatronsFromList );
32 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
34 template_name => "patron_lists/patron-lists-tab.tt",
38 { circulate => 'circulate_remaining_permissions' }, { borrowers => '*' }, { tools => 'manage_patron_lists' }
43 my $logged_in_user = Koha::Patrons->find( { borrowernumber => $loggedinuser } );
44 my $patronnumber = $cgi->param('borrowernumber');
45 my $patron = Koha::Patrons->find($patronnumber);
46 my ( @in_lists, %list_id_lookup, @available_lists );
48 my $list_id = $cgi->param('patron_list_id');
49 my @patrons_to_add = $cgi->multi_param('patrons_to_add');
50 my @patrons_to_remove = $cgi->multi_param('patrons_to_remove');
52 if ( !$logged_in_user->can_see_patron_infos($patron) ) {
53 $template->param( 'no_access_to_patron' => 1 );
55 my $has_perms = C4::Auth::haspermission( $logged_in_user->userid, { 'tools' => 'manage_patron_lists' } );
56 if ( $list_id && $has_perms ) {
57 my ($list) = GetPatronLists( { patron_list_id => $list_id } );
59 if (@patrons_to_add) {
60 AddPatronsToList( { list => $list, cardnumbers => \@patrons_to_add } );
63 if (@patrons_to_remove) {
64 DelPatronsFromList( { list => $list, patron_list_patrons => \@patrons_to_remove } );
69 @in_lists = $patron->get_lists_with_patron;
70 foreach my $list (@in_lists) {
71 my @existing = $list->patron_list_patrons;
72 for my $plp (@existing) {
73 if ( $plp->borrowernumber->borrowernumber == $patronnumber ) {
74 $list_id_lookup{ $list->patron_list_id } = $plp->patron_list_patron_id;
80 @available_lists = GetPatronLists();
81 @available_lists = grep { !$list_id_lookup{ $_->patron_list_id } } @available_lists;
85 in_lists => \@in_lists,
86 list_id_lookup => \%list_id_lookup,
87 available_lists => \@available_lists,
88 borrowernumber => $patronnumber,
89 cardnumber => $patron->cardnumber,
92 output_html_with_http_headers( $cgi, $cookie, $template->output );