Adding trinaries to handle unpopulated itemcallnumber and/or barcode fields
[koha_fer] / labels / label-item-search.pl
index 1087d28..386c97d 100755 (executable)
@@ -32,9 +32,17 @@ use C4::Acquisition;
 use C4::Search;
 use C4::Dates qw( DHTMLcalendar );
 use C4::Koha;    # XXX subfield_is_koha_internal_p
-
+use C4::Debug;
+use List::Util qw( max min );
 #use Smart::Comments;
-use Data::Dumper;
+
+BEGIN {
+    $debug = $debug || $cgi_debug;
+    if ($debug) {
+        require Data::Dumper;
+        import Data::Dumper qw(Dumper);
+    }
+}
 
 # Creates a scrolling list with the associated default value.
 # Using more than one scrolling list in a CGI assigns the same default value to all the
@@ -50,7 +58,9 @@ my $dbh = C4::Context->dbh;
 
 my $startfrom = $query->param('startfrom') || 0;
 my ( $template, $loggedinuser, $cookie );
+my $total_hits;
 my (@marclist,@and_or,@excluding,@operator,@value,$orderby,@tags,$results,$total,$error,$marcresults);
+# XXX should this be maxItemsInSearchResults or numSearchResults preference instead of 19?
 my $resultsperpage = $query->param('resultsperpage') || 19;
 
 my $show_results = 0;
