Koha/patroncards/image-manage.pl
Jonathan Druart 9d6d641d1f Bug 17600: Standardize our EXPORT_OK
On bug 17591 we discovered that there was something weird going on with
the way we export and use subroutines/modules.
This patch tries to standardize our EXPORT to use EXPORT_OK only.

That way we will need to explicitely define the subroutine we want to
use from a module.

This patch is a squashed version of:
Bug 17600: After export.pl
Bug 17600: After perlimport
Bug 17600: Manual changes
Bug 17600: Other manual changes after second perlimports run
Bug 17600: Fix tests

And a lot of other manual changes.

export.pl is a dirty script that can be found on bug 17600.

"perlimport" is:
git clone https://github.com/oalders/App-perlimports.git
cd App-perlimports/
cpanm --installdeps .
export PERL5LIB="$PERL5LIB:/kohadevbox/koha/App-perlimports/lib"
find . \( -name "*.pl" -o -name "*.pm" \) -exec perl App-perlimports/script/perlimports --inplace-edit --no-preserve-unused --filename {} \;

The ideas of this patch are to:
* use EXPORT_OK instead of EXPORT
* perltidy the EXPORT_OK list
* remove '&' before the subroutine names
* remove some uneeded use statements
* explicitely import the subroutines we need within the controllers or
modules

Note that the private subroutines (starting with _) should not be
exported (and not used from outside of the module except from tests).

EXPORT vs EXPORT_OK (from
https://www.thegeekstuff.com/2010/06/perl-exporter-examples/)
"""
Export allows to export the functions and variables of modules to user’s namespace using the standard import method. This way, we don’t need to create the objects for the modules to access it’s members.

@EXPORT and @EXPORT_OK are the two main variables used during export operation.

@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

@EXPORT_OK does export of symbols on demand basis.
"""

If this patch caused a conflict with a patch you wrote prior to its
push:
* Make sure you are not reintroducing a "use" statement that has been
removed
* "$subroutine" is not exported by the C4::$MODULE module
means that you need to add the subroutine to the @EXPORT_OK list
* Bareword "$subroutine" not allowed while "strict subs"
means that you didn't imported the subroutine from the module:
  - use $MODULE qw( $subroutine list );
You can also use the fully qualified namespace: C4::$MODULE::$subroutine

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-07-16 08:58:47 +02:00

208 lines
7.2 KiB
Perl
Executable file

#!/usr/bin/perl
use Modern::Perl;
use CGI qw ( -utf8 );
use Graphics::Magick;
use C4::Context;
use C4::Auth qw( get_template_and_user );
use C4::Output qw( output_html_with_http_headers );
use C4::Creators qw( html_table );
use C4::Patroncards qw( get_image put_image rm_image );
my $cgi = CGI->new;
my ($template, $loggedinuser, $cookie) = get_template_and_user({
template_name => "patroncards/image-manage.tt",
query => $cgi,
type => "intranet",
flagsrequired => {tools => 'label_creator'},
});
my $file_name = $cgi->param('uploadfile') || '';
my $image_name = $cgi->param('image_name') || $file_name;
my $upload_file = $cgi->upload('uploadfile') || '';
my $op = $cgi->param('op') || 'none';
my @image_ids = $cgi->multi_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}},
{_delete => {label => 'Delete', 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') {
# Checking for duplicate image name
my $dbh = C4::Context->dbh;
my $query = "SELECT COUNT(*) FROM creator_images WHERE image_name=?";
my ( $exists ) = $dbh->selectrow_array( $query, undef, $image_name );
if ( $exists ) {
$errstr = 304;
$template->param(
IMPORT_SUCCESSFUL => 0,
SOURCE_FILE => $source_file,
IMAGE_NAME => $image_name,
TABLE => $table,
error => $errstr,
);
} else {
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 => $errstr,
);
}
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 => $errstr,
);
}
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 => $errstr,
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 => $errstr,
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 => $errstr,
);
}
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 <cnighswonger AT foundations DOT edu>
=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 3 of the License, or
(at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with Koha; if not, see <http://www.gnu.org/licenses>.
=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