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