Bug 27045: Fix other exports using CSV profiles
[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 qw( get_template_and_user );
47 use C4::Output qw( output_html_with_http_headers );
48 use Koha::Biblios;
49 use Koha::CoverImages;
50 use Koha::Items;
51 use Koha::UploadedFiles;
52 use C4::Log qw( logaction );
53
54 my $input = CGI->new;
55
56 my $fileID = $input->param('uploadedfileid');
57 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
58     {
59         template_name   => "tools/upload-images.tt",
60         query           => $input,
61         type            => "intranet",
62         flagsrequired   => { tools => 'upload_local_cover_images' },
63     }
64 );
65
66 my $filetype       = $input->param('filetype');
67 my $biblionumber   = $input->param('biblionumber');
68 my $itemnumber     = $input->param('itemnumber');
69 #my $uploadfilename = $input->param('uploadfile'); # obsolete?
70 my $replace        = !C4::Context->preference("AllowMultipleCovers")
71   || $input->param('replace');
72 my $op        = $input->param('op');
73
74 my $error;
75
76 $template->param(
77     filetype     => $filetype,
78     biblionumber => $biblionumber,
79     itemnumber   => $itemnumber,
80 );
81
82 my $total = 0;
83
84 if ($fileID) {
85     my $upload = Koha::UploadedFiles->find( $fileID );
86     if ( $filetype eq 'image' ) {
87         my $fh       = $upload->file_handle;
88         my $srcimage = GD::Image->new($fh);
89         $fh->close if $fh;
90         if ( defined $srcimage ) {
91             eval {
92                 if ( $replace ) {
93                     if ( $biblionumber ) {
94                         Koha::Biblios->find($biblionumber)->cover_images->delete;
95                     } elsif ( $itemnumber ) {
96                         Koha::Items->find($itemnumber)->cover_images->delete;
97                     }
98                 }
99
100                 Koha::CoverImage->new(
101                     {
102                         biblionumber => $biblionumber,
103                         itemnumber   => $itemnumber,
104                         src_image    => $srcimage
105                     }
106                 )->store;
107             };
108
109             if ($@) {
110                 warn $@;
111                 $error = 'DBERR';
112             }
113             else {
114                 $total = 1;
115             }
116         }
117         else {
118             $error = 'OPNIMG';
119         }
120         undef $srcimage;
121     }
122     else {
123         my $filename = $upload->full_path;
124         my $dirname = File::Temp::tempdir( CLEANUP => 1 );
125         qx/unzip $filename -d $dirname/;
126         my $exit_code = $?;
127         unless ( $exit_code == 0 ) {
128             $error = 'UZIPFAIL';
129         }
130         else {
131             my @directories;
132             push @directories, "$dirname";
133             foreach my $recursive_dir (@directories) {
134                 my $dir;
135                 opendir $dir, $recursive_dir;
136                 while ( my $entry = readdir $dir ) {
137                     push @directories, "$recursive_dir/$entry"
138                       if ( -d "$recursive_dir/$entry" and $entry !~ /^[._]/ );
139                 }
140                 closedir $dir;
141             }
142             foreach my $dir (@directories) {
143                 my $file;
144                 if ( -e "$dir/idlink.txt" ) {
145                     $file = "$dir/idlink.txt";
146                 }
147                 elsif ( -e "$dir/datalink.txt" ) {
148                     $file = "$dir/datalink.txt";
149                 }
150                 else {
151                     next;
152                 }
153                 if ( open( my $fh, '<', $file ) ) {
154                     while ( my $line = <$fh> ) {
155                         my $delim =
156                             ( $line =~ /\t/ ) ? "\t"
157                           : ( $line =~ /,/ )  ? ","
158                           :                     "";
159
160                         unless ( $delim eq "," || $delim eq "\t" ) {
161                             warn
162 "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
163                             $error = 'DELERR';
164                         }
165                         else {
166                             ( $biblionumber, $filename ) = split $delim, $line, 2;
167                             $biblionumber =~
168                               s/[\"\r\n]//g;    # remove offensive characters
169                             $filename =~ s/[\"\r\n]//g;
170                             $filename =~ s/^\s+//;
171                             $filename =~ s/\s+$//;
172                             if (C4::Context->preference("CataloguingLog")) {
173                                 logaction('CATALOGUING', 'MODIFY', $biblionumber, "biblio cover image: $filename");
174                             }
175                             my $srcimage = GD::Image->new("$dir/$filename");
176                             if ( defined $srcimage ) {
177                                 $total++;
178                                 eval {
179                                     if ( $replace ) {
180                                         if ( $biblionumber ) {
181                                             Koha::Biblios->find($biblionumber)->cover_images->delete;
182                                         } elsif ( $itemnumber ) {
183                                             Koha::Items->find($itemnumber)->cover_images->delete;
184                                         }
185                                     }
186
187                                     Koha::CoverImage->new(
188                                         {
189                                             biblionumber => $biblionumber,
190                                             itemnumber   => $itemnumber,
191                                             src_image    => $srcimage
192                                         }
193                                     )->store;
194                                 };
195
196                                 if ($@) {
197                                     $error = 'DBERR';
198                                 }
199                             }
200                             else {
201                                 $error = 'OPNIMG';
202                             }
203                             undef $srcimage;
204                         }
205                     }
206                     close($fh);
207                 }
208                 else {
209                     $error = 'OPNLINK';
210                 }
211             }
212         }
213     }
214
215     $template->param(
216         total        => $total,
217         uploadimage  => 1,
218         error        => $error,
219         biblionumber => $biblionumber || Koha::Items->find($itemnumber)->biblionumber,
220         itemnumber   => $itemnumber,
221     );
222 }
223
224 output_html_with_http_headers $input, $cookie, $template->output;
225
226 exit 0;
227
228 =head1 AUTHORS
229
230 Written by Jared Camins-Esakov of C & P Bibliography Services, in part based on
231 code by Koustubha Kale of Anant Corporation and Chris Nighswonger of Foundation
232 Bible College.
233
234 =cut