Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / tools / upload-cover-image.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2011 C & P Bibliography Services
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 #
20 #
21 #
22
23 =head1 NAME
24
25 upload-cover-image.pl - Script for handling uploading of both single and bulk coverimages and importing them into the database.
26
27 =head1 SYNOPSIS
28
29 upload-cover-image.pl
30
31 =head1 DESCRIPTION
32
33 This script is called and presents the user with an interface allowing him/her to upload a single cover image or bulk cover images via a zip file.
34 Images will be resized into thumbnails of 140x200 pixels and larger images of
35 800x600 pixels. If the images that are uploaded are larger, they will be
36 resized, maintaining aspect ratio.
37
38 =cut
39
40 use Modern::Perl;
41
42 use File::Temp;
43 use CGI qw ( -utf8 );
44 use GD;
45 use C4::Context;
46 use C4::Auth;
47 use C4::Output;
48 use Koha::Biblios;
49 use Koha::CoverImages;
50 use Koha::Items;
51 use Koha::UploadedFiles;
52 use C4::Log;
53
54 my $debug = 1;
55
56 my $input = CGI->new;
57
58 my $fileID = $input->param('uploadedfileid');
59 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60     {
61         template_name   => "tools/upload-images.tt",
62         query           => $input,
63         type            => "intranet",
64         flagsrequired   => { tools => 'upload_local_cover_images' },
65     }
66 );
67
68 my $filetype       = $input->param('filetype');
69 my $biblionumber   = $input->param('biblionumber');
70 my $itemnumber     = $input->param('itemnumber');
71 #my $uploadfilename = $input->param('uploadfile'); # obsolete?
72 my $replace        = !C4::Context->preference("AllowMultipleCovers")
73   || $input->param('replace');
74 my $op        = $input->param('op');
75 my %cookies   = parse CGI::Cookie($cookie);
76 my $sessionID = $cookies{'CGISESSID'}->value;
77
78 my $error;
79
80 $template->param(
81     filetype     => $filetype,
82     biblionumber => $biblionumber,
83     itemnumber   => $itemnumber,
84 );
85
86 my $total = 0;
87
88 if ($fileID) {
89     my $upload = Koha::UploadedFiles->find( $fileID );
90     if ( $filetype eq 'image' ) {
91         my $fh       = $upload->file_handle;
92         my $srcimage = GD::Image->new($fh);
93         $fh->close if $fh;
94         if ( defined $srcimage ) {
95             eval {
96                 if ( $replace ) {
97                     if ( $biblionumber ) {
98                         Koha::Biblios->find($biblionumber)->cover_images->delete;
99                     } elsif ( $itemnumber ) {
100                         Koha::Items->find($itemnumber)->cover_images->delete;
101                     }
102                 }
103
104                 Koha::CoverImage->new(
105                     {
106                         biblionumber => $biblionumber,
107                         itemnumber   => $itemnumber,
108                         src_image    => $srcimage
109                     }
110                 )->store;
111             };
112
113             if ($@) {
114                 warn $@;
115                 $error = 'DBERR';
116             }
117             else {
118                 $total = 1;
119             }
120         }
121         else {
122             $error = 'OPNIMG';
123         }
124         undef $srcimage;
125     }
126     else {
127         my $filename = $upload->full_path;
128         my $dirname = File::Temp::tempdir( CLEANUP => 1 );
129         qx/unzip $filename -d $dirname/;
130         my $exit_code = $?;
131         unless ( $exit_code == 0 ) {
132             $error = 'UZIPFAIL';
133         }
134         else {
135             my @directories;
136             push @directories, "$dirname";
137             foreach my $recursive_dir (@directories) {
138                 my $dir;
139                 opendir $dir, $recursive_dir;
140                 while ( my $entry = readdir $dir ) {
141                     push @directories, "$recursive_dir/$entry"
142                       if ( -d "$recursive_dir/$entry" and $entry !~ /^[._]/ );
143                 }
144                 closedir $dir;
145             }
146             foreach my $dir (@directories) {
147                 my $file;
148                 if ( -e "$dir/idlink.txt" ) {
149                     $file = "$dir/idlink.txt";
150                 }
151                 elsif ( -e "$dir/datalink.txt" ) {
152                     $file = "$dir/datalink.txt";
153                 }
154                 else {
155                     next;
156                 }
157                 if ( open( my $fh, '<', $file ) ) {
158                     while ( my $line = <$fh> ) {
159                         my $delim =
160                             ( $line =~ /\t/ ) ? "\t"
161                           : ( $line =~ /,/ )  ? ","
162                           :                     "";
163
164                         #$debug and warn "Delimeter is \'$delim\'";
165                         unless ( $delim eq "," || $delim eq "\t" ) {
166                             warn
167 "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
168                             $error = 'DELERR';
169                         }
170                         else {
171                             ( $biblionumber, $filename ) = split $delim, $line, 2;
172                             $biblionumber =~
173                               s/[\"\r\n]//g;    # remove offensive characters
174                             $filename =~ s/[\"\r\n]//g;
175                             $filename =~ s/^\s+//;
176                             $filename =~ s/\s+$//;
177                             if (C4::Context->preference("CataloguingLog")) {
178                                 logaction('CATALOGUING', 'MODIFY', $biblionumber, "biblio cover image: $filename");
179                             }
180                             my $srcimage = GD::Image->new("$dir/$filename");
181                             if ( defined $srcimage ) {
182                                 $total++;
183                                 eval {
184                                     if ( $replace ) {
185                                         if ( $biblionumber ) {
186                                             Koha::Biblios->find($biblionumber)->cover_images->delete;
187                                         } elsif ( $itemnumber ) {
188                                             Koha::Items->find($itemnumber)->cover_images->delete;
189                                         }
190                                     }
191
192                                     Koha::CoverImage->new(
193                                         {
194                                             biblionumber => $biblionumber,
195                                             itemnumber   => $itemnumber,
196                                             src_image    => $srcimage
197                                         }
198                                     )->store;
199                                 };
200
201                                 if ($@) {
202                                     $error = 'DBERR';
203                                 }
204                             }
205                             else {
206                                 $error = 'OPNIMG';
207                             }
208                             undef $srcimage;
209                         }
210                     }
211                     close($fh);
212                 }
213                 else {
214                     $error = 'OPNLINK';
215                 }
216             }
217         }
218     }
219
220     $template->param(
221         total        => $total,
222         uploadimage  => 1,
223         error        => $error,
224         biblionumber => $biblionumber || Koha::Items->find($itemnumber)->biblionumber,
225         itemnumber   => $itemnumber,
226     );
227 }
228
229 output_html_with_http_headers $input, $cookie, $template->output;
230
231 exit 0;
232
233 =head1 AUTHORS
234
235 Written by Jared Camins-Esakov of C & P Bibliography Services, in part based on
236 code by Koustubha Kale of Anant Corporation and Chris Nighswonger of Foundation
237 Bible College.
238
239 =cut