Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / members / deletemem.pl
1 #!/usr/bin/perl
2
3 #script to delete items
4 #written 2/5/00
5 #by chris@katipo.co.nz
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 use Modern::Perl;
25
26 use CGI qw ( -utf8 );
27
28 use Try::Tiny;
29
30 use C4::Context;
31 use C4::Output;
32 use C4::Auth;
33 use C4::Members;
34 use C4::Suggestions qw( SearchSuggestion );
35 use Koha::Patrons;
36 use Koha::Token;
37 use Koha::Patron::Categories;
38
39 my $input = CGI->new;
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {   template_name => "members/deletemem.tt",
43         query         => $input,
44         type          => "intranet",
45         flagsrequired => { borrowers => 'delete_borrowers' },
46     }
47 );
48
49 #print $input->header;
50 my $member       = $input->param('member');
51
52 #Do not delete yourself...
53 if ( $loggedinuser == $member ) {
54     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_YOURSELF");
55     exit 0; # Exit without error
56 }
57
58 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
59 my $patron         = Koha::Patrons->find( $member );
60 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
61
62 my $debits = $patron->account->outstanding_debits->total_outstanding;
63 my $credits = abs $patron->account->outstanding_credits->total_outstanding;
64 my $countissues = $patron->checkouts->count;
65 my $userenv = C4::Context->userenv;
66
67 if ($patron->category->category_type eq "S") {
68     unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
69         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
70         exit 0; # Exit without error
71     }
72 } else {
73     unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>'delete_borrowers'})) {
74         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
75         exit 0; # Exit without error
76     }
77 }
78
79 if (C4::Context->preference("IndependentBranches")) {
80     my $userenv = C4::Context->userenv;
81     if ( !C4::Context->IsSuperLibrarian() && $patron->branchcode){
82         unless ($userenv->{branch} eq $patron->branchcode){
83             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
84             exit 0; # Exit without error
85         }
86     }
87 }
88
89 if ( my $anonymous_patron = C4::Context->preference("AnonymousPatron") ) {
90     if ( $patron->id eq $anonymous_patron ) {
91         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_ANONYMOUS_PATRON");
92         exit 0;    # Exit without error
93     }
94 }
95
96 my $op = $input->param('op') || 'delete_confirm';
97 my $dbh = C4::Context->dbh;
98 my $is_guarantor = $patron->guarantee_relationships->count;
99 my $countholds = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE borrowernumber=?", undef, $member);
100
101 # Add warning if patron has pending suggestions
102 $template->param(
103     pending_suggestions => scalar @{
104     C4::Suggestions::SearchSuggestion(
105             { suggestedby => $member, STATUS => 'ASKED' }
106         )
107     }
108 );
109
110 $template->param(
111     patron        => $patron,
112     ItemsOnIssues => $countissues,
113     debits        => $debits,
114     credits       => $credits,
115     is_guarantor  => $is_guarantor,
116     ItemsOnHold   => $countholds,
117 );
118
119 if ( $op eq 'delete_confirm' or $countissues > 0 or $debits or $is_guarantor ) {
120     $template->param(
121         op         => 'delete_confirm',
122         csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
123     );
124
125 } elsif ( $op eq 'delete_confirmed' ) {
126     output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
127         unless Koha::Token->new->check_csrf( {
128             session_id => $input->cookie('CGISESSID'),
129             token  => scalar $input->param('csrf_token'),
130         });
131     my $patron = Koha::Patrons->find( $member );
132     $patron->move_to_deleted;
133     try {
134         $patron->delete;
135         print $input->redirect("/cgi-bin/koha/members/members-home.pl");
136     } catch {
137         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_ANONYMOUS_PATRON");
138     };
139     # TODO Tell the user everything went ok
140     exit 0; # Exit without error
141 }
142
143 output_html_with_http_headers $input, $cookie, $template->output;