[Koha-cvs] koha/C4 Biblio.pm [rel_3_0]

Antoine Farnault antoine at koha-fr.org
Thu Nov 30 18:17:01 CET 2006


CVSROOT:	/sources/koha
Module name:	koha
Branch:		rel_3_0
Changes by:	Antoine Farnault <toins>	06/11/30 17:17:01

Modified files:
	C4             : Biblio.pm 

Log message:
	following functions moved from Search.p to Biblio.pm :
	- bibdata
	- itemsissues
	- addauthor
	- getMARCNotes
	- getMARCsubjects

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/koha/C4/Biblio.pm?cvsroot=koha&only_with_tag=rel_3_0&r1=1.178.2.28&r2=1.178.2.29

Patches:
Index: Biblio.pm
===================================================================
RCS file: /sources/koha/koha/C4/Biblio.pm,v
retrieving revision 1.178.2.28
retrieving revision 1.178.2.29
diff -u -b -r1.178.2.28 -r1.178.2.29
--- Biblio.pm	28 Nov 2006 15:15:03 -0000	1.178.2.28
+++ Biblio.pm	30 Nov 2006 17:17:01 -0000	1.178.2.29
@@ -39,13 +39,12 @@
 @ISA = qw(Exporter);
 
 @EXPORT = qw(
-
   &AddBiblio &AddItem
   &ModBiblio &ModItem
   &DelBiblio &DelItem
   &ModBiblioframework
+  &bibdata
   &PrepareItemrecordDisplay
-
   &updateBiblio &updateBiblioItem &updateItem
   &itemcount &newbiblio &newbiblioitem
   &modnote &newsubject &newsubtitle
@@ -62,14 +61,12 @@
   &getbiblioitem &getitemsbybiblioitem
   &skip &getitemtypes
   &get_itemnumbers_of
-
   &MARCfind_marc_from_kohafield
   &MARCfindsubfield
   &MARCfind_frameworkcode
   &MARCgettagslib
   &MARCmoditemonefield
   &zebraop
-
   &MARCaddbiblio &MARCadditem &MARCmodLCindex
   &MARCmodsubfield
   &MARCmodbiblio &MARCmoditem
@@ -80,10 +77,14 @@
   &MARCdelsubfield
   &char_decode
   &DisplayISBN
-&itemcalculator &calculatelc
-&GetItemInfosOf &GetItemStatus &GetItemLocation
-&GetBiblioItemInfosOf
+  &itemcalculator &calculatelc
+  &GetItemInfosOf &GetItemStatus &GetItemLocation
+  &GetBiblioItemInfosOf
   &bibitemdata
+  &itemissues
+  &addauthor
+  &getMARCnotes
+  &getMARCsubjects
 );
 
 =head1 NAME
@@ -448,7 +449,7 @@
     my $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
     $sth->execute($biblionumber);
     while ( my $biblioitemnumber = $sth->fetchrow ) {
-        my @issues = C4::Search::itemissues($biblioitemnumber);
+        my @issues = itemissues($biblioitemnumber);
         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
@@ -524,6 +525,82 @@
     &MARCaddbiblio($dbh,$newrec,$biblionumber,);
 }
 
+
+=head2 bibdata
+
+  $data = &bibdata($biblionumber, $type);
+
+Returns information about the book with the given biblionumber.
+
+C<$type> is ignored.
+
+C<&bibdata> returns a reference-to-hash. The keys are the fields in
+the C<biblio>, C<biblioitems>, and C<bibliosubtitle> tables in the
+Koha database.
+
+In addition, C<$data-E<gt>{subject}> is the list of the book's
+subjects, separated by C<" , "> (space, comma, space).
+
+If there are multiple biblioitems with the given biblionumber, only
+the first one is considered.
+
+=cut
+
+#'
+sub bibdata {
+    my ( $bibnum, $type ) = @_;
+    my $dbh = C4::Context->dbh;
+
+    my $query = "
+        SELECT * , biblioitems.notes AS bnotes, biblio.notes
+        FROM biblio
+            LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
+            LEFT JOIN bibliosubtitle ON biblio.biblionumber = bibliosubtitle.biblionumber
+            LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
+        WHERE biblio.biblionumber = ?
+            AND biblioitems.biblionumber = biblio.biblionumber
+    ";
+    my $sth = $dbh->prepare($query);
+    $sth->execute($bibnum);
+    my $data;
+    $data = $sth->fetchrow_hashref;
+    $sth->finish;
+
+    # handle management of repeated subtitle
+    $sth = $dbh->prepare("Select * from bibliosubtitle where biblionumber = ?");
+    $sth->execute($bibnum);
+    my @subtitles;
+    while ( my $dat = $sth->fetchrow_hashref ) {
+        my %line;
+        $line{subtitle} = $dat->{subtitle};
+        push @subtitles, \%line;
+    }    # while
+    $data->{subtitles} = \@subtitles;
+    $sth->finish;
+    $sth = $dbh->prepare("Select * from bibliosubject where biblionumber = ?");
+    $sth->execute($bibnum);
+    my @subjects;
+    while ( my $dat = $sth->fetchrow_hashref ) {
+        my %line;
+        $line{subject} = $dat->{'subject'};
+        push @subjects, \%line;
+    }    # while
+    $data->{subjects} = \@subjects;
+    $sth->finish;
+    $sth =
+      $dbh->prepare("Select * from additionalauthors where biblionumber = ?");
+    $sth->execute($bibnum);
+    while ( my $dat = $sth->fetchrow_hashref ) {
+        $data->{'additionalauthors'} .= "$dat->{'author'} - ";
+    }    # while
+    chop $data->{'additionalauthors'};
+    chop $data->{'additionalauthors'};
+    chop $data->{'additionalauthors'};
+    $sth->finish;
+    return ($data);
+}    # sub bibdata
+
+
 =head2 z3950_extended_services
 
 =over 4
