partial 1481: adding to templates, fixing non-display of data
[koha.git] / C4 / Biblio.pm
index 00688fd..8ebb148 100644 (file)
@@ -68,6 +68,7 @@ push @EXPORT, qw(
   &GetUsedMarcStructure
 
   &GetItemsInfo
+  &GetItemsByBiblioitemnumber
   &GetItemnumberFromBarcode
   &get_itemnumbers_of
   &GetXmlBiblio
@@ -393,10 +394,10 @@ sub ModBiblio {
     # adding biblionumber
     my ($tag_biblionumber, $subfield_biblionumber) = GetMarcFromKohaField('biblio.biblionumber',$frameworkcode);
     $record->append_fields(
-       MARC::Field->new(
-               $tag_biblionumber,'','',$subfield_biblionumber => $biblionumber
-       )
-    );
+           MARC::Field->new(
+                   $tag_biblionumber,'','',$subfield_biblionumber => $biblionumber
+           )
+    ) unless ($record->subfield($tag_biblionumber,$subfield_biblionumber));
     
     # update the MARC record (that now contains biblio and items) with the new record data
     &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
@@ -783,11 +784,11 @@ sub GetItemsInfo {
     my ( $biblionumber, $type ) = @_;
     my $dbh   = C4::Context->dbh;
     my $query = "SELECT *,items.notforloan as itemnotforloan
-                 FROM items, biblio, biblioitems
+                 FROM items 
+                 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
+                 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
                  LEFT JOIN itemtypes on biblioitems.itemtype = itemtypes.itemtype
                 WHERE items.biblionumber = ?
-                    AND biblioitems.biblioitemnumber = items.biblioitemnumber
-                    AND biblio.biblionumber = items.biblionumber
                 ORDER BY items.dateaccessioned desc
                  ";
     my $sth = $dbh->prepare($query);
@@ -800,10 +801,9 @@ sub GetItemsInfo {
         my $datedue = '';
         my $isth    = $dbh->prepare(
             "SELECT issues.*,borrowers.cardnumber,borrowers.surname,borrowers.firstname
-            FROM   issues, borrowers
+            FROM   issues LEFT JOIN borrowers ON issues.borrowernumber=borrowers.borrowernumber
             WHERE  itemnumber = ?
-                AND returndate IS NULL
-                AND issues.borrowernumber=borrowers.borrowernumber"
+                AND returndate IS NULL"
         );
         $isth->execute( $data->{'itemnumber'} );
         if ( my $idata = $isth->fetchrow_hashref ) {
@@ -1177,18 +1177,18 @@ that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
 
 #'
 sub GetBiblioItemData {
-    my ($bibitem) = @_;
+    my ($biblioitemnumber) = @_;
     my $dbh       = C4::Context->dbh;
     my $sth       =
       $dbh->prepare(
-"Select *,biblioitems.notes as bnotes from biblioitems, biblio,itemtypes where biblio.biblionumber = biblioitems.biblionumber and biblioitemnumber = ? and biblioitems.itemtype = itemtypes.itemtype"
+       "SELECT *,biblioitems.notes AS bnotes
+               FROM biblioitems,biblio,itemtypes 
+       WHERE biblio.biblionumber = biblioitems.biblionumber 
+               AND biblioitemnumber = ? "
       );
     my $data;
-
-    $sth->execute($bibitem);
-
+    $sth->execute($biblioitemnumber);
     $data = $sth->fetchrow_hashref;
-
     $sth->finish;
     return ($data);
 }    # sub &GetBiblioItemData
@@ -1262,10 +1262,10 @@ sub GetBiblioFromItemNumber {
     my ( $itemnumber ) = @_;
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare(
-        "SELECT * FROM biblio,items,biblioitems
-         WHERE items.itemnumber = ?
-           AND biblio.biblionumber = items.biblionumber
-           AND biblioitems.biblioitemnumber = items.biblioitemnumber"
+        "SELECT * FROM items 
+        LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
+        LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
+         WHERE items.itemnumber = ?"
     );
 
     $sth->execute($itemnumber);
@@ -1392,6 +1392,68 @@ sub GetItemInfosOf {
     return get_infos_of( $query, 'itemnumber' );
 }
 
+=head2 GetItemsByBiblioitemnumber
+
+=over 4
+
+GetItemsByBiblioitemnumber($biblioitemnumber);
+
+Returns an arrayref of hashrefs suitable for use in a TMPL_LOOP
+Called by moredetail.pl
+
+=back
+
+=cut
+
+sub GetItemsByBiblioitemnumber {
+       my ( $bibitem ) = @_;
+       my $dbh = C4::Context->dbh;
+       my $sth = $dbh->prepare("SELECT * FROM items WHERE items.biblioitemnumber = ?") || die $dbh->errstr;
+       # Get all items attached to a biblioitem
+    my $i = 0;
+    my @results; 
+    $sth->execute($bibitem) || die $sth->errstr;
+    while ( my $data = $sth->fetchrow_hashref ) {  
+               # Foreach item, get circulation information
+               my $sth2 = $dbh->prepare( "SELECT * FROM issues,borrowers
+                                   WHERE itemnumber = ?
+                                   AND returndate is NULL
+                                   AND issues.borrowernumber = borrowers.borrowernumber"
+        );
+        $sth2->execute( $data->{'itemnumber'} );
+        if ( my $data2 = $sth2->fetchrow_hashref ) {
+                       # if item is out, set the due date and who it is out too
+                       $data->{'date_due'}   = $data2->{'date_due'};
+                       $data->{'cardnumber'} = $data2->{'cardnumber'};
+                       $data->{'borrowernumber'}   = $data2->{'borrowernumber'};
+               }
+        else {
+                       # set date_due to blank, so in the template we check itemlost, and wthdrawn 
+                       $data->{'date_due'} = '';                                                                                                         
+               }    # else         
+        $sth2->finish;
+        # Find the last 3 people who borrowed this item.                  
+        my $query2 = "SELECT * FROM issues, borrowers WHERE itemnumber = ?
+                      AND issues.borrowernumber = borrowers.borrowernumber
+                      AND returndate is not NULL
+                      ORDER BY returndate desc,timestamp desc LIMIT 3";
+        $sth2 = $dbh->prepare($query2) || die $dbh->errstr;
+        $sth2->execute( $data->{'itemnumber'} ) || die $sth2->errstr;
+        my $i2 = 0;
+        while ( my $data2 = $sth2->fetchrow_hashref ) {
+                       $data->{"timestamp$i2"} = $data2->{'timestamp'};
+                       $data->{"card$i2"}      = $data2->{'cardnumber'};
+                       $data->{"borrower$i2"}  = $data2->{'borrowernumber'};
+                       $i2++;
+               }
+        $sth2->finish;
+        push(@results,$data);
+    } 
+    $sth->finish;
+    return (\@results); 
+}
+
+
 =head2 GetBiblioItemInfosOf
 
 =over 4
@@ -1589,10 +1651,12 @@ sub GetMarcBiblio {
      $marcxml =~ s/\x1f//g;
      $marcxml =~ s/\x1d//g;
      $marcxml =~ s/\x0f//g;
-     $marcxml =~ s/\x0c//g;
+     $marcxml =~ s/\x0c//g;  
 #   warn $marcxml;
     my $record = MARC::Record->new();
-     $record = MARC::Record::new_from_xml( $marcxml, "utf8",C4::Context->preference('marcflavour')) if $marcxml;
+     
+      $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8",C4::Context->preference('marcflavour'))} if ($marcxml);
+     if ($@) {warn $@;}
 #      $record = MARC::Record::new_from_usmarc( $marc) if $marc;
     return $record;
 }
@@ -1820,6 +1884,8 @@ The authors are stored in differents places depending on MARC flavour
 sub GetMarcAuthors {
     my ( $record, $marcflavour ) = @_;
     my ( $mintag, $maxtag );
+    # tagslib useful for UNIMARC author reponsabilities
+    my $tagslib = &GetMarcStructure( 1, '' ); # FIXME : we don't have the framework available, we take the default framework. May be bugguy on some setups, will be usually correct.
     if ( $marcflavour eq "MARC21" ) {
         $mintag = "100";
         $maxtag = "111"; 
@@ -1842,7 +1908,6 @@ sub GetMarcAuthors {
                 $marcflavour ne 'MARC21'
                 and (
                     ($authors_subfield->[0] eq '3') or
-                    ($authors_subfield->[0] eq '4') or
                     ($authors_subfield->[0] eq '5')
                 )
             )
@@ -1854,7 +1919,17 @@ sub GetMarcAuthors {
             }
             $count_auth++;
             my $subfieldcode = $authors_subfield->[0];
-            my $value        = $authors_subfield->[1];
+            my $value;
+            # deal with UNIMARC author responsibility
+            if (
+                $marcflavour ne 'MARC21'
+                and ($authors_subfield->[0] eq '4')
+            )
+            {
+                $value = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
+            } else {
+                $value        = $authors_subfield->[1];
+            }
             $hash{tag}       = $field->tag;
             $hash{value}    .= $value . " " if ($subfieldcode != 9) ;
             $hash{link}     .= $value if ($subfieldcode eq 9);
@@ -2214,7 +2289,6 @@ sub TransformHtmlToMarc {
     my $record  = MARC::Record->new();
     my $i=0;
     my @fields;
-    
     while ($params->[$i]){ # browse all CGI params
         my $param = $params->[$i];
         my $newfield=0;
@@ -2242,7 +2316,7 @@ sub TransformHtmlToMarc {
             
             my $ind1 = substr($cgi->param($param),0,1);
             my $ind2 = substr($cgi->param($param),1,1);
-            
+            $newfield=0;
             my $j=$i+1;
             
             if($tag < 10){ # no code for theses fields
@@ -3079,25 +3153,25 @@ sub _AddBiblioNoZebra {
                         next unless $_; # skip  empty values (multiple spaces)
                         # if the entry is already here, improve weight
 #                         warn "managing $_";
-                        if ($result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) {
+                        if ($result{$key}->{"$_"} =~ /$biblionumber,$title\-(\d);/) {
                             my $weight=$1+1;
-                            $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
-                            $result{$key}->{$_} .= "$biblionumber,$title-$weight;";
+                            $result{$key}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
+                            $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
                         } else {
                             # get the value if it exist in the nozebra table, otherwise, create it
                             $sth2->execute($server,$key,$_);
                             my $existing_biblionumbers = $sth2->fetchrow;
                             # it exists
                             if ($existing_biblionumbers) {
-                                $result{$key}->{$_} =$existing_biblionumbers;
+                                $result{$key}->{"$_"} =$existing_biblionumbers;
                                 my $weight=$1+1;
-                                $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
-                                $result{$key}->{$_} .= "$biblionumber,$title-$weight;";
+                                $result{$key}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
+                                $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
                             # create a new ligne for this entry
                             } else {
 #                             warn "INSERT : $server / $key / $_";
                                 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
-                                $result{$key}->{$_}.="$biblionumber,$title-1;";
+                                $result{$key}->{"$_"}.="$biblionumber,$title-1;";
                             }
                         }
                     }
@@ -3111,24 +3185,24 @@ sub _AddBiblioNoZebra {
                 foreach (split / /,$line) {
                     next unless $_; # skip  empty values (multiple spaces)
                     # if the entry is already here, improve weight
-                    if ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
+                    if ($result{'__RAW__'}->{"$_"} =~ /$biblionumber,$title\-(\d);/) {
                         my $weight=$1+1;
-                        $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
-                        $result{'__RAW__'}->{$_} .= "$biblionumber,$title-$weight;";
+                        $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
+                        $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
                     } else {
                         # get the value if it exist in the nozebra table, otherwise, create it
                         $sth2->execute($server,'__RAW__',$_);
                         my $existing_biblionumbers = $sth2->fetchrow;
                         # it exists
                         if ($existing_biblionumbers) {
-                            $result{'__RAW__'}->{$_} =$existing_biblionumbers;
+                            $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
                             my $weight=$1+1;
-                            $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
-                            $result{'__RAW__'}->{$_} .= "$biblionumber,$title-$weight;";
+                            $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
+                            $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
                         # create a new ligne for this entry
                         } else {
                             $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).',  indexname="__RAW__",value='.$dbh->quote($_));
-                            $result{'__RAW__'}->{$_}.="$biblionumber,$title-1;";
+                            $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
                         }
                     }
                 }
@@ -3332,11 +3406,16 @@ sub _koha_modify_biblioitem {
     $biblioitem->{'bnotes'}       = $dbh->quote( $biblioitem->{'bnotes'} );
     $biblioitem->{'size'}         = $dbh->quote( $biblioitem->{'size'} );
     $biblioitem->{'place'}        = $dbh->quote( $biblioitem->{'place'} );
+    $biblioitem->{'collectiontitle'}        = $dbh->quote( $biblioitem->{'collectiontitle'} );
+    $biblioitem->{'collectionissn'}         = $dbh->quote( $biblioitem->{'collectionissn'} );
+    $biblioitem->{'collectionvolume'}       = $dbh->quote( $biblioitem->{'collectionvolume'} );
+    $biblioitem->{'editionstatement'}       = $dbh->quote( $biblioitem->{'editionstatement'} );
+    $biblioitem->{'editionresponsibility'}  = $dbh->quote( $biblioitem->{'editionresponsibility'} );
     $biblioitem->{'ccode'}        = $dbh->quote( $biblioitem->{'ccode'} );
     $biblioitem->{'biblionumber'} =
       $dbh->quote( $biblioitem->{'biblionumber'} );
 
-    $query = "Update biblioitems set
+    $query = "UPDATE biblioitems SET
         itemtype        = $biblioitem->{'itemtype'},
         url             = $biblioitem->{'url'},
         isbn            = $biblioitem->{'isbn'},
@@ -3352,6 +3431,11 @@ sub _koha_modify_biblioitem {
         notes           = $biblioitem->{'bnotes'},
         size            = $biblioitem->{'size'},
         place           = $biblioitem->{'place'},
+        collectiontitle = $biblioitem->{'collectiontitle'},
+        collectionissn  = $biblioitem->{'collectionissn'},
+        collectionvolume= $biblioitem->{'collectionvolume'},
+        editionstatement= $biblioitem->{'editionstatement'},
+        editionresponsibility= $biblioitem->{'editionresponsibility'},
         ccode           = $biblioitem->{'ccode'}
         where biblionumber = $biblioitem->{'biblionumber'}";
 
@@ -3399,8 +3483,11 @@ sub _koha_add_biblioitem {
             volumeddesc      = ?, illus           = ?,
             pages            = ?, notes           = ?,
             size             = ?, lccn            = ?,
-            marc             = ?, lcsort          =?,
-            place            = ?, ccode           = ?
+            marc             = ?, lcsort          = ?,
+            place            = ?, ccode           = ?,
+            collectiontitle  = ?, collectionissn  = ?,
+            collectionvolume = ?, editionstatement= ?,
+            editionresponsibility= ?
           "
     );
     my ($lcsort) =
@@ -3418,7 +3505,10 @@ sub _koha_add_biblioitem {
         $biblioitem->{'pages'},          $biblioitem->{'bnotes'},
         $biblioitem->{'size'},           $biblioitem->{'lccn'},
         $biblioitem->{'marc'},           $biblioitem->{'place'},
-        $lcsort,                         $biblioitem->{'ccode'}
+        $lcsort,                         $biblioitem->{'ccode'},
+        $biblioitem->{'collectiontitle'},$biblioitem->{'collectionissn'},
+        $biblioitem->{'collectionvolume'},$biblioitem->{'editionstatement'},
+        $biblioitem->{'editionresponsibility'}
     );
     $sth->finish;
     return ($bibitemnum);
@@ -3465,13 +3555,12 @@ sub _koha_new_items {
         $sth = $dbh->prepare(
             "Insert into items set
             itemnumber           = ?,     biblionumber     = ?,
-            multivolumepart      = ?,
             biblioitemnumber     = ?,     barcode          = ?,
             booksellerid         = ?,     dateaccessioned  = NOW(),
             homebranch           = ?,     holdingbranch    = ?,
             price                = ?,     replacementprice = ?,
             replacementpricedate = NOW(), datelastseen     = NOW(),
-            multivolume          = ?,     stack            = ?,
+                       stack            = ?,
             itemlost             = ?,     wthdrawn         = ?,
             paidfor              = ?,     itemnotes        = ?,
             itemcallnumber       =?,      notforloan       = ?,
@@ -3480,11 +3569,11 @@ sub _koha_new_items {
         );
         $sth->execute(
             $itemnumber,                $item->{'biblionumber'},
-            $item->{'multivolumepart'}, $item->{'biblioitemnumber'},
+                       $item->{'biblioitemnumber'},
             $barcode,                   $item->{'booksellerid'},
             $item->{'homebranch'},      $item->{'holdingbranch'},
             $item->{'price'},           $item->{'replacementprice'},
-            $item->{multivolume},       $item->{stack},
+                       $item->{stack},
             $item->{itemlost},          $item->{wthdrawn},
             $item->{paidfor},           $item->{'itemnotes'},
             $item->{'itemcallnumber'},  $item->{'notforloan'},
@@ -3495,13 +3584,12 @@ sub _koha_new_items {
         $sth = $dbh->prepare(
             "INSERT INTO items SET
             itemnumber           = ?,     biblionumber     = ?,
-            multivolumepart      = ?,
             biblioitemnumber     = ?,     barcode          = ?,
             booksellerid         = ?,     dateaccessioned  = ?,
             homebranch           = ?,     holdingbranch    = ?,
             price                = ?,     replacementprice = ?,
             replacementpricedate = NOW(), datelastseen     = NOW(),
-            multivolume          = ?,     stack            = ?,
+                       stack            = ?,
             itemlost             = ?,     wthdrawn         = ?,
             paidfor              = ?,     itemnotes        = ?,
             itemcallnumber       = ?,     notforloan       = ?,
@@ -3511,11 +3599,11 @@ sub _koha_new_items {
         );
         $sth->execute(
             $itemnumber,                 $item->{'biblionumber'},
-            $item->{'multivolumepart'},  $item->{'biblioitemnumber'},
+                       $item->{'biblioitemnumber'},
             $barcode,                    $item->{'booksellerid'},
             $item->{'dateaccessioned'},  $item->{'homebranch'},
             $item->{'holdingbranch'},    $item->{'price'},
-            $item->{'replacementprice'}, $item->{multivolume},
+            $item->{'replacementprice'},
             $item->{stack},              $item->{itemlost},
             $item->{wthdrawn},           $item->{paidfor},
             $item->{'itemnotes'},        $item->{'itemcallnumber'},
@@ -3546,10 +3634,10 @@ sub _koha_modify_item {
     # if all we're doing is setting statuses, just update those and get out
     if ( $op eq "setstatus" ) {
         my $query =
-          "UPDATE items SET itemlost=?,wthdrawn=?,binding=? WHERE itemnumber=?";
+          "UPDATE items SET itemlost=?,wthdrawn=? WHERE itemnumber=?";
         my @bind = (
             $item->{'itemlost'}, $item->{'wthdrawn'},
-            $item->{'binding'},  $item->{'itemnumber'}
+                       $item->{'itemnumber'}
         );
         my $sth = $dbh->prepare($query);
         $sth->execute(@bind);
@@ -3561,32 +3649,29 @@ sub _koha_modify_item {
       itemcalculator( $dbh, $item->{'bibitemnum'}, $item->{'itemcallnumber'} );
 
     my $query = "UPDATE items SET
-barcode=?,itemnotes=?,itemcallnumber=?,notforloan=?,location=?,multivolumepart=?,multivolume=?,stack=?,wthdrawn=?,holdingbranch=?,homebranch=?,cutterextra=?, onloan=?, binding=?";
+barcode=?,itemnotes=?,itemcallnumber=?,notforloan=?,location=?,stack=?,wthdrawn=?,holdingbranch=?,homebranch=?,cutterextra=?, onloan=?";
 
     my @bind = (
         $item->{'barcode'},        $item->{'notes'},
         $item->{'itemcallnumber'}, $item->{'notforloan'},
-        $item->{'location'},       $item->{multivolumepart},
-        $item->{multivolume},      $item->{stack},
+        $item->{'location'},       $item->{stack},
         $item->{wthdrawn},         $item->{holdingbranch},
         $item->{homebranch},       $cutterextra,
-        $item->{onloan},           $item->{binding}
+        $item->{onloan},           
     );
     if ( $item->{'lost'} ne '' ) {
         $query =
 "update items set biblioitemnumber=?,barcode=?,itemnotes=?,homebranch=?,
                             itemlost=?,wthdrawn=?,itemcallnumber=?,notforloan=?,
-                             location=?,multivolumepart=?,multivolume=?,stack=?,wthdrawn=?,holdingbranch=?,cutterextra=?,onloan=?, binding=?";
+                             location=?,stack=?,wthdrawn=?,holdingbranch=?,cutterextra=?,onloan=?";
         @bind = (
             $item->{'bibitemnum'},     $item->{'barcode'},
             $item->{'notes'},          $item->{'homebranch'},
             $item->{'lost'},           $item->{'wthdrawn'},
             $item->{'itemcallnumber'}, $item->{'notforloan'},
-            $item->{'location'},       $item->{multivolumepart},
-            $item->{multivolume},      $item->{stack},
+            $item->{'location'},       $item->{stack},
             $item->{wthdrawn},         $item->{holdingbranch},
-            $cutterextra,              $item->{onloan},
-            $item->{binding}
+            $cutterextra,              $item->{onloan}
         );
         if ( $item->{homebranch} ) {
             $query .= ",homebranch=?";