Bug 7813: Ability to delete local cover images
[koha.git] / svc / cover_images
1 #!/usr/bin/perl
2
3 # Copyright 2013 Universidad Nacional de Cordoba
4 #                Tomas Cohen Arazi
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #
21
22 use Modern::Perl;
23
24 use CGI;
25 use C4::Auth qw/check_cookie_auth/;
26 use C4::Images;
27 use JSON qw/to_json/;
28
29 my $input = new CGI;
30
31 my ( $auth_status, $sessionID ) =
32         check_cookie_auth(
33             $input->cookie('CGISESSID'),
34             { tools => 'upload_local_cover_images' } );
35
36 if ( $auth_status ne "ok" ) {
37     exit 0;
38 }
39
40 my $action       = $input->param('action');
41 my $biblionumber = $input->param('biblionumber');
42 my @imagenumbers = $input->param('imagenumber');
43
44 # Array to store the reponse JSON
45 my $response = [];
46
47 if ( $action eq "delete" ) {
48     # Build a hash of valid imagenumbers fr the given biblionumber
49     my %valid_imagenumbers = map {$_ => 1} ListImagesForBiblio($biblionumber);
50
51     foreach my $imagenumber ( @imagenumbers ) {
52         if ( exists( $valid_imagenumbers{ $imagenumber } ) ) {
53             DelImage($imagenumber);
54             push @$response, {
55                 imagenumber => $imagenumber,
56                 deleted => 1
57             };
58         } else {
59             push @$response, {
60                 imagenumber => $imagenumber,
61                 deleted => 0,
62                 error => "MSG_INVALID_IMAGENUMBER"
63             };
64         }
65     }
66 } else {
67     # invalid action
68     exit 0;
69 }
70
71 binmode STDOUT, ":encoding(UTF-8)";
72 print $input->header(
73     -type => 'application/json',
74     -charset => 'UTF-8'
75 );
76
77 print to_json( $response );