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