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

Antoine Farnault antoine at koha-fr.org
Fri Oct 6 15:23:49 CEST 2006


CVSROOT:	/sources/koha
Module name:	koha
Branch:		rel_3_0
Changes by:	Antoine Farnault <toins>	06/10/06 13:23:49

Modified files:
	C4             : Biblio.pm Serials.pm 
	cataloguing    : addbiblio.pl additem.pl isbnsearch.pl 
	misc           : rebuildnonmarc.pl 

Log message:
	Synch with dev_week.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/koha/C4/Biblio.pm?cvsroot=koha&only_with_tag=rel_3_0&r1=1.178.2.10&r2=1.178.2.11
http://cvs.savannah.gnu.org/viewcvs/koha/C4/Serials.pm?cvsroot=koha&only_with_tag=rel_3_0&r1=1.5.2.2&r2=1.5.2.3
http://cvs.savannah.gnu.org/viewcvs/koha/cataloguing/addbiblio.pl?cvsroot=koha&only_with_tag=rel_3_0&r1=1.14.2.4&r2=1.14.2.5
http://cvs.savannah.gnu.org/viewcvs/koha/cataloguing/additem.pl?cvsroot=koha&only_with_tag=rel_3_0&r1=1.7.2.4&r2=1.7.2.5
http://cvs.savannah.gnu.org/viewcvs/koha/cataloguing/isbnsearch.pl?cvsroot=koha&only_with_tag=rel_3_0&r1=1.3&r2=1.3.2.1
http://cvs.savannah.gnu.org/viewcvs/koha/misc/rebuildnonmarc.pl?cvsroot=koha&only_with_tag=rel_3_0&r1=1.5&r2=1.5.2.1

Patches:
Index: C4/Biblio.pm
===================================================================
RCS file: /sources/koha/koha/C4/Biblio.pm,v
retrieving revision 1.178.2.10
retrieving revision 1.178.2.11
diff -u -b -r1.178.2.10 -r1.178.2.11
--- C4/Biblio.pm	2 Oct 2006 09:32:02 -0000	1.178.2.10
+++ C4/Biblio.pm	6 Oct 2006 13:23:49 -0000	1.178.2.11
@@ -18,12 +18,15 @@
 # Suite 330, Boston, MA  02111-1307 USA
 
 use strict;
+use warnings; no warnings 'uninitialized';
+
 require Exporter;
 use C4::Context;
 use C4::Database;
+
 use MARC::Record;
 use MARC::File::USMARC;
-use MARC::File::XML(BinaryEncoding=>"utf8");
+use MARC::File::XML;
 use ZOOM;
 use Data::Dumper;
 use C4::Koha;
@@ -36,11 +39,13 @@
 
 @ISA = qw(Exporter);
 
-#
-# don't forget MARCxxx subs are exported only for testing purposes. Should not be used
-# as the old-style API and the NEW one are the only public functions.
-#
 @EXPORT = qw(
+
+  &AddBiblio &AddItem
+  &ModBiblio &ModItem
+  &DelBiblio &DelItem
+  &ModBiblioframework
+
   &updateBiblio &updateBiblioItem &updateItem
   &itemcount &newbiblio &newbiblioitem
   &modnote &newsubject &newsubtitle
@@ -71,14 +76,13 @@
   &zebraop
 
   &MARCaddbiblio &MARCadditem &MARCmodLCindex
-  &MARCmodsubfield &MARCaddsubfield
+  &MARCmodsubfield
   &MARCmodbiblio &MARCmoditem
   &MARCkoha2marcBiblio &MARCmarc2koha
   &MARCkoha2marcItem &MARChtml2marc &MARChtml2xml
   &MARCgetbiblio &MARCgetitem &XMLgetbiblio
   &MARCaddword &MARCdelword 
   &MARCdelsubfield
- 
   &MARCgetbiblio2
   &char_decode
   &DisplayISBN
@@ -87,157 +91,548 @@
 &GetBiblioItemInfosOf
 );
 
-#
-#
-# MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
-#
-#
-# all the following subs takes a MARC::Record as parameter and manage
-# the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
-# NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
-
 =head1 NAME
 
-C4::Biblio - acquisition, catalog  management functions
+C4::Biblio - acquisitions and cataloging management functions
 
-=head1 SYNOPSIS
+=head1 DESCRIPTION
 
-move from 1.2 to 1.4 version :
-1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
-In the 1.4 version, we want to do 2 differents things :
- - keep populating the old-DB, that has a LOT less datas than MARC
- - populate the MARC-DB
-To populate the DBs we have 2 differents sources :
- - the standard acquisition system (through book sellers), that does'nt use MARC data
- - the MARC acquisition system, that uses MARC data.
+Biblio.pm contains functions for managing storage and editing of bibliographic data within Koha. Most of the functions in this module are used for cataloging records: adding, editing, or removing biblios, biblioitems, or items. Koha's stores bibliographic information in three places:
 
-Thus, we have 2 differents cases :
-- with the standard acquisition system, we have non MARC data and want to populate old-DB and MARC-DB, knowing it's an incomplete MARC-record
-- with the MARC acquisition system, we have MARC datas, and want to loose nothing in MARC-DB. So, we can't store datas in old-DB, then copy in MARC-DB. we MUST have an API for true MARC data, that populate MARC-DB then old-DB
+=over 4
 
-That's why we need 4 subs :
-all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
-all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
-all I<subs beginning by NEW> manage both OLD-DB and MARC tables. They use MARC::Record as parameters. it's the API that MUST be used in MARC acquisition system
-all I<subs beginning by seomething else> are the old-style API. They use old-DB as parameter, then call internally the OLD and MARC subs.
+1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
 
-- NEW and old-style API should be used in koha to manage biblio
-- MARCsubs are divided in 2 parts :
-* some of them manage MARC parameters. They are heavily used in koha.
-* some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
-- OLD are used internally only
+2. as raw MARC in the Zebra index and storage engine
 
-all subs requires/use $dbh as 1st parameter.
+3. as raw MARC the biblioitems.marc
 
-I<NEWxxx related subs>
+=back
 
-all subs requires/use $dbh as 1st parameter.
-those subs are used by the MARC-compliant version of koha : marc import, or marc management.
+In the 2.4 version of Koha, the authoritative record-level information is in biblioitems.marc and the authoritative items information is in the items table.
 
-I<OLDxxx related subs>
+Because the data isn't completely normalized there's a chance for information to get out of sync. The design choice to go with a un-normalized schema was driven by performance and stability concerns:
 
-all subs requires/use $dbh as 1st parameter.
-those subs are used by the MARC-compliant version of koha : marc import, or marc management.
+=over 4
 
-They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
-The OLDxxx is called by the original xxx sub.
-the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
+1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
 
-WARNING : there is 1 difference between initialxxx and OLDxxx :
-the db header $dbh is always passed as parameter to avoid over-DB connexion
+2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
 
-=head1 DESCRIPTION
+=back
+
+Because of this design choice, the process of managing storage and editing is a bit convoluted. Historically, Biblio.pm's grown to an unmanagable size and as a result we have several types of functions currently:
 
 =over 4
 
-=item @tagslib = &MARCgettagslib($dbh,1|0,$itemtype);
+1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
 
-last param is 1 for liblibrarian and 0 for libopac
-$itemtype contains the itemtype framework reference. If empty or does not exist, the default one is used
-returns a hash with tag/subfield meaning
-=item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
+2. _koha_* - low-level internal functions for managing the koha tables
 
-finds MARC tag and subfield for a given kohafield
-kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
+3. MARC* functions for interacting with the MARC data in both biblioitems.marc Zebra (biblioitems.marc is authoritative)
 
-=item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
+4. Zebra functions used to update the Zebra index
 
-finds a old-db biblio number for a given MARCbibid number
+5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
 
-=item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
+6. other functions that don't belong in Biblio.pm that will be cleaned out in time. (like MARCfind_marc_from_kohafield which belongs in Search.pm)
 
-finds a MARC bibid from a old-db biblionumber
+In time, as we solidify the new API these older functions will be weeded out.
 
-=item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
+=back
 
-MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
+=head1 EXPORTED FUNCTIONS
 
-=item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
+=head2 AddBiblio
 
-MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
+=over 4
 
-=item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
+($biblionumber,$oldbibnum,$oldbibitemnum) = AddBiblio($dbh,$record,$frameworkcode);
 
-MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
+Exported function (core API) for adding a new biblio to koha.
 
-=item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
+=back
 
-builds a hash with old-db datas from a MARC::Record
+=cut
 
-=item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
+sub AddBiblio {
+    my ( $dbh, $record, $frameworkcode) = @_;
+    my $oldbibnum;
+    my $oldbibitemnum;
 
-creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
+    # transform the data into koha-table style data
+    my $olddata = MARCmarc2koha( $dbh, $record,$frameworkcode );
 
-=item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
+    #
+    $oldbibnum = _koha_add_biblio( $dbh, $olddata );
+    $olddata->{'biblionumber'} = $oldbibnum;
+    $oldbibitemnum = _koha_add_biblioitem( $dbh, $olddata );
 
-adds a subfield in a biblio (in the MARC tables only).
+    # search subtiles, addiauthors and subjects
+    my ( $tagfield, $tagsubfield ) = MARCfind_marc_from_kohafield( $dbh, "additionalauthors.author",$frameworkcode );
+    my @addiauthfields = $record->field($tagfield);
+    foreach my $addiauthfield (@addiauthfields) {
+        my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
+        foreach my $subfieldcount ( 0 .. $#addiauthsubfields ) {
+            _koha_modify_additionalauthor( $dbh, $oldbibnum, $addiauthsubfields[$subfieldcount] );
+        }
+    }
+    ( $tagfield, $tagsubfield ) = MARCfind_marc_from_kohafield( $dbh, "bibliosubtitle.subtitle",$frameworkcode );
+    my @subtitlefields = $record->field($tagfield);
+    foreach my $subtitlefield (@subtitlefields) {
+        my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
+        foreach my $subfieldcount ( 0 .. $#subtitlesubfields ) {
+            _koha_add_subtitle( $dbh, $oldbibnum, $subtitlesubfields[$subfieldcount] );
+        }
+    }
+    ( $tagfield, $tagsubfield ) = MARCfind_marc_from_kohafield( $dbh, "bibliosubject.subject",$frameworkcode );
+    my @subj = $record->field($tagfield);
+    my @subjects;
+    foreach my $subject (@subj) {
+        my @subjsubfield = $subject->subfield($tagsubfield);
+        foreach my $subfieldcount ( 0 .. $#subjsubfield ) {
+            push @subjects, $subjsubfield[$subfieldcount];
+        }
+    }
+    _koha_modify_subject( $dbh, $oldbibnum, 1, @subjects );
 
-=item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
+    # we must add bibnum and bibitemnum in MARC::Record...
+    # we build the new field with biblionumber and biblioitemnumber
+    # we drop the original field
+    # we add the new builded field.
+    # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
+    # (steve and paul : thinks 090 is a good choice)
+    my $sth = $dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
+    $sth->execute("biblio.biblionumber");
+    ( my $tagfield1, my $tagsubfield1 ) = $sth->fetchrow;
+    $sth->execute("biblioitems.biblioitemnumber");
+   ( my $tagfield2, my $tagsubfield2 ) = $sth->fetchrow;
 
-Returns a MARC::Record for the biblio $bibid.
+    my $newfield;
+    # biblionumber & biblioitemnumber are in different fields
+    if ( $tagfield1 != $tagfield2 ) {
+        # deal with biblionumber
+        if ($tagfield1<10) {
+            $newfield = MARC::Field->new(
+                $tagfield1, $oldbibnum,
+            );
+        } else {
+            $newfield = MARC::Field->new(
+                $tagfield1, '', '', "$tagsubfield1" => $oldbibnum,
+            );
+        }
+        # drop old field and create new one...
+        my $old_field = $record->field($tagfield1);
+        $record->delete_field($old_field);
+        $record->append_fields($newfield);
+        # deal with biblioitemnumber
+        if ($tagfield2<10) {
+            $newfield = MARC::Field->new(
+                $tagfield2, $oldbibitemnum,
+            );
+        } else {
+            $newfield = MARC::Field->new(
+                $tagfield2, '', '', "$tagsubfield2" => $oldbibitemnum,
+            );
+        }
+        # drop old field and create new one...
+        $old_field = $record->field($tagfield2);
+        $record->delete_field($old_field);
+        $record->insert_fields_ordered($newfield);
+    # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
+    } else {
+        my $newfield = MARC::Field->new(
+            $tagfield1, '', '', "$tagsubfield1" => $oldbibnum,
+            "$tagsubfield2" => $oldbibitemnum
+        );
+        # drop old field and create new one...
+        my $old_field = $record->field($tagfield1);
+        $record->delete_field($old_field);
+        $record->insert_fields_ordered($newfield);
+    }
 
-=item &MARCmodbiblio($bibid,$record,$frameworkcode,$delete);
+    ###NEU specific add cataloguers cardnumber as well
+    my $cardtag=C4::Context->preference('cataloguersfield');
+    if ($cardtag) {
+        my $tag=substr($cardtag,0,3);
+        my $subf=substr($cardtag,3,1);
+        my $me= C4::Context->userenv;
+        my $cataloger=$me->{'cardnumber'} if ($me);
+        my $newtag=  MARC::Field->new($tag, '', '', $subf => $cataloger) if ($me);
+        $record->delete_field($newtag);
+        $record->insert_fields_ordered($newtag);
+    }
+
+    # now add the record
+    my $biblionumber = MARCaddbiblio( $dbh, $record, $oldbibnum, $frameworkcode );
+    return ( $biblionumber, $oldbibnum, $oldbibitemnum );
+}
+
+=head2 AddItem
 
-MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
-It 1st delete the biblio, then recreates it.
-WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
-=item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
+=over 4
 
-MARCmodsubfield changes the value of a given subfield
+$bib = AddItem( $dbh, $record, $biblionumber)
 
-=item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
+Exported function (core API) for adding a new item to Koha
 
-MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
-Returns -1 if more than 1 answer
+=back
 
-=item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
+=cut
 
-MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
+sub AddItem {
+    my ( $dbh, $record, $biblionumber ) = @_;
+    # add item in old-DB
+    my $frameworkcode=MARCfind_frameworkcode($dbh,$biblionumber);
+    my $item = &MARCmarc2koha( $dbh, $record,$frameworkcode );
+    # needs old biblionumber and biblioitemnumber
+    $item->{'biblionumber'} = $biblionumber;
+    my $sth =
+      $dbh->prepare(
+        "select biblioitemnumber,itemtype from biblioitems where biblionumber=?");
+    $sth->execute( $item->{'biblionumber'} );
+    my $itemtype;
+    ( $item->{'biblioitemnumber'}, $itemtype ) = $sth->fetchrow;
+    $sth=$dbh->prepare("select notforloan from itemtypes where itemtype='$itemtype'");
+    $sth->execute();
+    my $notforloan=$sth->fetchrow;
+    ##Change the notforloan field if $notforloan found
+    if ($notforloan >0){
+        $item->{'notforloan'}=$notforloan;
+        &MARCitemchange($dbh,$record,"items.notforloan",$notforloan);
+    }
+    if(!$item->{'dateaccessioned'}||$item->{'dateaccessioned'} eq ''){
+        # find today's date
+        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
+            localtime(time); $year +=1900; $mon +=1;
+        my $date = "$year-".sprintf ("%0.2d", $mon)."-".sprintf("%0.2d",$mday);
+        $item->{'dateaccessioned'}=$date;
+        &MARCitemchange($dbh,$record,"items.dateaccessioned",$date);
+    }
+    my ( $itemnumber, $error ) = &_koha_new_items( $dbh, $item, $item->{barcode} );
+    # add itemnumber to MARC::Record before adding the item.
+    $sth =
+      $dbh->prepare(
+"select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?"
+    );
+    &MARCkoha2marcOnefield( $sth, $record, "items.itemnumber", $itemnumber,$frameworkcode );
 
-=item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
+    ##NEU specific add cataloguers cardnumber as well
+    my $cardtag=C4::Context->preference('itemcataloguersubfield');
+    if ($cardtag){
+        $sth->execute($frameworkcode,"items.itemnumber");
+        my ($itemtag,$subtag)=$sth->fetchrow;
+        my $me= C4::Context->userenv;
+        my $cataloguer=$me->{'cardnumber'} if ($me);
+        my $newtag= $record->field($itemtag);
+        $newtag->update($cardtag=>$cataloguer) if ($me);
+        $record->delete_field($newtag);
+        $record->append_fields($newtag);
+    }
+    # add the item
+    my $bib = &MARCadditem( $dbh, $record, $item->{'biblionumber'} );
+}
 
-MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
-If $subfieldorder is not set, delete all the $tag$subfield subfields 
+=head2 ModBiblio
 
-=item &MARCdelbiblio($dbh,$bibid);
+=over 4
 
-MARCdelbiblio delete biblio $bibid
+ModBiblio($dbh,$record,$biblionumber,$frameworkcode);
 
-=item &MARCkoha2marcOnefield
+Exported function (core API) to modify a biblio
 
-used by MARCkoha2marc and should not be useful elsewhere
+=back
 
-=item &MARCmarc2kohaOnefield
+=cut
 
-used by MARCmarc2koha and should not be useful elsewhere
+sub ModBiblio {
+    my ($dbh,$record,$biblionumber,$frameworkcode) =@_;
 
-=item MARCaddword
+    $frameworkcode="" unless $frameworkcode;
 
-used to manage MARC_word table and should not be useful elsewhere
+    # update the MARC record with the new record data
+    &MARCmodbiblio($dbh,$biblionumber,$record,$frameworkcode,1);
 
-=item MARCdelword
+    # loat the koha-style data
+    my $oldbiblio = MARCmarc2koha($dbh,$record,$frameworkcode);
 
-used to manage MARC_word table and should not be useful elsewhere
+    # modify the other koha tables
+    my $oldbiblionumber = _koha_modify_biblio($dbh,$oldbiblio);
+    _koha_modify_biblioitem($dbh,$oldbiblio);
+
+    # now, modify addi authors, subject, addititles.
+    my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author",$frameworkcode);
+    my @addiauthfields = $record->field($tagfield);
+    foreach my $addiauthfield (@addiauthfields) {
+        my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
+        foreach my $subfieldcount (0..$#addiauthsubfields) {
+            _koha_modify_additionalauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
+        }
+    }
+    ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle",$frameworkcode);
+    my @subtitlefields = $record->field($tagfield);
+    foreach my $subtitlefield (@subtitlefields) {
+        my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
+        # delete & create subtitle again because _koha_modify_subtitle can't handle new subtitles
+        # between 2 modifs
+        $dbh->do("delete from bibliosubtitle where biblionumber=$oldbiblionumber");
+        foreach my $subfieldcount (0..$#subtitlesubfields) {
+            foreach my $subtit(split /\||#/,$subtitlesubfields[$subfieldcount]) {
+                _koha_add_subtitle($dbh,$oldbiblionumber,$subtit);
+            }
+        }
+    }
+    ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject",$frameworkcode);
+    my @subj = $record->field($tagfield);
+    my @subjects;
+    foreach my $subject (@subj) {
+        my @subjsubfield = $subject->subfield($tagsubfield);
+        foreach my $subfieldcount (0..$#subjsubfield) {
+            push @subjects,$subjsubfield[$subfieldcount];
+        }
+    }
+    _koha_modify_subject($dbh,$oldbiblionumber,1, at subjects);
+    return 1;
+}
+
+=head2 ModItem
+
+=over 4
+
+Exported function (core API) for modifying an item in Koha.
+
+=back
+
+=cut
+
+
+sub ModItem {
+    my ( $dbh, $record, $biblionumber, $itemnumber, $delete ) = @_;
+
+    &MARCmoditem( $dbh, $record, $biblionumber, $itemnumber, $delete );
+    my $frameworkcode=MARCfind_frameworkcode($dbh,$biblionumber);
+    my $olditem = MARCmarc2koha( $dbh, $record,$frameworkcode );
+    _koha_modify_item( $dbh, $olditem );
+}
+
+=head2 DelBiblio
+
+=over 4
+
+my $error = &DelBiblio($dbh,$bibid);
+
+Exported function (core API) for deleting a biblio in koha.
+
+Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
+
+Also backs it up to deleted* tables
+
+Checks to make sure there are not issues on any of the items
+
+return:
+C<$error> : undef unless an error occurs
+
+=back
+
+=cut
+
+sub DelBiblio {
+    my ( $dbh, $biblionumber ) = @_;
+    my $error; # for error handling
+
+    # First make sure there are no items with issues are still attached
+    my $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
+    $sth->execute($biblionumber);
+    while ( my $biblioitemnumber = $sth->fetchrow ) {
+        my @issues = C4::Search::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
+                # 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 from Zebra
+    # get the xml of the record from Zebra
+    my $xmlrecord = getRecord("biblioserver","Local-number=$biblionumber");
+    my $serviceOptions;
+    $serviceOptions->{'databaseName'} = "biblios";
+    $serviceOptions->{'record'} = $xmlrecord;
+    $error = &z3950_extended_services("biblioserver","update","recordDelete",$serviceOptions);
+    return $error if $error;
+
+    # run commit operation
+    #if ( (C4::Context->preference("commitImmediately")) ) {
+    $error = &z3950_extended_services("biblioserver","commit");
+    #}
+    #zebraop($dbh,$biblionumber,"recordDelete","biblioserver");
+    return $error if $error;
+
+    # delete biblio from Koha tables and save in deletedbiblio
+    $error = &_koha_delete_biblio( $dbh, $biblionumber );
+    return $error if $error;
+
+    # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
+    $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
+    $sth->execute($biblionumber);
+    while ( my $biblioitemnumber = $sth->fetchrow ) {
+
+        # 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_items( $dbh, $itemnumber );
+            return $error if $error;
+        }
+    }
+	# delete from other koha tables
+	$sth = $dbh->prepare("DELETE FROM bibliosubject WHERE biblionumber=?");
+	$sth->execute($biblionumber);
+	$sth = $dbh->prepare("DELETE FROM additionalauthors WHERE biblionumber=?");
+	$sth->execute($biblionumber);
+}
+
+
+=head2 DelItem
+
+=over 4
+
+DelItem( $dbh, $biblionumber, $itemnumber );
+
+Exported function (core API) for deleting an item record in Koha.
+
+=back
+
+=cut
+
+sub DelItem {
+    my ( $dbh, $biblionumber, $itemnumber ) = @_;
+    &_koha_delete_item( $dbh, $itemnumber );
+    my $newrec=&MARCdelitem( $dbh, $biblionumber, $itemnumber );
+    &MARCaddbiblio($dbh,$newrec,$biblionumber,);
+}
+
+=head2 z3950_extended_services
+
+=over 4
+
+z3950_extended_services($serviceType,$serviceOptions,$record);
+
+    z3950_extended_services is used to handle all interactions with Zebra's extended serices package, which is employed to perform all management of the MARC data stored in Zebra.
+
+C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
+
+C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
+
+    action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
+
+and maybe
+
+    recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
+    syntax => the record syntax (transfer syntax)
+    databaseName = Database from connection object
+
+    To set serviceOptions, call set_service_options($serviceType)
+
+C<$record> the record, if one is needed for the service type
+
+    A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
+
+=back
+
+=cut
+
+sub z3950_extended_services {
+    my ($server,$serviceType,$action,$serviceOptions) = @_;
+
+	# get our connection object
+    my $Zconn = C4::Context->Zconn($server,0,1);
+
+    # create a new package object
+    my $Zpackage = $Zconn->package();
+
+    # set our options
+    $Zpackage->option(action => $action);
+
+    if ($serviceOptions->{'databaseName'}) {
+        $Zpackage->option(databaseName => $serviceOptions->{'databaseName'});
+    }
+    if ($serviceOptions->{'recordIdNumber'}) {
+        $Zpackage->option(recordIdNumber => $serviceOptions->{'recordIdNumber'});
+    }
+    if ($serviceOptions->{'recordIdOpaque'}) {
+        $Zpackage->option(recordIdOpaque => $serviceOptions->{'recordIdOpaque'});
+    }
+
+    # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
+    #if ($serviceType eq 'itemorder') {
+    #   $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
+    #   $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
+    #   $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
+    #   $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
+    #}
+
+    if ($serviceOptions->{record}) {
+        $Zpackage->option(record => $serviceOptions->{record});
+
+		# can be xml or marc
+        if ($serviceOptions->{'syntax'}) {
+            $Zpackage->option(syntax => $serviceOptions->{'syntax'});
+        }
+    }
+
+    # send the request, handle any exception encountered
+    eval { $Zpackage->send($serviceType) };
+        if ($@ && $@->isa("ZOOM::Exception")) {
+            return "error:  ".$@->code()." ".$@->message()."\n";
+        }
+    # free up package resources
+    $Zpackage->destroy();
+}
+
+=head2 set_service_options
+
+=over 4
+
+my $serviceOptions = set_service_options($serviceType);
+
+C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
+
+Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
+
+=back
+
+=cut
+
+sub set_service_options {
+    my ($serviceType) = @_;
+    my $serviceOptions;
+
+    # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
+    #   $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
+
+    if ($serviceType eq 'commit') {
+    # nothing to do
+    }
+    if ($serviceType eq 'create') {
+	# nothing to do
+    }
+    if ($serviceType eq 'drop') {
+        die "ERROR: 'drop' not currently supported (by Zebra)";
+    }
+    return $serviceOptions;
+}
+
+=head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
+
+=head2 MARCgettagslib
 
 =cut
 
