Bug 22082: ambiguous column in patron stats
[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 strict;
41 use warnings;
42
43 use File::Temp;
44 use CGI qw ( -utf8 );
45 use GD;
46 use C4::Context;
47 use C4::Auth;
48 use C4::Output;
49 use C4::Images;
50 use Koha::UploadedFiles;
51 use C4::Log;
52
53 my $debug = 1;
54
55 my $input = new CGI;
56
57 my $fileID = $input->param('uploadedfileid');
58 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
59     {
60         template_name   => "tools/upload-images.tt",
61         query           => $input,
62         type            => "intranet",
63         authnotrequired => 0,
64         flagsrequired   => { tools => 'upload_local_cover_images' },
65         debug           => 0,
66     }
67 );
68
69 my $filetype       = $input->param('filetype');
70 my $biblionumber   = $input->param('biblionumber');
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->{VARS}->{'filetype'}     = $filetype;
81 $template->{VARS}->{'biblionumber'} = $biblionumber;
82
83 my $total = 0;
84
85 if ($fileID) {
86     my $upload = Koha::UploadedFiles->find( $fileID );
87     if ( $filetype eq 'image' ) {
88         my $fh       = $upload->file_handle;
89         my $srcimage = GD::Image->new($fh);
90         $fh->close if $fh;
91         if ( defined $srcimage ) {
92             my $dberror = PutImage( $biblionumber, $srcimage, $replace );
93             if ($dberror) {
94                 $error = 'DBERR';
95             }
96             else {
97                 $total = 1;
98             }
99         }
100         else {
101             $error = 'OPNIMG';
102         }
103         undef $srcimage;
104     }
105     else {
106         my $filename = $upload->full_path;
107         my $dirname = File::Temp::tempdir( CLEANUP => 1 );
108         qx/unzip $filename -d $dirname/;
109         my $exit_code = $?;
110         unless ( $exit_code == 0 ) {
111             $error = 'UZIPFAIL';
112         }
113         else {
114             my @directories;
115             push @directories, "$dirname";
116             foreach my $recursive_dir (@directories) {
117                 my $dir;
118                 opendir $dir, $recursive_dir;
119                 while ( my $entry = readdir $dir ) {
120                     push @directories, "$recursive_dir/$entry"
121                       if ( -d "$recursive_dir/$entry" and $entry !~ /^[._]/ );
122                 }
123                 closedir $dir;
124             }
125             foreach my $dir (@directories) {
126                 my $file;
127                 if ( -e "$dir/idlink.txt" ) {
128                     $file = "$dir/idlink.txt";
129                 }
130                 elsif ( -e "$dir/datalink.txt" ) {
131                     $file = "$dir/datalink.txt";
132                 }
133                 else {
134                     next;
135                 }
136                 if ( open( FILE, $file ) ) {
137                     while ( my $line = <FILE> ) {
138                         my $delim =
139                             ( $line =~ /\t/ ) ? "\t"
140                           : ( $line =~ /,/ )  ? ","
141                           :                     "";
142
143                         #$debug and warn "Delimeter is \'$delim\'";
144                         unless ( $delim eq "," || $delim eq "\t" ) {
145                             warn
146 "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
147                             $error = 'DELERR';
148                         }
149                         else {
150                             ( $biblionumber, $filename ) = split $delim, $line, 2;
151                             $biblionumber =~
152                               s/[\"\r\n]//g;    # remove offensive characters
153                             $filename =~ s/[\"\r\n]//g;
154                             $filename =~ s/^\s+//;
155                             $filename =~ s/\s+$//;
156                             if (C4::Context->preference("CataloguingLog")) {
157                                 logaction('CATALOGUING', 'MODIFY', $biblionumber, "biblio cover image: $filename");
158                             }
159                             my $srcimage = GD::Image->new("$dir/$filename");
160                             if ( defined $srcimage ) {
161                                 $total++;
162                                 my $dberror =
163                                   PutImage( $biblionumber, $srcimage,
164                                     $replace );
165                                 if ($dberror) {
166                                     $error = 'DBERR';
167                                 }
168                             }
169                             else {
170                                 $error = 'OPNIMG';
171                             }
172                             undef $srcimage;
173                         }
174                     }
175                     close(FILE);
176                 }
177                 else {
178                     $error = 'OPNLINK';
179                 }
180             }
181         }
182     }
183     $template->{VARS}->{'total'}        = $total;
184     $template->{VARS}->{'uploadimage'}  = 1;
185     $template->{VARS}->{'error'}        = $error;
186     $template->{VARS}->{'biblionumber'} = $biblionumber;
187 }
188
189 output_html_with_http_headers $input, $cookie, $template->output;
190
191 exit 0;
192
193 =head1 AUTHORS
194
195 Written by Jared Camins-Esakov of C & P Bibliography Services, in part based on
196 code by Koustubha Kale of Anant Corporation and Chris Nighswonger of Foundation
197 Bible College.
198
199 =cut