# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
-use strict;
-use warnings;
+use Modern::Perl;
+use Storable qw( dclone );
use autouse 'Data::Dumper' => qw(Dumper);
use C4::Context;
-use C4::Debug;
BEGIN {
use base qw(Exporter);
sub _SELECT {
my @params = @_;
- my $query = "SELECT $params[0] FROM $params[1]";
+ my $fieldname = _add_backtics($params[0]);
+ my $query = "SELECT $fieldname FROM $params[1]";
$params[2] ? $query .= " WHERE $params[2];" : $query .= ';';
my $sth = C4::Context->dbh->prepare($query);
# $sth->{'TraceLevel'} = 3;
return $record_set;
}
+sub _add_backtics {
+ my ( @args ) = @_;
+ s/(?:^|\b)(\w+)(?:\b|$)/`$1`/g for @args;
+ # Too bad that we need to correct a few exceptions: aggregate functions
+ my @aggregates = ( 'COUNT', 'MAX', 'MIN', 'SUM' ); # add when needed..
+ foreach my $aggr (@aggregates) {
+ s/`$aggr`\(/$aggr\(/gi for @args;
+ }
+ # And correct aliases
+ s/(`|\))\s+`AS`\s+`/$1 AS `/gi for @args;
+ return wantarray ? @args : $args[0];
+}
+
my $barcode_types = [
{type => 'CODE39', name => 'Code 39', desc => 'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern.', selected => 0},
{type => 'CODE39MOD', name => 'Code 39 + Modulo43', desc => 'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern. Encodes Mod 43 checksum.', selected => 0},
sub _build_query {
my ( $params, $table ) = @_;
- my @fields = exists $params->{fields} ? @{ $params->{fields} } : ();
- my $query = "SELECT " . ( @fields ? join(', ', @fields ) : '*' ) . " FROM $table";
+ my @fields = exists $params->{fields} ? _add_backtics( @{ $params->{fields} } ) : ('*');
+ my $query = "SELECT " . join(', ', @fields ) . " FROM $table";
my @where_args;
if ( exists $params->{filters} ) {
$query .= ' WHERE 1 ';
while ( my ( $field, $values ) = each %{ $params->{filters} } ) {
if ( ref( $values ) ) {
- $query .= " AND $field IN ( " . ( ('?,') x (@$values-1) ) . "? ) "; # a comma separates elements in a list...
+ $query .= " AND `$field` IN ( " . ( ('?,') x (@$values-1) ) . "? ) "; # a comma separates elements in a list...
push @where_args, @$values;
} else {
- $query .= " AND $field = ? ";
+ $query .= " AND `$field` = ? ";
push @where_args, $values;
}
}
my $image_names = [];
my $query = "SELECT image_name FROM creator_images";
my $sth = C4::Context->dbh->prepare($query);
-# $sth->{'TraceLevel'} = 3 if $debug;
$sth->execute();
if ($sth->err) {
warn sprintf('Database returned the following error: %s', $sth->errstr);
sub get_batch_summary {
my ( $params ) = @_;
my @batches = ();
- $params->{fields} = ['batch_id', 'count(batch_id) as _item_count'];
+ $params->{fields} = ['batch_id', 'description', 'count(batch_id) as _item_count'];
my ( $query, @where_args ) = _build_query( $params, 'creator_batches' );
- $query .= " GROUP BY batch_id";
+ $query .= " GROUP BY batch_id, description";
my $sth = C4::Context->dbh->prepare($query);
$sth->execute( @where_args );
if ($sth->err) {
=cut
sub get_barcode_types {
- return $barcode_types;
+ return dclone $barcode_types;
}
=head2 C4::Creators::Lib::get_label_types()
=cut
sub get_label_types {
- return $label_types;
+ return dclone $label_types;
}
=head2 C4::Creators::Lib::get_font_types()
=cut
sub get_font_types {
- return $font_types;
+ return dclone $font_types;
}
=head2 C4::Creators::Lib::get_text_justification_types()
=cut
sub get_text_justification_types {
- return $text_justification_types;
+ return dclone $text_justification_types;
}
=head2 C4::Creators::Lib::get_unit_values()
=cut
sub get_unit_values {
- return $unit_values;
+ return dclone $unit_values;
}
=head2 C4::Creators::Lib::get_output_formats()
=cut
sub get_output_formats {
- return $output_formats;
+ return dclone $output_formats;
}
sub html_table {
my $headers = shift;
my $data = shift;
- return undef if scalar(@$data) == 0; # no need to generate a table if there is not data to display
+ return if scalar(@$data) == 0; # no need to generate a table if there is not data to display
my $table = [];
my $fields = [];
my @table_columns = ();