@@ -311,6 +706,10 @@
     return $res;
 }
 
+=head2 MARCfind_marc_from_kohafield
+
+=cut
+
 sub MARCfind_marc_from_kohafield {
     my ( $dbh, $kohafield,$frameworkcode ) = @_;
     return 0, 0 unless $kohafield;
@@ -327,20 +726,27 @@
     return $MARCbibid;
 }
 
+=head2 MARCfind_MARCbibid_from_oldbiblionumber
+
+=cut
+
 sub MARCfind_MARCbibid_from_oldbiblionumber {
     my ( $dbh, $oldbiblionumber ) = @_;
-#    my $sth =
- #     $dbh->prepare("select bibid from marc_biblio where biblionumber=?");
- #   $sth->execute($oldbiblionumber);
- #   my ($bibid) = $sth->fetchrow;
     return $oldbiblionumber;
 }
 
+=head2 MARCaddbiblio
+
+&MARCaddbiblio($dbh,$newrec,$biblionumber,$frameworkcode);
+
+Add MARC data for a biblio to koha 
+
+=cut
+
 sub MARCaddbiblio {
 
 # pass the MARC::Record to this function, and it will create the records in the marc tables
-	my ($record,$biblionumber,$frameworkcode,$bibid) = @_;
-	my $dbh = C4::Context->dbh;
+	my ($dbh,$record,$biblionumber,$frameworkcode) = @_;
 	my @fields=$record->fields();
 	if (!$frameworkcode){
 		$frameworkcode="";
@@ -359,7 +765,7 @@
           $record->insert_fields_ordered(MARC::Field->new(100,"","","a"=>$string));
       }
     }
-    my $sth =$dbh->prepare("update biblioitems set marc=?,marcxml=?  where biblionumber=?"   );
+    $sth =$dbh->prepare("update biblioitems set marc=?,marcxml=?  where biblionumber=?"   );
     $sth->execute( $record->as_usmarc(),$record->as_xml_record() , $biblionumber);     
     warn $record->as_xml_record();
     $sth->finish;
@@ -367,6 +773,10 @@
     return $biblionumber;
 }
 
+=head2 MARCadditem
+
+=cut
+
 sub MARCadditem {
 
 # pass the MARC::Record to this function, and it will create the records in the marc tables
@@ -378,18 +788,20 @@
      foreach my $field (@fields) {
 	  $newrec->append_fields($field);
 	}
-my $bibid=&MARCaddbiblio($newrec,$biblionumber);
-    return $bibid;
+	# FIXME: should we be making sure the biblionumbers are the same?
+	my $newbiblionumber=&MARCaddbiblio($dbh,$newrec,$biblionumber);
+    return $newbiblionumber;
 }
 
-sub MARCaddsubfield {
+=head2 MARCgetbiblio
 
-}
+Returns MARC::Record of the biblio passed in parameter.
+
+=cut
 
 sub MARCgetbiblio {
 	my ( $dbh, $bibid ) = @_;
-	my $dbh = C4::Context->dbh;
-	my $sth = $dbh->prepare("select marcxml from biblioitems where biblionumber=? "  );
+	my $sth=$dbh->prepare("select marcxml from biblioitems where biblionumber=? "  );
     $sth->execute($bibid);
     my ($marcxml)=$sth->fetchrow;
     my $record = MARC::Record->new();
@@ -416,8 +828,6 @@
 
     # Returns MARC::XML of the biblio passed in parameter.
     my ( $dbh, $biblionumber ) = @_;
-  	my $dbh = C4::Context->dbh;
-
     my $sth = $dbh->prepare("select marcxml from biblioitems where biblionumber=? "  );
     
     $sth->execute($biblionumber);
@@ -428,6 +838,19 @@
 # 	print Dumper($marcxml);
  	return $marcxml;
 }
+
+=head2 MARCgetbiblio2
+
+    my $sth =
+      $dbh->prepare("select marc from biblioitems where biblionumber=? "  );
+    
+    $sth->execute($biblionumber);
+   	my ($marc)=$sth->fetchrow;
+	$marc=MARC::File::USMARC::decode($marc);
+	my $marcxml=$marc->as_xml_record();
+ 	return $marcxml;
+=cut
+
 sub MARCgetbiblio2 {
 
     # Returns MARC::Record of the biblio passed in parameter.
@@ -449,6 +872,10 @@
 }
 }
 
+=head2 MARCgetitem_frombarcode
+
+=cut
+
 sub MARCgetitem_frombarcode {
 
     my ( $dbh, $biblionumber, $barcode ) = @_;
@@ -474,6 +901,10 @@
     return $itemrecord;
 }
 
+=head2 MARCgetitem
+
+=cut
+
 sub MARCgetitem {
     # Returns MARC::Record of the item passed in parameter.
     my ( $dbh, $bibid, $itemnumber ) = @_;
@@ -495,27 +926,38 @@
 
       if ($field->subfield($itemnumbersubfield) eq $itemnumber ){
 
-	$newrecord->add_fields($field);
+	$newrecord->insert_fields_ordered($field);
 	}
 }
     return $newrecord;
 }
+
+=head2 MARCmodbiblio
+
+MARCmodbibio($dbh,$bibid,$record,$frameworkcode,1);
+
+Modify a biblio record with the option to save items data
+
+=cut
+
 sub MARCmodbiblio {
-	my ($bibid,$record,$frameworkcode,$delete)=@_;
-	my $dbh = C4::Context->dbh;
-#delete original marcrecord
-	my $newrec=&MARCdelbiblio($dbh,$bibid,$delete);
-# 2nd recreate it
+	my ($dbh,$biblionumber,$record,$frameworkcode,$keep_items)=@_;
+
+	# delete original record but save the items
+	my $newrec=&MARCdelbiblio($dbh,$biblionumber,$keep_items);
+
+	# recreate it and add the new fields
 	my @fields = $record->fields();
      foreach my $field (@fields) {
-
-	  $newrec->append_fields($field);
+		# this requires a more recent version of MARC::Record
+		# but ensures the fields are in order
+		$newrec->insert_fields_ordered($field);
 	}
-##correct the leader
-	$newrec->leader($record->leader());
-	&MARCmodLCindex($dbh,$newrec,$frameworkcode);
-	&MARCaddbiblio($newrec,$bibid,$frameworkcode,$bibid);
 	
+	# give back our old leader
+	$newrec->leader($record->leader());
+	# add the record back with the items info preserved
+	&MARCaddbiblio($dbh,$newrec,$biblionumber,$frameworkcode);
 }
 
 =head2 find_biblioitemnumber
@@ -539,6 +981,9 @@
 	return $biblioitemnumber;
 }
 
+=head2 MARCdelbiblio
+
+=cut
 
 sub MARCdelbiblio {
     my ( $dbh, $bibid, $keep_items ) = @_;
@@ -552,20 +997,15 @@
 # if deletion called before MARCmodbiblio => won't do anything, as the oldbiblionumber doesn't
     # exist in deletedbiblio table
     my $record = MARCgetbiblio( $dbh, $bibid );
-    my $oldbiblionumber =
-      MARCfind_oldbiblionumber_from_MARCbibid( $dbh, $bibid );
-    my $copy2deleted =
-      $dbh->prepare("update deletedbiblio set marc=? where biblionumber=?");
+    my $oldbiblionumber = $bibid;
+    my $copy2deleted = $dbh->prepare("update deletedbiblio set marc=? where biblionumber=?");
     $copy2deleted->execute( $record->as_usmarc(), $oldbiblionumber );
  my @fields = $record->fields();
   # now, delete in MARC tables.
     if ( $keep_items eq 1 ) {
 
         #search item field code
-        my $sth =
-          $dbh->prepare(
-"select tagfield from marc_subfield_structure where kohafield like 'items.%'"
-        );
+        my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
         $sth->execute;
         my $itemtag = $sth->fetchrow_hashref->{tagfield};
 
@@ -587,6 +1027,10 @@
       return $record;     
 }
 
+=head2 MARCdelitem
+
+=cut
+
 sub MARCdelitem {
 
     # delete the item passed in parameter in MARC tables.
@@ -619,7 +1063,9 @@
 return $record;
 }
 
+=head2 MARCmoditemonefield
 
+=cut
 
 sub MARCmoditemonefield{
 my ($dbh,$biblionumber,$itemnumber,$itemfield,$newvalue)=@_;
@@ -645,16 +1091,21 @@
            
 		$tag->update($tagsubfield =>$newvalue);
 		$record->delete_field($tag);
-                $record->add_fields($tag);
+                $record->insert_fields_ordered($tag);
 	
 	&MARCmoditem($dbh,$record,$biblionumber,$itemnumber,0);
 	}
      }	
 
 }
+
+=head2 MARCmoditem
+
+=cut
+
 sub MARCmoditem {
 	my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
-	my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
+	my $biblionumber = $bibid;
 	my $newrec=&MARCdelitem($dbh,$bibid,$itemnumber);
 
 # 2nd recreate it
@@ -670,9 +1121,14 @@
 	}
 	  $newrec->append_fields($field);
 	}
-	&MARCaddbiblio($newrec,$biblionumber);
+	&MARCaddbiblio($dbh,$newrec,$biblionumber);
 	
 }
+
+=head2 MARCmodsubfield
+
+=cut
+
 sub MARCmodsubfield {
 
     # Subroutine changes a subfield value given a subfieldid.
@@ -735,6 +1191,10 @@
         return ( $subfieldid, $subfieldvalue );
 }
 
+=head2 MARCfindsubfield
+
+=cut
+
 sub MARCfindsubfield {
     my ( $dbh, $bibid, $tag, $subfieldcode, $subfieldorder, $subfieldvalue ) =
       @_;
@@ -772,6 +1232,10 @@
     }
 }
 
+=head2 MARCfindsubfieldid
+
+=cut
+
 sub MARCfindsubfieldid {
     my ( $dbh, $bibid, $tag, $tagorder, $subfield, $subfieldorder ) = @_;
     my $sth = $dbh->prepare( "select subfieldid from marc_subfield_table
@@ -791,6 +1255,10 @@
     return $res;
 }
 
+=head2 MARCfind_frameworkcode
+
+=cut
+
 sub MARCfind_frameworkcode {
     my ( $dbh, $bibid ) = @_;
     my $sth =
@@ -800,6 +1268,10 @@
     return $frameworkcode;
 }
 
+=head2 MARCdelsubfield
+
+=cut
+
 sub MARCdelsubfield {
 
     # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
@@ -818,6 +1290,10 @@
 			}
 }
 
