4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
35 use Koha::Patron::Image;
39 my ($template, $loggedinuser, $cookie)
40 = get_template_and_user({template_name => "tools/picture-upload.tt",
44 flagsrequired => { tools => 'batch_upload_patron_images'},
48 my $filetype = $input->param('filetype');
49 my $cardnumber = $input->param('cardnumber');
50 my $uploadfilename = $input->param('uploadfile');
51 my $uploadfile = $input->upload('uploadfile');
52 my $borrowernumber = $input->param('borrowernumber');
53 my $op = $input->param('op') || '';
55 #FIXME: This code is really in the rough. The variables need to be re-scoped as the two subs depend on global vars to operate.
56 # Other parts of this code could be optimized as well, I think. Perhaps the file upload could be done with YUI's upload
59 $debug and warn "Params are: filetype=$filetype, cardnumber=$cardnumber, borrowernumber=$borrowernumber, uploadfile=$uploadfilename";
63 picture-upload.pl - Script for handling uploading of both single and bulk patronimages and importing them into the database.
71 This script is called and presents the user with an interface allowing him/her to upload a single patron image or bulk patron images via a zip file.
72 Files greater than 100K will be refused. Images should be 140x200 pixels. If they are larger they will be auto-resized to comply.
76 $debug and warn "Operation requested: $op";
78 my ( $total, $handled, @counts, $tempfile, $tfh, %errors );
80 # Case is important in these operational values as the template must use case to be visually pleasing!
81 if ( ( $op eq 'Upload' ) && $uploadfile ) {
82 my $dirname = File::Temp::tempdir( CLEANUP => 1 );
83 $debug and warn "dirname = $dirname";
85 if ( $uploadfilename =~ m/(\..+)$/i ) {
89 File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 );
90 $debug and warn "tempfile = $tempfile";
91 my ( @directories, $results );
94 if ( $uploadfilename !~ /\.zip$/i && $filetype =~ m/zip/i );
95 $errors{'NOWRITETEMP'} = 1 unless ( -w $dirname );
96 $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 );
99 $template->param( ERRORS => [ \%errors ] );
100 output_html_with_http_headers $input, $cookie, $template->output;
103 while (<$uploadfile>) {
107 if ( $filetype eq 'zip' ) {
108 unless ( system( "unzip", $tempfile, '-d', $dirname ) == 0 ) {
109 $errors{'UZIPFAIL'} = $uploadfilename;
110 $template->param( ERRORS => [ \%errors ] );
111 # This error is fatal to the import, so bail out here
112 output_html_with_http_headers $input, $cookie, $template->output;
115 push @directories, "$dirname";
116 foreach my $recursive_dir (@directories) {
117 opendir RECDIR, $recursive_dir;
118 while ( my $entry = readdir RECDIR ) {
119 push @directories, "$recursive_dir/$entry"
120 if ( -d "$recursive_dir/$entry" and $entry !~ /^\./ );
121 $debug and warn "$recursive_dir/$entry";
125 foreach my $dir (@directories) {
126 $results = handle_dir( $dir, $filesuffix, $template );
127 $handled++ if $results == 1;
129 $total = scalar @directories;
132 #if ($filetype eq 'zip' )
133 $results = handle_dir( $dirname, $filesuffix, $template, $cardnumber,
135 $handled++ if $results == 1;
139 if ( $results!=1 || %errors ) {
140 $template->param( ERRORS => [$results] );
144 map { $filecount += $_->{count} } @counts;
145 $debug and warn "Total directories processed: $total";
146 $debug and warn "Total files processed: $filecount";
151 TCOUNTS => ( $filecount > 0 ? $filecount : undef ),
153 $template->param( borrowernumber => $borrowernumber )
157 elsif ( ( $op eq 'Upload' ) && !$uploadfile ) {
158 warn "Problem uploading file or no file uploaded.";
159 $template->param( cardnumber => $cardnumber );
160 $template->param( filetype => $filetype );
162 elsif ( $op eq 'Delete' ) {
163 my $dberror = RmPatronImage($borrowernumber);
164 $debug and warn "Patron image deleted for $borrowernumber";
165 warn "Database returned $dberror" if $dberror;
167 if ( $borrowernumber && !%errors && !$template->param('ERRORS') ) {
168 print $input->redirect(
169 "/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
172 output_html_with_http_headers $input, $cookie, $template->output;
176 my ( $dir, $suffix, $template, $cardnumber, $source ) = @_;
177 my ( %counts, %direrrors );
178 $debug and warn "Entering sub handle_dir; passed \$dir=$dir, \$suffix=$suffix";
179 if ( $suffix =~ m/zip/i ) {
180 # If we were sent a zip file, process any included data/idlink.txt files
181 my ( $file, $filename );
183 $debug and warn "Passed a zip file.";
185 while ( my $filename = readdir DIR ) {
186 $file = "$dir/$filename"
187 if ( $filename =~ m/datalink\.txt/i
188 || $filename =~ m/idlink\.txt/i );
190 unless ( open( FILE, $file ) ) {
191 warn "Opening $dir/$file failed!";
192 $direrrors{'OPNLINK'} = $file;
193 # This error is fatal to the import of this directory contents
194 # so bail and return the error to the caller
198 while ( my $line = <FILE> ) {
199 $debug and warn "Reading contents of $file";
201 $debug and warn "Examining line: $line";
202 my $delim = ( $line =~ /\t/ ) ? "\t" : ( $line =~ /,/ ) ? "," : "";
203 $debug and warn "Delimeter is \'$delim\'";
204 unless ( $delim eq "," || $delim eq "\t" ) {
205 warn "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
206 $direrrors{'DELERR'} = 1;
207 # This error is fatal to the import of this directory contents
208 # so bail and return the error to the caller
211 ( $cardnumber, $filename ) = split $delim, $line;
212 $cardnumber =~ s/[\"\r\n]//g; # remove offensive characters
213 $filename =~ s/[\"\r\n\s]//g;
214 $debug and warn "Cardnumber: $cardnumber Filename: $filename";
215 $source = "$dir/$filename";
216 %counts = handle_file( $cardnumber, $source, $template, %counts );
222 %counts = handle_file( $cardnumber, $source, $template, %counts );
224 push @counts, \%counts;
229 my ( $cardnumber, $source, $template, %count ) = @_;
230 $debug and warn "Entering sub handle_file; passed \$cardnumber=$cardnumber, \$source=$source";
231 $count{filenames} = () if !$count{filenames};
232 $count{source} = $source if !$count{source};
233 $count{count} = 0 unless exists $count{count};
236 if ( $filetype eq 'image' ) {
237 $filename = $uploadfilename;
240 $filename = $1 if ( $source && $source =~ /\/([^\/]+)$/ );
242 if ( $cardnumber && $source ) {
243 # Now process any imagefiles
244 $debug and warn "Source: $source";
245 my $size = ( stat($source) )[7];
246 if ( $size > 550000 ) {
247 # This check is necessary even with image resizing to avoid possible security/performance issues...
248 $filerrors{'OVRSIZ'} = 1;
249 push my @filerrors, \%filerrors;
250 push @{ $count{filenames} },
252 filerrors => \@filerrors,
254 cardnumber => $cardnumber
256 $template->param( ERRORS => 1 );
257 # this one is fatal so bail here...
260 my ( $srcimage, $image );
261 if ( open( IMG, "$source" ) ) {
262 $srcimage = GD::Image->new(*IMG);
264 if ( defined $srcimage ) {
266 my $mimetype = 'image/png';
267 # GD autodetects three basic image formats: PNG, JPEG, XPM
268 # we will convert all to PNG which is lossless...
269 # Check the pixel size of the image we are about to import...
270 my ( $width, $height ) = $srcimage->getBounds();
271 $debug and warn "$filename is $width pix X $height pix.";
272 if ( $width > 200 || $height > 300 ) {
273 # MAX pixel dims are 200 X 300...
274 $debug and warn "$filename exceeds the maximum pixel dimensions of 200 X 300. Resizing...";
275 # Percent we will reduce the image dimensions by...
277 if ( $width > 200 ) {
278 # If the width is oversize, scale based on width overage...
279 $percent_reduce = sprintf( "%.5f", ( 140 / $width ) );
282 # otherwise scale based on height overage.
283 $percent_reduce = sprintf( "%.5f", ( 200 / $height ) );
286 sprintf( "%.0f", ( $width * $percent_reduce ) );
288 sprintf( "%.0f", ( $height * $percent_reduce ) );
290 and warn "Reducing $filename by "
291 . ( $percent_reduce * 100 )
292 . "\% or to $width_reduce pix X $height_reduce pix";
293 #'1' creates true color image...
294 $image = GD::Image->new( $width_reduce, $height_reduce, 1 );
295 $image->copyResampled( $srcimage, 0, 0, 0, 0, $width_reduce,
296 $height_reduce, $width, $height );
297 $imgfile = $image->png();
299 and warn "$filename is "
301 . " bytes after resizing.";
303 undef $srcimage; # This object can get big...
307 $imgfile = $image->png();
309 and warn "$filename is " . length($imgfile) . " bytes.";
311 undef $srcimage; # This object can get big...
313 $debug and warn "Image is of mimetype $mimetype";
316 my $patron = Koha::Patrons->find({ cardnumber => $cardnumber });
318 my $image = $patron->image;
319 $image ||= Koha::Patron::Image->new({ borrowernumber => $patron->borrowernumber });
321 mimetype => $mimetype,
322 imagefile => $imgfile,
324 eval { $image->store };
326 # Errors from here on are fatal only to the import of a particular image
327 #so don't bail, just note the error and keep going
328 warn "Database returned error: $@";
329 $filerrors{'DBERR'} = 1;
330 push my @filerrors, \%filerrors;
331 push @{ $count{filenames} },
333 filerrors => \@filerrors,
335 cardnumber => $cardnumber
337 $template->param( ERRORS => 1 );
340 push @{ $count{filenames} },
341 { source => $filename, cardnumber => $cardnumber };
344 warn "Patron with the cardnumber '$cardnumber' does not exist";
345 $filerrors{'CARDNUMBER_DOES_NOT_EXIST'} = 1;
346 push my @filerrors, \%filerrors;
347 push @{ $count{filenames} },
349 filerrors => \@filerrors,
351 cardnumber => $cardnumber
353 $template->param( ERRORS => 1 );
357 warn "Unable to determine mime type of $filename. Please verify mimetype.";
358 $filerrors{'MIMERR'} = 1;
359 push my @filerrors, \%filerrors;
360 push @{ $count{filenames} },
362 filerrors => \@filerrors,
364 cardnumber => $cardnumber
366 $template->param( ERRORS => 1 );
370 warn "Contents of $filename corrupted!";
372 $filerrors{'CORERR'} = 1;
373 push my @filerrors, \%filerrors;
374 push @{ $count{filenames} },
376 filerrors => \@filerrors,
378 cardnumber => $cardnumber
380 $template->param( ERRORS => 1 );
384 warn "Opening $source failed!";
385 $filerrors{'OPNERR'} = 1;
386 push my @filerrors, \%filerrors;
387 push @{ $count{filenames} },
389 filerrors => \@filerrors,
391 cardnumber => $cardnumber
393 $template->param( ERRORS => 1 );
397 # The need for this seems a bit unlikely, however, to maximize error trapping it is included
402 : ( $filename ? "cardnumber" : "cardnumber and filename" )
404 $filerrors{'CRDFIL'} = (
407 : ( $filename ? "cardnumber" : "cardnumber and filename" )
409 push my @filerrors, \%filerrors;
410 push @{ $count{filenames} },
412 filerrors => \@filerrors,
414 cardnumber => $cardnumber
416 $template->param( ERRORS => 1 );
423 Original contributor(s) undocumented
425 Database storage, single patronimage upload option, and extensive error trapping contributed by Chris Nighswonger cnighswonger <at> foundations <dot> edu
426 Image scaling/resizing contributed by the same.