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 );
16 my ($template, $loggedinuser, $cookie) = get_template_and_user({
17 template_name => "patroncards/image-manage.tt",
20 flagsrequired => {tools => 'label_creator'},
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');
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 )
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
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'}},
45 my $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));
47 my $image_limit = C4::Context->preference('ImageLimit') || '';
48 my $errstr = ''; # NOTE: For error codes see error-messages.inc
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 );
58 IMPORT_SUCCESSFUL => 0,
59 SOURCE_FILE => $source_file,
60 IMAGE_NAME => $image_name,
66 warn sprintf('An error occurred while attempting to upload file %s.', $source_file);
69 IMPORT_SUCCESSFUL => 0,
70 SOURCE_FILE => $source_file,
71 IMAGE_NAME => $image_name,
77 my $image = Graphics::Magick->new;
78 eval{$image->Read($cgi->tmpFileName($file_name));};
80 warn sprintf('An error occurred while creating the image object: %s',$@);
83 IMPORT_SUCCESSFUL => 0,
84 SOURCE_FILE => $source_file,
85 IMAGE_NAME => $image_name,
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;
100 IMPORT_SUCCESSFUL => 0,
101 SOURCE_FILE => $source_file,
102 IMAGE_NAME => $image_name,
105 image_limit => $image_limit,
109 $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name")); # refresh table data after successfully performing save operation
111 IMPORT_SUCCESSFUL => 1,
112 SOURCE_FILE => $source_file,
113 IMAGE_NAME => $image_name,
121 elsif ($op eq 'delete') {
125 $err = rm_image(\@image_ids);
126 $errstr = 102 if $err;
129 warn sprintf('No image ids passed in to delete.');
134 DELETE_SUCCESSFULL => 0,
135 IMAGE_IDS => join(', ', @image_ids),
138 image_ids => join(',',@image_ids),
142 $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name")); # refresh table data after successfully performing delete operation
144 DELETE_SUCCESSFULL => 1,
149 elsif ($op eq 'none') {
151 IMPORT_SUCCESSFUL => 0,
152 SOURCE_FILE => $source_file,
153 IMAGE_NAME => $image_name,
157 else { # to trap unsupported operations
158 warn sprintf('Image upload interface called an unsupported operation: %s',$op);
161 IMPORT_SUCCESSFUL => 0,
162 SOURCE_FILE => $source_file,
163 IMAGE_NAME => $image_name,
169 output_html_with_http_headers $cgi, $cookie, $template->output;
175 image-upload.pl - Script for handling uploading of single images and importing them into the database.
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.
187 Chris Nighswonger <cnighswonger AT foundations DOT edu>
191 Copyright 2009 Foundations Bible College.
195 This file is part of Koha.
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.
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.
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>.
210 =head1 DISCLAIMER OF WARRANTY
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.