+=head MARCkoha2marcBiblio
+
+=cut
+
 sub MARCkoha2marcBiblio {
 
     # this function builds partial MARC::Record from the old koha-DB fields
@@ -895,6 +1371,10 @@
     return $record;
 }
 
+=head2 MARCkoha2marcItem
+
+=cut
+
 sub MARCkoha2marcItem {
 
     # this function builds partial MARC::Record from the old koha-DB fields
@@ -930,6 +1410,10 @@
     return $record;
 }
 
+=head2 MARCkoha2marcSubtitle
+
+=cut
+
 sub MARCkoha2marcSubtitle {
 
     # this function builds partial MARC::Record from the old koha-DB fields
@@ -944,6 +1428,10 @@
     return $record;
 }
 
+=head2 MARCkoha2marcOnefield
+
+=cut
+
 sub MARCkoha2marcOnefield {
     my ( $sth, $record, $kohafieldname, $value,$frameworkcode ) = @_;
     my $tagfield;
@@ -963,19 +1451,23 @@
         if ($tag) {
                 $tag->update( $tagsubfield=> $value );
                 $record->delete_field($tag);
-                $record->add_fields($tag);
+                $record->insert_fields_ordered($tag);
 
             
         }else {
-            $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
+            $record->insert_fields_ordered( $tagfield, " ", " ", $tagsubfield => $value );
         }
     }
 
     return $record;
 }
+
+=head MARChtml2xml
+
+=cut
+
 sub MARChtml2xml {
 	my ($tags,$subfields,$values,$indicator,$ind_tag) = @_;        
-	#use MARC::File::XML;
 	my $xml= MARC::File::XML::header('UTF-8'); 
 	#$xml =~ s/UTF-8/ISO-8859-1/;
     my $prevvalue;
@@ -1004,9 +1496,16 @@
 			#warn "IND:".substr(@$indicator[$j],0,1).substr(@$indicator[$j],1,1)." ".@$tags[$i];
 			if (!$first){
 		    	$xml.="</datafield>\n";
-				if ((@$tags[$i] > 10) && (@$values[$i] ne "")){
+				if ((@$tags[$i] && @$tags[$i] > 10)  && (@$values[$i] ne "")){
 						my $ind1 = substr(@$indicator[$j],0,1);
-                        my $ind2 = substr(@$indicator[$j],1,1);
+						my $ind2;
+						if (@$indicator[$j]) {
+                        	$ind2 = substr(@$indicator[$j],1,1);
+						} 
+						else {
+							warn "Indicator is empty";
+							$ind2 = " ";
+						}
                         $xml.="<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
                         $xml.="<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
                         $first=0;
@@ -1051,6 +1550,11 @@
 	#warn $xml;
 	return $xml;
 }
+
+=head2 MARChtml2marc
+
+=cut
+
 sub MARChtml2marc {
 	my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
 	my $prevtag = -1;
@@ -1067,7 +1571,7 @@
 				if ($prevvalue) {
 
 					if ($prevtag ne '000') {
-						$record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
+						$record->insert_fields_ordered((sprintf "%03s",$prevtag),$prevvalue);
 					} else {
 
 						$record->leader($prevvalue);
@@ -1076,7 +1580,7 @@
 				}
 			} else {
 				if ($field) {
-					$record->add_fields($field);
+					$record->insert_fields_ordered($field);
 				}
 			}
 			$indicators{@$rtags[$i]}.='  ';
@@ -1102,20 +1606,17 @@
 		}
 	}
 	# the last has not been included inside the loop... do it now !
-	$record->add_fields($field) if $field;
-	use POSIX qw(strftime);
-    my $string=strftime ("%Y%m%d",localtime(time));
-    $string = sprintf("%-*s",35, $string);
-    substr($string,22,6,"frey50");
-    unless ($record->subfield(100,"a")){
-      $record->insert_fields_ordered(MARC::Field->new(100,"","","a"=>$string));
-    }
+	$record->insert_fields_ordered($field) if $field;
 # 	warn "HTML2MARC=".$record->as_formatted;
 	$record->encoding( 'UTF-8' );
 #	$record->MARC::File::USMARC::update_leader();
 	return $record;
 }
 
+=head MARCmarc2koha
+
+=cut
+
 sub MARCmarc2koha {
 	my ($dbh,$record,$frameworkcode) = @_;
 	my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?");
@@ -1163,6 +1664,10 @@
 	return $result;
 }
 
+=head2 MARCmarc2kohaOneField
+
+=cut
+
 sub MARCmarc2kohaOneField {
 
 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
@@ -1180,219 +1685,87 @@
 				$result->{$kohafield} = $field->data();
 			}
 		} else {
-			if ( $field->subfields ) {
-				my @subfields = $field->subfields();
-				foreach my $subfieldcount ( 0 .. $#subfields ) {
-					if ($subfields[$subfieldcount][0] eq $subfield) {
-						if ( $result->{$kohafield} ) {
-							$result->{$kohafield} .= " | " . $subfields[$subfieldcount][1];
-						}
-						else {
-							$result->{$kohafield} = $subfields[$subfieldcount][1];
-						}
-					}
-				}
-			}
-		}
-    }
-# 	warn "OneField for $kohatable.$kohafield and $frameworkcode=> $tagfield, $subfield";
-    return $result;
-}
-
-sub MARCaddword {
-
-    # split a subfield string and adds it into the word table.
-    # removes stopwords
-    my (
-        $dbh,        $bibid,         $tag,    $tagorder,
-        $subfieldid, $subfieldorder, $sentence
-      )
-      = @_;
-    $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\}|\/)/ /g;
-    my @words = split / /, $sentence;
-    my $stopwords = C4::Context->stopwords;
-    my $sth       =
-      $dbh->prepare(
-"insert into marc_word (bibid, tagsubfield, tagorder, subfieldorder, word, sndx_word)
-			values (?,concat(?,?),?,?,?,soundex(?))"
-    );
-    foreach my $word (@words) {
-# we record only words one char long and not in stopwords hash
-	if (length($word)>=1 and !($stopwords->{uc($word)})) {
-	    $sth->execute($bibid,$tag,$subfieldid,$tagorder,$subfieldorder,$word,$word);
-	    if ($sth->err()) {
-		warn "ERROR ==> insert into marc_word (bibid, tagsubfield, tagorder, subfieldorder, word, sndx_word) values ($bibid,concat($tag,$subfieldid),$tagorder,$subfieldorder,$word,soundex($word))\n";
-	    }
-	}
-    }
-}
-
-sub MARCdelword {
-
-# delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
-    my ( $dbh, $bibid, $tag, $tagorder, $subfield, $subfieldorder ) = @_;
-    my $sth =
-      $dbh->prepare(
-"delete from marc_word where bibid=? and tagsubfield=concat(?,?) and tagorder=? and subfieldorder=?"
-    );
-    $sth->execute( $bibid, $tag, $subfield, $tagorder, $subfieldorder );
-}
-
-#
-#
-# NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
-#
-#
-# all the following subs are useful to manage MARC-DB with complete MARC records.
-# it's used with marcimport, and marc management tools
-#
-
-=item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
-
-creates a new biblio from a MARC::Record. The 3rd and 4th parameter are hashes and may be ignored. If only 2 params are passed to the sub, the old-db hashes
-are builded from the MARC::Record. If they are passed, they are used.
-
-=item NEWnewitem($dbh, $record,$bibid);
-
-adds an item in the db.
-
-=cut
-
-sub NEWnewbiblio {
-    my ( $dbh, $record, $frameworkcode) = @_;
-    my $oldbibnum;
-    my $oldbibitemnum;
-    my $olddata = MARCmarc2koha( $dbh, $record,$frameworkcode );
-    $oldbibnum = OLDnewbiblio( $dbh, $olddata );
-	$olddata->{'biblionumber'} = $oldbibnum;
-    $oldbibitemnum = OLDnewbiblioitem( $dbh, $olddata );
-
-    # search subtiles, addiauthors and subjects
-    my ( $tagfield, $tagsubfield ) =
-      MARCfind_marc_from_kohafield( $dbh, "additionalauthors.author",$frameworkcode );
-    my @addiauthfields = $record->field($tagfield);
-    foreach my $addiauthfield (@addiauthfields) {
-        my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
-        foreach my $subfieldcount ( 0 .. $#addiauthsubfields ) {
-            OLDmodaddauthor( $dbh, $oldbibnum,
-                $addiauthsubfields[$subfieldcount] );
-        }
-    }
-    ( $tagfield, $tagsubfield ) =
-      MARCfind_marc_from_kohafield( $dbh, "bibliosubtitle.subtitle",$frameworkcode );
-    my @subtitlefields = $record->field($tagfield);
-    foreach my $subtitlefield (@subtitlefields) {
-        my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
-        foreach my $subfieldcount ( 0 .. $#subtitlesubfields ) {
-            OLDnewsubtitle( $dbh, $oldbibnum,
-                $subtitlesubfields[$subfieldcount] );
-        }
-    }
-    ( $tagfield, $tagsubfield ) =
-      MARCfind_marc_from_kohafield( $dbh, "bibliosubject.subject",$frameworkcode );
-    my @subj = $record->field($tagfield);
-    my @subjects;
-    foreach my $subject (@subj) {
-        my @subjsubfield = $subject->subfield($tagsubfield);
-        foreach my $subfieldcount ( 0 .. $#subjsubfield ) {
-            push @subjects, $subjsubfield[$subfieldcount];
-        }
-    }
-    OLDmodsubject( $dbh, $oldbibnum, 1, @subjects );
-	
-    # we must add bibnum and bibitemnum in MARC::Record...
-    # we build the new field with biblionumber and biblioitemnumber
-    # we drop the original field
-    # we add the new builded field.
-# NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
-    # (steve and paul : thinks 090 is a good choice)
-    my $sth =
-      $dbh->prepare(
-"select tagfield,tagsubfield from marc_subfield_structure where kohafield=?"
-    );
-    $sth->execute("biblio.biblionumber");
-    ( my $tagfield1, my $tagsubfield1 ) = $sth->fetchrow;
-    $sth->execute("biblioitems.biblioitemnumber");
-   ( my $tagfield2, my $tagsubfield2 ) = $sth->fetchrow;
-
-	my $newfield;
-	# biblionumber & biblioitemnumber are in different fields
-    if ( $tagfield1 != $tagfield2 ) {
-		# deal with biblionumber
-		if ($tagfield1<10) {
-			$newfield = MARC::Field->new(
-				$tagfield1, $oldbibnum,
-			);
-		} else {
-			$newfield = MARC::Field->new(
-				$tagfield1, '', '', "$tagsubfield1" => $oldbibnum,
-			);
-		}
-		# drop old field and create new one...
-		my $old_field = $record->field($tagfield1);
-		$record->delete_field($old_field);
-		$record->append_fields($newfield);
-		# deal with biblioitemnumber
-		if ($tagfield2<10) {
-			$newfield = MARC::Field->new(
-				$tagfield2, $oldbibitemnum,
-			);
-		} else {
-			$newfield = MARC::Field->new(
-				$tagfield2, '', '', "$tagsubfield2" => $oldbibitemnum,
-			);
-		}
-		# drop old field and create new one...
-		$old_field = $record->field($tagfield2);
-		$record->delete_field($old_field);
-		$record->add_fields($newfield);
-	# biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
-	} else {
-		my $newfield = MARC::Field->new(
-			$tagfield1, '', '', "$tagsubfield1" => $oldbibnum,
-			"$tagsubfield2" => $oldbibitemnum
-		);
-		# drop old field and create new one...
-		my $old_field = $record->field($tagfield1);
-		$record->delete_field($old_field);
-		$record->add_fields($newfield);
+			if ( $field->subfields ) {
+				my @subfields = $field->subfields();
+				foreach my $subfieldcount ( 0 .. $#subfields ) {
+					if ($subfields[$subfieldcount][0] eq $subfield) {
+						if ( $result->{$kohafield} ) {
+							$result->{$kohafield} .= " | " . $subfields[$subfieldcount][1];
 	}
-# 	warn "REC : ".$record->as_formatted;
-###NEU specific add cataloguers cardnumber as well
-my $cardtag=C4::Context->preference('cataloguersfield');
-if ($cardtag){
-my $tag=substr($cardtag,0,3);
-my $subf=substr($cardtag,3,1);		
-my $me= C4::Context->userenv;
-my $cataloger=$me->{'cardnumber'} if ($me);
-my $newtag=  MARC::Field->new($tag, '', '', $subf => $cataloger) if ($me);
-$record->delete_field($newtag);
-$record->add_fields($newtag);	
+						else {
+							$result->{$kohafield} = $subfields[$subfieldcount][1];
+						}
+					}
+				}
+			}
+		}
+    }
+# 	warn "OneField for $kohatable.$kohafield and $frameworkcode=> $tagfield, $subfield";
+    return $result;
 }
