Bug 17181: Check for duplicate image names when uploading image to patron card creator
[koha.git] / patroncards / image-manage.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use CGI qw ( -utf8 );
7 use Graphics::Magick;
8 use POSIX qw(ceil);
9 use autouse 'Data::Dumper' => qw(Dumper);
10
11 use C4::Context;
12 use C4::Auth;
13 use C4::Output;
14 use C4::Debug;
15 use C4::Creators;
16 use C4::Patroncards;
17 use Data::Dumper;
18
19 my $cgi = CGI->new;
20
21 my ($template, $loggedinuser, $cookie) = get_template_and_user({
22                     template_name       => "patroncards/image-manage.tt",
23                     query               => $cgi,
24                     type                => "intranet",
25                     authnotrequired     => 0,
26                     flagsrequired       => {tools => 'batch_upload_patron_images'}, # FIXME: establish flag for patron card creator
27                     debug               => 0,
28                     });
29
30 my $file_name = $cgi->param('uploadfile') || '';
31 my $image_name = $cgi->param('image_name') || $file_name;
32 my $upload_file = $cgi->upload('uploadfile') || '';
33 my $op = $cgi->param('op') || 'none';
34 my @image_ids = $cgi->multi_param('image_id') if $cgi->param('image_id');
35
36 my $source_file = "$file_name"; # otherwise we end up with what amounts to a pointer to a filehandle rather than a user-friendly filename
37
38 my $display_columns = { image =>    [  #{db column      => {label => 'col label', is link?          }},
39                                         {image_id       => {label => 'ID',      link_field      => 0}},
40                                         {image_name     => {label => 'Name',    link_field      => 0}},
41                                         {_delete        => {label => 'Delete', link_field => 0}},
42                                         {select         => {label => 'Select',  value           => 'image_id'}},
43                                     ],
44 };
45 my $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));
46
47 my $image_limit = C4::Context->preference('ImageLimit') || '';
48 my $errstr = '';        # NOTE: For error codes see error-messages.inc
49
50 if ($op eq 'upload') {
51     # Checking for duplicate image name
52     my $duplicate;
53     my $dbh = C4::Context->dbh;
54     my $query = "SELECT COUNT(*) FROM creator_images WHERE image_name=?";
55     my $sth = $dbh->prepare($query);
56     $sth->execute($image_name);
57     my $count = $sth->fetchrow_arrayref;
58     if ( $count->[0] > 0 ) {
59         $duplicate = 1;
60         warn sprintf('Image name already exists.');
61         $errstr = 304;
62         $template->param(
63             IMPORT_SUCCESSFUL => 0,
64             SOURCE_FILE => $source_file,
65             IMAGE_NAME => $image_name,
66             TABLE => $table,
67             error => $errstr,
68         );
69     }
70     unless ($duplicate) {
71         if (!$upload_file) {
72             warn sprintf('An error occurred while attempting to upload file %s.', $source_file);
73             $errstr = 301;
74             $template->param(
75                 IMPORT_SUCCESSFUL => 0,
76                 SOURCE_FILE => $source_file,
77                 IMAGE_NAME => $image_name,
78                 TABLE => $table,
79                 error => $errstr,
80             );
81         }
82         else {
83             my $image = Graphics::Magick->new;
84             eval{$image->Read($cgi->tmpFileName($file_name));};
85             if ($@) {
86                 warn sprintf('An error occurred while creating the image object: %s',$@);
87                 $errstr = 202;
88                 $template->param(
89                     IMPORT_SUCCESSFUL => 0,
90                     SOURCE_FILE => $source_file,
91                     IMAGE_NAME => $image_name,
92                     TABLE => $table,
93                     error => $errstr,
94                 );
95             }
96             else {
97                 my $errstr = '';
98                 my $size = $image->Get('filesize');
99                 $errstr =  302 if $size > 500000;
100                 $image->Set(magick => 'png'); # convert all images to png as this is a lossless format which is important for resizing operations later on
101                 my $err = put_image($image_name, $image->ImageToBlob()) || '0';
102                 $errstr = 101 if $err == 1;
103                 $errstr = 303 if $err == 202;
104                 if ($errstr) {
105                     $template->param(
106                         IMPORT_SUCCESSFUL => 0,
107                         SOURCE_FILE => $source_file,
108                         IMAGE_NAME => $image_name,
109                         TABLE => $table,
110                         error => $errstr,
111                         image_limit => $image_limit,
112                     );
113                 }
114                 else {
115                     $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));  # refresh table data after successfully performing save operation
116                     $template->param(
117                         IMPORT_SUCCESSFUL => 1,
118                         SOURCE_FILE => $source_file,
119                         IMAGE_NAME => $image_name,
120                         TABLE => $table,
121                     );
122                 }
123             }
124         }
125     }
126 }
127 elsif ($op eq 'delete') {
128     my $err = '';
129     my $errstr = '';
130     if (@image_ids) {
131         $err = rm_image(\@image_ids);
132         $errstr = 102 if $err;
133     }
134     else {
135         warn sprintf('No image ids passed in to delete.');
136         $errstr = 202;
137     }
138     if ($errstr) {
139         $template->param(
140             DELETE_SUCCESSFULL => 0,
141             IMAGE_IDS => join(', ', @image_ids),
142             TABLE => $table,
143             error => $errstr,
144             image_ids => join(',',@image_ids),
145         );
146     }
147     else {
148         $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));  # refresh table data after successfully performing delete operation
149         $template->param(
150             DELETE_SUCCESSFULL => 1,
151             TABLE => $table,
152         );
153     }
154 }
155 elsif ($op eq 'none') {
156     $template->param(
157         IMPORT_SUCCESSFUL => 0,
158         SOURCE_FILE => $source_file,
159         IMAGE_NAME => $image_name,
160         TABLE => $table,
161     );
162 }
163 else { # to trap unsupported operations
164     warn sprintf('Image upload interface called an unsupported operation: %s',$op);
165     $errstr = 201;
166     $template->param(
167         IMPORT_SUCCESSFUL => 0,
168         SOURCE_FILE => $source_file,
169         IMAGE_NAME => $image_name,
170         TABLE => $table,
171         error => $errstr,
172     );
173 }
174
175 output_html_with_http_headers $cgi, $cookie, $template->output;
176
177 __END__
178
179 =head1 NAME
180
181 image-upload.pl - Script for handling uploading of single images and importing them into the database.
182
183 =head1 SYNOPSIS
184
185 image-upload.pl
186
187 =head1 DESCRIPTION
188
189 This script is called and presents the user with an interface allowing him/her to upload a single image file. Files greater than 500K will be refused.
190
191 =head1 AUTHOR
192
193 Chris Nighswonger <cnighswonger AT foundations DOT edu>
194
195 =head1 COPYRIGHT
196
197 Copyright 2009 Foundations Bible College.
198
199 =head1 LICENSE
200
201 This file is part of Koha.
202
203 Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
204 Foundation; either version 2 of the License, or (at your option) any later version.
205
206 You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
207 Fifth Floor, Boston, MA 02110-1301 USA.
208
209 =head1 DISCLAIMER OF WARRANTY
210
211 Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
212 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
213
214 =cut