@@ -3601,6 +3678,100 @@
     }
 }
 
+sub getMARCnotes {
+    my ( $dbh, $biblionumber, $marcflavour ) = @_;
+    my $scope;
+    if ( $marcflavour eq "MARC21" ) {
+        $scope = '5..';
+    }
+    else {    # assume unimarc if not marc21
+        $scope = '3..';
+    }
+
+    my $record = MARCgetbiblio( $dbh, $biblionumber );
+    my @marcnotes;
+    my $note = "";
+    my $tag  = "";
+    my $marcnote;
+
+    foreach my $field ( $record->field($scope) ) {
+        my $value = $field->as_string();
+        if ( $note ne "" ) {
+            $marcnote = { marcnote => $note, };
+            push @marcnotes, $marcnote;
+            $note = $value;
+        }
+        if ( $note ne $value ) {
+            $note = $note . " " . $value;
+        }
+    }
+
+    if ($note) {
+        $marcnote = { marcnote => $note };
+        push @marcnotes, $marcnote;    #load last tag into array
+    }
+
+    my $marcnotesarray = \@marcnotes;
+    return $marcnotesarray;
+}    # end getMARCnotes
+
+sub getMARCsubjects {
+    my ( $dbh, $biblionumber, $marcflavour ) = @_;
+    my ( $mintag, $maxtag );
+    if ( $marcflavour eq "MARC21" ) {
+        $mintag = "600";
+        $maxtag = "699";
+    }
+    else {    # assume unimarc if not marc21
+        $mintag = "600";
+        $maxtag = "619";
+    }
+    my $record = MARCgetbiblio( $dbh, $biblionumber );
+    my @marcsubjcts;
+    my $subjct   = "";
+    my $subfield = "";
+    my $marcsubjct;
+
+    foreach my $field ( $record->fields ) {
+        next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
+        my @subfields_loop;
+
+        #my $value = $field->subfield('a');
+        #$marcsubjct = {MARCSUBJCT => $value,};
+        my @subfields = $field->subfields();
+
+        #warn "subfields:".join " ", @$subfields;
+        my $counter = 0;
+        my @link_loop;
+        for my $subject_subfield (@subfields) {
+            my $code      = $subject_subfield->[0];
+            my $value     = $subject_subfield->[1];
+            my $linkvalue = $value;
+            $linkvalue =~ s/(\(|\))//g;
+            my $operator = " and " unless $counter == 0;
+            push @link_loop, { link => $linkvalue, operator => $operator };
+            my $separator = C4::Context->preference("authoritysep")
+              unless $counter == 0;
+            push @subfields_loop,
+              {
+                code      => $code,
+                value     => $value,
+                link_loop => \@link_loop,
+                separator => $separator
+              };
+            $counter++;
+        }
+        push @marcsubjcts, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
+
+        #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
+        #push @marcsubjcts, $marcsubjct;
+        #$subjct = $value;
+
+    }
+    my $marcsubjctsarray = \@marcsubjcts;
+    return $marcsubjctsarray;
+}    #end getMARCsubjects
+
 sub GetItemInfosOf {
     my @itemnumbers = @_;
 
@@ -3627,6 +3798,169 @@
 }
 
 
