Bug 21290: Updating documentation for ModItem
[koha.git] / C4 / Creators / Lib.pm
index 67ee0e8..c16a2c8 100644 (file)
@@ -4,18 +4,18 @@ package C4::Creators::Lib;
 #
 # 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.
+# 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.
+# 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, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# 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;
@@ -26,7 +26,6 @@ use C4::Context;
 use C4::Debug;
 
 BEGIN {
-    use version; our $VERSION = qv('3.07.00.049');
     use base qw(Exporter);
     our @EXPORT = qw(get_all_templates
                      get_all_layouts
@@ -40,7 +39,6 @@ BEGIN {
                      get_font_types
                      get_text_justification_types
                      get_output_formats
-                     get_column_names
                      get_table_names
                      get_unit_values
                      html_table
@@ -69,7 +67,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;
@@ -85,12 +84,25 @@ 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},
     {type => 'CODE39MOD10',     name => 'Code 39 + Modulo10',   desc => 'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern. Encodes Mod 10 checksum.',         selected => 0},
     {type => 'COOP2OF5',        name => 'COOP2of5',             desc => 'Creates COOP2of5 barcodes from a string consisting of the numeric characters 0-9',                                                                     selected => 0},
-#    {type => 'EAN13',           name => 'EAN13',                desc => 'Creates EAN13 barcodes from a string of 12 or 13 digits. The check number (the 13:th digit) is calculated if not supplied.',                           selected => 0},
+    {type => 'EAN13',           name => 'EAN13',                desc => 'Creates EAN13 barcodes from a string of 12 or 13 digits. The check number (the 13:th digit) is calculated if not supplied.',                           selected => 0},
 #    {type => 'EAN8',            name => 'EAN8',                 desc => 'Translates a string of 7 or 8 digits to EAN8 barcodes. The check number (the 8:th digit) is calculated if not supplied.',                              selected => 0},
 #    {type => 'IATA2of5',        name => 'IATA2of5',             desc => 'Creates IATA2of5 barcodes from a string consisting of the numeric characters 0-9',                                                                     selected => 0},
     {type => 'INDUSTRIAL2OF5',  name => 'Industrial2of5',       desc => 'Creates Industrial2of5 barcodes from a string consisting of the numeric characters 0-9',                                                               selected => 0},
@@ -119,6 +131,7 @@ my $font_types = [
     {type => 'CO',      name => 'Courier-Oblique',              selected => 0},
     {type => 'CBO',     name => 'Courier-Bold-Oblique',         selected => 0},
     {type => 'H',       name => 'Helvetica',                    selected => 0},
+    {type => 'HO',      name => 'Helvetica-Oblique',            selected => 0},
     {type => 'HB',      name => 'Helvetica-Bold',               selected => 0},
     {type => 'HBO',     name => 'Helvetica-Bold-Oblique',       selected => 0},
 ];
@@ -143,6 +156,27 @@ my $output_formats = [
     {type       => 'csv',       desc    => 'CSV File'},
 ];
 
+sub _build_query {
+    my ( $params, $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...
+                push @where_args, @$values;
+            } else {
+                $query .= " AND `$field` = ? ";
+                push @where_args, $values;
+            }
+        }
+    }
+    $query .= (exists $params->{orderby} ? " ORDER BY $params->{orderby} " : '');
+    return ( $query, @where_args );
+}
+
 =head2 C4::Creators::Lib::get_all_templates()
 
   my $templates = get_all_templates();
