From cd4fa27fc66cf7c55262a13e9acad3b269f0181c Mon Sep 17 00:00:00 2001 From: Chris Nighswonger Date: Mon, 11 Jan 2010 15:33:41 -0500 Subject: [PATCH] [21/30] Patron Card Creator image management interface and code --- .../en/modules/patroncards/image-manage.tmpl | 195 +++++++++++++++++ patroncards/image-manage.pl | 196 ++++++++++++++++++ 2 files changed, 391 insertions(+) create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/patroncards/image-manage.tmpl create mode 100755 patroncards/image-manage.pl diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/patroncards/image-manage.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/patroncards/image-manage.tmpl new file mode 100644 index 0000000000..3d6775266a --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/patroncards/image-manage.tmpl @@ -0,0 +1,195 @@ + + Koha › Patron Card Creator › Manage Images + + + + + + + + +
+
+
+
+ +
+
+

Upload Images

+ +
+
  • Image successfully uploaded.

  • +
  • File:
  • +
  • Image name:
  • +
    + +
    +
    +
    + NOTE: Only PNG, GIF, JPEG, XPM formats are supported. Images must be less than 500KB. +
    +
  • + + + +
  • +
  • + +
    + This will be the name by which you will refer to this image in the patron card layout editor. +
    + +
  • +
    +
    + + + Cancel +
    +
    +
    +
    +

    Delete Images

    + +
    +
  • Image(s) successfully deleted.

  • +
    + + +
    +
    +
    + Select one or more images to delete. +
    +
  • + + + + + + + + + + + + + + + + + + + + + + +
    " /> 
    +
  • +
    +
    + + + Cancel +
    +
    + +
    +
    + No images are currently available. +
    +
    + +
    +
    +
    +
    +
    + +
    +
    + diff --git a/patroncards/image-manage.pl b/patroncards/image-manage.pl new file mode 100755 index 0000000000..f16778264b --- /dev/null +++ b/patroncards/image-manage.pl @@ -0,0 +1,196 @@ +#!/usr/bin/perl + +use warnings; +use strict; + +use CGI; +use Graphics::Magick; +use POSIX qw(ceil); +use autouse 'Data::Dumper' => qw(Dumper); + +use C4::Context; +use C4::Auth; +use C4::Output; +use C4::Debug; +use C4::Creators::Lib 1.000000 qw(html_table); +use C4::Patroncards::Lib 1.000000 qw(put_image get_image rm_image); + +my $cgi = CGI->new; + +my ($template, $loggedinuser, $cookie) = get_template_and_user({ + template_name => "patroncards/image-manage.tmpl", + query => $cgi, + type => "intranet", + authnotrequired => 0, + flagsrequired => {tools => 'batch_upload_patron_images'}, # FIXME: establish flag for patron card creator + debug => 0, + }); + +my $image_name = $cgi->param('image_name') || ''; +my $file_name = $cgi->param('uploadfile') || ''; +my $upload_file = $cgi->upload('uploadfile') || ''; +my $op = $cgi->param('op') || 'none'; +my @image_ids = $cgi->param('image_id') if $cgi->param('image_id'); + +my $source_file = "$file_name"; # otherwise we end up with what amounts to a pointer to a filehandle rather than a user-friendly filename + +my $display_columns = { image => [ #{db column => {label => 'col label', is link? }}, + {image_id => {label => 'ID', link_field => 0}}, + {image_name => {label => 'Name', link_field => 0}}, + {select => {label => 'Select', value => 'image_id'}}, + ], +}; +my $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name")); + +my $image_limit = C4::Context->preference('ImageLimit') || ''; +my $errstr = ''; # NOTE: For error codes see error-messages.inc + +if ($op eq 'upload') { + if (!$upload_file) { + warn sprintf('An error occurred while attempting to upload file %s.', $source_file); + $errstr = 301; + $template->param( + IMPORT_SUCCESSFUL => 0, + SOURCE_FILE => $source_file, + IMAGE_NAME => $image_name, + TABLE => $table, + error => 1, + $errstr => 1, + ); + } + else { + my $image = Graphics::Magick->new; + eval{$image->Read($cgi->tmpFileName($file_name));}; + if ($@) { + warn sprintf('An error occurred while creating the image object: %s',$@); + $errstr = 202; + $template->param( + IMPORT_SUCCESSFUL => 0, + SOURCE_FILE => $source_file, + IMAGE_NAME => $image_name, + TABLE => $table, + error => 1, + $errstr => 1, + ); + } + else { + my $errstr = ''; + my $size = $image->Get('filesize'); + $errstr = 302 if $size > 500000; + $image->Set(magick => 'png'); # convert all images to png as this is a lossless format which is important for resizing operations later on + my $err = put_image($image_name, $image->ImageToBlob()) || '0'; + $errstr = 101 if $err == 1; + $errstr = 303 if $err == 202; + if ($errstr) { + $template->param( + IMPORT_SUCCESSFUL => 0, + SOURCE_FILE => $source_file, + IMAGE_NAME => $image_name, + TABLE => $table, + error => 1, + $errstr => 1, + image_limit => $image_limit, + ); + } + else { + $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name")); # refresh table data after successfully performing save operation + $template->param( + IMPORT_SUCCESSFUL => 1, + SOURCE_FILE => $source_file, + IMAGE_NAME => $image_name, + TABLE => $table, + ); + } + } + } +} +elsif ($op eq 'delete') { + my $err = ''; + my $errstr = ''; + if (@image_ids) { + $err = rm_image(\@image_ids); + $errstr = 102 if $err; + } + else { + warn sprintf('No image ids passed in to delete.'); + $errstr = 202; + } + if ($errstr) { + $template->param( + DELETE_SUCCESSFULL => 0, + IMAGE_IDS => join(', ', @image_ids), + TABLE => $table, + error => 1, + $errstr => 1, + image_ids => join(',',@image_ids), + ); + } + else { + $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name")); # refresh table data after successfully performing delete operation + $template->param( + DELETE_SUCCESSFULL => 1, + TABLE => $table, + ); + } +} +elsif ($op eq 'none') { + $template->param( + IMPORT_SUCCESSFUL => 0, + SOURCE_FILE => $source_file, + IMAGE_NAME => $image_name, + TABLE => $table, + ); +} +else { # to trap unsupported operations + warn sprintf('Image upload interface called an unsupported operation: %s',$op); + $errstr = 201; + $template->param( + IMPORT_SUCCESSFUL => 0, + SOURCE_FILE => $source_file, + IMAGE_NAME => $image_name, + TABLE => $table, + error => 1, + $errstr => 1, + ); +} + +output_html_with_http_headers $cgi, $cookie, $template->output; + +__END__ + +=head1 NAME + +image-upload.pl - Script for handling uploading of single images and importing them into the database. + +=head1 SYNOPSIS + +image-upload.pl + +=head1 DESCRIPTION + +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. + +=head1 AUTHOR + +Chris Nighswonger + +=head1 COPYRIGHT + +Copyright 2009 Foundations Bible College. + +=head1 LICENSE + +This file is part of Koha. + +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 +Foundation; either version 2 of the License, or (at your option) any later version. + +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, +Suite 330, Boston, MA 02111-1307 USA + +=head1 DISCLAIMER OF WARRANTY + +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 +A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +=cut -- 2.39.5