BugFixing : viewlog.pl was broken (comma missing)
[koha_fer] / C4 / Biblio.pm
index 6f5e280..11df070 100644 (file)
@@ -65,6 +65,7 @@ push @EXPORT, qw(
   &GetMarcBiblio
   &GetMarcAuthors
   &GetMarcSeries
+  GetMarcUrls
   &GetUsedMarcStructure
 
   &GetItemsInfo
@@ -594,26 +595,15 @@ sub DelBiblio {
     my ( $biblionumber ) = @_;
     my $dbh = C4::Context->dbh;
     my $error;    # for error handling
+       
+       # First make sure this biblio has no items attached
+       my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
+       $sth->execute($biblionumber);
+       if (my $itemnumber = $sth->fetchrow){
+               # Fix this to use a status the template can understand
+               $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
+       }
 
-    # First make sure there are no items with issues are still attached
-    my $sth =
-      $dbh->prepare(
-        "SELECT itemnumber FROM items WHERE biblionumber=?");
-    $sth->execute($biblionumber);
-    while ( my $itemnumber = $sth->fetchrow ) {
-        my $issues = GetItemIssues($itemnumber);
-        foreach my $issue (@$issues) {
-            if (   ( $issue->{date_due} )
-                && ( $issue->{date_due} ne "Available" ) )
-            {
-
-#FIXME: we need a status system in Biblio like in Circ to return standard codes and messages
-# instead of hard-coded strings
-                $error .=
-"Item is checked out to a patron -- you must return it before deleting the Biblio";
-            }
-        }
-    }
     return $error if $error;
 
     # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
@@ -636,16 +626,6 @@ sub DelBiblio {
         # delete this biblioitem
         $error = &_koha_delete_biblioitems( $dbh, $biblioitemnumber );
         return $error if $error;
-
-        # delete items
-        my $items_sth =
-          $dbh->prepare(
-            "SELECT itemnumber FROM items WHERE biblioitemnumber=?");
-        $items_sth->execute($biblioitemnumber);
-        while ( my $itemnumber = $items_sth->fetchrow ) {
-            $error = &_koha_delete_item( $dbh, $itemnumber );
-            return $error if $error;
-        }
     }
     &logaction(C4::Context->userenv->{'number'},"CATALOGUING","DELETE",$biblionumber,"") 
         if C4::Context->preference("CataloguingLog");
@@ -666,6 +646,10 @@ Exported function (core API) for deleting an item record in Koha.
 sub DelItem {
     my ( $biblionumber, $itemnumber ) = @_;
     my $dbh = C4::Context->dbh;
+       
+       # check the item has no current issues
+       
+       
     &_koha_delete_item( $dbh, $itemnumber );
     # get the MARC record
     my $record = GetMarcBiblio($biblionumber);
@@ -713,7 +697,7 @@ sub GetBiblioData {
     my $dbh = C4::Context->dbh;
 
     my $query = "
-        SELECT * , biblioitems.notes AS bnotes, biblio.notes
+        SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
         FROM biblio
             LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
             LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
@@ -784,11 +768,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);
@@ -801,10 +785,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 ) {
@@ -1178,18 +1161,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
@@ -1263,10 +1246,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);
@@ -1652,10 +1635,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;
 }
@@ -1938,11 +1923,50 @@ sub GetMarcAuthors {
     return \@marcauthors;
 }
 
+=head2 GetMarcUrls
+
+=over 4
+
+$marcurls = GetMarcUrls($record,$marcflavour);
+Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
+Assumes web resources (not uncommon in MARC21 to omit resource type ind) 
+
+=back
+
+=cut
+
+sub GetMarcUrls {
+    my ($record, $marcflavour) = @_;
+    my @marcurls;
+    my $marcurl;
+    for my $field ($record->field('856')) {
+        my $url = $field->subfield('u');
+        my @notes;
+        for my $note ( $field->subfield('z')) {
+            push @notes , {note => $note};
+        }        
+        $marcurl = {  MARCURL => $url,
+                      notes => \@notes,
+                                       };
+               if($marcflavour eq 'MARC21') {
+               my $s3 = $field->subfield('3');
+                       my $link = $field->subfield('y');
+            $marcurl->{'linktext'} = $link || $s3 || $url ;;
+            $marcurl->{'part'} = $s3 if($link);
+            $marcurl->{'toc'} = 1 if($s3 =~ /^[Tt]able/) ;
+               } else {
+                       $marcurl->{'linktext'} = $url;
+               }
+        push @marcurls, $marcurl;    
+       }
+    return \@marcurls;
+}  #end GetMarcUrls
+
 =head2 GetMarcSeries
 
 =over 4
 
-$marcseriessarray = GetMarcSeries($record,$marcflavour);
+$marcseriesarray = GetMarcSeries($record,$marcflavour);
 Get all series from the MARC record and returns them in an array.
 The series are stored in differents places depending on MARC flavour
 
@@ -3267,7 +3291,7 @@ sub _koha_add_biblio {
     $sth->finish;
     $sth = $dbh->prepare(
         "INSERT INTO biblio
-    SET biblionumber  = ?, title = ?, author = ?, copyrightdate = ?, serial = ?, seriestitle = ?, notes = ?, abstract = ?, unititle = ?, frameworkcode = ? "
+    SET datecreated=NOW(), biblionumber  = ?, title = ?, author = ?, copyrightdate = ?, serial = ?, seriestitle = ?, notes = ?, abstract = ?, unititle = ?, frameworkcode = ? "
     );
     $sth->execute(
         $biblionumber,         $biblio->{'title'},
@@ -3554,13 +3578,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       = ?,
@@ -3569,11 +3592,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'},
@@ -3584,13 +3607,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       = ?,
@@ -3600,11 +3622,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'},
@@ -3635,10 +3657,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);
@@ -3650,32 +3672,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=?";