Merge remote-tracking branch 'origin/new/bug_8233'
[koha.git] / C4 / Creators / Lib.pm
index 0b9e5b7..0a57b59 100644 (file)
@@ -13,9 +13,9 @@ package C4::Creators::Lib;
 # 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., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# 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.
 
 use strict;
 use warnings;
@@ -26,27 +26,37 @@ use C4::Context;
 use C4::Debug;
 
 BEGIN {
-    use version; our $VERSION = qv('1.0.0_1');
+    use version; our $VERSION = qv('3.07.00.049');
     use base qw(Exporter);
-    our @EXPORT_OK = qw(get_all_templates
-                        get_all_layouts
-                        get_all_profiles
-                        get_all_image_names
-                        get_batch_summary
-                        get_label_summary
-                        get_card_summary
-                        get_barcode_types
-                        get_label_types
-                        get_font_types
-                        get_text_justification_types
-                        get_output_formats
-                        get_column_names
-                        get_table_names
-                        get_unit_values
-                        html_table
+    our @EXPORT = qw(get_all_templates
+                     get_all_layouts
+                     get_all_profiles
+                     get_all_image_names
+                     get_batch_summary
+                     get_label_summary
+                     get_card_summary
+                     get_barcode_types
+                     get_label_types
+                     get_font_types
+                     get_text_justification_types
+                     get_output_formats
+                     get_column_names
+                     get_table_names
+                     get_unit_values
+                     html_table
     );
 }
 
+=head1 NAME
+
+C4::Creators::Lib
+
+=cut
+
+=head1 FUNCTIONS
+
+=cut
+
 #=head2 C4::Creators::Lib::_SELECT()
 #
 #    This function returns a recordset upon success and 1 upon failure. Errors are logged to the Apache log.
@@ -80,7 +90,7 @@ my $barcode_types = [
     {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},
@@ -135,11 +145,9 @@ my $output_formats = [
 
 =head2 C4::Creators::Lib::get_all_templates()
 
-    This function returns a reference to a hash containing all templates upon success and 1 upon failure. Errors are logged to the Apache log.
+  my $templates = get_all_templates();
 
-    examples:
-
-        my $templates = get_all_templates();
+This function returns a reference to a hash containing all templates upon success and 1 upon failure. Errors are logged to the Apache log.
 
 =cut
 
@@ -163,11 +171,9 @@ sub get_all_templates {
 
 =head2 C4::Creators::Lib::get_all_layouts()
 
-    This function returns a reference to a hash containing all layouts upon success and 1 upon failure. Errors are logged to the Apache log.
-
-    examples:
+  my $layouts = get_all_layouts();
 
-        my $layouts = get_all_layouts();
+This function returns a reference to a hash containing all layouts upon success and 1 upon failure. Errors are logged to the Apache log.
 
 =cut
 
@@ -191,16 +197,15 @@ sub get_all_layouts {
 
 =head2 C4::Creators::Lib::get_all_profiles()
 
-    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
-    fields. ie. "field_1, field_2, ...field_n" The second limits the records returned based on a string containing a valud SQL 'WHERE' filter.
+  my $profiles = get_all_profiles();
 
-    NOTE: Do not pass in the keyword 'WHERE.'
+  my $profiles = get_all_profiles(field_list => field_list, filter => filter_string);
 
-    examples:
+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
+fields. ie. "field_1, field_2, ...field_n" The second limits the records returned based on a string containing a valud SQL 'WHERE' filter.
 
-        my $profiles = get_all_profiles();
-        my $profiles = get_all_profiles(field_list => field_list, filter => filter_string);
+NOTE: Do not pass in the keyword 'WHERE.'
 
 =cut
 
@@ -243,24 +248,24 @@ sub get_all_image_names {
 
 =head2 C4::Creators::Lib::get_batch_summary()
 
-    This function returns an arrayref whose elements are hashes containing the batch_ids of current batches along with the item count
-    for each batch upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the Apache log.
-    One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter.
+  my $batches = get_batch_summary();
 
-    NOTE: Do not pass in the keyword 'WHERE.'
+  my $batches = get_batch_summary(filter => filter_string);
 
-    examples:
+This function returns an arrayref whose elements are hashes containing the batch_ids of current batches along with the item count
+for each batch upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the Apache log.
+One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter.
 
-        my $batches = get_batch_summary();
-        my $batches = get_batch_summary(filter => filter_string);
+NOTE: Do not pass in the keyword 'WHERE.'
 
 =cut
 
 sub get_batch_summary {
     my %params = @_;
     my @batches = ();
-    my $query = "SELECT DISTINCT batch_id FROM creator_batches WHERE creator=?";
-    $query .= ($params{'filter'} ? " AND $params{'filter'};" : ';');
+    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";
     my $sth = C4::Context->dbh->prepare($query);
 #    $sth->{'TraceLevel'} = 3;
     $sth->execute($params{'creator'});
@@ -268,17 +273,7 @@ sub get_batch_summary {
         warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
         return -1;
     }
-    ADD_BATCHES:
     while (my $batch = $sth->fetchrow_hashref) {
-        my $query = "SELECT count(batch_id) FROM creator_batches WHERE batch_id=? AND creator=?;";
-        my $sth1 = C4::Context->dbh->prepare($query);
-        $sth1->execute($batch->{'batch_id'}, $params{'creator'});
-        if ($sth1->err) {
-            warn sprintf('Database returned the following error on attempted SELECT count: %s', $sth1->errstr);
-            return -1;
-        }
-        my $count = $sth1->fetchrow_arrayref;
-        $batch->{'_item_count'} = @$count[0];
         push(@batches, $batch);
     }
     return \@batches;
@@ -286,16 +281,15 @@ sub get_batch_summary {
 
 =head2 C4::Creators::Lib::get_label_summary()
 
-    This function returns an arrayref whose elements are hashes containing the label_ids of current labels along with the item count
-    for each label upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the Apache log.
-    One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter.
+  my $labels = get_label_summary();
 
-    NOTE: Do not pass in the keyword 'WHERE.'
+  my $labels = get_label_summary(items => @item_list);
 
-    examples:
+This function returns an arrayref whose elements are hashes containing the label_ids of current labels along with the item count
+for each label upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the Apache log.
+One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter.
 
-        my $labels = get_label_summary();
-        my $labels = get_label_summary(items => @item_list);
+NOTE: Do not pass in the keyword 'WHERE.'
 
 =cut
 
@@ -303,7 +297,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
+    my $query = "     SELECT b.title, b.author, bi.itemtype, i.barcode, 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)
@@ -318,14 +312,15 @@ sub get_label_summary {
             return -1;
         }
         my $record = $sth->fetchrow_hashref;
-        my $label_summary->{'_label_number'} = $label_number;
+        my $label_summary;
+        $label_summary->{'_label_number'} = $label_number;
         $record->{'author'} =~ s/[^\.|\w]$// if $record->{'author'};  # strip off ugly trailing chars... but not periods or word chars
         $record->{'title'} =~ s/\W*$//;  # strip off ugly trailing chars
         # FIXME contructing staff interface URLs should be done *much* higher up the stack - for the most part, C4 module code
         # should not know that it's part of a web app
         $record->{'title'} = '<a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' . $record->{'biblionumber'} . '"> ' . $record->{'title'} . '</a>';
         $label_summary->{'_summary'} = $record->{'title'} . " | " . ($record->{'author'} ? $record->{'author'} : 'N/A');
-        $label_summary->{'_item_type'} = $record->{'itemtype'};
+        $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->{'_label_id'} = $item->{'label_id'};
@@ -336,16 +331,15 @@ sub get_label_summary {
 
 =head2 C4::Creators::Lib::get_card_summary()
 
-    This function returns an arrayref whose elements are hashes containing the label_ids of current cards along with the item count
-    for each card upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the Apache log.
-    One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter.
+  my $cards = get_card_summary();
 
-    NOTE: Do not pass in the keyword 'WHERE.'
+  my $cards = get_card_summary(items => @item_list);
 
-    examples:
+This function returns an arrayref whose elements are hashes containing the label_ids of current cards along with the item count
+for each card upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the Apache log.
+One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter.
 
-        my $cards = get_card_summary();
-        my $cards = get_card_summary(items => @item_list);
+NOTE: Do not pass in the keyword 'WHERE.'
 
 =cut
 
@@ -374,11 +368,9 @@ sub get_card_summary {
 
 =head2 C4::Creators::Lib::get_barcode_types()
 
-    This function returns a reference to an array of hashes containing all barcode types along with their name and description.
-
-    examples:
+  my $barcode_types = get_barcode_types();
 
-        my $barcode_types = get_barcode_types();
+This function returns a reference to an array of hashes containing all barcode types along with their name and description.
 
 =cut
 
@@ -388,11 +380,9 @@ sub get_barcode_types {
 
 =head2 C4::Creators::Lib::get_label_types()
 
-    This function returns a reference to an array of hashes containing all label types along with their name and description.
+  my $label_types = get_label_types();
 
-    examples:
-
-        my $label_types = get_label_types();
+This function returns a reference to an array of hashes containing all label types along with their name and description.
 
 =cut
 
@@ -402,11 +392,9 @@ sub get_label_types {
 
 =head2 C4::Creators::Lib::get_font_types()
 
-    This function returns a reference to an array of hashes containing all font types along with their name and description.
-
-    examples:
+  my $font_types = get_font_types();
 
-        my $font_types = get_font_types();
+This function returns a reference to an array of hashes containing all font types along with their name and description.
 
 =cut
 
@@ -416,11 +404,9 @@ sub get_font_types {
 
 =head2 C4::Creators::Lib::get_text_justification_types()
 
-    This function returns a reference to an array of hashes containing all text justification types along with their name and description.
+  my $text_justification_types = get_text_justification_types();
 
-    examples:
-
-        my $text_justification_types = get_text_justification_types();
+This function returns a reference to an array of hashes containing all text justification types along with their name and description.
 
 =cut
 
@@ -430,12 +416,11 @@ sub get_text_justification_types {
 
 =head2 C4::Creators::Lib::get_unit_values()
 
-    This function returns a reference to an array of  hashes containing all unit types along with their description and multiplier. NOTE: All units are relative to a PostScript Point.
-    There are 72 PS points to the inch.
-
-    examples:
+  my $unit_values = get_unit_values();
 
-        my $unit_values = get_unit_values();
+This function returns a reference to an array of  hashes containing all unit types along with their description and multiplier.
+NOTE: All units are relative to a PostScript Point.
+There are 72 PS points to the inch.
 
 =cut
 
@@ -445,11 +430,9 @@ sub get_unit_values {
 
 =head2 C4::Creators::Lib::get_output_formats()
 
-    This function returns a reference to an array of hashes containing all label output formats along with their description.
+  my $label_output_formats = get_output_formats();
 
-    examples:
-
-        my $label_output_formats = get_output_formats();
+This function returns a reference to an array of hashes containing all label output formats along with their description.
 
 =cut
 
@@ -493,15 +476,13 @@ 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.
-
-    examples:
+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.
 
-       my $table = html_table(header_fields, array_of_row_data);
-       $template->param(
-            TABLE => $table,
-       );
+   my $table = html_table(header_fields, array_of_row_data);
+   $template->param(
+       TABLE => $table,
+   );
 
     html example: