Bug 32482: (follow-up) Add markup comments
[koha.git] / patroncards / image-manage.pl
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use CGI qw ( -utf8 );
6 use Graphics::Magick;
7
8 use C4::Context;
9 use C4::Auth qw( get_template_and_user );
10 use C4::Output qw( output_html_with_http_headers );
11 use C4::Creators qw( html_table );
12 use C4::Patroncards qw( get_image put_image rm_image );
13
14 my $cgi = CGI->new;
15
16 my ($template, $loggedinuser, $cookie) = get_template_and_user({
17                     template_name       => "patroncards/image-manage.tt",
18                     query               => $cgi,
19                     type                => "intranet",
20                     flagsrequired       => {tools => 'label_creator'},
21                     });
22
23 my $file_name = $cgi->param('uploadfile') || '';
24 my $image_name = $cgi->param('image_name') || $file_name;
25 my $upload_file = $cgi->upload('uploadfile') || '';
26 my $op = $cgi->param('op') || 'none';
27 my @image_ids = $cgi->multi_param('image_id');
28
29 my @errors = ( 'pdferr', 'errnocards', 'errba', 'errpl', 'errpt', 'errlo', 'errtpl', );
30 foreach my $param (@errors) {
31     my $error = $cgi->param($param) ? 1 : 0;
32     $template->param( 'error_' . $param => $error )
33       if $error;
34 }
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 $dbh = C4::Context->dbh;
53     my $query = "SELECT COUNT(*) FROM creator_images WHERE image_name=?";
54     my ( $exists ) = $dbh->selectrow_array( $query, undef, $image_name );
55     if ( $exists ) {
56         $errstr = 304;
57         $template->param(
58             IMPORT_SUCCESSFUL => 0,
59             SOURCE_FILE => $source_file,
60             IMAGE_NAME => $image_name,
61             TABLE => $table,
62             error => $errstr,
63         );
64     } else {
65         if (!$upload_file) {
66             warn sprintf('An error occurred while attempting to upload file %s.', $source_file);
67             $errstr = 301;
68             $template->param(
69                 IMPORT_SUCCESSFUL => 0,
70                 SOURCE_FILE => $source_file,
71                 IMAGE_NAME => $image_name,
72                 TABLE => $table,
73                 error => $errstr,
74             );
75         }
76         else {
77             my $image = Graphics::Magick->new;
78             eval{$image->Read($cgi->tmpFileName($file_name));};
79             if ($@) {
80                 warn sprintf('An error occurred while creating the image object: %s',$@);
81                 $errstr = 202;
82                 $template->param(
83                     IMPORT_SUCCESSFUL => 0,
84                     SOURCE_FILE => $source_file,
85                     IMAGE_NAME => $image_name,
86                     TABLE => $table,
87                     error => $errstr,
88                 );
89             }
90             else {
91                 my $errstr = '';
92                 my $size = $image->Get('filesize');
93                 $errstr =  302 if $size > 2097152;
94                 $image->Set(magick => 'png'); # convert all images to png as this is a lossless format which is important for resizing operations later on
95                 my $err = put_image($image_name, $image->ImageToBlob()) || '0';
96                 $errstr = 101 if $err == 1;
97                 $errstr = 303 if $err == 202;
98                 if ($errstr) {
99                     $template->param(
100                         IMPORT_SUCCESSFUL => 0,
101                         SOURCE_FILE => $source_file,
102                         IMAGE_NAME => $image_name,
103                         TABLE => $table,
104                         error => $errstr,
105                         image_limit => $image_limit,
106                     );
107                 }
108                 else {
109                     $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));  # refresh table data after successfully performing save operation
110                     $template->param(
111                         IMPORT_SUCCESSFUL => 1,
112                         SOURCE_FILE => $source_file,
113                         IMAGE_NAME => $image_name,
114                         TABLE => $table,
115                     );
116                 }
117             }
118         }
119     }
120 }
121 elsif ($op eq 'delete') {
122     my $err = '';
123     my $errstr = '';
124     if (@image_ids) {
125         $err = rm_image(\@image_ids);
126         $errstr = 102 if $err;
127     }
128     else {
129         warn sprintf('No image ids passed in to delete.');
130         $errstr = 202;
131     }
132     if ($errstr) {
133         $template->param(
134             DELETE_SUCCESSFULL => 0,
135             IMAGE_IDS => join(', ', @image_ids),
136             TABLE => $table,
137             error => $errstr,
138             image_ids => join(',',@image_ids),
139         );
140     }
141     else {
142         $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));  # refresh table data after successfully performing delete operation
143         $template->param(
144             DELETE_SUCCESSFULL => 1,
145             TABLE => $table,
146         );
147     }
148 }
149 elsif ($op eq 'none') {
150     $template->param(
151         IMPORT_SUCCESSFUL => 0,
152         SOURCE_FILE => $source_file,
153         IMAGE_NAME => $image_name,
154         TABLE => $table,
155     );
156 }
157 else { # to trap unsupported operations
158     warn sprintf('Image upload interface called an unsupported operation: %s',$op);
159     $errstr = 201;
160     $template->param(
161         IMPORT_SUCCESSFUL => 0,
162         SOURCE_FILE => $source_file,
163         IMAGE_NAME => $image_name,
164         TABLE => $table,
165         error => $errstr,
166     );
167 }
168
169 output_html_with_http_headers $cgi, $cookie, $template->output;
170
171 __END__
172
173 =head1 NAME
174
175 image-upload.pl - Script for handling uploading of single images and importing them into the database.
176
177 =head1 SYNOPSIS
178
179 image-upload.pl
180
181 =head1 DESCRIPTION
182
183 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.
184
185 =head1 AUTHOR
186
187 Chris Nighswonger <cnighswonger AT foundations DOT edu>
188
189 =head1 COPYRIGHT
190
191 Copyright 2009 Foundations Bible College.
192
193 =head1 LICENSE
194
195 This file is part of Koha.
196
197 Koha is free software; you can redistribute it and/or modify it
198 under the terms of the GNU General Public License as published by
199 the Free Software Foundation; either version 3 of the License, or
200 (at your option) any later version.
201
202 Koha is distributed in the hope that it will be useful, but
203 WITHOUT ANY WARRANTY; without even the implied warranty of
204 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
205 GNU General Public License for more details.
206
207 You should have received a copy of the GNU General Public License
208 along with Koha; if not, see <http://www.gnu.org/licenses>.
209
210 =head1 DISCLAIMER OF WARRANTY
211
212 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
213 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
214
215 =cut