Bug 12497: Fix search history non-accessible when OPAC was private
[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 strict;
25 #use warnings; FIXME - Bug 2505
26
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::Token;
35
36 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
37     load Koha::NorwegianPatronDB, qw( NLMarkForDeletion NLSync );
38 }
39
40 my $input = new CGI;
41
42 my ($template, $borrowernumber, $cookie)
43                 = get_template_and_user({template_name => "members/deletemem.tt",
44                                         query => $input,
45                                         type => "intranet",
46                                         authnotrequired => 0,
47                                         flagsrequired => {borrowers => 1},
48                                         debug => 1,
49                                         });
50
51 #print $input->header;
52 my $member       = $input->param('member');
53
54 #Do not delete yourself...
55 if ($borrowernumber == $member ) {
56     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_YOURSELF");
57     exit 0; # Exit without error
58 }
59
60 # Handle deletion from the Norwegian national patron database, if it is enabled
61 # If the "deletelocal" parameter is set to "false", the regular deletion will be
62 # short circuited, and only a deletion from the national database can be carried
63 # out. If "deletelocal" is set to "true", or not set to anything normal
64 # deletion will be done.
65 my $deletelocal  = $input->param('deletelocal')  eq 'false' ? 0 : 1; # Deleting locally is the default
66 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
67     if ( $input->param('deleteremote') eq 'true' ) {
68         # Mark for deletion, then try a live sync
69         NLMarkForDeletion( $member );
70         NLSync({ 'borrowernumber' => $member });
71     }
72 }
73
74 my $issues = GetPendingIssues($member);     # FIXME: wasteful call when really, we only want the count
75 my $countissues = scalar(@$issues);
76
77 my $patron = Koha::Patrons->find( $member );
78 unless ( $patron ) {
79     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
80     exit;
81 }
82 my $flags = C4::Members::patronflags( $patron->unblessed );
83 my $userenv = C4::Context->userenv;
84
85  
86
87 if ($patron->category->category_type eq "S") {
88     unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
89         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
90         exit 0; # Exit without error
91     }
92 } else {
93     unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>1})) {
94         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
95         exit 0; # Exit without error
96     }
97 }
98
99 if (C4::Context->preference("IndependentBranches")) {
100     my $userenv = C4::Context->userenv;
101     if ( !C4::Context->IsSuperLibrarian() && $patron->branchcode){
102         unless ($userenv->{branch} eq $patron->branchcode){
103             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
104             exit 0; # Exit without error
105         }
106     }
107 }
108
109 my $op = $input->param('op') || 'delete_confirm';
110 my $dbh = C4::Context->dbh;
111 my $is_guarantor = $dbh->selectrow_array("SELECT COUNT(*) FROM borrowers WHERE guarantorid=?", undef, $member);
112 if ( $op eq 'delete_confirm' or $countissues > 0 or $flags->{'CHARGES'}  or $is_guarantor or $deletelocal == 0) {
113     $template->param( picture => 1 ) if $patron->image;
114
115     $template->param( adultborrower => 1 ) if $patron->category->category_type =~ /^(A|I)$/;
116
117     $template->param(
118         # FIXME The patron object should be passed to the template
119         borrowernumber => $patron->borrowernumber,
120         surname => $patron->surname,
121         title => $patron->title,
122         cardnumber => $patron->cardnumber,
123         firstname => $patron->firstname,
124         categorycode => $patron->categorycode,
125         category_type => $patron->category->category_type,
126         categoryname  => $patron->category->description,
127         address => $patron->address,
128         address2 => $patron->address2,
129         city => $patron->city,
130         zipcode => $patron->zipcode,
131         country => $patron->country,
132         phone => $patron->phone,
133         email => $patron->email,
134         branchcode => $patron->branchcode,
135         RoutingSerials => C4::Context->preference('RoutingSerials'),
136     );
137     if ($countissues >0) {
138         $template->param(ItemsOnIssues => $countissues);
139     }
140     if ($flags->{'CHARGES'} ne '') {
141         $template->param(charges => $flags->{'CHARGES'}->{'amount'});
142     }
143     if ($is_guarantor) {
144         $template->param(guarantees => 1);
145     }
146     if ($deletelocal == 0) {
147         $template->param(keeplocal => 1);
148     }
149     # This is silly written but reflect the same conditions as above
150     if ( not $countissues > 0 and not $flags->{CHARGES} ne '' and not $is_guarantor and not $deletelocal == 0 ) {
151         $template->param(
152             op         => 'delete_confirm',
153             csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
154         );
155     }
156 } elsif ( $op eq 'delete_confirmed' ) {
157
158     die "Wrong CSRF token"
159         unless Koha::Token->new->check_csrf( {
160             session_id => $input->cookie('CGISESSID'),
161             token  => scalar $input->param('csrf_token'),
162         });
163     my $patron = Koha::Patrons->find( $member );
164     $patron->move_to_deleted;
165     $patron->delete;
166     # TODO Tell the user everything went ok
167     print $input->redirect("/cgi-bin/koha/members/members-home.pl");
168     exit 0; # Exit without error
169 }
170
171 output_html_with_http_headers $input, $cookie, $template->output;