Bug 26819: (QA follow-up) authorized_value should be authorised_value
[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 use POSIX qw(ceil);
8
9 use C4::Context;
10 use C4::Auth;
11 use C4::Output;
12 use C4::Debug;
13 use C4::Creators;
14 use C4::Patroncards;
15
16 my $cgi = CGI->new;
17
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                     });
25
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');
31
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
33
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'}},
39                                     ],
40 };
41 my $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));
42
43 my $image_limit = C4::Context->preference('ImageLimit') || '';
44 my $errstr = '';        # NOTE: For error codes see error-messages.inc
45
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,
59         );
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,
70             );
71         }
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,
84                 );
85             }
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,
102                     );
103                 }
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,
111                     );
112                 }
113             }
114         }
115     }
116 }
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;
123     }
124     else {
125         warn sprintf('No image ids passed in to delete.');
126         $errstr = 202;
127     }
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),
135         );
136     }
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,
142         );
143     }
144 }
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,
151     );
152 }
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,
162     );
163 }
164
165 output_html_with_http_headers $cgi, $cookie, $template->output;
166
167 __END__
168
169 =head1 NAME
170
171 image-upload.pl - Script for handling uploading of single images and importing them into the database.
172
173 =head1 SYNOPSIS
174
175 image-upload.pl
176
177 =head1 DESCRIPTION
178
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.
180
181 =head1 AUTHOR
182
183 Chris Nighswonger <cnighswonger AT foundations DOT edu>
184
185 =head1 COPYRIGHT
186
187 Copyright 2009 Foundations Bible College.
188
189 =head1 LICENSE
190
191 This file is part of Koha.
192
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.
197
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.
202
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>.
205
206 =head1 DISCLAIMER OF WARRANTY
207
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.
210
211 =cut