(bug #3726) fix ISBD url translation
[koha.git] / C4 / Labels.pm
index 1632eb2..e4fbd5d 100644 (file)
@@ -25,15 +25,17 @@ use Text::Wrap;
 use Algorithm::CheckDigits;
 use C4::Members;
 use C4::Branch;
-# use Data::Dumper;
-# use Smart::Comments;
+use C4::Debug;
+use C4::Biblio;
+use Text::CSV_XS;
+use Data::Dumper;
 
 BEGIN {
        $VERSION = 0.03;
        require Exporter;
        @ISA    = qw(Exporter);
        @EXPORT = qw(
-               &get_label_options &get_label_items
+               &get_label_options &GetLabelItems
                &build_circ_barcode &draw_boundaries
                &drawbox &GetActiveLabelTemplate
                &GetAllLabelTemplates &DeleteTemplate
@@ -49,7 +51,7 @@ BEGIN {
                &GetItemFields
                &get_text_fields
                get_layout &save_layout &add_layout
-               &set_active_layout &by_order
+               &set_active_layout
                &build_text_dropbox
                &delete_layout &get_active_layout
                &get_highest_batch
@@ -60,7 +62,6 @@ BEGIN {
        );
 }
 
-my $DEBUG = 0;
 
 =head1 NAME
 
@@ -68,9 +69,7 @@ C4::Labels - Functions for printing spine labels and barcodes in Koha
 
 =head1 FUNCTIONS
 
-=over 2
-
-=item get_label_options;
+=head2 get_label_options;
 
        $options = get_label_options()
 
@@ -78,20 +77,14 @@ Return a pointer on a hash list containing info from labels_conf table in Koha D
 
 =cut
 
-#'
 sub get_label_options {
-    my $dbh    = C4::Context->dbh;
-    my $query2 = " SELECT * FROM labels_conf where active = 1";
-    my $sth    = $dbh->prepare($query2);
+    my $query2 = " SELECT * FROM labels_conf where active = 1";                # FIXME: exact same as get_active_layout
+    my $sth    = C4::Context->dbh->prepare($query2);
     $sth->execute();
-    my $conf_data = $sth->fetchrow_hashref;
-    $sth->finish;
-    return $conf_data;
+    return $sth->fetchrow_hashref;
 }
 
 sub get_layouts {
-
-## FIXME: this if/else could be compacted...
     my $dbh = C4::Context->dbh;
     my @data;
     my $query = " Select * from labels_conf";
@@ -104,9 +97,6 @@ sub get_layouts {
         push( @resultsloop, $data );
     }
     $sth->finish;
-
-    # @resultsloop
-
     return @resultsloop;
 }
 
@@ -124,16 +114,10 @@ sub get_layout {
 }
 
 sub get_active_layout {
-    my ($layout_id) = @_;
-    my $dbh = C4::Context->dbh;
-
-    # get the actual items to be printed.
-    my $query = " Select * from labels_conf where active = 1";
-    my $sth   = $dbh->prepare($query);
+    my $query = " Select * from labels_conf where active = 1";         # FIXME: exact same as get_label_options
+    my $sth   = C4::Context->dbh->prepare($query);
     $sth->execute();
-    my $data = $sth->fetchrow_hashref;
-    $sth->finish;
-    return $data;
+    return $sth->fetchrow_hashref;
 }
 
 sub delete_layout {
@@ -150,12 +134,14 @@ sub delete_layout {
 sub get_printingtypes {
     my ($layout_id) = @_;
     my @printtypes;
-
-    push( @printtypes, { code => 'BAR',    desc => "barcode" } );
-    push( @printtypes, { code => 'BIB',    desc => "biblio" } );
+# FIXME hard coded print types
+    push( @printtypes, { code => 'BAR',    desc => "barcode only" } );
+    push( @printtypes, { code => 'BIB',    desc => "biblio only" } );
     push( @printtypes, { code => 'BARBIB', desc => "barcode / biblio" } );
     push( @printtypes, { code => 'BIBBAR', desc => "biblio / barcode" } );
     push( @printtypes, { code => 'ALT',    desc => "alternating labels" } );
+    push( @printtypes, { code => 'CSV',    desc => "csv output" } );
+    push( @printtypes, { code => 'PATCRD', desc => "patron cards" } );
 
     my $conf             = get_layout($layout_id);
     my $active_printtype = $conf->{'printingtype'};
@@ -164,19 +150,18 @@ sub get_printingtypes {
 
     foreach my $printtype (@printtypes) {
         if ( $printtype->{'code'} eq $active_printtype ) {
-            $printtype->{'active'} = 'MOO';
+            $printtype->{'active'} = 1;
         }
     }
     return @printtypes;
 }
 
+# this sub (build_text_dropbox) is deprecated and should be deleted. 
+# rch 2008.04.15
+#
 sub build_text_dropbox {
     my ($order) = @_;
-
-    #  my @fields      = get_text_fields();
-    #    my $field_count = scalar @fields;
-    my $field_count = 10;    # <-----------       FIXME hard coded
-
+    my $field_count = 7;    # <-----------       FIXME hard coded
     my @lines;
     !$order
       ? push( @lines, { num => '', selected => '1' } )
@@ -186,139 +171,150 @@ sub build_text_dropbox {
         $line->{'selected'} = 1 if $i eq $order;
         push( @lines, $line );
     }
-
-    # add a blank row too
-
     return @lines;
 }
 
 sub get_text_fields {
-    my ($layout_id, $sorttype) = @_;
-
-    my ( $a, $b, $c, $d, $e, $f, $g, $h, $i ,$j, $k );
-
+    my ( $layout_id, $sorttype ) = @_;
+    my @sorted_fields;
+    my $error;
     my $sortorder = get_layout($layout_id);
+    if ( $sortorder->{formatstring} ) {
+        if ( !$sorttype ) {
+            return $sortorder->{formatstring};
+        }
+        else {
+            my $csv    = Text::CSV_XS->new( { allow_whitespace => 1 } );
+            my $line   = $sortorder->{formatstring};
+            my $status = $csv->parse($line);
+            @sorted_fields =
+              map { { 'code' => $_, desc => $_ } } $csv->fields();
+            $error = $csv->error_input();
+            warn $error if $error;    # TODO - do more with this.
+        }
+    }
+    else {
 
-    # $sortorder
-
-    $a = {
-        code  => 'itemtype',
-        desc  => "Item Type",
-        order => $sortorder->{'itemtype'}
-    };
-    $b = {
-        code  => 'dewey',
-        desc  => "Dewey",
-        order => $sortorder->{'dewey'}
-    };
-    $c = { code => 'issn', desc => "ISSN", 
-        order => $sortorder->{'issn'} };
-    $d = { code => 'isbn', desc => "ISBN", 
-            order => $sortorder->{'isbn'} };
-    $e = {
-        code  => 'class',
-        desc  => "Classification",
-        order => $sortorder->{'class'}
-    };
-    $f = {
-        code  => 'subclass',
-        desc  => "Sub-Class",
-        order => $sortorder->{'subclass'}
-    };
-    $g = {
-        code  => 'barcode',
-        desc  => "Barcode",
-        order => $sortorder->{'barcode'}
-    };
-    $h =
-      { code => 'author', desc => "Author", order => $sortorder->{'author'} };
-    $i = { code => 'title', desc => "Title", order => $sortorder->{'title'} };
-    $j = { code => 'itemcallnumber', desc => "Call Number", order => $sortorder->{'itemcallnumber'} };
-       $k = { code => 'subtitle', desc => "Subtitle", order => $sortorder->{'subtitle'} }; 
-    
-       my @text_fields = ( $a, $b, $c, $d, $e, $f, $g, $h, $i ,$j, $k );
+     # These fields are hardcoded based on the template for label-edit-layout.pl
+        my @text_fields = (
+            {
+                code  => 'itemtype',
+                desc  => "Item Type",
+                order => $sortorder->{'itemtype'}
+            },
+            {
+                code  => 'issn',
+                desc  => "ISSN",
+                order => $sortorder->{'issn'}
+            },
+            {
+                code  => 'isbn',
+                desc  => "ISBN",
+                order => $sortorder->{'isbn'}
+            },
+            {
+                code  => 'barcode',
+                desc  => "Barcode",
+                order => $sortorder->{'barcode'}
+            },
+            {
+                code  => 'author',
+                desc  => "Author",
+                order => $sortorder->{'author'}
+            },
+            {
+                code  => 'title',
+                desc  => "Title",
+                order => $sortorder->{'title'}
+            },
+            {
+                code  => 'itemcallnumber',
+                desc  => "Call Number",
+                order => $sortorder->{'itemcallnumber'}
+            },
+        );
+
+        my @new_fields = ();
+        foreach my $field (@text_fields) {
+            push( @new_fields, $field ) if $field->{'order'} > 0;
+        }
 
-    my @new_fields;
-    foreach my $field (@text_fields) {
-        push( @new_fields, $field ) if $field->{'order'} > 0;
+        @sorted_fields = sort { $$a{order} <=> $$b{order} } @new_fields;
     }
 
-    my @sorted_fields = sort by_order @new_fields;
+    # if we have a 'formatstring', then we ignore these hardcoded fields.
     my $active_fields;
-    foreach my $field (@sorted_fields) {
-     $sorttype eq 'codes' ?   $active_fields .= "$field->{'code'} " :
-          $active_fields .= "$field->{'desc'} ";
+
+    if ( $sorttype eq 'codes' )
+    { # FIXME: This sub should really always return the array of hashrefs and let the caller take what he wants from that -fbcit
+        return @sorted_fields;
+    }
+    else {
+        foreach my $field (@sorted_fields) {
+            $active_fields .= "$field->{'desc'} ";
+        }
+        return $active_fields;
     }
-    return $active_fields;
 
 }
 
-sub by_order {
-    $$a{order} <=> $$b{order};
-}
+=head2 sub add_batch
+
+=over 4
+
+ add_batch($batch_type,\@batch_list);
+ if $batch_list is supplied,
+   create a new batch with those items.
+ else, return the next available batch_id.
 
-sub add_batch {
-    my ( $batch_type ) = @_;
-    my $new_batch;
+=back
+
+=cut
+
+sub add_batch ($;$) {
+       my $table = (@_ and 'patroncards' eq shift) ? 'patroncards' : 'labels';
+    my $batch_list = (@_) ? shift : undef;
     my $dbh = C4::Context->dbh;
-    my $q =
-      "SELECT DISTINCT batch_id FROM $batch_type ORDER BY batch_id desc LIMIT 1";
+    # FIXME : batch_id  should be an auto_incr INT.  Temporarily casting as int ( see koha bug 2555 )
+    # until a label_batches table is added, and we can convert batch_id to int.
+    my $q ="SELECT MAX( CAST(batch_id AS SIGNED) ) FROM $table";
     my $sth = $dbh->prepare($q);
     $sth->execute();
-    my $data = $sth->fetchrow_hashref;
-    $sth->finish;
-
-    if ( !$data->{'batch_id'} ) {
-        $new_batch = 1;
-    }
-    else {
-        $new_batch = ( $data->{'batch_id'} + 1 );
-    }
-
-    return $new_batch;
+    my ($batch_id) = $sth->fetchrow_array || 0;
+       $batch_id++;
+       if ($batch_list) {
+               if ($table eq 'patroncards') {
+                       $sth = $dbh->prepare("INSERT INTO $table (`batch_id`,`borrowernumber`) VALUES (?,?)"); 
+               } else {
+                       $sth = $dbh->prepare("INSERT INTO $table (`batch_id`,`itemnumber`    ) VALUES (?,?)"); 
+               }
+               for (@$batch_list) {
+                       $sth->execute($batch_id,$_);
+               }
+       }
+       return $batch_id;
 }
 
 #FIXME: Needs to be ported to receive $batch_type
+# ... this looks eerily like add_batch() ...
 sub get_highest_batch {
-    my $new_batch;
-    my $dbh = C4::Context->dbh;
+       my $table = (@_ and 'patroncards' eq shift) ? 'patroncards' : 'labels';
     my $q =
-      "select distinct batch_id from labels order by batch_id desc limit 1";
-    my $sth = $dbh->prepare($q);
+      "select distinct batch_id from $table order by batch_id desc limit 1";
+    my $sth = C4::Context->dbh->prepare($q);
     $sth->execute();
-    my $data = $sth->fetchrow_hashref;
-    $sth->finish;
-
-    if ( !$data->{'batch_id'} ) {
-        $new_batch = 1;
-    }
-    else {
-        $new_batch =  $data->{'batch_id'};
-    }
-
-    return $new_batch;
+    my $data = $sth->fetchrow_hashref or return 1;
+       return ($data->{'batch_id'} || 1);
 }
 
 
-#FIXME: Needs to be ported to receive $batch_type
-sub get_batches {
-    my $dbh = C4::Context->dbh;
-    my $q   = "select batch_id, count(*) as num from labels group by batch_id";
-    my $sth = $dbh->prepare($q);
+sub get_batches (;$) {
+       my $table = (@_ and 'patroncards' eq shift) ? 'patroncards' : 'labels';
+    my $q   = "SELECT batch_id, COUNT(*) AS num FROM $table GROUP BY batch_id";
+    my $sth = C4::Context->dbh->prepare($q);
     $sth->execute();
-    my @resultsloop;
-    while ( my $data = $sth->fetchrow_hashref ) {
-        push( @resultsloop, $data );
-    }
-    $sth->finish;
-
-# Not sure why we are doing this rather than simply telling the user that no batches are currently defined.
-# So I'm commenting this out and modifying label-manager.tmpl to properly inform the user as stated. -fbcit
-    # adding a dummy batch=1 value , if none exists in the db
-#    if ( !scalar(@resultsloop) ) {
-#        push( @resultsloop, { batch_id => '1' , num => '0' } );
-#    }
-    return @resultsloop;
+       my $batches = $sth->fetchall_arrayref({});
+       return @$batches;
 }
 
 sub delete_batch {
@@ -368,7 +364,7 @@ sub GetTextWrapCols {
     my $strwidth;
     my $count = 0;
 #    my $textlimit = $label_width - ($left_text_margin);
-    my $textlimit = $label_width - ( 2* $left_text_margin);
+    my $textlimit = $label_width - ( * $left_text_margin);
 
     while ( $strwidth < $textlimit ) {
         $strwidth = prStrWidth( $string, $font, $fontsize );
@@ -445,7 +441,7 @@ sub SaveTemplate {
         $leftmargin,  $cols,        $rows,         $colgap,
         $rowgap,      $font,        $fontsize,     $units
     ) = @_;
-    warn "Passed \$font:$font";
+    $debug and warn "Passed \$font:$font";
     my $dbh = C4::Context->dbh;
     my $query =
       " UPDATE labels_templates SET tmpl_code=?, tmpl_desc=?, page_width=?,
@@ -517,9 +513,9 @@ sub add_layout {
 
     my (
         $barcodetype,  $title,                 $subtitle,      $isbn,       $issn,
-        $itemtype,     $bcn,            $dcn,        $classif,
-        $subclass,     $itemcallnumber, $author,     $tmpl_id,
-        $printingtype, $guidebox,       $startlabel, $layoutname
+        $itemtype,     $bcn,            $text_justify,        $callnum_split,
+        $itemcallnumber, $author,     $tmpl_id,
+        $printingtype, $guidebox,       $startlabel, $layoutname, $formatstring
     ) = @_;
 
     my $dbh    = C4::Context->dbh;
@@ -528,16 +524,16 @@ sub add_layout {
     $sth2->execute();
     $query2 = "INSERT INTO labels_conf
             ( barcodetype, title, subtitle, isbn,issn, itemtype, barcode,
-              dewey, class, subclass, itemcallnumber, author, printingtype,
-                guidebox, startlabel, layoutname, active )
-               values ( ?, ?, ?, ?, ?, ?, ?,  ?,?, ?, ?, ?, ?, ?,?,?, 1 )";
+              text_justify, callnum_split, itemcallnumber, author, printingtype,
+                guidebox, startlabel, layoutname, formatstring, active )
+               values ( ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?, 1 )";
     $sth2 = $dbh->prepare($query2);
     $sth2->execute(
         $barcodetype, $title, $subtitle, $isbn, $issn,
 
-        $itemtype, $bcn,            $dcn,    $classif,
-        $subclass, $itemcallnumber, $author, $printingtype,
-        $guidebox, $startlabel,     $layoutname
+        $itemtype, $bcn,            $text_justify,    $callnum_split,
+        $itemcallnumber, $author, $printingtype,
+        $guidebox, $startlabel,     $layoutname, $formatstring
     );
     $sth2->finish;
 
@@ -549,9 +545,9 @@ sub save_layout {
 
     my (
         $barcodetype,  $title,          $subtitle,     $isbn,       $issn,
-        $itemtype,     $bcn,            $dcn,        $classif,
-        $subclass,     $itemcallnumber, $author,     $tmpl_id,
-        $printingtype, $guidebox,       $startlabel, $layoutname,
+        $itemtype,     $bcn,            $text_justify,        $callnum_split,
+        $itemcallnumber, $author,     $tmpl_id,
+        $printingtype, $guidebox,       $startlabel, $layoutname, $formatstring,
         $layout_id
     ) = @_;
 ### $layoutname
@@ -560,22 +556,22 @@ sub save_layout {
     my $dbh    = C4::Context->dbh;
     my $query2 = "update labels_conf set 
              barcodetype=?, title=?, subtitle=?, isbn=?,issn=?, 
-            itemtype=?, barcode=?,    dewey=?, class=?,
-             subclass=?, itemcallnumber=?, author=?,  printingtype=?,  
-               guidebox=?, startlabel=?, layoutname=? where id = ?";
+            itemtype=?, barcode=?,    text_justify=?, callnum_split=?,
+            itemcallnumber=?, author=?,  printingtype=?,  
+               guidebox=?, startlabel=?, layoutname=?, formatstring=? where id = ?";
     my $sth2 = $dbh->prepare($query2);
     $sth2->execute(
         $barcodetype, $title,          $subtitle,      $isbn,       $issn,
-        $itemtype,    $bcn,            $dcn,        $classif,
-        $subclass,    $itemcallnumber, $author,     $printingtype,
-        $guidebox,    $startlabel,     $layoutname, $layout_id
+        $itemtype,    $bcn,            $text_justify,        $callnum_split,
+        $itemcallnumber, $author,     $printingtype,
+        $guidebox,    $startlabel,     $layoutname, $formatstring,  $layout_id
     );
     $sth2->finish;
 
     return;
 }
 
-=item GetAllPrinterProfiles;
+=head2 GetAllPrinterProfiles;
 
     @profiles = GetAllPrinterProfiles()
 
@@ -599,7 +595,7 @@ sub GetAllPrinterProfiles {
     return @resultsloop;
 }
 
-=item GetSinglePrinterProfile;
+=head2 GetSinglePrinterProfile;
 
     $profile = GetSinglePrinterProfile()
 
@@ -618,7 +614,7 @@ sub GetSinglePrinterProfile {
     return $template;
 }
 
-=item SaveProfile;
+=head2 SaveProfile;
 
     SaveProfile('parameters')
 
@@ -642,7 +638,7 @@ sub SaveProfile {
     $sth->finish;
 }
 
-=item CreateProfile;
+=head2 CreateProfile;
 
     CreateProfile('parameters')
 
@@ -671,7 +667,7 @@ sub CreateProfile {
     return $error;
 }
 
-=item DeleteProfile;
+=head2 DeleteProfile;
 
     DeleteProfile(prof_id)
 
@@ -690,7 +686,7 @@ sub DeleteProfile {
     return $error;
 }
 
-=item GetAssociatedProfile;
+=head2 GetAssociatedProfile;
 
     $assoc_prof = GetAssociatedProfile(tmpl_id)
 
@@ -713,7 +709,7 @@ sub GetAssociatedProfile {
     return $assoc_prof;
 }
 
-=item SetAssociatedProfile;
+=head2 SetAssociatedProfile;
 
     SetAssociatedProfile($prof_id, $tmpl_id)
 
@@ -733,16 +729,16 @@ sub SetAssociatedProfile {
     $sth->finish;
 }
 
-=item get_label_items;
 
-        $options = get_label_items()
+=head2 GetLabelItems;
 
-Returns an array of references-to-hash, whos keys are the field from the biblio, biblioitems, items and labels tables in the Koha database.
+        $options = GetLabelItems()
+
+Returns an array of references-to-hash, whos keys are the fields from the biblio, biblioitems, items and labels tables in the Koha database.
 
 =cut
 
-#'
-sub get_label_items {
+sub GetLabelItems {
     my ($batch_id) = @_;
     my $dbh = C4::Context->dbh;
 
@@ -750,16 +746,20 @@ sub get_label_items {
     my $count;
     my @data;
     my $sth;
-
+    
     if ($batch_id) {
-        my $query3 = "Select * from labels where batch_id = ? order by labelid ";
+        my $query3 = "
+            SELECT *
+            FROM labels
+            WHERE batch_id = ?
+            ORDER BY labelid";
         $sth = $dbh->prepare($query3);
         $sth->execute($batch_id);
-
     }
     else {
-
-        my $query3 = "Select * from labels";
+        my $query3 = "
+            SELECT *
+            FROM labels";
         $sth = $dbh->prepare($query3);
         $sth->execute();
     }
@@ -768,20 +768,21 @@ sub get_label_items {
     while ( my $data = $sth->fetchrow_hashref ) {
 
         # lets get some summary info from each item
-        my $query1 = " 
-        select i.*, bi.*, b.*  from items i,biblioitems bi,biblio b 
-               where itemnumber=? and  i.biblioitemnumber=bi.biblioitemnumber and                  
-               bi.biblionumber=b.biblionumber"; 
-     
-               my $sth1 = $dbh->prepare($query1);
+        my $query1 =
+#            FIXME This makes for a very bulky data structure; data from tables w/duplicate col names also gets overwritten.
+#            Something like this, perhaps, but this also causes problems because we need more fields sometimes.
+#            SELECT i.barcode, i.itemcallnumber, i.itype, bi.isbn, bi.issn, b.title, b.author
+           "SELECT bi.*, i.*, b.*
+            FROM items AS i, biblioitems AS bi ,biblio AS b
+            WHERE itemnumber=? AND i.biblioitemnumber=bi.biblioitemnumber AND bi.biblionumber=b.biblionumber";
+        my $sth1 = $dbh->prepare($query1);
         $sth1->execute( $data->{'itemnumber'} );
 
         my $data1 = $sth1->fetchrow_hashref();
         $data1->{'labelno'}  = $i1;
         $data1->{'labelid'}  = $data->{'labelid'};
         $data1->{'batch_id'} = $batch_id;
-        $data1->{'summary'} =
-          "$data1->{'barcode'}, $data1->{'title'}, $data1->{'isbn'}";
+        $data1->{'summary'} = "$data1->{'barcode'}, $data1->{'title'}, $data1->{'isbn'}";
 
         push( @resultsloop, $data1 );
         $sth1->finish;
@@ -795,31 +796,115 @@ sub get_label_items {
 
 sub GetItemFields {
     my @fields = qw (
-      barcode title subtitle
-      dewey isbn issn author class
-      itemtype subclass itemcallnumber
-
+      barcode           title
+      isbn              issn
+      author            itemtype
+      itemcallnumber
     );
     return @fields;
 }
 
+=head2 GetBarcodeData
+
+=over 4
+
+Parse labels_conf.formatstring value
+(one value of the csv, which has already been split)
+and return string from koha tables or MARC record.
+
+=back
+
+=cut
+
+sub GetBarcodeData {
+    my ( $f, $item, $record ) = @_;
+    my $kohatables = &_descKohaTables();
+    my $datastring = '';
+    my $match_kohatable = join(
+        '|',
+        (
+            @{ $kohatables->{biblio} },
+            @{ $kohatables->{biblioitems} },
+            @{ $kohatables->{items} }
+        )
+    );
+    while ($f) {  
+        $f =~ s/^\s?//;
+        if ( $f =~ /^'(.*)'.*/ ) {
+            # single quotes indicate a static text string.
+            $datastring .= $1;
+            $f = $';
+        }
+        elsif ( $f =~ /^($match_kohatable).*/ ) {
+            $datastring .= $item->{$f};
+            $f = $';
+        }
+        elsif ( $f =~ /^([0-9a-z]{3})(\w)(\W?).*?/ ) {
+            my ($field,$subf,$ws) = ($1,$2,$3);
+            my $subf_data;
+            my ($itemtag, $itemsubfieldcode) = &GetMarcFromKohaField("items.itemnumber",'');
+            my @marcfield = $record->field($field);
+            if(@marcfield) {
+                if($field eq $itemtag) {  # item-level data, we need to get the right item.
+                    foreach my $itemfield (@marcfield) {
+                        if ( $itemfield->subfield($itemsubfieldcode) eq $item->{'itemnumber'} ) {
+                            $datastring .= $itemfield->subfield($subf ) . $ws;
+                            last;
+                        }
+                    }
+                } else {  # bib-level data, we'll take the first matching tag/subfield.
+                    $datastring .= $marcfield[0]->subfield($subf) . $ws ;
+                }
+            }
+            $f = $';
+        }
+        else {
+            warn "failed to parse label formatstring: $f";
+            last;    # Failed to match
+        }
+    }
+    return $datastring;
+}
+
+=head2 descKohaTables
+
+Return a hashref of an array of hashes,
+with name,type keys.
+
+=cut
+
+sub _descKohaTables {
+       my $dbh = C4::Context->dbh();
+       my $kohatables;
+       for my $table ( 'biblio','biblioitems','items' ) {
+               my $sth = $dbh->column_info(undef,undef,$table,'%');
+               while (my $info = $sth->fetchrow_hashref()){
+                       push @{$kohatables->{$table}} , $info->{'COLUMN_NAME'} ;
+               }
+               $sth->finish;
+       }
+       return $kohatables;
+}
+
 sub GetPatronCardItems {
 
     my ( $batch_id ) = @_;
     my @resultsloop;
     
-    warn "Received batch id: $batch_id";
     my $dbh = C4::Context->dbh;
-    my $query = "SELECT * FROM patroncards WHERE batch_id = ? ORDER BY borrowernumber";
+#    my $query = "SELECT * FROM patroncards WHERE batch_id = ? ORDER BY borrowernumber";
+    my $query = "SELECT * FROM patroncards WHERE batch_id = ? ORDER BY cardid";
     my $sth = $dbh->prepare($query);
-    warn "Executing query...\n";
     $sth->execute($batch_id);
-    warn "Parsing results...\n";
+    my $cardno = 1;
     while ( my $data = $sth->fetchrow_hashref ) {
-        warn "for borrowernumber $data->{'borrowernumber'}\n";
         my $patron_data = GetMember( $data->{'borrowernumber'} );
         $patron_data->{'branchname'} = GetBranchName( $patron_data->{'branchcode'} );
+        $patron_data->{'cardno'} = $cardno;
+        $patron_data->{'cardid'} = $data->{'cardid'};
+        $patron_data->{'batch_id'} = $batch_id;
         push( @resultsloop, $patron_data );
+        $cardno++;
     }
     $sth->finish;
     return @resultsloop;
@@ -827,138 +912,233 @@ sub GetPatronCardItems {
 }
 
 sub deduplicate_batch {
-       my $batch_id = shift or return undef;
+       my ( $batch_id, $batch_type ) = @_;
        my $query = "
        SELECT DISTINCT
-                       batch_id,itemnumber,
-                       count(labelid) as count 
-       FROM     labels 
-       WHERE    batch_id = ?
-       GROUP BY itemnumber,batch_id
-       HAVING   count > 1
+                       batch_id," . (($batch_type eq 'labels') ? 'itemnumber' : 'borrowernumber') . ",
+                       count(". (($batch_type eq 'labels') ? 'labelid' : 'cardid') . ") as count 
+       FROM $batch_type 
+       WHERE batch_id = ?
+       GROUP BY " . (($batch_type eq 'labels') ? 'itemnumber' : 'borrowernumber') . ",batch_id
+       HAVING count > 1
        ORDER BY batch_id,
-                        count DESC  ";
+       count DESC  ";
        my $sth = C4::Context->dbh->prepare($query);
        $sth->execute($batch_id);
-       $sth->rows or return undef;
+        warn $sth->errstr if $sth->errstr;
+       $sth->rows or return undef, $sth->errstr;
 
-       my $del_query = qq(
+       my $del_query = "
        DELETE 
-       FROM     labels 
+       FROM     $batch_type
        WHERE    batch_id = ?
-       AND      itemnumber = ?
+       AND      " . (($batch_type eq 'labels') ? 'itemnumber' : 'borrowernumber') . " = ?
        ORDER BY timestamp ASC
-       );
+       ";
        my $killed = 0;
        while (my $data = $sth->fetchrow_hashref()) {
-               my $itemnumber = $data->{itemnumber} or next;
+               my $itemnumber = $data->{(($batch_type eq 'labels') ? 'itemnumber' : 'borrowernumber')} or next;
                my $limit      = $data->{count} - 1  or next;
                my $sth2 = C4::Context->dbh->prepare("$del_query  LIMIT $limit");
                # die sprintf "$del_query LIMIT %s\n (%s, %s)", $limit, $batch_id, $itemnumber;
                # $sth2->execute($batch_id, C4::Context->dbh->quote($data->{itemnumber}), $data->{count} - 1)
                $sth2->execute($batch_id, $itemnumber) and
                        $killed += ($data->{count} - 1);
+                warn $sth2->errstr if $sth2->errstr;
        }
-       return $killed;
+       return $killed, undef;
+}
+
+our $possible_decimal = qr/\d+(?:\.\d+)?/;
+
+sub split_lccn {
+    my ($lccn) = @_;    
+    $_ = $lccn;
+    # lccn examples: 'HE8700.7 .P6T44 1983', 'BS2545.E8 H39 1996';
+    my (@parts) = m/
+        ^([a-zA-Z]+)      # HE          # BS
+        (\d+(?:\.\d)*)    # 8700.7      # 2545
+        \s*
+        (\.*\D+\d*)       # .P6         # .E8
+        \s*
+        (.*)              # T44 1983    # H39 1996   # everything else (except any bracketing spaces)
+        \s*
+        /x;
+    unless (scalar @parts)  {
+        $debug and print STDERR "split_lccn regexp failed to match string: $_\n";
+        push @parts, $_;     # if no match, just push the whole string.
+    }
+    push @parts, split /\s+/, pop @parts;   # split the last piece into an arbitrary number of pieces at spaces
+    $debug and print STDERR "split_lccn array: ", join(" | ", @parts), "\n";
+    return @parts;
+}
+
+sub split_ddcn {
+    my ($ddcn) = @_;
+    $_ = $ddcn;
+    s/\///g;   # in theory we should be able to simply remove all segmentation markers and arrive at the correct call number...
+    # ddcn examples: 'R220.3 H2793Z H32 c.2', 'BIO JP2 R5c.1'
+
+    my (@parts) = m/
+        ^([a-zA-Z-]+(?:$possible_decimal)?) # R220.3            # BIO   # first example will require extra splitting
+        \s*
+        (.+)                               # H2793Z H32 c.2   # R5c.1   # everything else (except bracketing spaces)
+        \s*
+        /x;
+    unless (scalar @parts)  {
+        $debug and print STDERR "split_ddcn regexp failed to match string: $_\n";
+        push @parts, $_;     # if no match, just push the whole string.
+    }
+
+    if ($parts[ 0] =~ /^([a-zA-Z]+)($possible_decimal)$/) {
+          shift @parts;         # pull off the mathching first element, like example 1
+        unshift @parts, $1, $2; # replace it with the two pieces
+    }
+
+    push @parts, split /\s+/, pop @parts;   # split the last piece into an arbitrary number of pieces at spaces
+
+    if ($parts[-1] !~ /^.*\d-\d.*$/ && $parts[-1] =~ /^(.*\d+)(\D.*)$/) {
+         pop @parts;            # pull off the mathching last element, like example 2
+        push @parts, $1, $2;    # replace it with the two pieces
+    }
+
+    $debug and print STDERR "split_ddcn array: ", join(" | ", @parts), "\n";
+    return @parts;
+}
+
+sub split_fcn {
+    my ($fcn) = @_;
+    my @fcn_split = ();
+    # Split fiction call numbers based on spaces
+    SPLIT_FCN:
+    while ($fcn) {
+        if ($fcn =~ m/([A-Za-z0-9]+\.?[0-9]?)(\W?).*?/x) {
+            push (@fcn_split, $1);
+            $fcn = $';
+        }
+        else {
+            last SPLIT_FCN;     # No match, break out of the loop
+        }
+    }
+    return @fcn_split;
 }
 
 sub DrawSpineText {
 
     my ( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize, $left_text_margin,
-        $text_wrap_cols, $item, $conf_data, $printingtype )
-      = @_;
-# hack to fix column name mismatch betwen labels_conf.class, and bibitems.classification
-       $$item->{'class'} = $$item->{'classification'};
-    $Text::Wrap::columns   = $text_wrap_cols;
-    $Text::Wrap::separator = "\n";
+        $text_wrap_cols, $item, $conf_data, $printingtype ) = @_;
+    
+    # Replaced item's itemtype with the more user-friendly description...
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes");
+    $sth->execute();
+    while ( my $data = $sth->fetchrow_hashref ) {
+        $$item->{'itemtype'} = $data->{'description'} if ($$item->{'itemtype'} eq $data->{'itemtype'});
+        $$item->{'itype'} = $data->{'description'} if ($$item->{'itype'} eq $data->{'itemtype'});
+    }
 
-    my $str;
+    my $str = '';
 
     my $top_text_margin = ( $fontsize + 3 );    #FIXME: This should be a template parameter and passed in...
     my $line_spacer = ( $fontsize * 1 );    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
 
-    # add your printable fields manually in here
-
     my $layout_id = $$conf_data->{'id'};
 
-#    my @fields = GetItemFields();
+    my $vPos = ( $y_pos + ( $label_height - $top_text_margin ) );
 
-    my $str_fields = get_text_fields($layout_id, 'codes' );
-    my @fields = split(/ /, $str_fields);
-    #warn Dumper(@fields);
+    my @str_fields = get_text_fields($layout_id, 'codes' );  
+    my $record = GetMarcBiblio($$item->{biblionumber});
+    # FIXME - returns all items, so you can't get data from an embedded holdings field.
+    # TODO - add a GetMarcBiblio1item(bibnum,itemnum) or a GetMarcItem(itemnum).
 
-    my $vPos   = ( $y_pos + ( $label_height - $top_text_margin ) );
-    my $font = prFont($fontname);
-
-    # warn Dumper $conf_data;
-    #warn Dumper $item;
-
-    foreach my $field (@fields) {
-
-        # testing hack
-#     $$item->{"$field"} = $field . ": " . $$item->{"$field"};
+    my $old_fontname = $fontname; # We need to keep track of the original font passed in...
 
+    # Grab the cn_source and if that is NULL, the DefaultClassificationSource syspref
+    my $cn_source = ($$item->{'cn_source'} ? $$item->{'cn_source'} : C4::Context->preference('DefaultClassificationSource'));
+    for my $field (@str_fields) {
+        $field->{'code'} or warn "get_text_fields($layout_id, 'codes') element missing 'code' field";
+        if ($field->{'code'} eq 'itemtype') {
+            $field->{'data'} = C4::Context->preference('item-level_itypes') ? $$item->{'itype'} : $$item->{'itemtype'};
+        }
+        elsif ($$conf_data->{'formatstring'}) {
+            # if labels_conf.formatstring has a value, then it overrides the  hardcoded option.
+            $field->{'data'} =  GetBarcodeData($field->{'code'},$$item,$record) ;
+        }
+        else {
+            $field->{data} =   $$item->{$field->{'code'}}  ;
+        }
+        # This allows us to print the title in italic (oblique) type... (Times Roman has a different nomenclature.)
+        # It seems there should be a better way to handle fonts in the label/patron card tool altogether -fbcit
+        ($field->{code} eq 'title') ? (($old_fontname =~ /T/) ? ($fontname = 'TI') : ($fontname = ($old_fontname . 'O'))) : ($fontname = $old_fontname);
+        my $font = prFont($fontname);
         # if the display option for this field is selected in the DB,
         # and the item record has some values for this field, display it.
-        if ( $$conf_data->{"$field"} && $$item->{"$field"} ) {
-
-            #            warn "CONF_TYPE = $field";
-
+        # Or if there is a csv list of fields to display, display them.
+        if ( ($$conf_data->{'formatstring'}) || ( $$conf_data->{$field->{code}} && $$item->{$field->{code}} ) ) {
             # get the string
-            $str = $$item->{"$field"};
+            my $str = $field->{data} ;
             # strip out naughty existing nl/cr's
             $str =~ s/\n//g;
             $str =~ s/\r//g;
-            # wrap lines based on call number dividers '/'
             my @strings;
-
-            while ( $str =~ /\// ) {
-                $str =~ /^(.*)\/(.*)$/;
-
-                #warn "\$2=$2";
-                unshift @strings, $2;
-                $str = $1;
+            my @callnumber_list = ('itemcallnumber', '050a', '050b', '082a', '952o'); # Fields which hold call number data  ( 060? 090? 092? 099? )
+            if ((grep {$field->{code} =~ m/$_/} @callnumber_list) and ($printingtype eq 'BIB') and ($$conf_data->{'callnum_split'})) { # If the field contains the call number, we do some sp
+                if ($cn_source eq 'lcc') {
+                    @strings = split_lccn($str);
+                    @strings = split_fcn($str) if !@strings;    # If it was not a true lccn, try it as a fiction call number
+                    push (@strings, $str) if !@strings;         # If it was not that, send it on unsplit
+                } elsif ($cn_source eq 'ddc') {
+                    @strings = split_ddcn($str);
+                    @strings = split_fcn($str) if !@strings;
+                    push (@strings, $str) if !@strings;
+                } else {
+                    # FIXME Need error trapping here; something to be informative to the user perhaps -crn
+                    push @strings, $str;
+                }
+            } else {
+                $str =~ s/\/$//g;       # Here we will strip out all trailing '/' in fields other than the call number...
+                $str =~ s/\(/\\\(/g;    # Escape '(' and ')' for the postscript stream...
+                $str =~ s/\)/\\\)/g;
+                # Wrap text lines exceeding $text_wrap_cols length...
+                $Text::Wrap::columns = $text_wrap_cols;
+                my @line = split(/\n/ ,wrap('', '', $str));
+                # If this is a title field, limit to two lines; all others limit to one...
+                if ($field->{code} eq 'title' && scalar(@line) >= 2) {
+                    while (scalar(@line) > 2) {
+                        pop @line;
+                    }
+                } else {
+                    while (scalar(@line) > 1) {
+                        pop @line;
+                    }
+                }
+                push(@strings, @line);
             }
-            
-            unshift @strings, $str;
-            
-            # strip out division slashes
-            #$str =~ s/\///g;
-            #warn "\$str after striping division marks: $str";
-            # chop the string up into _upto_ 12 chunks
-            # and seperate the chunks with newlines
-
-            #$str = wrap( "", "", "$str" );
-            #$str = wrap( "", "", "$str" );
-
-            # split the chunks between newline's, into an array
-            #my @strings = split /\n/, $str;
-
-            # then loop for each string line
+            # loop for each string line
             foreach my $str (@strings) {
-                my $hPos;
-                if ( $printingtype eq 'BIB' ) { #FIXME: This is a hack and needs to be implimented as a text justification option in the template...
-                    # some code to try and center each line on the label based on font size and string point width...
-                    my $stringwidth = prStrWidth($str, $fontname, $fontsize);
-                    my $whitespace = ( $label_width - ( $stringwidth + (2 * $left_text_margin) ) );
-                    $hPos = ( ( $whitespace  / 2 ) + $x_pos + $left_text_margin );
-                    #warn "\$label_width=$label_width \$stringwidth=$stringwidth \$whitespace=$whitespace \$left_text_margin=$left_text_margin for $str\n";
+                next if $str eq '';
+                my $hPos = 0;
+                my $stringwidth = prStrWidth($str, $fontname, $fontsize);
+                if ( $$conf_data->{'text_justify'} eq 'R' ) { 
+                    $hPos = $x_pos + $label_width - ( $left_text_margin + $stringwidth );
+                } elsif($$conf_data->{'text_justify'} eq 'C') {
+                     # some code to try and center each line on the label based on font size and string point width...
+                     my $whitespace = ( $label_width - ( $stringwidth + (2 * $left_text_margin) ) );
+                     $hPos = ( ( $whitespace  / 2 ) + $x_pos + $left_text_margin );
+                #warn "\$label_width=$label_width \$stringwidth=$stringwidth \$whitespace=$whitespace \$left_text_margin=$left_text_margin for $str\n";
                 } else {
                     $hPos = ( $x_pos + $left_text_margin );
                 }
                 PrintText( $hPos, $vPos, $font, $fontsize, $str );
                 $vPos = $vPos - $line_spacer;
-                
             }
-        }    # if field is     
-    }    #foreach feild
+       }
+    }  #foreach field
 }
 
 sub PrintText {
     my ( $hPos, $vPos, $font, $fontsize, $text ) = @_;
     my $str = "BT /$font $fontsize Tf $hPos $vPos Td ($text) Tj ET";
-    warn $str;
     prAdd($str);
 }
 
@@ -973,10 +1153,10 @@ sub DrawPatronCardText {
     my $vPos   = ( $y_pos + ( $label_height - $top_text_margin ) );
     my $font = prFont($fontname);
 
-    my $hPos;
+    my $hPos = 0;
 
     foreach my $line (keys %$text) {
-        warn "Current text is \"$line\" and font size for \"$line\" is $text->{$line} points";
+        $debug and warn "Current text is \"$line\" and font size for \"$line\" is $text->{$line} points";
         # some code to try and center each line on the label based on font size and string point width...
         my $stringwidth = prStrWidth($line, $fontname, $text->{$line});
         my $whitespace = ( $label_width - ( $stringwidth + (2 * $left_text_margin) ) );
@@ -1004,10 +1184,10 @@ sub DrawBarcode {
     my ( $x_pos, $y_pos, $height, $width, $barcode, $barcodetype ) = @_;
     my $num_of_bars = length($barcode);
     my $bar_width   = $width * .8;        # %80 of length of label width
-    my $tot_bar_length;
-    my $bar_length;
+    my $tot_bar_length = 0;
+    my $bar_length = 0;
     my $guard_length = 10;
-    my $xsize_ratio;
+    my $xsize_ratio = 0;
 
     if ( $barcodetype eq 'CODE39' ) {
         $bar_length = '17.5';
@@ -1022,6 +1202,7 @@ sub DrawBarcode {
                 ySize         => ( .02 * $height ),
                 xSize         => $xsize_ratio,
                 hide_asterisk => 1,
+                mode          => 'graphic',  # the only other option here is Type3...
             );
         };
         if ($@) {
@@ -1047,6 +1228,7 @@ sub DrawBarcode {
                 ySize         => ( .02 * $height ),
                 xSize         => $xsize_ratio,
                 hide_asterisk => 1,
+                mode          => 'graphic',  # the only other option here is Type3...
             );
         };
 
@@ -1072,7 +1254,8 @@ sub DrawBarcode {
                 ySize         => ( .02 * $height ),
                 xSize         => $xsize_ratio,
                 hide_asterisk => 1,
-                               text         => 0, 
+               text          => 0, 
+                mode          => 'graphic',  # the only other option here is Type3...
             );
         };
 
@@ -1122,16 +1305,16 @@ sub DrawBarcode {
 
     my $moo2 = $tot_bar_length * $xsize_ratio;
 
-    warn "$x_pos, $y_pos, $barcode, $barcodetype" if $DEBUG;
-    warn "BAR_WDTH = $bar_width, TOT.BAR.LGHT=$tot_bar_length  R*TOT.BAR =$moo2" if $DEBUG;
+    warn "$x_pos, $y_pos, $barcode, $barcodetype" if $debug;
+    warn "BAR_WDTH = $bar_width, TOT.BAR.LGHT=$tot_bar_length  R*TOT.BAR =$moo2" if $debug;
 }
 
-=item build_circ_barcode;
+=head2 build_circ_barcode;
 
   build_circ_barcode( $x_pos, $y_pos, $barcode,
                $barcodetype, \$item);
 
-$item is the result of a previous call to get_label_items();
+$item is the result of a previous call to GetLabelItems();
 
 =cut
 
@@ -1190,6 +1373,7 @@ sub build_circ_barcode {
                 xSize => .85,
 
                 ySize => 1.3,
+                mode            => 'graphic',  # the only other option here is Type3...
             );
         };
         if ($@) {
@@ -1422,7 +1606,7 @@ sub build_circ_barcode {
 
 }
 
-=item draw_boundaries
+=head2 draw_boundaries
 
  sub draw_boundaries ($x_pos_spine, $x_pos_circ1, $x_pos_circ2,
                 $y_pos, $spine_width, $label_height, $circ_width)  
@@ -1456,7 +1640,7 @@ sub draw_boundaries {
     }
 }
 
-=item drawbox
+=head2 drawbox
 
        sub drawbox {   $lower_left_x, $lower_left_y, 
                        $upper_right_x, $upper_right_y )
@@ -1494,8 +1678,6 @@ END { }    # module clean-up code here (global destructor)
 1;
 __END__
 
-=back
-
 =head1 AUTHOR
 
 Mason James <mason@katipo.co.nz>