-## We must add the indexing fields for LC in MARC record--TG
-	&MARCmodLCindex($dbh,$record,$frameworkcode);
 
+=head2 MARCaddword
+
+=cut
+
+sub MARCaddword {
 
-    my $bibid = MARCaddbiblio($record, $oldbibnum, $frameworkcode );
-    return ( $bibid, $oldbibnum, $oldbibitemnum );
+    # split a subfield string and adds it into the word table.
+    # removes stopwords
+    my (
+        $dbh,        $bibid,         $tag,    $tagorder,
+        $subfieldid, $subfieldorder, $sentence
+      )
+      = @_;
+    $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\}|\/)/ /g;
+    my @words = split / /, $sentence;
+    my $stopwords = C4::Context->stopwords;
+    my $sth       =
+      $dbh->prepare(
+"insert into marc_word (bibid, tagsubfield, tagorder, subfieldorder, word, sndx_word)
+			values (?,concat(?,?),?,?,?,soundex(?))"
+    );
+    foreach my $word (@words) {
+# we record only words one char long and not in stopwords hash
+	if (length($word)>=1 and !($stopwords->{uc($word)})) {
+	    $sth->execute($bibid,$tag,$subfieldid,$tagorder,$subfieldorder,$word,$word);
+	    if ($sth->err()) {
+		warn "ERROR ==> insert into marc_word (bibid, tagsubfield, tagorder, subfieldorder, word, sndx_word) values ($bibid,concat($tag,$subfieldid),$tagorder,$subfieldorder,$word,soundex($word))\n";
+	    }
+	}
+    }
 }
 
+=head2 MARCdelword
 
+=cut
 
