Merge commit 'pianohacker-koha/prefs-submit' into master
[koha.git] / patroncards / image-manage.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use CGI;
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::Lib 1.000000 qw(html_table);
16 use C4::Patroncards::Lib 1.000000 qw(put_image get_image rm_image);
17
18 my $cgi = CGI->new;
19
20 my ($template, $loggedinuser, $cookie) = get_template_and_user({
21                     template_name       => "patroncards/image-manage.tmpl",
22                     query               => $cgi,
23                     type                => "intranet",
24                     authnotrequired     => 0,
25                     flagsrequired       => {tools => 'batch_upload_patron_images'}, # FIXME: establish flag for patron card creator
26                     debug               => 0,
27                     });
28
29 my $image_name = $cgi->param('image_name') || '';
30 my $file_name = $cgi->param('uploadfile') || '';
31 my $upload_file = $cgi->upload('uploadfile') || '';
32 my $op = $cgi->param('op') || 'none';
33 my @image_ids = $cgi->param('image_id') if $cgi->param('image_id');
34
35 my $source_file = "$file_name"; # otherwise we end up with what amounts to a pointer to a filehandle rather than a user-friendly filename
36
37 my $display_columns = { image =>    [  #{db column      => {label => 'col label', is link?          }},
38                                         {image_id       => {label => 'ID',      link_field      => 0}},
39                                         {image_name     => {label => 'Name',    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     if (!$upload_file) {
50         warn sprintf('An error occurred while attempting to upload file %s.', $source_file);
51         $errstr = 301;
52         $template->param(
53             IMPORT_SUCCESSFUL => 0,
54             SOURCE_FILE => $source_file,
55             IMAGE_NAME => $image_name,
56             TABLE => $table,
57             error => 1,
58             $errstr => 1,
59         );
60     }
61     else {
62         my $image = Graphics::Magick->new;
63         eval{$image->Read($cgi->tmpFileName($file_name));};
64         if ($@) {
65             warn sprintf('An error occurred while creating the image object: %s',$@);
66             $errstr = 202;
67             $template->param(
68                 IMPORT_SUCCESSFUL => 0,
69                 SOURCE_FILE => $source_file,
70                 IMAGE_NAME => $image_name,
71                 TABLE => $table,
72                 error => 1,
73                 $errstr => 1,
74             );
75         }
76         else {
77             my $errstr = '';
78             my $size = $image->Get('filesize');
79             $errstr =  302 if $size > 500000;
80             $image->Set(magick => 'png'); # convert all images to png as this is a lossless format which is important for resizing operations later on
81             my $err = put_image($image_name, $image->ImageToBlob()) || '0';
82             $errstr = 101 if $err == 1;
83             $errstr = 303 if $err == 202;
84             if ($errstr) {
85                 $template->param(
86                     IMPORT_SUCCESSFUL => 0,
87                     SOURCE_FILE => $source_file,
88                     IMAGE_NAME => $image_name,
89                     TABLE => $table,
90                     error => 1,
91                     $errstr => 1,
92                     image_limit => $image_limit,
93                 );
94             }
95             else {
96                 $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));  # refresh table data after successfully performing save operation
97                 $template->param(
98                     IMPORT_SUCCESSFUL => 1,
99                     SOURCE_FILE => $source_file,
100                     IMAGE_NAME => $image_name,
101                     TABLE => $table,
102                 );
103             }
104         }
105     }
106 }
107 elsif ($op eq 'delete') {
108     my $err = '';
109     my $errstr = '';
110     if (@image_ids) {
111         $err = rm_image(\@image_ids);
112         $errstr = 102 if $err;
113     }
114     else {
115         warn sprintf('No image ids passed in to delete.');
116         $errstr = 202;
117     }
118     if ($errstr) {
119         $template->param(
120             DELETE_SUCCESSFULL => 0,
121             IMAGE_IDS => join(', ', @image_ids),
122             TABLE => $table,
123             error => 1,
124             $errstr => 1,
125             image_ids => join(',',@image_ids),
126         );
127     }
128     else {
129         $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));  # refresh table data after successfully performing delete operation
130         $template->param(
131             DELETE_SUCCESSFULL => 1,
132             TABLE => $table,
133         );
134     }
135 }
136 elsif ($op eq 'none') {
137     $template->param(
138         IMPORT_SUCCESSFUL => 0,
139         SOURCE_FILE => $source_file,
140         IMAGE_NAME => $image_name,
141         TABLE => $table,
142     );
143 }
144 else { # to trap unsupported operations
145     warn sprintf('Image upload interface called an unsupported operation: %s',$op);
146     $errstr = 201;
147     $template->param(
148         IMPORT_SUCCESSFUL => 0,
149         SOURCE_FILE => $source_file,
150         IMAGE_NAME => $image_name,
151         TABLE => $table,
152         error => 1,
153         $errstr => 1,
154     );
155 }
156
157 output_html_with_http_headers $cgi, $cookie, $template->output;
158
159 __END__
160
161 =head1 NAME
162
163 image-upload.pl - Script for handling uploading of single images and importing them into the database.
164
165 =head1 SYNOPSIS
166
167 image-upload.pl
168
169 =head1 DESCRIPTION
170
171 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.
172
173 =head1 AUTHOR
174
175 Chris Nighswonger <cnighswonger AT foundations DOT edu>
176
177 =head1 COPYRIGHT
178
179 Copyright 2009 Foundations Bible College.
180
181 =head1 LICENSE
182
183 This file is part of Koha.
184
185 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
186 Foundation; either version 2 of the License, or (at your option) any later version.
187
188 You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
189 Suite 330, Boston, MA  02111-1307 USA
190
191 =head1 DISCLAIMER OF WARRANTY
192
193 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
194 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
195
196 =cut