Bug 17600: Standardize our EXPORT_OK
[srvgit] / C4 / Creators / Lib.pm
index 3c53498..d4831ab 100644 (file)
@@ -17,13 +17,12 @@ package C4::Creators::Lib;
 # 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);
@@ -67,7 +66,8 @@ C4::Creators::Lib
 
 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;
@@ -83,6 +83,19 @@ sub _SELECT {
     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},
@@ -144,17 +157,17 @@ my $output_formats = [
 
 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;
             }
         }
@@ -252,7 +265,6 @@ sub get_all_image_names {
     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);
@@ -279,9 +291,9 @@ NOTE: Do not pass in the keyword 'WHERE.'
 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) {
@@ -391,7 +403,7 @@ This function returns a reference to an array of hashes containing all barcode t
 =cut
 
 sub get_barcode_types {
-    return $barcode_types;
+    return dclone $barcode_types;
 }
 
 =head2 C4::Creators::Lib::get_label_types()
@@ -403,7 +415,7 @@ This function returns a reference to an array of hashes containing all label typ
 =cut
 
 sub get_label_types {
-    return $label_types;
+    return dclone $label_types;
 }
 
 =head2 C4::Creators::Lib::get_font_types()
@@ -415,7 +427,7 @@ This function returns a reference to an array of hashes containing all font type
 =cut
 
 sub get_font_types {
-    return $font_types;
+    return dclone $font_types;
 }
 
 =head2 C4::Creators::Lib::get_text_justification_types()
@@ -427,7 +439,7 @@ This function returns a reference to an array of hashes containing all text just
 =cut
 
 sub get_text_justification_types {
-    return $text_justification_types;
+    return dclone $text_justification_types;
 }
 
 =head2 C4::Creators::Lib::get_unit_values()
@@ -441,7 +453,7 @@ There are 72 PS points to the inch.
 =cut
 
 sub get_unit_values {
-    return $unit_values;
+    return dclone $unit_values;
 }
 
 =head2 C4::Creators::Lib::get_output_formats()
@@ -453,7 +465,7 @@ This function returns a reference to an array of hashes containing all label out
 =cut
 
 sub get_output_formats {
-    return $output_formats;
+    return dclone $output_formats;
 }
 
 
@@ -513,7 +525,7 @@ be passed off as a template parameter and used to build an html table.
 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 = ();