Bug 17736: [QA Follow-up] Script basket.pl is not Plack compliant
[koha.git] / members / deletemem.pl
blob09439946ee0051a6563280c25472c72e1e01164f
1 #!/usr/bin/perl
3 #script to delete items
4 #written 2/5/00
5 #by chris@katipo.co.nz
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
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.
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.
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>.
24 use strict;
25 #use warnings; FIXME - Bug 2505
27 use CGI qw ( -utf8 );
28 use C4::Context;
29 use C4::Output;
30 use C4::Auth;
31 use C4::Members;
32 use Module::Load;
33 use Koha::Patrons;
34 use Koha::Patron::Images;
35 use Koha::Token;
37 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
38 load Koha::NorwegianPatronDB, qw( NLMarkForDeletion NLSync );
41 my $input = new CGI;
43 my ($template, $borrowernumber, $cookie)
44 = get_template_and_user({template_name => "members/deletemem.tt",
45 query => $input,
46 type => "intranet",
47 authnotrequired => 0,
48 flagsrequired => {borrowers => 1},
49 debug => 1,
50 });
52 #print $input->header;
53 my $member = $input->param('member');
55 #Do not delete yourself...
56 if ($borrowernumber == $member ) {
57 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_YOURSELF");
58 exit 0; # Exit without error
61 # Handle deletion from the Norwegian national patron database, if it is enabled
62 # If the "deletelocal" parameter is set to "false", the regular deletion will be
63 # short circuited, and only a deletion from the national database can be carried
64 # out. If "deletelocal" is set to "true", or not set to anything normal
65 # deletion will be done.
66 my $deletelocal = $input->param('deletelocal') eq 'false' ? 0 : 1; # Deleting locally is the default
67 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
68 if ( $input->param('deleteremote') eq 'true' ) {
69 # Mark for deletion, then try a live sync
70 NLMarkForDeletion( $member );
71 NLSync({ 'borrowernumber' => $member });
75 my $issues = GetPendingIssues($member); # FIXME: wasteful call when really, we only want the count
76 my $countissues = scalar(@$issues);
78 my $bor = C4::Members::GetMember( borrowernumber => $member );
79 my $flags = C4::Members::patronflags( $bor );
80 my $userenv = C4::Context->userenv;
84 if ($bor->{category_type} eq "S") {
85 unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
86 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
87 exit 0; # Exit without error
89 } else {
90 unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>1})) {
91 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
92 exit 0; # Exit without error
96 if (C4::Context->preference("IndependentBranches")) {
97 my $userenv = C4::Context->userenv;
98 if ( !C4::Context->IsSuperLibrarian() && $bor->{'branchcode'}){
99 unless ($userenv->{branch} eq $bor->{'branchcode'}){
100 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
101 exit 0; # Exit without error
106 my $op = $input->param('op') || 'delete_confirm';
107 my $dbh = C4::Context->dbh;
108 my $is_guarantor = $dbh->selectrow_array("SELECT COUNT(*) FROM borrowers WHERE guarantorid=?", undef, $member);
109 if ( $op eq 'delete_confirm' or $countissues > 0 or $flags->{'CHARGES'} or $is_guarantor or $deletelocal == 0) {
110 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
111 $template->param( picture => 1 ) if $patron_image;
113 $template->param(borrowernumber => $member,
114 surname => $bor->{'surname'},
115 title => $bor->{'title'},
116 cardnumber => $bor->{'cardnumber'},
117 firstname => $bor->{'firstname'},
118 categorycode => $bor->{'categorycode'},
119 category_type => $bor->{'category_type'},
120 categoryname => $bor->{'description'},
121 address => $bor->{'address'},
122 address2 => $bor->{'address2'},
123 city => $bor->{'city'},
124 zipcode => $bor->{'zipcode'},
125 country => $bor->{'country'},
126 phone => $bor->{'phone'},
127 email => $bor->{'email'},
128 branchcode => $bor->{'branchcode'},
129 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
130 RoutingSerials => C4::Context->preference('RoutingSerials'),
132 if ($countissues >0) {
133 $template->param(ItemsOnIssues => $countissues);
135 if ($flags->{'CHARGES'} ne '') {
136 $template->param(charges => $flags->{'CHARGES'}->{'amount'});
138 if ($is_guarantor) {
139 $template->param(guarantees => 1);
141 if ($deletelocal == 0) {
142 $template->param(keeplocal => 1);
144 # This is silly written but reflect the same conditions as above
145 if ( not $countissues > 0 and not $flags->{CHARGES} ne '' and not $is_guarantor and not $deletelocal == 0 ) {
146 $template->param(
147 op => 'delete_confirm',
148 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
151 } elsif ( $op eq 'delete_confirmed' ) {
153 die "Wrong CSRF token"
154 unless Koha::Token->new->check_csrf( {
155 session_id => $input->cookie('CGISESSID'),
156 token => scalar $input->param('csrf_token'),
158 my $patron = Koha::Patrons->find( $member );
159 $patron->move_to_deleted;
160 $patron->delete;
161 # TODO Tell the user everything went ok
162 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
163 exit 0; # Exit without error
166 output_html_with_http_headers $input, $cookie, $template->output;