Bug 766: Create a plugin routine to build dropdown list
[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;
16 use C4::Patroncards;
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 => $errstr,
58         );
59     }
60     else {
61         my $image = Graphics::Magick->new;
62         eval{$image->Read($cgi->tmpFileName($file_name));};
63         if ($@) {
64             warn sprintf('An error occurred while creating the image object: %s',$@);
65             $errstr = 202;
66             $template->param(
67                 IMPORT_SUCCESSFUL => 0,
68                 SOURCE_FILE => $source_file,
69                 IMAGE_NAME => $image_name,
70                 TABLE => $table,
71                 error => $errstr,
72             );
73         }
74         else {
75             my $errstr = '';
76             my $size = $image->Get('filesize');
77             $errstr =  302 if $size > 500000;
78             $image->Set(magick => 'png'); # convert all images to png as this is a lossless format which is important for resizing operations later on
79             my $err = put_image($image_name, $image->ImageToBlob()) || '0';
80             $errstr = 101 if $err == 1;
81             $errstr = 303 if $err == 202;
82             if ($errstr) {
83                 $template->param(
84                     IMPORT_SUCCESSFUL => 0,
85                     SOURCE_FILE => $source_file,
86                     IMAGE_NAME => $image_name,
87                     TABLE => $table,
88                     error => $errstr,
89                     image_limit => $image_limit,
90                 );
91             }
92             else {
93                 $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));  # refresh table data after successfully performing save operation
94                 $template->param(
95                     IMPORT_SUCCESSFUL => 1,
96                     SOURCE_FILE => $source_file,
97                     IMAGE_NAME => $image_name,
98                     TABLE => $table,
99                 );
100             }
101         }
102     }
103 }
104 elsif ($op eq 'delete') {
105     my $err = '';
106     my $errstr = '';
107     if (@image_ids) {
108         $err = rm_image(\@image_ids);
109         $errstr = 102 if $err;
110     }
111     else {
112         warn sprintf('No image ids passed in to delete.');
113         $errstr = 202;
114     }
115     if ($errstr) {
116         $template->param(
117             DELETE_SUCCESSFULL => 0,
118             IMAGE_IDS => join(', ', @image_ids),
119             TABLE => $table,
120             error => $errstr,
121             image_ids => join(',',@image_ids),
122         );
123     }
124     else {
125         $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));  # refresh table data after successfully performing delete operation
126         $template->param(
127             DELETE_SUCCESSFULL => 1,
128             TABLE => $table,
129         );
130     }
131 }
132 elsif ($op eq 'none') {
133     $template->param(
134         IMPORT_SUCCESSFUL => 0,
135         SOURCE_FILE => $source_file,
136         IMAGE_NAME => $image_name,
137         TABLE => $table,
138     );
139 }
140 else { # to trap unsupported operations
141     warn sprintf('Image upload interface called an unsupported operation: %s',$op);
142     $errstr = 201;
143     $template->param(
144         IMPORT_SUCCESSFUL => 0,
145         SOURCE_FILE => $source_file,
146         IMAGE_NAME => $image_name,
147         TABLE => $table,
148         error => $errstr,
149     );
150 }
151
152 output_html_with_http_headers $cgi, $cookie, $template->output;
153
154 __END__
155
156 =head1 NAME
157
158 image-upload.pl - Script for handling uploading of single images and importing them into the database.
159
160 =head1 SYNOPSIS
161
162 image-upload.pl
163
164 =head1 DESCRIPTION
165
166 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.
167
168 =head1 AUTHOR
169
170 Chris Nighswonger <cnighswonger AT foundations DOT edu>
171
172 =head1 COPYRIGHT
173
174 Copyright 2009 Foundations Bible College.
175
176 =head1 LICENSE
177
178 This file is part of Koha.
179
180 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
181 Foundation; either version 2 of the License, or (at your option) any later version.
182
183 You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
184 Fifth Floor, Boston, MA 02110-1301 USA.
185
186 =head1 DISCLAIMER OF WARRANTY
187
188 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
189 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
190
191 =cut