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