+=head2 itemissues
+
+  @issues = &itemissues($biblioitemnumber, $biblio);
+
+Looks up information about who has borrowed the bookZ<>(s) with the
+given biblioitemnumber.
+
+C<$biblio> is ignored.
+
+C<&itemissues> returns an array of references-to-hash. The keys
+include the fields from the C<items> table in the Koha database.
+Additional keys include:
+
+=over 4
+
+=item C<date_due>
+
+If the item is currently on loan, this gives the due date.
+
+If the item is not on loan, then this is either "Available" or
+"Cancelled", if the item has been withdrawn.
+
+=item C<card>
+
+If the item is currently on loan, this gives the card number of the
+patron who currently has the item.
+
+=item C<timestamp0>, C<timestamp1>, C<timestamp2>
+
+These give the timestamp for the last three times the item was
+borrowed.
+
+=item C<card0>, C<card1>, C<card2>
+
+The card number of the last three patrons who borrowed this item.
+
+=item C<borrower0>, C<borrower1>, C<borrower2>
+
+The borrower number of the last three patrons who borrowed this item.
+
+=back
+
+=cut
+
+#'
+sub itemissues {
+    my ( $bibitem, $biblio ) = @_;
+    my $dbh = C4::Context->dbh;
+
+    # FIXME - If this function die()s, the script will abort, and the
+    # user won't get anything; depending on how far the script has
+    # gotten, the user might get a blank page. It would be much better
+    # to at least print an error message. The easiest way to do this
+    # is to set $SIG{__DIE__}.
+    my $sth =
+      $dbh->prepare("Select * from items where items.biblioitemnumber = ?")
+      || die $dbh->errstr;
+    my $i = 0;
+    my @results;
+
+    $sth->execute($bibitem) || die $sth->errstr;
+
+    while ( my $data = $sth->fetchrow_hashref ) {
+
+        # Find out who currently has this item.
+        # FIXME - Wouldn't it be better to do this as a left join of
+        # some sort? Currently, this code assumes that if
+        # fetchrow_hashref() fails, then the book is on the shelf.
+        # fetchrow_hashref() can fail for any number of reasons (e.g.,
+        # database server crash), not just because no items match the
+        # search criteria.
+        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 ) {
+            $data->{'date_due'} = $data2->{'date_due'};
+            $data->{'card'}     = $data2->{'cardnumber'};
+            $data->{'borrower'} = $data2->{'borrowernumber'};
+        }
+        else {
+            if ( $data->{'wthdrawn'} eq '1' ) {
+                $data->{'date_due'} = 'Cancelled';
+            }
+            else {
+                $data->{'date_due'} = 'Available';
+            }    # else
+        }    # else
+
+        $sth2->finish;
+
+        # Find the last 3 people who borrowed this item.
+        $sth2 = $dbh->prepare(
+            "select * from issues, borrowers
+                        where itemnumber = ?
+                                    and issues.borrowernumber = borrowers.borrowernumber
+                                    and returndate is not NULL
+                                    order by returndate desc,timestamp desc"
+        );
+
+#        $sth2 = $dbh->prepare("
+#            SELECT *
+#            FROM issues
+#                LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
+#            WHERE   itemnumber = ?
+#                AND returndate is not NULL
+#            ORDER BY returndate DESC,timestamp DESC
+#        ");
+
+        $sth2->execute( $data->{'itemnumber'} );
+        for ( my $i2 = 0 ; $i2 < 2 ; $i2++ )
+        {    # FIXME : error if there is less than 3 pple borrowing this item
+            if ( my $data2 = $sth2->fetchrow_hashref ) {
+                $data->{"timestamp$i2"} = $data2->{'timestamp'};
+                $data->{"card$i2"}      = $data2->{'cardnumber'};
+                $data->{"borrower$i2"}  = $data2->{'borrowernumber'};
+            }    # if
+        }    # for
+
+        $sth2->finish;
+        $results[$i] = $data;
+        $i++;
+    }
+
+    $sth->finish;
+    return (@results);
+}
+
+=head2 addauthor
+
+  ($count, $authors) = &addauthors($biblionumber);
+
+Looks up the additional authors for the book with the given
+biblionumber.
+
+Returns a two-element list. C<$authors> is a reference-to-array, where
+each element is an additional author, and C<$count> is the number of
+elements in C<$authors>.
+
+=cut
+
+#'
+sub addauthor {
+    my ($bibnum) = @_;
+    my $dbh      = C4::Context->dbh;
+    my $sth      =
+      $dbh->prepare("Select * from additionalauthors where biblionumber=?");
+    $sth->execute($bibnum);
+    my @results;
+    my $i = 0;
+    while ( my $data = $sth->fetchrow_hashref ) {
+        $results[$i] = $data;
+        $i++;
+    }
+    $sth->finish;
+    return ( $i, \@results );
+}
+
+
 END { }    # module clean-up code here (global destructor)
 
 1;
@@ -3645,8 +3979,16 @@
 
 =cut
 
-# $Id: Biblio.pm,v 1.178.2.28 2006/11/28 15:15:03 toins Exp $
+# $Id: Biblio.pm,v 1.178.2.29 2006/11/30 17:17:01 toins Exp $
 # $Log: Biblio.pm,v $
+# Revision 1.178.2.29  2006/11/30 17:17:01  toins
+# following functions moved from Search.p to Biblio.pm :
+# - bibdata
+# - itemsissues
+# - addauthor
+# - getMARCNotes
+# - getMARCsubjects
+#
 # Revision 1.178.2.28  2006/11/28 15:15:03  toins
 # sync with dev_week.
 # (deleteditems table wasn't getting populaated because the execute was commented out. This puts it back
@@ -3793,7 +4135,3 @@
 # Revision 1.115.2.51.2.11  2006/05/28 18:49:12  tgarip1957
 # This is an unusual commit. The main purpose is a working model of Zebra on a modified rel2_2.
 # Any questions regarding these commits should be asked to Joshua Ferraro unless you are Joshua whom I'll report to
-#
-# Revision 1.115.2.18  2005/08/02 07:45:44  tipaul
-# fix for bug http://bugs.koha.org/cgi-bin/bugzilla/show_bug.cgi?id=1009
-# (Not all items fields mapped to MARC)





More information about the Koha-cvs mailing list