Bug 20783: Use iframe to embed Youtube videos
[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 use C4::Context;
28 use C4::Output;
29 use C4::Auth;
30 use C4::Members;
31 use Koha::Patrons;
32 use Koha::Token;
33 use Koha::Patron::Categories;
34
35 my $input = new CGI;
36
37 my ($template, $loggedinuser, $cookie)
38                 = get_template_and_user({template_name => "members/deletemem.tt",
39                                         query => $input,
40                                         type => "intranet",
41                                         flagsrequired => {borrowers => 'edit_borrowers'},
42                                         debug => 1,
43                                         });
44
45 #print $input->header;
46 my $member       = $input->param('member');
47
48 #Do not delete yourself...
49 if ( $loggedinuser == $member ) {
50     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_YOURSELF");
51     exit 0; # Exit without error
52 }
53
54 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
55 my $patron         = Koha::Patrons->find( $member );
56 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
57
58 my $charges = $patron->account->non_issues_charges;
59 my $countissues = $patron->checkouts->count;
60 my $userenv = C4::Context->userenv;
61
62 if ($patron->category->category_type eq "S") {
63     unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
64         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
65         exit 0; # Exit without error
66     }
67 } else {
68     unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>'edit_borrowers'})) {
69         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
70         exit 0; # Exit without error
71     }
72 }
73
74 if (C4::Context->preference("IndependentBranches")) {
75     my $userenv = C4::Context->userenv;
76     if ( !C4::Context->IsSuperLibrarian() && $patron->branchcode){
77         unless ($userenv->{branch} eq $patron->branchcode){
78             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
79             exit 0; # Exit without error
80         }
81     }
82 }
83
84 my $op = $input->param('op') || 'delete_confirm';
85 my $dbh = C4::Context->dbh;
86 my $is_guarantor = $dbh->selectrow_array("SELECT COUNT(*) FROM borrowers WHERE guarantorid=?", undef, $member);
87 if ( $op eq 'delete_confirm' or $countissues > 0 or $charges or $is_guarantor ) {
88
89     $template->param(
90         patron => $patron,
91     );
92     if ($countissues >0) {
93         $template->param(ItemsOnIssues => $countissues);
94     }
95     if ( $charges > 0 ) {
96         $template->param(charges => $charges);
97     }
98     if ($is_guarantor) {
99         $template->param(guarantees => 1);
100     }
101
102     # This is silly written but reflect the same conditions as above
103     if ( not $countissues > 0 and not $charges > 0 and not $is_guarantor ) {
104         $template->param(
105             op         => 'delete_confirm',
106             csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
107         );
108     }
109 } elsif ( $op eq 'delete_confirmed' ) {
110
111     output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
112         unless Koha::Token->new->check_csrf( {
113             session_id => $input->cookie('CGISESSID'),
114             token  => scalar $input->param('csrf_token'),
115         });
116     my $patron = Koha::Patrons->find( $member );
117     $patron->move_to_deleted;
118     $patron->delete;
119     # TODO Tell the user everything went ok
120     print $input->redirect("/cgi-bin/koha/members/members-home.pl");
121     exit 0; # Exit without error
122 }
123
124 output_html_with_http_headers $input, $cookie, $template->output;