@@ -65,7 +75,7 @@ if ( $op eq "do_search" ) {
       #catalogsearch( $dbh, \@tags, \@and_or, \@excluding, \@operator, \@value,
       #  $startfrom * $resultsperpage,
       #  $resultsperpage, $orderby );
-               ($error, $marcresults) = SimpleSearch($marclist[0]);
+               ( $error, $marcresults, $total_hits ) = SimpleSearch( $marclist[0], $startfrom, $resultsperpage );
                if ($marcresults) {
                        $show_results = scalar @$marcresults;
                } else {
@@ -77,41 +87,46 @@ if ( $op eq "do_search" ) {
 
 if ( $show_results ) {
        my $hits = $show_results;
-       my (@results,@results2);
+        my (@results, @items);
         # This code needs to be refactored using these subs...
         #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
         #my $dat = &GetBiblioData( $biblio->{biblionumber} );
        for(my $i=0; $i<$hits; $i++) {
-            #DEBUG Notes: Decode the MARC record from each resulting MARC record...
-           my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
-            #DEBUG Notes: Transform it to Koha form...
-           my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
-           #build the hash for the template.
-           $biblio->{highlight}       = ($i % 2)?(1):(0);
-            #DEBUG Notes: Stuff it into @results... (used below to supply fields not existing in the item data)
-            push @results, $biblio;
-           my $biblionumber = $biblio->{'biblionumber'};
-            #DEBUG Notes: Grab the item numbers associated with this MARC record...
-            my $itemnums = get_itemnumbers_of($biblionumber);
-            #DEBUG Notes: Retrieve the item data for each number... 
-            my $iii = $itemnums->{$biblionumber};
+        #DEBUG Notes: Decode the MARC record from each resulting MARC record...
+       my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
+        #DEBUG Notes: Transform it to Koha form...
+       my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
+       # Begin building the hash for the template...
+        # I don't think we need this with the current template design, but I'm leaving it in place. -fbcit
+       #$biblio->{highlight}       = ($i % 2)?(1):(0);
+        #DEBUG Notes: Stuff the bib into @results...
+        push @results, $biblio;
+       my $biblionumber = $biblio->{'biblionumber'};
+        #DEBUG Notes: Grab the item numbers associated with this MARC record...
+        my $itemnums = get_itemnumbers_of($biblionumber);
+        #DEBUG Notes: Retrieve the item data for each number... 
+        my $iii = $itemnums->{$biblionumber};
            if ($iii) {
-                my @titem_results = GetItemsInfo( $itemnums->{$biblionumber}, 'intra' );
                my $item_results =  GetItemInfosOf( @$iii );
-               foreach my $item (keys %$item_results) {
-                   for my $bibdata (keys %{$results[$i]}) {
-                       if ( !$item_results->{$item}{$bibdata} ) {      #Only add the data from the bibliodata if the data does not already exit in itemdata.
-                                                                        #Otherwise we just build duplicate records rather than unique records per item.
-                            $item_results->{$item}{$bibdata} = $results[$i]->{$bibdata};
-                        }
+                foreach my $item (keys %$item_results) {
+                    #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
+                    if ($item_results->{$item}->{'biblionumber'} eq $results[$i]->{'biblionumber'}) {
+                        # NOTE: The order of the elements in this array must be preserved or the table dependent on it will be incorrectly rendered.
+                        # This is a real hack, but I can't think of a better way right now. -fbcit
+                        # It is conceivable that itemcallnumber and/or barcode fields might be empty so the trinaries cover this possibility.
+                        push @{$results[$i]->{'item'}}, { i_itemnumber1         => $item_results->{$item}->{'itemnumber'} };
+                        push @{$results[$i]->{'item'}}, { i_itemcallnumber      => ($item_results->{$item}->{'itemcallnumber'} ? $item_results->{$item}->{'itemcallnumber'} : 'NA') };
+                        push @{$results[$i]->{'item'}}, { i_dateaccessioned     => $item_results->{$item}->{'dateaccessioned'} };
+                        push @{$results[$i]->{'item'}}, { i_barcode             => ($item_results->{$item}->{'barcode'} ? $item_results->{$item}->{'barcode'} : 'NA')};
+                        push @{$results[$i]->{'item'}}, { i_itemnumber2         => $item_results->{$item}->{'itemnumber'} };
                     }
-                    #DEBUG Notes: After merging the bib and item data, stuff the results into $results2...
-                   push @results2, $item_results->{$item};
-               }
-                #warn Dumper(@results2);
+                }
            }
-    }
-    ( $template, $loggedinuser, $cookie ) = get_template_and_user(
+        }
+        $debug and warn "**********\@results**********\n";
+        $debug and warn Dumper(@results);
+  
+  ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         {
             template_name   => "labels/result.tmpl",
             query           => $query,
@@ -123,13 +138,6 @@ if ( $show_results ) {
         }
     );
 
-    # multi page display gestion
-    my $displaynext = 0;
-    my $displayprev = $startfrom;
-    if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
-        $displaynext = 1;
-    }
-
     my @field_data = ();
 
        # FIXME: this relies on symmetric order of CGI params that IS NOT GUARANTEED by spec.
@@ -143,6 +151,7 @@ if ( $show_results ) {
     }
 
     my @numbers = ();
+    $total = $total_hits;
     if ( $total > $resultsperpage ) {
         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
             if ( $i < 16 ) {
@@ -159,42 +168,26 @@ if ( $show_results ) {
         }
     }
 
-    my $from = $startfrom * $resultsperpage + 1;
-       my $temp = ( $startfrom + 1 ) * $resultsperpage;
-    my $to   = ($total < $temp) ? $total : $temp;
-
-    # this gets the results of the search (which are bibs)
-    # and then does a lookup on all items that exist for that bib
-    # then pushes the items onto a new array, as we really want the
-    # items attached to the bibs not thew bibs themselves
-
-   # my @results2;
-    # for (my $i = 0 ; $i < $total ; $i++ )
-    # {
-        #warn $i;
-        #warn Dumper $results->[$i]{'bibid'};
-    #     my $type = 'intra';
-    #     my @item_results = &ItemInfo( 0, $results->[$i]{'biblionumber'}, $type );
-                       # FIXME: ItemInfo doesn't exist !!
-#              push @results2,\@item_results;
-        # foreach my $item (@item_results) {
-        #      warn Dumper $item;
-        #      push @results2, $item;
-        # }
-#     }
-# 
+    # multi page display gestion
+    my $displaynext = 0;
+    my $displayprev = $startfrom;
+    # XXX Kludge. We show the "next" link if we retrieved the max number of results. There could be 0 more.
+    if ( scalar @results == $resultsperpage ) {
+        $displaynext = 1;
+    }
+
     $template->param(
-        result         => \@results2,
+        result         => \@results,
         startfrom      => $startfrom,
         displaynext    => $displaynext,
         displayprev    => $displayprev,
         resultsperpage => $resultsperpage,
-        startfromnext  => $startfrom + 1,
-        startfromprev  => $startfrom - 1,
+        startfromnext  => $startfrom + min( $resultsperpage, scalar @results ),
+        startfromprev  => max( $startfrom - $resultsperpage, 0 ),
         searchdata     => \@field_data,
-        total          => $total,
-        from           => $from,
-        to             => $to,
+        total          => $total_hits,
+        from           => $startfrom + 1,
+        to             => $startfrom + min( $resultsperpage, scalar @results ),
         numbers        => \@numbers,
         batch_id       => $batch_id,
         type           => $type,