@@ -152,12 +186,11 @@ This function returns a reference to a hash containing all templates upon succes
 =cut
 
 sub get_all_templates {
-    my %params = @_;
+    my ( $params ) = @_;
     my @templates = ();
-    my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM creator_templates";
-    $query .= ($params{'filter'} ? " WHERE $params{'filter'};" : ';');
+    my ( $query, @where_args ) = _build_query( $params, 'creator_templates' );
     my $sth = C4::Context->dbh->prepare($query);
-    $sth->execute();
+    $sth->execute( @where_args );
     if ($sth->err) {
         warn sprintf('Database returned the following error: %s', $sth->errstr);
         return -1;
@@ -178,12 +211,11 @@ This function returns a reference to a hash containing all layouts upon success
 =cut
 
 sub get_all_layouts {
-    my %params = @_;
+    my ( $params ) = @_;
     my @layouts = ();
-    my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM creator_layouts";
-    $query .= ($params{'filter'} ? " WHERE $params{'filter'};" : ';');
+    my ( $query, @where_args ) = _build_query( $params, 'creator_layouts' );
     my $sth = C4::Context->dbh->prepare($query);
-    $sth->execute();
+    $sth->execute( @where_args );
     if ($sth->err) {
         warn sprintf('Database returned the following error: %s', $sth->errstr);
         return -1;
@@ -199,7 +231,7 @@ sub get_all_layouts {
 
   my $profiles = get_all_profiles();
 
-  my $profiles = get_all_profiles(field_list => field_list, filter => filter_string);
+  my $profiles = get_all_profiles({ fields => [@fields], filters => { filters => [$value1, $value2] } });
 
 This function returns an arrayref whose elements are hashes containing all profiles upon success and 1 upon failure. Errors are logged
 to the Apache log. Two parameters are accepted. The first limits the field(s) returned. This parameter should be string of comma separted
@@ -210,13 +242,11 @@ NOTE: Do not pass in the keyword 'WHERE.'
 =cut
 
 sub get_all_profiles {
-    my %params = @_;
+    my ( $params ) = @_;
     my @profiles = ();
-    my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM printers_profile";
-    $query .= ($params{'filter'} ? " WHERE $params{'filter'};" : ';');
+    my ( $query, @where_args ) = _build_query( $params, 'printers_profile' );
     my $sth = C4::Context->dbh->prepare($query);
-#    $sth->{'TraceLevel'} = 3 if $debug;
-    $sth->execute();
+    $sth->execute( @where_args );
     if ($sth->err) {
         warn sprintf('Database returned the following error: %s', $sth->errstr);
         return -1;
@@ -261,14 +291,13 @@ NOTE: Do not pass in the keyword 'WHERE.'
 =cut
 
 sub get_batch_summary {
-    my %params = @_;
+    my ( $params ) = @_;
     my @batches = ();
-    my $query = "SELECT batch_id,count(batch_id) as _item_count FROM creator_batches WHERE creator=?";
-    $query .= ($params{'filter'} ? " AND $params{'filter'}" : '');
-    $query .= " GROUP BY batch_id";
+    $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, description";
     my $sth = C4::Context->dbh->prepare($query);
-#    $sth->{'TraceLevel'} = 3;
-    $sth->execute($params{'creator'});
+    $sth->execute( @where_args );
     if ($sth->err) {
         warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
         return -1;
@@ -297,7 +326,7 @@ sub get_label_summary {
     my %params = @_;
     my $label_number = 0;
     my @label_summaries = ();
-    my $query = "     SELECT b.title, b.author, bi.itemtype, i.barcode, i.biblionumber, i.itype
+    my $query = "     SELECT b.title, b.author, bi.itemtype, i.barcode, i.itemcallnumber, i.biblionumber, i.itype
                       FROM creator_batches AS c LEFT JOIN items AS i ON (c.item_number=i.itemnumber)
                       LEFT JOIN biblioitems AS bi ON (i.biblioitemnumber=bi.biblioitemnumber)
                       LEFT JOIN biblio AS b ON (bi.biblionumber=b.biblionumber)
@@ -323,6 +352,7 @@ sub get_label_summary {
         $label_summary->{'_item_type'} = C4::Context->preference("item-level_itypes") ? $record->{'itype'} : $record->{'itemtype'};
         $label_summary->{'_barcode'} = $record->{'barcode'};
         $label_summary->{'_item_number'} = $item->{'item_number'};
+        $label_summary->{'_item_cn'} = $record->{'itemcallnumber'};
         $label_summary->{'_label_id'} = $item->{'label_id'};
         push (@label_summaries, $label_summary);
     }
@@ -440,22 +470,6 @@ sub get_output_formats {
     return $output_formats;
 }
 
-=head2 C4::Creators::Lib::get_column_names($table_name)
-
-Return an arrayref of an array containing the column names of the supplied table.
-
-=cut
-
-sub get_column_names {
-    my $table = shift;
-    my $dbh = C4::Context->dbh();
-    my $column_names = [];
-    my $sth = $dbh->column_info(undef,undef,$table,'%');
-    while (my $info = $sth->fetchrow_hashref()){
-        $$column_names[$info->{'ORDINAL_POSITION'}] = $info->{'COLUMN_NAME'};
-    }
-    return $column_names;
-}
 
 =head2 C4::Creators::Lib::get_table_names($search_term)
 
@@ -477,37 +491,35 @@ sub get_table_names {
 =head2 C4::Creators::Lib::html_table()
 
 This function returns an arrayref of an array of hashes contianing the supplied data formatted suitably to
-be passed off as a T::P template parameter and used to build an html table.
+be passed off as a template parameter and used to build an html table.
 
    my $table = html_table(header_fields, array_of_row_data);
    $template->param(
-       TABLE => $table,
+       table_loop => $table,
    );
 
     html example:
 
-       <table>
-            <!-- TMPL_LOOP NAME="TABLE" -->
-            <!-- TMPL_IF NAME="header_fields" -->
-            <tr>
-            <!-- TMPL_LOOP NAME="header_fields" -->
-                <th><!-- TMPL_VAR NAME="field_label" --></th>
-            <!-- /TMPL_LOOP -->
-            </tr>
-            <!-- TMPL_ELSE -->
-            <tr>
-            <!-- TMPL_LOOP NAME="text_fields" -->
-            <!-- TMPL_IF NAME="select_field" -->
-                <td align="center"><input type="checkbox" name="action" value="<!-- TMPL_VAR NAME="field_value" -->" /></td>
-            <!-- TMPL_ELSIF NAME="field_value" -->
-                <td><!-- TMPL_VAR NAME="field_value" --></td>
-            <!-- TMPL_ELSE -->
-                <td>&nbsp;</td>
-            <!-- /TMPL_IF -->
-            <!-- /TMPL_LOOP -->
-            </tr>
-            <!-- /TMPL_IF -->
-            <!-- /TMPL_LOOP -->
+        <table>
+            [% FOREACH table_loo IN table_loop %]
+                [% IF ( table_loo.header_fields ) %]
+                    <tr>
+                        [% FOREACH header_field IN table_loo.header_fields %]
+                            <th>[% header_field.field_label %]</th>
+                        [% END %]
+                    </tr>
+                [% ELSE %]
+                    <tr>
+                        [% FOREACH text_field IN table_loo.text_fields %]
+                            [% IF ( text_field.select_field ) %]
+                                <td><input type="checkbox" name="action" value="[% text_field.field_value %]"></td>
+                            [% ELSE %]
+                                <td>[% text_field.field_value %]</td>
+                            [% END %]
+                        [% END %]
+                    </tr>
+                [% END %]
+            [% END %]
         </table>
 
 =cut
@@ -558,7 +570,7 @@ sub html_table {
                 next POPULATE_ROW;
             }
             elsif ($table_column =~ m/^_((.*)_(.*$))/) {   # this a special case
-                my $table_name = get_table_names($2);
+                my $table_name = get_table_names('creator_'.$2); #Bug 14143 fix to remove ambiguity with table 'club_template_enrollment_fields'
                 my $record_set = _SELECT($1, @$table_name[0], $2 . "_id = " . $db_row->{$2 . "_id"});
                 $$fields[$col_index] = {hidden => 0, link_field => $link_field->{$table_column}, select_field => 0, field_name => ($table_column . "_tbl"), field_value => $$record_set[0]{$1}};
                 $col_index++;