-sub MARCmodLCindex{
-my ($dbh,$record,$frameworkcode)=@_;
-if(!$frameworkcode){
-$frameworkcode="";
-}
-my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"biblioitems.classification",$frameworkcode);
-my ($tagfield,$tagsubfieldsub) = MARCfind_marc_from_kohafield($dbh,"biblioitems.subclass",$frameworkcode);
-my $tag=$record->field($tagfield);
-if ($tag){
-my ($lcsort)=calculatelc($tag->subfield($tagsubfield)).$tag->subfield($tagsubfieldsub);
+sub MARCdelword {
 
- &MARCkoha2marcOnefield( undef, $record, "biblioitems.lcsort", $lcsort,$frameworkcode);
+# delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
+    my ( $dbh, $bibid, $tag, $tagorder, $subfield, $subfieldorder ) = @_;
+    my $sth =
+      $dbh->prepare(
+"delete from marc_word where bibid=? and tagsubfield=concat(?,?) and tagorder=? and subfieldorder=?"
+    );
+    $sth->execute( $bibid, $tag, $subfield, $tagorder, $subfieldorder );
 }
-return $record;
+
+=head2 MARCitemchange
+
+=cut
+
+sub MARCitemchange {
+    my ($dbh,$record,$itemfield,$newvalue)=@_;
+    my ($tagfield, $tagsubfield)=MARCfind_marc_from_kohafield($dbh,$itemfield,"");
+    if (($tagfield) && ($tagsubfield))  {
+        my $tag = $record->field($tagfield);
+        if ( $tag)  {
+            $tag->update($tagsubfield => $newvalue);
+            $record->delete_field($tag);
+            $record->insert_fields_ordered($tag);
+        }
+    }
 }
 
 sub NEWmodbiblioframework {
@@ -1406,7 +1779,7 @@
 	my ($record,$bibid,$frameworkcode) =@_;
 	my $dbh = C4::Context->dbh;
 	$frameworkcode="" unless $frameworkcode;
-	&MARCmodbiblio($bibid,$record,$frameworkcode,1);
+	&MARCmodbiblio($dbh, $bibid,$record,$frameworkcode,1);
 	my $oldbiblio = MARCmarc2koha($dbh,$record,$frameworkcode);
 
 	
@@ -1481,7 +1854,7 @@
     $sth->execute( $item->{'biblionumber'} );
 my $itemtype;
     ( $item->{'biblioitemnumber'}, $itemtype ) = $sth->fetchrow;
-my $sth=$dbh->prepare("select notforloan from itemtypes where itemtype='$itemtype'");
+$sth=$dbh->prepare("select notforloan from itemtypes where itemtype='$itemtype'");
 $sth->execute();
 my $notforloan=$sth->fetchrow;
 ##Change the notforloan field if $notforloan found
@@ -1521,20 +1894,25 @@
     my $bib = &MARCadditem( $dbh, $record, $item->{'biblionumber'} );
 }
 
-sub MARCitemchange {
-my ($dbh,$record,$itemfield,$newvalue)=@_;
-    my ($tagfield, $tagsubfield)=MARCfind_marc_from_kohafield($dbh,$itemfield,"");
-    if (($tagfield) && ($tagsubfield))  {
- my $tag = $record->field($tagfield);
+=head2 ModBiblioframework
 
-        if ( $tag)  {
-	   	$tag->update($tagsubfield =>$newvalue);
-		$record->delete_field($tag);
-                $record->add_fields($tag);
-	}
+=over 4
 
-    }
+ModBiblioframework($dbh,$biblionumber,$frameworkcode);
+
+Exported function to modify a biblio framework
+
+=back
+
+=cut
+
+sub ModBiblioframework {
+	my ($dbh,$bibid,$frameworkcode) =@_;
+	my $sth = $dbh->prepare("Update biblio SET frameworkcode=? WHERE biblionumber=$bibid");
+	$sth->execute($frameworkcode);
+	return 1;
 }
+
 sub NEWmoditem {
     my ( $dbh, $record, $bibid, $itemnumber, $delete ) = @_;
 
@@ -1549,79 +1927,24 @@
     my $biblio = &MARCfind_oldbiblionumber_from_MARCbibid( $dbh, $bibid );
     &OLDdelitem( $dbh, $itemnumber );
     my $newrec=&MARCdelitem( $dbh, $bibid, $itemnumber );
-&MARCaddbiblio($newrec,$bibid,);
+&MARCaddbiblio($dbh,$newrec,$bibid,);
 }
-#
-#
-# OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
-#
-#
-
-=item $biblionumber = OLDnewbiblio($dbh,$biblio);
-
-adds a record in biblio table. Datas are in the hash $biblio.
-
-=item $biblionumber = OLDmodbiblio($dbh,$biblio);
-
-modify a record in biblio table. Datas are in the hash $biblio.
-
-=item OLDmodsubtitle($dbh,$bibnum,$subtitle);
-
-modify subtitles in bibliosubtitle table.
-
-=item OLDmodaddauthor($dbh,$bibnum,$author);
-
-adds or modify additional authors
-NOTE :  Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
-
-=item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
-
-modify/adds subjects
-
-=item OLDmodbibitem($dbh, $biblioitem);
 
-modify a biblioitem
+=head1 INTERNAL FUNCTIONS
 
-=item OLDmodnote($dbh,$bibitemnum,$note
+=head2 _koha_add_biblio
 
-modify a note for a biblioitem
-
-=item OLDnewbiblioitem($dbh,$biblioitem);
-
-adds a biblioitem ($biblioitem is a hash with the values)
-
-=item OLDnewsubject($dbh,$bibnum);
-
-adds a subject
-
-=item OLDnewsubtitle($dbh,$bibnum,$subtitle);
-
-create a new subtitle
-
-=item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
-
-create a item. $item is a hash and $barcode the barcode.
-
-=item OLDmoditem($dbh,$item);
-
-modify item
-
-=item OLDdelitem($dbh,$itemnum);
-
-delete item
-
-=item OLDdeletebiblioitem($dbh,$biblioitemnumber);
+=over 4
 
-deletes a biblioitem
-NOTE : not standard sub name. Should be OLDdelbiblioitem()
+_koha_add_biblio($dbh,$biblioitem);
 
-=item OLDdelbiblio($dbh,$biblio);
+Internal function to add a biblio ($biblio is a hash with the values)
 
-delete a biblio
+=back
 
 =cut
 
-sub OLDnewbiblio {
+sub _koha_add_biblio {
     my ( $dbh, $biblio ) = @_;
 
     #  my $dbh    = &C4Connect;
@@ -1651,18 +1974,20 @@
     return ($bibnum);
 }
 
-sub OLDmodbiblio {
-    my ( $dbh, $biblio ) = @_;
+=head2 _koha_modify_biblio
 
-    #  my $dbh   = C4Connect;
-    my $query;
-    my $sth;
+over 4
 
-    $query = "";
-    $sth   =
-      $dbh->prepare(
-"Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?, seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?"
-    );
+Internal function for updating the biblio table
+
+=back
+
+=cut
+
+sub _koha_modify_biblio {
+    my ( $dbh, $biblio ) = @_;
+	# FIXME: this code could be made more portable by not hard-coding the values that are supposed to be in biblio table
+    my $sth = $dbh->prepare("Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?, seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?");
     $sth->execute(
         $biblio->{'title'},       $biblio->{'author'},
         $biblio->{'abstract'},    $biblio->{'copyrightdate'},
@@ -1673,9 +1998,13 @@
 
     $sth->finish;
     return ( $biblio->{'biblionumber'} );
-}    # sub modbiblio
+}
+
+=head2 _koha_modify_subtitle
+
+=cut
 
-sub OLDmodsubtitle {
+sub _koha_modify_subtitle {
     my ( $dbh, $bibnum, $subtitle ) = @_;
     my $sth =
       $dbh->prepare(
@@ -1684,7 +2013,7 @@
     $sth->finish;
 }    # sub modsubtitle
 
-sub OLDmodaddauthor {
+sub _koha_modify_additionalauthor {
     my ( $dbh, $bibnum, @authors ) = @_;
 
     #    my $dbh   = C4Connect;
@@ -1707,7 +2036,7 @@
     }
 }    # sub modaddauthor
 
-sub OLDmodsubject {
+sub _koha_modify_subject {
     my ( $dbh, $bibnum, $force, @subject ) = @_;
 
     #  my $dbh   = C4Connect;
@@ -1773,9 +2102,8 @@
     return ($error);
 }    # sub modsubject
 
-sub OLDmodbibitem {
+sub _koha_modify_biblioitem {
     my ( $dbh, $biblioitem ) = @_;
-	my $dbh = C4::Context->dbh; # FIXME unused to pass $dbh n input arg.
     my $query;
 ##Recalculate LC in case it changed --TG
 
@@ -1794,42 +2122,38 @@
     $biblioitem->{'bnotes'}          = $dbh->quote( $biblioitem->{'bnotes'} );
     $biblioitem->{'size'}            = $dbh->quote( $biblioitem->{'size'} );
     $biblioitem->{'place'}           = $dbh->quote( $biblioitem->{'place'} );
+	$biblioitem->{'ccode'}       = $dbh->quote( $biblioitem->{'ccode'} );
 
-	my ($lcsort) = calculatelc($biblioitem->{'classification'}).$biblioitem->{'subclass'};
-	$lcsort = "NULL";
+#	my($lcsort) = calculatelc($biblioitem->{'classification'}).$biblioitem->{'subclass'};
 #	$lcsort=$dbh->quote($lcsort);
 
-	$query = "
-		UPDATE biblioitems SET
-		itemtype        = ".$biblioitem->{'itemtype'}.",
-		url             = ".$biblioitem->{'url'}.",
-		isbn            = ".$biblioitem->{'isbn'}.",
-		issn            = ".$biblioitem->{'issn'}.",
-		publishercode   = ".$biblioitem->{'publishercode'}.",
-		publicationyear = ".$biblioitem->{'publicationyear'}.",
-		classification  = ".$biblioitem->{'classification'}.",
-		dewey           = ".$biblioitem->{'dewey'}.",
-		subclass        = ".$biblioitem->{'subclass'}.",
-		illus           = ".$biblioitem->{'illus'}.",
-		pages           = ".$biblioitem->{'pages'}.",
-		volumeddesc     = ".$biblioitem->{'volumeddesc'}.",
-		notes 		    = ".$biblioitem->{'bnotes'}.",
-		size		    = ".$biblioitem->{'size'}.",
-		place		    = ".$biblioitem->{'place'}.",
-		lcsort	        = ".$lcsort."";
-	# where biblionumber = ".$biblioitem->{'biblionumber'}."
-	#";
 
-    my $sth = $dbh->prepare($query);
-	$sth->execute;	
+ $query = "Update biblioitems set
+itemtype        = $biblioitem->{'itemtype'},
+url             = $biblioitem->{'url'},
+isbn            = $biblioitem->{'isbn'},
+issn            = $biblioitem->{'issn'},
+publishercode   = $biblioitem->{'publishercode'},
+publicationyear = $biblioitem->{'publicationyear'},
+classification  = $biblioitem->{'classification'},
+dewey           = $biblioitem->{'dewey'},
+subclass        = $biblioitem->{'subclass'},
+illus           = $biblioitem->{'illus'},
+pages           = $biblioitem->{'pages'},
+volumeddesc     = $biblioitem->{'volumeddesc'},
+notes 		= $biblioitem->{'bnotes'},
+size		= $biblioitem->{'size'},
+place		= $biblioitem->{'place'},
+ccode		= $biblioitem->{'ccode'}
+where biblionumber = $biblioitem->{'biblionumber'}";
 
+    $dbh->do($query);
     if ( $dbh->errstr ) {
-		warn "[error]=> $query";
+		warn "$query";
+    }
     }
 
-}    # sub modbibitem
-
-sub OLDmodnote {
+sub _koha_modify_note {
     my ( $dbh, $bibitemnum, $note ) = @_;
 
     #  my $dbh=C4Connect;
@@ -1842,7 +2166,17 @@
     #  $dbh->disconnect;
 }
 
-sub OLDnewbiblioitem {
+=item
+
+=over 4
+
+Internal function to add a biblioitem
+
+=back
+
+=cut
+
+sub _koha_add_biblioitem {
     my ( $dbh, $biblioitem ) = @_;
 
     #  my $dbh   = C4Connect;
@@ -1892,7 +2226,7 @@
     return ($bibitemnum);
 }
 
-sub OLDnewsubject {
+sub _koha_add_subject {
     my ( $dbh, $bibnum ) = @_;
     my $sth =
       $dbh->prepare("insert into bibliosubject (biblionumber) values (?)");
@@ -1900,7 +2234,7 @@
     $sth->finish;
 }
 
-sub OLDnewsubtitle {
+sub _koha_add_subtitle {
     my ( $dbh, $bibnum, $subtitle ) = @_;
     my $sth =
       $dbh->prepare(
@@ -1909,7 +2243,7 @@
     $sth->finish;
 }
 
-sub OLDnewitems {
+sub _koha_new_items {
     my ( $dbh, $item, $barcode ) = @_;
 
     #  my $dbh   = C4Connect;
@@ -2001,7 +2335,7 @@
     return ( $itemnumber, $error );
 }
 
-sub OLDmoditem {
+sub _koha_modify_item {
     my ( $dbh, $item ) = @_;
     $item->{'itemnum'} = $item->{'itemnumber'} unless $item->{'itemnum'};
 
@@ -2049,10 +2383,19 @@
     #  $dbh->disconnect;
 }
 
-sub OLDdelitem {
+=head2 _koha_delete_item
+
+=over 4
+
+Internal function to delete an item record from the koha tables
+
+=back
+
+=cut
+
+sub _koha_delete_item {
     my ( $dbh, $itemnum ) = @_;
 
-    #  my $dbh=C4Connect;
     my $sth = $dbh->prepare("select * from items where itemnumber=?");
     $sth->execute($itemnum);
     my $data = $sth->fetchrow_hashref;
@@ -2076,7 +2419,7 @@
     #  $dbh->disconnect;
 }
 
-sub OLDdeletebiblioitem {
+sub _koha_delete_biblioitem {
     my ( $dbh, $biblioitemnumber ) = @_;
 
     #    my $dbh   = C4Connect;
@@ -2137,31 +2480,139 @@
     $sth->finish();
 
     #    $dbh->disconnect;
-}    # sub deletebiblioitem
+}    
+
+=item _koha_delete_biblio
+
+$error = _koha_delete_biblio($dbh,$biblionumber);
+
+Internal sub for deleting from biblio table -- also saves to deletedbiblio
+
+C<$dbh> - the database handle
+C<$biblionumber> - the biblionumber of the biblio to be deleted
+
+=cut
+# FIXME: add error handling
+
+sub _koha_delete_biblio {
+    my ( $dbh, $biblionumber ) = @_;
+
+	# get all the data for this biblio
+    my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
+    $sth->execute($biblionumber);
 
-sub OLDdelbiblio {
-    my ( $dbh, $biblio ) = @_;
-    my $sth = $dbh->prepare("select * from biblio where biblionumber=?");
-    $sth->execute($biblio);
     if ( my $data = $sth->fetchrow_hashref ) {
-        $sth->finish;
-        my $query = "Insert into deletedbiblio set ";
+		# save the record in deletedbiblio
+		# find the fields to save
+        my $query = "INSERT INTO deletedbiblio SET ";
         my @bind  = ();
         foreach my $temp ( keys %$data ) {
             $query .= "$temp = ?,";
             push ( @bind, $data->{$temp} );
         }
 
-        #replacing the last , by ",?)"
+        # replace the last , by ",?)"
         $query =~ s/\,$//;
-        $sth = $dbh->prepare($query);
-        $sth->execute(@bind);
+        my $bkup_sth = $dbh->prepare($query);
+        $bkup_sth->execute(@bind);
+        $bkup_sth->finish;
+
+		# delete the biblio
+        my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
+        $del_sth->execute($biblionumber);
+        $del_sth->finish;
+    }
         $sth->finish;
-        $sth = $dbh->prepare("Delete from biblio where biblionumber=?");
-        $sth->execute($biblio);
+	return undef;
+}
+
+=item _koha_delete_biblioitems
+
+$error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
+
+Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
+
+C<$dbh> - the database handle
+C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
+
+=cut
+# FIXME: add error handling
+
+sub _koha_delete_biblioitems {
+    my ( $dbh, $biblioitemnumber ) = @_;
+
+    # get all the data for this biblioitem
+    my $sth = $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
+    $sth->execute($biblioitemnumber);
+
+    if ( my $data = $sth->fetchrow_hashref ) {
+        # save the record in deletedbiblioitems
+        # find the fields to save
+        my $query = "INSERT INTO deletedbiblioitems SET ";
+        my @bind  = ();
+        foreach my $temp ( keys %$data ) {
+            $query .= "$temp = ?,";
+            push ( @bind, $data->{$temp} );
+        }
+
+        # replace the last , by ",?)"
+        $query =~ s/\,$//;
+        my $bkup_sth = $dbh->prepare($query);
+        $bkup_sth->execute(@bind);
+        $bkup_sth->finish;
+
+        # delete the biblioitem
+        my $del_sth = $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
+        $del_sth->execute($biblioitemnumber);
+        $del_sth->finish;
+    }
         $sth->finish;
+    return undef;
+}
+
+
+=item _koha_delete_items
+
+$error = _koha_delete_items($dbh,$itemnumber);
+
+Internal sub for deleting from items table -- also saves to deleteditems
+
+C<$dbh> - the database handle
+C<$itemnumber> - the itemnumber of the item to be deleted
+
+=cut
+# FIXME: add error handling
+
+sub _koha_delete_items {
+    my ( $dbh, $itemnumber ) = @_;
+
+    # get all the data for this item
+    my $sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
+    $sth->execute($itemnumber);
+
+    if ( my $data = $sth->fetchrow_hashref ) {
+        # save the record in deleteditems
+        # find the fields to save
+        my $query = "INSERT INTO deleteditems SET ";
+        my @bind  = ();
+        foreach my $temp ( keys %$data ) {
+            $query .= "$temp = ?,";
+            push ( @bind, $data->{$temp} );
+        }
+
+        # replace the last , by ",?)"
+        $query =~ s/\,$//;
+        my $bkup_sth = $dbh->prepare($query);
+        $bkup_sth->execute(@bind);
+        $bkup_sth->finish;
+
+        # delete the item
+        my $del_sth = $dbh->prepare("DELETE FROM items WHERE itemnumber=?");
+        $del_sth->execute($itemnumber);
+        $del_sth->finish;
     }
     $sth->finish;
+    return undef;
 }
 
 #
@@ -2185,11 +2636,11 @@
 sub newbiblio {
     my ($biblio) = @_;
     my $dbh    = C4::Context->dbh;
-    my $bibnum = OLDnewbiblio( $dbh, $biblio );
+    my $bibnum = _koha_add_biblio( $dbh, $biblio );
     # finds new (MARC bibid
     # 	my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
     my $record = &MARCkoha2marcBiblio( $dbh, $bibnum );
-    MARCaddbiblio($record, $bibnum,'' );
+    MARCaddbiblio($dbh,$record, $bibnum,'' );
     return ($bibnum);
 }
 
@@ -2218,7 +2669,7 @@
 	my $record = MARCkoha2marcBiblio($dbh,$biblionumber,$biblionumber);
 	# finds new (MARC bibid
 	my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
-	MARCmodbiblio($bibid,$record,"",0);
+	MARCmodbiblio($dbh,$bibid,$record,"",0);
 	return($biblionumber);
 } # sub modbiblio
 
@@ -2237,7 +2688,7 @@
 sub modsubtitle {
     my ( $bibnum, $subtitle ) = @_;
     my $dbh = C4::Context->dbh;
-    &OLDmodsubtitle( $dbh, $bibnum, $subtitle );
+    &_koha_modify_subtitle( $dbh, $bibnum, $subtitle );
 }    # sub modsubtitle
 
 =item modaddauthor
@@ -2253,7 +2704,7 @@
 sub modaddauthor {
     my ( $bibnum, @authors ) = @_;
     my $dbh = C4::Context->dbh;
-    &OLDmodaddauthor( $dbh, $bibnum, @authors );
+    &_koha_modify_additionalauthor( $dbh, $bibnum, @authors );
 }    # sub modaddauthor
 
 =item modsubject
@@ -2269,7 +2720,7 @@
 sub modsubject {
     my ( $bibnum, $force, @subject ) = @_;
     my $dbh = C4::Context->dbh;
-    my $error = &OLDmodsubject( $dbh, $bibnum, $force, @subject );
+    my $error = &_koha_modify_subject( $dbh, $bibnum, $force, @subject );
     if ($error eq ''){
 		# When MARC is off, ensures that the MARC biblio table gets updated with new
 		# subjects, of course, it deletes the biblio in marc, and then recreates.
@@ -2277,7 +2728,7 @@
 		if (C4::Context->preference("MARC") eq '0'){
 			my $MARCRecord = &MARCkoha2marcBiblio($dbh,$bibnum);
 			my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
-			&MARCmodbiblio($bibid, $MARCRecord);
+			&MARCmodbiblio($dbh,$bibid, $MARCRecord);
 		}
 	}
 	return ($error);
@@ -2292,30 +2743,32 @@
 sub modnote {
     my ( $bibitemnum, $note ) = @_;
     my $dbh = C4::Context->dbh;
-    &OLDmodnote( $dbh, $bibitemnum, $note );
+    &_koha_modify_note( $dbh, $bibitemnum, $note );
 }
 
 sub newbiblioitem {
     my ($biblioitem) = @_;
     my $dbh        = C4::Context->dbh;
-    my $bibitemnum = &OLDnewbiblioitem( $dbh, $biblioitem );
+    my $bibitemnum = &_koha_add_biblioitem( $dbh, $biblioitem );
+
     my $MARCbiblio =
       MARCkoha2marcBiblio( $dbh, 0, $bibitemnum )
       ; # the 0 means "do NOT retrieve biblio, only biblioitem, in the MARC record
-    &MARCaddbiblio($MARCbiblio, $biblioitem->{biblionumber}, '' );
+
+    &MARCaddbiblio( $dbh, $MARCbiblio, $biblioitem->{biblionumber}, '' );
     return ($bibitemnum);
 }
 
 sub newsubject {
     my ($bibnum) = @_;
     my $dbh = C4::Context->dbh;
-    &OLDnewsubject( $dbh, $bibnum );
+    &_koha_add_subject( $dbh, $bibnum );
 }
 
 sub newsubtitle {
     my ( $bibnum, $subtitle ) = @_;
     my $dbh = C4::Context->dbh;
-    &OLDnewsubtitle( $dbh, $bibnum, $subtitle );
+    &_koha_add_subtitle( $dbh, $bibnum, $subtitle );
 }
 
 sub newitems {
@@ -2325,10 +2778,9 @@
     my $itemnumber;
     my $error;
     foreach my $barcode (@barcodes) {
-        ( $itemnumber, $error ) = &OLDnewitems( $dbh, $item, uc($barcode) );
+        ( $itemnumber, $error ) = &_koha_new_items( $dbh, $item, uc($barcode) );
         $errors .= $error;
-        my $MARCitem =
-          &MARCkoha2marcItem( $dbh, $item->{biblionumber}, $itemnumber );
+        my $MARCitem = &MARCkoha2marcItem( $dbh, $item->{biblionumber}, $itemnumber );
         &MARCadditem( $dbh, $MARCitem, $item->{biblionumber} );
     }
     return ($errors);
@@ -2337,7 +2789,7 @@
 sub moditem {
     my ($item) = @_;
     my $dbh = C4::Context->dbh;
-    &OLDmoditem( $dbh, $item );
+    &_koha_modify_item( $dbh, $item );
     my $MARCitem =
       &MARCkoha2marcItem( $dbh, $item->{'biblionumber'}, $item->{'itemnum'} );
     my $bibid =
@@ -3169,35 +3621,44 @@
 	} 
 	closedir DIR;
 	my $filename = $zebradir.$biblionumber;
+
 if ($record){
 	open (OUTPUT,">", $filename.".xml");
 	print OUTPUT $record;
-
 	close OUTPUT;
 }
+}
+=item zebraop
 
+=over 4
 
-}
 
 
+=back
 
+=cut
 
 sub zebraop{
 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
 	my ($dbh,$biblionumber,$op,$server) = @_;
-	my $dbh = C4::Context->dbh;
+	#warn "SERVER:".$server;
 	my @Zconnbiblio;
 	my $tried=0;
 	my $recon=0;
 	my $reconnect=0;
 	my $record;
 	my $shadow;
-	warn "Server is: ".$server;
+
 reconnect:
-	$Zconnbiblio[0]=C4::Context->Zconnauth($server);
+	$Zconnbiblio[0]=C4::Context->Zconn($server,0,1);
+
 	if ($server eq "biblioserver"){
-		$record =XMLgetbiblio($dbh,$biblionumber);
-#         warn "record: $record";
+		# it's unclear to me whether this should be in xml or MARC format
+		# but it is clear it should be nabbed from zebra rather than from
+		# the koha tables
+		$record=MARCgetbiblio($dbh,$biblionumber);
+		$record = $record->as_xml_record() if $record;
+		#warn "RECORD".$record->as_usmarc();
 		$shadow="biblioservershadow";
 	}elsif($server eq "authorityserver"){
 		$record =C4::AuthoritiesMarc::XMLgetauthority($dbh,$biblionumber);
@@ -3218,16 +3679,18 @@
 	}
 	my($error, $errmsg, $addinfo, $diagset) = $Zconnbiblio[0]->error_x();
 	if ($error==10000 && $reconnect==0) { ## This is serious ZEBRA server is not available -reconnect
+		warn "problem with zebra server connection";
 		$reconnect=1;
 		my $res=system('sc start "Z39.50 Server" >c:/zebraserver/error.log');
-		warn "Trying to restart ZEBRA Server";
-		goto "reconnect";
+		#warn "Trying to restart ZEBRA Server";
+		#goto "reconnect";
    	 }elsif ($error==10007 && $tried<2) {## timeout --another 30 looonng seconds for this update
 		$tried=$tried+1;
 		goto "retry";
 	}elsif($error==10004 && $recon==0){##Lost connection -reconnect
 		$recon=1;
 		goto "reconnect";
+	# as a last resort, we save the data to the filesystem to be indexed in batch
 	}elsif ($error){
 		warn "Error-$server   $op $biblionumber /errcode:, $error, /MSG:,$errmsg,$addinfo \n";	
 		$Zpackage->destroy();
@@ -3267,7 +3730,10 @@
 }#while
 
 my $other=length($lc1);
-if(!$lc1){$other==0;}
+if (!$lc1) {
+	$other=0;
+}
+
 my $extras;
 if ($other<4){
 	for (1..(4-$other)){
@@ -3307,6 +3773,24 @@
 
 }
 
+=item getRecord
+
+get a single record in piggyback mode from Zebra and return it in the requested record syntax
+
+default record syntax is XML
+
+=cut
+
+sub getRecord {
+    my ($server,$koha_query,$recordSyntax) = @_;
+	$recordSyntax = "xml" unless $recordSyntax;
+    my $Zconn = C4::Context->Zconn($server,0,1,1,$recordSyntax);
+    my $rs = $Zconn->search(new ZOOM::Query::CCL2RPN($koha_query,$Zconn));
+	if ($rs->record(0)) {
+    	return $rs->record(0)->raw();
+	}
+}
+
 sub GetItemInfosOf {
     my @itemnumbers = @_;
 
@@ -3343,10 +3827,15 @@
 
 Paul POULAIN paul.poulain at free.fr
 
+Joshua Ferraro jmf at liblime.com
+
 =cut
 
-# $Id: Biblio.pm,v 1.178.2.10 2006/10/02 09:32:02 hdl Exp $
+# $Id: Biblio.pm,v 1.178.2.11 2006/10/06 13:23:49 toins Exp $
 # $Log: Biblio.pm,v $
+# Revision 1.178.2.11  2006/10/06 13:23:49  toins
+# Synch with dev_week.
+#
 # Revision 1.178.2.10  2006/10/02 09:32:02  hdl
 # Adding GetItemStatus and GetItemLocation function in order to make serials-receive.pl work.
 #
@@ -3422,7 +3911,7 @@
 # (Not all items fields mapped to MARC)
 #
 # Revision 1.115.2.17  2005/08/01 15:15:43  tipaul
-# adding decoder for Ä string
+# adding decoder for Ä string
 #
 # Revision 1.115.2.16  2005/07/28 19:56:15  tipaul
 # * removing a useless & CPU consuming call to MARCgetbiblio
@@ -3435,7 +3924,7 @@
 # Revision 1.115.2.15  2005/07/19 15:25:40  tipaul
 # * fixing a bug in subfield order when MARCgetbiblio
 # * getting rid with the limit "biblionumber & biblioitemnumber must be in the same tag". So, we can put biblionumber in 001 (field that has no subfields, so we can't put biblioitemnumber in this field), and use biblionumber as identifier in the MARC biblio too. Still to be deeply tested.
-# * adding some diacritic decoding (Ä, Ü...)
+# * adding some diacritic decoding (Ä, Ü...)
 #
 # Revision 1.115.2.14  2005/06/27 23:24:06  hdl
 # Display dashed ISBN

Index: C4/Serials.pm
===================================================================
RCS file: /sources/koha/koha/C4/Serials.pm,v
retrieving revision 1.5.2.2
retrieving revision 1.5.2.3
diff -u -b -r1.5.2.2 -r1.5.2.3
--- C4/Serials.pm	2 Oct 2006 09:15:44 -0000	1.5.2.2
+++ C4/Serials.pm	6 Oct 2006 13:23:49 -0000	1.5.2.3
@@ -17,7 +17,7 @@
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
-# $Id: Serials.pm,v 1.5.2.2 2006/10/02 09:15:44 hdl Exp $
+# $Id: Serials.pm,v 1.5.2.3 2006/10/06 13:23:49 toins Exp $
 
 use strict;
 use C4::Date;
@@ -31,7 +31,7 @@
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
 # set the version for version checking
-$VERSION = do { my @v = '$Revision: 1.5.2.2 $' =~ /\d+/g;
+$VERSION = do { my @v = '$Revision: 1.5.2.3 $' =~ /\d+/g;
         shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
 
 
@@ -1337,7 +1337,7 @@
 		    $marcrecord->insert_fields_ordered($newField);
 		}
 	    }
-	    NEWnewitem($dbh,$marcrecord,$bibid);
+	    AddItem($dbh,$marcrecord,$bibid);
             return 1;
         }
         return (0, at errors);

Index: cataloguing/addbiblio.pl
===================================================================
RCS file: /sources/koha/koha/cataloguing/addbiblio.pl,v
retrieving revision 1.14.2.4
retrieving revision 1.14.2.5
diff -u -b -r1.14.2.4 -r1.14.2.5
--- cataloguing/addbiblio.pl	26 Sep 2006 08:26:29 -0000	1.14.2.4
+++ cataloguing/addbiblio.pl	6 Oct 2006 13:23:49 -0000	1.14.2.5
@@ -1,6 +1,6 @@
 #!/usr/bin/perl 
 
-# $Id: addbiblio.pl,v 1.14.2.4 2006/09/26 08:26:29 hdl Exp $
+# $Id: addbiblio.pl,v 1.14.2.5 2006/10/06 13:23:49 toins Exp $
 
 # Copyright 2000-2002 Katipo Communications
 #
@@ -31,7 +31,11 @@
 use C4::Log;
 use C4::Koha; # XXX subfield_is_koha_internal_p
 use HTML::Template;
-
+use MARC::File::USMARC;
+use MARC::File::XML;
+if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
+	MARC::File::XML->default_record_format( 'UNIMARC' );
+}
 use vars qw( $tagslib);
 use vars qw( $authorised_values_sth);
 use vars qw( $is_a_modif );
@@ -405,9 +409,9 @@
 my $input = new CGI;
 my $error = $input->param('error');
 my $biblionumber=$input->param('biblionumber'); # if biblionumber exists, it's a modif, not a new biblio.
+my $oldbiblionumber;
 if (!$biblionumber){
-    $biblionumber=$input->param('oldbiblionumber');
-	warn "OLDBIBLIONUMBER".$biblionumber;
+    $oldbiblionumber=$input->param('oldbiblionumber');
     }
 my $breedingid = $input->param('breedingid');
 my $z3950 = $input->param('z3950');
@@ -415,7 +419,16 @@
 my $frameworkcode = $input->param('frameworkcode');
 my $dbh = C4::Context->dbh;
 
-$frameworkcode = &MARCfind_frameworkcode($dbh,$biblionumber) if ($biblionumber and not ($frameworkcode));
+
+if ($oldbiblionumber) {
+	$biblionumber = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
+	# find framework type
+	$frameworkcode = &MARCfind_frameworkcode($dbh,$biblionumber) if ($biblionumber and not ($frameworkcode));
+}
+else {
+	$biblionumber = $input->param('bibid');
+	$frameworkcode = &MARCfind_frameworkcode($dbh,$biblionumber) if ($biblionumber and not ($frameworkcode));
+}
 $frameworkcode='' if ($frameworkcode eq 'Default');
 my ($template, $loggedinuser, $cookie)
     = get_template_and_user({template_name => "cataloguing/addbiblio.tmpl",
@@ -439,7 +452,8 @@
 	$select_fwk{$fwk} = $description;
 }
 $curfwk=$frameworkcode;
-my $framework=CGI::scrolling_list( -name     => 'Frameworks',
+my $framework=CGI::scrolling_list( 
+			-name     => 'Frameworks',
 			-id => 'Frameworks',
 			-default => $curfwk,
 			-OnChange => 'Changefwk(this);',
@@ -453,7 +467,7 @@
 my $record=-1;
 my $encoding="";
 #$record = MARCgetbiblio($dbh,$biblionumber) if ($biblionumber);
-my $dbh = C4::Context->dbh;
+$dbh = C4::Context->dbh;
 $record=MARCgetbiblio($dbh,$biblionumber) if ($biblionumber);
 # warn "record :".$record->as_formatted;
 ($record,$encoding) = MARCfindbreeding($dbh,$breedingid) if ($breedingid);
@@ -490,14 +504,14 @@
 	# it is not a duplicate (determined either by Koha itself or by user checking it's not a duplicate)
 	if (!$duplicatebiblionumber or $confirm_not_duplicate) {
 		# MARC::Record built => now, record in DB
+		my $oldbibnum;
+		my $oldbibitemnum;
 		if ($is_a_modif) {
-		warn "CONFIRM ITS A MODIF :".$biblionumber;
-			NEWmodbiblioframework($biblionumber,$frameworkcode);
-			NEWmodbiblio($record,$biblionumber,$frameworkcode);
-		} else {
-			my $biblioitemnumber;
-			($biblionumber,$biblioitemnumber) = NEWnewbiblio($dbh,$record,$frameworkcode);
-			logaction($loggedinuser,"acqui.simple","add",$biblionumber,"record : ".$record->as_formatted) if (C4::Context->preference("Activate_Log"));
+			ModBiblioframework($dbh,$biblionumber,$frameworkcode);
+			ModBiblio($dbh,$record,$biblionumber,$frameworkcode);
+		} 
+		else {
+			($biblionumber,$oldbibnum,$oldbibitemnum) = AddBiblio($dbh,$record,$frameworkcode);
 		}
 	# now, redirect to additem page
 		print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
@@ -555,10 +569,13 @@
 		biblioitemnumber         => $biblioitemnumber );
 } elsif ($op eq "delete") {
 #------------------------------------------------------------------------------------------------------------------------------
-	&NEWdelbiblio($dbh,$biblionumber);
-	logaction($loggedinuser,"acqui.simple","del",$biblionumber,"") if (logstatus);
-	
+	my $error = &DelBiblio($dbh,$biblionumber);
+	if ($error != 1) {
+		print "Content-Type: text/html\n\n$error";
+	}
+	else {
 	print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=/cgi-bin/koha/search.marc/search.pl?type=intranet\"></html>";
+	}
 	exit;
 #------------------------------------------------------------------------------------------------------------------------------
 #------------------------------------------------------------------------------------------------------------------------------
@@ -593,5 +610,9 @@
 		frameworkcode => $frameworkcode,
 		itemtype => $frameworkcode, # HINT: if the library has itemtype = framework, itemtype is auto filled !
 		hide_marc => C4::Context->preference('hide_marc'),
+		intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
+		intranetstylesheet => C4::Context->preference("intranetstylesheet"),
+		IntranetNav => C4::Context->preference("IntranetNav"),
+		advancedMARCEditor => C4::Context->preference("advancedMARCEditor"),
 		);
 output_html_with_http_headers $input, $cookie, $template->output;

Index: cataloguing/additem.pl
===================================================================
RCS file: /sources/koha/koha/cataloguing/additem.pl,v
retrieving revision 1.7.2.4
retrieving revision 1.7.2.5
diff -u -b -r1.7.2.4 -r1.7.2.5
--- cataloguing/additem.pl	25 Sep 2006 14:56:31 -0000	1.7.2.4
+++ cataloguing/additem.pl	6 Oct 2006 13:23:49 -0000	1.7.2.5
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 
-# $Id: additem.pl,v 1.7.2.4 2006/09/25 14:56:31 hdl Exp $
+# $Id: additem.pl,v 1.7.2.5 2006/10/06 13:23:49 toins Exp $
 
 # Copyright 2000-2002 Katipo Communications
 #
@@ -30,7 +30,6 @@
 use C4::Search;
 use HTML::Template;
 use MARC::File::XML;
-use Data::Dumper;
 
 sub find_value {
 	my ($tagfield,$insubfield,$record) = @_;
@@ -47,14 +46,7 @@
 	}
 	return($indicator,$result);
 }
-sub find_biblioitemnumber {
-	my ($dbh,$bibnum) = @_;
-	my $result;
-    my $rq=$dbh->prepare("SELECT biblioitemnumber from biblioitems where biblionumber=?");
-    $rq->execute($bibnum);
-    my ($result)=$rq->fetchrow;
-	return($result);
-}
+
 sub get_item_from_barcode {
     my ($barcode)=@_;
     my $dbh=C4::Context->dbh;
@@ -64,6 +56,7 @@
     my ($result)=$rq->fetchrow;
 	return($result);
 }
+
 my $input = new CGI;
 my $dbh = C4::Context->dbh;
 my $error = $input->param('error');
@@ -121,8 +114,13 @@
 	my $exists = get_item_from_barcode($addedolditem->{'barcode'});
 	push @errors,"barcode_not_unique" if($exists);
 	# if barcode exists, don't create, but report The problem.
-	$itemnumber = NEWnewitem($dbh,$record,$biblionumber,$biblioitemnumber) unless ($exists);
+	my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItem($dbh,$record,$biblioitemnumber) unless ($exists);
+	if ($exists) {
+	$nextop = "additem";
+		$itemrecord = $record;
+	} else {
 	$nextop = "additem";
+	}
 #------------------------------------------------------------------------------------------------------------------------------
 } elsif ($op eq "edititem") {
 #------------------------------------------------------------------------------------------------------------------------------
@@ -133,7 +131,7 @@
 } elsif ($op eq "delitem") {
 #------------------------------------------------------------------------------------------------------------------------------
 # retrieve item if exist => then, it's a modif
-	&NEWdelitem($dbh,$biblionumber,$itemnumber);
+	&DelItem($dbh,$biblionumber,$itemnumber);
 	$nextop="additem";
 #------------------------------------------------------------------------------------------------------------------------------
 } elsif ($op eq "saveitem") {
@@ -150,7 +148,7 @@
         my $itemrecord=MARC::Record::new_from_xml($xml, 'UTF-8');
 # MARC::Record builded => now, record in DB
 # warn "R: ".$record->as_formatted;
-	my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$itemrecord,$biblionumber,$itemnumber,0);
+	my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItem($dbh,$itemrecord,$biblionumber,$itemnumber,0);
 	$itemnumber="";
 	$nextop="additem";
 }
@@ -220,6 +218,7 @@
 		$items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
 	}
 	my %row_data;
+	$items_data =~ s/"/&quot;/g;
 	$row_data{item_value} = $items_data;
 	$row_data{itemnum} = $big_array[$i]->{itemnum};
 	#reporting this_row values
@@ -250,8 +249,10 @@
 		$subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
 		$subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
 		$subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
+		$subfield_data{hidden}= "display:none" if $tagslib->{$tag}->{$subfield}->{hidden};
 		my ($x,$value);
 		($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
+		$value =~ s/"/&quot;/g;
 		#testing branch value if IndependantBranches.
 		my $test = (C4::Context->preference("IndependantBranches")) && 
 					($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
@@ -312,6 +313,7 @@
 																		-default=>"$value",
 																		-labels => \%authorised_lib,
 																		-size=>1,
+ 																		-tabindex=>'',
 																		-multiple=>0,
 																		);
 		} elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
@@ -322,7 +324,7 @@
 			require $plugin;
 			my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
 			my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
-			$subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255 DISABLE READONLY OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
+			$subfield_data{marc_value}="<input type=\"text\" value=\"$value\" name=\"field_value\"  size=47 maxlength=255 DISABLE READONLY OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
 		} else {
 			$subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
 		}
@@ -333,6 +335,7 @@
 }
 
 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
+$template->param( title => $record->title() ) if ($record ne "-1");
 $template->param(item_loop => \@item_value_loop,
 						item_header_loop => \@header_value_loop,
 						biblionumber => $biblionumber,

Index: cataloguing/isbnsearch.pl
===================================================================
RCS file: /sources/koha/koha/cataloguing/isbnsearch.pl,v
retrieving revision 1.3
retrieving revision 1.3.2.1
diff -u -b -r1.3 -r1.3.2.1
--- cataloguing/isbnsearch.pl	10 Aug 2006 13:08:28 -0000	1.3
+++ cataloguing/isbnsearch.pl	6 Oct 2006 13:23:49 -0000	1.3.2.1
@@ -86,8 +86,6 @@
 	my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
 										\@excluding, \@operator, \@value,
 										$startfrom*$resultsperpage, $resultsperpage,'biblio.title','ASC');
-	warn "NB::::".$total;
-
 # 	@results = @$resultsref;
 
 #     my @loop_data = ();

Index: misc/rebuildnonmarc.pl
===================================================================
RCS file: /sources/koha/koha/misc/rebuildnonmarc.pl,v
retrieving revision 1.5
retrieving revision 1.5.2.1
diff -u -b -r1.5 -r1.5.2.1
--- misc/rebuildnonmarc.pl	1 Mar 2005 13:41:27 -0000	1.5
+++ misc/rebuildnonmarc.pl	6 Oct 2006 13:23:49 -0000	1.5.2.1
@@ -90,8 +90,8 @@
 	$frameworkcode="" unless $frameworkcode;
 # 	&MARCmodbiblio($dbh,$bibid,$record,$frameworkcode,0);
 	my $oldbiblio = MARCmarc2koha($dbh,$record,$frameworkcode);
-	my $oldbiblionumber = C4::Biblio::OLDmodbiblio($dbh,$oldbiblio);
-	C4::Biblio::OLDmodbibitem($dbh,$oldbiblio);
+	my $oldbiblionumber = C4::Biblio::_koha_modify_biblio($dbh,$oldbiblio);
+	C4::Biblio::_koha_modify_biblioitem($dbh,$oldbiblio);
 	# now, modify addi authors, subject, addititles.
 	my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author",$frameworkcode);
 	my @addiauthfields = $record->field($tagfield);
@@ -99,7 +99,7 @@
 	foreach my $addiauthfield (@addiauthfields) {
 		my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
 		foreach my $subfieldcount (0..$#addiauthsubfields) {
-			C4::Biblio::OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
+			C4::Biblio::_koha_modify_additionalauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
 		}
 	}
 	($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle",$frameworkcode);
@@ -107,7 +107,7 @@
 	foreach my $subtitlefield (@subtitlefields) {
 		my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
 		foreach my $subfieldcount (0..$#subtitlesubfields) {
-			C4::Biblio::OLDnewsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
+			C4::Biblio::_koha_add_subtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
 		}
 	}
 	($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject",$frameworkcode);
@@ -119,7 +119,7 @@
 			push @subjects,$subjsubfield[$subfieldcount];
 		}
 	}
-	C4::Biblio::OLDmodsubject($dbh,$oldbiblionumber,1, at subjects);
+	C4::Biblio::_koha_modify_subject($dbh,$oldbiblionumber,1, at subjects);
 	return 1;
 }
 
@@ -129,5 +129,5 @@
 #     &MARCmoditem( $dbh, $record, $bibid, $itemnumber, $delete );
 	my $frameworkcode=MARCfind_frameworkcode($dbh,$bibid);
     my $olditem = MARCmarc2koha( $dbh, $record,$frameworkcode );
-    C4::Biblio::OLDmoditem( $dbh, $olditem );
+    C4::Biblio::_koha_modify_item( $dbh, $olditem );
 }





More information about the Koha-cvs mailing list