Bug 34853: Add clearfix span to Invoice links
[koha.git] / patron_lists / patron-lists-tab.pl
1 #!/usr/bin/perl
2
3 # Copyright 2023 Washington County School District
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use CGI;
23
24 use C4::Auth   qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26
27 use Koha::Patrons;
28 use Koha::List::Patron qw( GetPatronLists AddPatronsToList DelPatronsFromList );
29
30 my $cgi = CGI->new;
31
32 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
33     {
34         template_name => "patron_lists/patron-lists-tab.tt",
35         query         => $cgi,
36         type          => "intranet",
37         flagsrequired => [
38             { circulate => 'circulate_remaining_permissions' }, { borrowers => '*' }, { tools => 'manage_patron_lists' }
39         ],
40     }
41 );
42
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 );
47
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');
51
52 if ( !$logged_in_user->can_see_patron_infos($patron) ) {
53     $template->param( 'no_access_to_patron' => 1 );
54 } else {
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 } );
58
59         if (@patrons_to_add) {
60             AddPatronsToList( { list => $list, cardnumbers => \@patrons_to_add } );
61         }
62
63         if (@patrons_to_remove) {
64             DelPatronsFromList( { list => $list, patron_list_patrons => \@patrons_to_remove } );
65         }
66     }
67
68     if ($patron) {
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;
75                     last;
76                 }
77             }
78         }
79     }
80     @available_lists = GetPatronLists();
81     @available_lists = grep { !$list_id_lookup{ $_->patron_list_id } } @available_lists;
82 }
83
84 $template->param(
85     in_lists        => \@in_lists,
86     list_id_lookup  => \%list_id_lookup,
87     available_lists => \@available_lists,
88     borrowernumber  => $patronnumber,
89     cardnumber      => $patron->cardnumber,
90 );
91
92 output_html_with_http_headers( $cgi, $cookie, $template->output );