fix for bug 1499 - normalize ISBN before dupe check
[koha.git] / C4 / Biblio.pm
index 5faa7e4..11df070 100644 (file)
@@ -20,7 +20,7 @@ package C4::Biblio;
 use strict;
 
 require Exporter;
-use utf8;
+use utf8;
 use C4::Context;
 use MARC::Record;
 use MARC::File::USMARC;
@@ -65,8 +65,11 @@ push @EXPORT, qw(
   &GetMarcBiblio
   &GetMarcAuthors
   &GetMarcSeries
+  GetMarcUrls
+  &GetUsedMarcStructure
 
   &GetItemsInfo
+  &GetItemsByBiblioitemnumber
   &GetItemnumberFromBarcode
   &get_itemnumbers_of
   &GetXmlBiblio
@@ -75,6 +78,7 @@ push @EXPORT, qw(
   &GetMarcStructure
   &GetMarcFromKohaField
   &GetFrameworkCode
+  &GetPublisherNameFromIsbn
   &TransformKohaToMarc
 );
 
@@ -110,6 +114,7 @@ push @EXPORT, qw(
 # Others functions
 push @EXPORT, qw(
   &TransformMarcToKoha
+  &TransformHtmlToMarc2
   &TransformHtmlToMarc
   &TransformHtmlToXml
   &PrepareItemrecordDisplay
@@ -280,10 +285,10 @@ sub AddBiblio {
 
 =head2 AddItem
 
-=over
+=over 2
 
-$biblionumber = AddItem( $record, $biblionumber)
-Exported function (core API) for adding a new item to Koha
+    $biblionumber = AddItem( $record, $biblionumber)
+    Exported function (core API) for adding a new item to Koha
 
 =back
 
@@ -332,8 +337,7 @@ sub AddItem {
       &_koha_new_items( $dbh, $item, $item->{barcode} );
 
     # add itemnumber to MARC::Record before adding the item.
-    $sth =
-      $dbh->prepare(
+    $sth = $dbh->prepare(
 "select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?"
       );
     &TransformKohaToMarcOneField( $sth, $record, "items.itemnumber", $itemnumber,
@@ -350,21 +354,16 @@ sub AddItem {
 
 =head2 ModBiblio
 
-=over
-
-ModBiblio( $record,$biblionumber,$frameworkcode);
-Exported function (core API) to modify a biblio
-
-=back
+    ModBiblio( $record,$biblionumber,$frameworkcode);
+    Exported function (core API) to modify a biblio
 
 =cut
 
 sub ModBiblio {
     my ( $record, $biblionumber, $frameworkcode ) = @_;
-    
-    if (C4::Context->preference("CataloguingLog")) {    
+    if (C4::Context->preference("CataloguingLog")) {
         my $newrecord = GetMarcBiblio($biblionumber);
-        &logaction(C4::Context->userenv->{'number'},"CATALOGUING","MODIFY",$biblionumber,$newrecord->as_formatted) 
+        &logaction(C4::Context->userenv->{'number'},"CATALOGUING","MODIFY",$biblionumber,"BEFORE=>".$newrecord->as_formatted);
     }
     
     my $dbh = C4::Context->dbh;
@@ -373,26 +372,49 @@ sub ModBiblio {
 
     # get the items before and append them to the biblio before updating the record, atm we just have the biblio
     my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
-    my $oldRecord = GetMarcBiblio($biblionumber);
-    my @fields = $oldRecord->field($itemtag);
-    $record->append_fields(@fields);
-
+    my $oldRecord = GetMarcBiblio( $biblionumber );
+    
+    # parse each item, and, for an unknown reason, re-encode each subfield 
+    # if you don't do that, the record will have encoding mixed
+    # and the biblio will be re-encoded.
+    # strange, I (Paul P.) searched more than 1 day to understand what happends
+    # but could only solve the problem this way...
+   my @fields = $oldRecord->field( $itemtag );
+    foreach my $fielditem ( @fields ){
+        my $field;
+        foreach ($fielditem->subfields()) {
+            if ($field) {
+                $field->add_subfields(Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
+            } else {
+                $field = MARC::Field->new("$itemtag",'','',Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
+            }
+          }
+        $record->append_fields($field);
+    }
+    
+    # adding biblionumber
+    my ($tag_biblionumber, $subfield_biblionumber) = GetMarcFromKohaField('biblio.biblionumber',$frameworkcode);
+    $record->append_fields(
+           MARC::Field->new(
+                   $tag_biblionumber,'','',$subfield_biblionumber => $biblionumber
+           )
+    ) unless ($record->subfield($tag_biblionumber,$subfield_biblionumber));
+    
     # update the MARC record (that now contains biblio and items) with the new record data
-    &ModBiblioMarc($record, $biblionumber, $frameworkcode );
-
+    &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
+    
     # load the koha-table data object
     my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
 
     # modify the other koha tables
-    $biblionumber = _koha_modify_biblio( $dbh, $oldbiblio );
+    _koha_modify_biblio( $dbh, $oldbiblio );
     _koha_modify_biblioitem( $dbh, $oldbiblio );
-
     return 1;
 }
 
 =head2 ModItem
 
-=over
+=over 2
 
 Exported function (core API) for modifying an item in Koha.
 
@@ -454,38 +476,19 @@ sub ModItemTransfer {
     return;
 }
 
-##New sub to dotransfer in marc tables as well. Not exported -TG 10/04/2006
-# sub domarctransfer {
-#     my ( $dbh, $itemnumber ) = @_;
-#     $itemnumber =~ s /\'//g;    ##itemnumber seems to come with quotes-TG
-#     my $sth =
-#       $dbh->prepare(
-#         "select biblionumber,holdingbranch from items where itemnumber=$itemnumber"
-#       );
-#     $sth->execute();
-#     while ( my ( $biblionumber, $holdingbranch ) = $sth->fetchrow ) {
-#         &ModItemInMarconefield( $biblionumber, $itemnumber,
-#             'items.holdingbranch', $holdingbranch );
-#     }
-#     return;
-# }
 =head2 ModBiblioframework
 
-=over
-
-ModBiblioframework($biblionumber,$frameworkcode);
-Exported function to modify a biblio framework
-
-=back
+    ModBiblioframework($biblionumber,$frameworkcode);
+    Exported function to modify a biblio framework
 
 =cut
 
 sub ModBiblioframework {
     my ( $biblionumber, $frameworkcode ) = @_;
     my $dbh = C4::Context->dbh;
-    my $sth =
-      $dbh->prepare(
-        "UPDATE biblio SET frameworkcode=? WHERE biblionumber=$biblionumber");
+    my $sth = $dbh->prepare(
+        "UPDATE biblio SET frameworkcode=? WHERE biblionumber=$biblionumber"
+    );
     $sth->execute($frameworkcode);
     return 1;
 }
@@ -592,26 +595,15 @@ sub DelBiblio {
     my ( $biblionumber ) = @_;
     my $dbh = C4::Context->dbh;
     my $error;    # for error handling
+       
+       # First make sure this biblio has no items attached
+       my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
+       $sth->execute($biblionumber);
+       if (my $itemnumber = $sth->fetchrow){
+               # Fix this to use a status the template can understand
+               $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
+       }
 
-    # First make sure there are no items with issues are still attached
-    my $sth =
-      $dbh->prepare(
-        "SELECT itemnumber FROM items WHERE biblionumber=?");
-    $sth->execute($biblionumber);
-    while ( my $itemnumber = $sth->fetchrow ) {
-        my $issues = GetItemIssues($itemnumber);
-        foreach my $issue (@$issues) {
-            if (   ( $issue->{date_due} )
-                && ( $issue->{date_due} ne "Available" ) )
-            {
-
-#FIXME: we need a status system in Biblio like in Circ to return standard codes and messages
-# instead of hard-coded strings
-                $error .=
-"Item is checked out to a patron -- you must return it before deleting the Biblio";
-            }
-        }
-    }
     return $error if $error;
 
     # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
@@ -634,16 +626,6 @@ sub DelBiblio {
         # delete this biblioitem
         $error = &_koha_delete_biblioitems( $dbh, $biblioitemnumber );
         return $error if $error;
-
-        # delete items
-        my $items_sth =
-          $dbh->prepare(
-            "SELECT itemnumber FROM items WHERE biblioitemnumber=?");
-        $items_sth->execute($biblioitemnumber);
-        while ( my $itemnumber = $items_sth->fetchrow ) {
-            $error = &_koha_delete_item( $dbh, $itemnumber );
-            return $error if $error;
-        }
     }
     &logaction(C4::Context->userenv->{'number'},"CATALOGUING","DELETE",$biblionumber,"") 
         if C4::Context->preference("CataloguingLog");
@@ -664,6 +646,10 @@ Exported function (core API) for deleting an item record in Koha.
 sub DelItem {
     my ( $biblionumber, $itemnumber ) = @_;
     my $dbh = C4::Context->dbh;
+       
+       # check the item has no current issues
+       
+       
     &_koha_delete_item( $dbh, $itemnumber );
     # get the MARC record
     my $record = GetMarcBiblio($biblionumber);
@@ -711,7 +697,7 @@ sub GetBiblioData {
     my $dbh = C4::Context->dbh;
 
     my $query = "
-        SELECT * , biblioitems.notes AS bnotes, biblio.notes
+        SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
         FROM biblio
             LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
             LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
@@ -782,11 +768,11 @@ sub GetItemsInfo {
     my ( $biblionumber, $type ) = @_;
     my $dbh   = C4::Context->dbh;
     my $query = "SELECT *,items.notforloan as itemnotforloan
-                 FROM items, biblio, biblioitems
+                 FROM items 
+                 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
+                 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
                  LEFT JOIN itemtypes on biblioitems.itemtype = itemtypes.itemtype
                 WHERE items.biblionumber = ?
-                    AND biblioitems.biblioitemnumber = items.biblioitemnumber
-                    AND biblio.biblionumber = items.biblionumber
                 ORDER BY items.dateaccessioned desc
                  ";
     my $sth = $dbh->prepare($query);
@@ -798,16 +784,17 @@ sub GetItemsInfo {
     while ( my $data = $sth->fetchrow_hashref ) {
         my $datedue = '';
         my $isth    = $dbh->prepare(
-            "SELECT issues.*,borrowers.cardnumber
-            FROM   issues, borrowers
+            "SELECT issues.*,borrowers.cardnumber,borrowers.surname,borrowers.firstname
+            FROM   issues LEFT JOIN borrowers ON issues.borrowernumber=borrowers.borrowernumber
             WHERE  itemnumber = ?
-                AND returndate IS NULL
-                AND issues.borrowernumber=borrowers.borrowernumber"
+                AND returndate IS NULL"
         );
         $isth->execute( $data->{'itemnumber'} );
         if ( my $idata = $isth->fetchrow_hashref ) {
             $data->{borrowernumber} = $idata->{borrowernumber};
             $data->{cardnumber}     = $idata->{cardnumber};
+            $data->{surname}     = $idata->{surname};
+            $data->{firstname}     = $idata->{firstname};
             $datedue                = format_date( $idata->{'date_due'} );
         }
         if ( $datedue eq '' ) {
@@ -1174,18 +1161,18 @@ that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
 
 #'
 sub GetBiblioItemData {
-    my ($bibitem) = @_;
+    my ($biblioitemnumber) = @_;
     my $dbh       = C4::Context->dbh;
     my $sth       =
       $dbh->prepare(
-"Select *,biblioitems.notes as bnotes from biblioitems, biblio,itemtypes where biblio.biblionumber = biblioitems.biblionumber and biblioitemnumber = ? and biblioitems.itemtype = itemtypes.itemtype"
+       "SELECT *,biblioitems.notes AS bnotes
+               FROM biblioitems,biblio,itemtypes 
+       WHERE biblio.biblionumber = biblioitems.biblionumber 
+               AND biblioitemnumber = ? "
       );
     my $data;
-
-    $sth->execute($bibitem);
-
+    $sth->execute($biblioitemnumber);
     $data = $sth->fetchrow_hashref;
-
     $sth->finish;
     return ($data);
 }    # sub &GetBiblioItemData
@@ -1259,10 +1246,10 @@ sub GetBiblioFromItemNumber {
     my ( $itemnumber ) = @_;
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare(
-        "SELECT * FROM biblio,items,biblioitems
-         WHERE items.itemnumber = ?
-           AND biblio.biblionumber = items.biblionumber
-           AND biblioitems.biblioitemnumber = items.biblioitemnumber"
+        "SELECT * FROM items 
+        LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
+        LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
+         WHERE items.itemnumber = ?"
     );
 
     $sth->execute($itemnumber);
@@ -1389,6 +1376,68 @@ sub GetItemInfosOf {
     return get_infos_of( $query, 'itemnumber' );
 }
 
+=head2 GetItemsByBiblioitemnumber
+
+=over 4
+
+GetItemsByBiblioitemnumber($biblioitemnumber);
+
+Returns an arrayref of hashrefs suitable for use in a TMPL_LOOP
+Called by moredetail.pl
+
+=back
+
+=cut
+
+sub GetItemsByBiblioitemnumber {
+       my ( $bibitem ) = @_;
+       my $dbh = C4::Context->dbh;
+       my $sth = $dbh->prepare("SELECT * FROM items WHERE items.biblioitemnumber = ?") || die $dbh->errstr;
+       # Get all items attached to a biblioitem
+    my $i = 0;
+    my @results; 
+    $sth->execute($bibitem) || die $sth->errstr;
+    while ( my $data = $sth->fetchrow_hashref ) {  
+               # Foreach item, get circulation information
+               my $sth2 = $dbh->prepare( "SELECT * FROM issues,borrowers
+                                   WHERE itemnumber = ?
+                                   AND returndate is NULL
+                                   AND issues.borrowernumber = borrowers.borrowernumber"
+        );
+        $sth2->execute( $data->{'itemnumber'} );
+        if ( my $data2 = $sth2->fetchrow_hashref ) {
+                       # if item is out, set the due date and who it is out too
+                       $data->{'date_due'}   = $data2->{'date_due'};
+                       $data->{'cardnumber'} = $data2->{'cardnumber'};
+                       $data->{'borrowernumber'}   = $data2->{'borrowernumber'};
+               }
+        else {
+                       # set date_due to blank, so in the template we check itemlost, and wthdrawn 
+                       $data->{'date_due'} = '';                                                                                                         
+               }    # else         
+        $sth2->finish;
+        # Find the last 3 people who borrowed this item.                  
+        my $query2 = "SELECT * FROM issues, borrowers WHERE itemnumber = ?
+                      AND issues.borrowernumber = borrowers.borrowernumber
+                      AND returndate is not NULL
+                      ORDER BY returndate desc,timestamp desc LIMIT 3";
+        $sth2 = $dbh->prepare($query2) || die $dbh->errstr;
+        $sth2->execute( $data->{'itemnumber'} ) || die $sth2->errstr;
+        my $i2 = 0;
+        while ( my $data2 = $sth2->fetchrow_hashref ) {
+                       $data->{"timestamp$i2"} = $data2->{'timestamp'};
+                       $data->{"card$i2"}      = $data2->{'cardnumber'};
+                       $data->{"borrower$i2"}  = $data2->{'borrowernumber'};
+                       $i2++;
+               }
+        $sth2->finish;
+        push(@results,$data);
+    } 
+    $sth->finish;
+    return (\@results); 
+}
+
+
 =head2 GetBiblioItemInfosOf
 
 =over 4
@@ -1464,7 +1513,8 @@ sub GetMarcStructure {
     $sth =
       $dbh->prepare(
 "select tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue from marc_subfield_structure where frameworkcode=? order by tagfield,tagsubfield"
-      );
+    );
+    
     $sth->execute($frameworkcode);
 
     my $subfield;
@@ -1508,6 +1558,38 @@ sub GetMarcStructure {
     return $res;
 }
 
+=head2 GetUsedMarcStructure
+
+    the same function as GetMarcStructure expcet it just take field
+    in tab 0-9. (used field)
+    
+    my $results = GetUsedMarcStructure($frameworkcode);
+    
+    L<$results> is a ref to an array which each case containts a ref
+    to a hash which each keys is the columns from marc_subfield_structure
+    
+    L<$frameworkcode> is the framework code. 
+    
+=cut
+
+sub GetUsedMarcStructure($){
+    my $frameworkcode = shift || '';
+    my $dbh           = C4::Context->dbh;
+    my $query         = qq/
+        SELECT *
+        FROM   marc_subfield_structure
+        WHERE   tab > -1 
+            AND frameworkcode = ?
+    /;
+    my @results;
+    my $sth = $dbh->prepare($query);
+    $sth->execute($frameworkcode);
+    while (my $row = $sth->fetchrow_hashref){
+        push @results,$row;
+    }
+    return \@results;
+}
+
 =head2 GetMarcFromKohaField
 
 =over 4
@@ -1553,10 +1635,12 @@ sub GetMarcBiblio {
      $marcxml =~ s/\x1f//g;
      $marcxml =~ s/\x1d//g;
      $marcxml =~ s/\x0f//g;
-     $marcxml =~ s/\x0c//g;
+     $marcxml =~ s/\x0c//g;  
 #   warn $marcxml;
     my $record = MARC::Record->new();
-     $record = MARC::Record::new_from_xml( $marcxml, "utf8",C4::Context->preference('marcflavour')) if $marcxml;
+     
+      $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8",C4::Context->preference('marcflavour'))} if ($marcxml);
+     if ($@) {warn $@;}
 #      $record = MARC::Record::new_from_usmarc( $marc) if $marc;
     return $record;
 }
@@ -1750,9 +1834,9 @@ sub GetMarcSubjects {
             }
             my $code = $subject_subfield->[0];
             $label .= $subject_subfield->[1].$authoritysep unless ( $code == 9 );
-            $link  .= $subject_subfield->[1] . " and su-to:" unless ( $code == 9 );
+            $link  .= " and su-to:".$subject_subfield->[1]  unless ( $code == 9 );
             if ( $code == 9 ) {
-                $link = "Koha-Auth-Number:".$subject_subfield->[1];
+                $link = "an:".$subject_subfield->[1];
                 $flag = 1;
             }
             elsif ( ! $flag ) {
@@ -1784,6 +1868,8 @@ The authors are stored in differents places depending on MARC flavour
 sub GetMarcAuthors {
     my ( $record, $marcflavour ) = @_;
     my ( $mintag, $maxtag );
+    # tagslib useful for UNIMARC author reponsabilities
+    my $tagslib = &GetMarcStructure( 1, '' ); # FIXME : we don't have the framework available, we take the default framework. May be bugguy on some setups, will be usually correct.
     if ( $marcflavour eq "MARC21" ) {
         $mintag = "100";
         $maxtag = "111"; 
@@ -1806,7 +1892,6 @@ sub GetMarcAuthors {
                 $marcflavour ne 'MARC21'
                 and (
                     ($authors_subfield->[0] eq '3') or
-                    ($authors_subfield->[0] eq '4') or
                     ($authors_subfield->[0] eq '5')
                 )
             )
@@ -1818,7 +1903,17 @@ sub GetMarcAuthors {
             }
             $count_auth++;
             my $subfieldcode = $authors_subfield->[0];
-            my $value        = $authors_subfield->[1];
+            my $value;
+            # deal with UNIMARC author responsibility
+            if (
+                $marcflavour ne 'MARC21'
+                and ($authors_subfield->[0] eq '4')
+            )
+            {
+                $value = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
+            } else {
+                $value        = $authors_subfield->[1];
+            }
             $hash{tag}       = $field->tag;
             $hash{value}    .= $value . " " if ($subfieldcode != 9) ;
             $hash{link}     .= $value if ($subfieldcode eq 9);
@@ -1828,11 +1923,50 @@ sub GetMarcAuthors {
     return \@marcauthors;
 }
 
+=head2 GetMarcUrls
+
+=over 4
+
+$marcurls = GetMarcUrls($record,$marcflavour);
+Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
+Assumes web resources (not uncommon in MARC21 to omit resource type ind) 
+
+=back
+
+=cut
+
+sub GetMarcUrls {
+    my ($record, $marcflavour) = @_;
+    my @marcurls;
+    my $marcurl;
+    for my $field ($record->field('856')) {
+        my $url = $field->subfield('u');
+        my @notes;
+        for my $note ( $field->subfield('z')) {
+            push @notes , {note => $note};
+        }        
+        $marcurl = {  MARCURL => $url,
+                      notes => \@notes,
+                                       };
+               if($marcflavour eq 'MARC21') {
+               my $s3 = $field->subfield('3');
+                       my $link = $field->subfield('y');
+            $marcurl->{'linktext'} = $link || $s3 || $url ;;
+            $marcurl->{'part'} = $s3 if($link);
+            $marcurl->{'toc'} = 1 if($s3 =~ /^[Tt]able/) ;
+               } else {
+                       $marcurl->{'linktext'} = $url;
+               }
+        push @marcurls, $marcurl;    
+       }
+    return \@marcurls;
+}  #end GetMarcUrls
+
 =head2 GetMarcSeries
 
 =over 4
 
-$marcseriessarray = GetMarcSeries($record,$marcflavour);
+$marcseriesarray = GetMarcSeries($record,$marcflavour);
 Get all series from the MARC record and returns them in an array.
 The series are stored in differents places depending on MARC flavour
 
@@ -1901,7 +2035,7 @@ sub GetMarcSeries {
 
 =over 4
 
-$frameworkcode = GetFrameworkCode( $biblionumber )
+    $frameworkcode = GetFrameworkCode( $biblionumber )
 
 =back
 
@@ -1910,21 +2044,50 @@ $frameworkcode = GetFrameworkCode( $biblionumber )
 sub GetFrameworkCode {
     my ( $biblionumber ) = @_;
     my $dbh = C4::Context->dbh;
-    my $sth =
-      $dbh->prepare("select frameworkcode from biblio where biblionumber=?");
+    my $sth = $dbh->prepare("select frameworkcode from biblio where biblionumber=?");
     $sth->execute($biblionumber);
     my ($frameworkcode) = $sth->fetchrow;
     return $frameworkcode;
 }
 
+=head2 GetPublisherNameFromIsbn
+
+    $name = GetPublishercodeFromIsbn($isbn);
+    if(defined $name){
+        ...
+    }
+
+=cut
+
+sub GetPublisherNameFromIsbn($){
+    my $isbn = shift;
+    $isbn =~ s/[- _]//g;
+    $isbn =~ s/^0*//;
+    my @codes = (split '-', DisplayISBN($isbn));
+    my $code = $codes[0].$codes[1].$codes[2];
+    my $dbh  = C4::Context->dbh;
+    my $query = qq{
+        SELECT distinct publishercode
+        FROM   biblioitems
+        WHERE  isbn LIKE ?
+        AND    publishercode IS NOT NULL
+        LIMIT 1
+    };
+    my $sth = $dbh->prepare($query);
+    $sth->execute("$code%");
+    my $name = $sth->fetchrow;
+    return $name if length $name;
+    return undef;
+}
+
 =head2 TransformKohaToMarc
 
 =over 4
 
-$record = TransformKohaToMarc( $hash )
-This function builds partial MARC::Record from a hash
-Hash entries can be from biblio or biblioitems.
-This function is called in acquisition module, to create a basic catalogue entry from user entry
+    $record = TransformKohaToMarc( $hash )
+    This function builds partial MARC::Record from a hash
+    Hash entries can be from biblio or biblioitems.
+    This function is called in acquisition module, to create a basic catalogue entry from user entry
 
 =back
 
@@ -1950,7 +2113,7 @@ sub TransformKohaToMarc {
 
 =over 4
 
-$record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
+    $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
 
 =back
 
@@ -1964,10 +2127,9 @@ sub TransformKohaToMarcOneField {
 
     if ( !defined $sth ) {
         my $dbh = C4::Context->dbh;
-        $sth =
-          $dbh->prepare(
-"select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?"
-          );
+        $sth = $dbh->prepare(
+            "select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?"
+        );
     }
     $sth->execute( $frameworkcode, $kohafieldname );
     if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
@@ -1994,6 +2156,7 @@ $auth_type contains :
 - nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
 - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
 - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
+
 =back
 
 =cut
@@ -2113,89 +2276,112 @@ sub TransformHtmlToXml {
         $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
         $xml .= "<subfield code=\"a\">$string</subfield>\n";
         $xml .= "</datafield>\n";
-    } 
+    }
     $xml .= MARC::File::XML::footer();
     return $xml;
 }
 
 =head2 TransformHtmlToMarc
 
-=over 4
-
-$record = TransformHtmlToMarc( $dbh, $rtags, $rsubfields, $rvalues, %indicators )
-
-=back
+    L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
+    L<$params> is a ref to an array as below:
+    {
+        'tag_010_indicator_531951' ,
+        'tag_010_code_a_531951_145735' ,
+        'tag_010_subfield_a_531951_145735' ,
+        'tag_200_indicator_873510' ,
+        'tag_200_code_a_873510_673465' ,
+        'tag_200_subfield_a_873510_673465' ,
+        'tag_200_code_b_873510_704318' ,
+        'tag_200_subfield_b_873510_704318' ,
+        'tag_200_code_e_873510_280822' ,
+        'tag_200_subfield_e_873510_280822' ,
+        'tag_200_code_f_873510_110730' ,
+        'tag_200_subfield_f_873510_110730' ,
+    }
+    L<$cgi> is the CGI object which containts the value.
+    L<$record> is the MARC::Record object.
 
 =cut
 
 sub TransformHtmlToMarc {
-    my ( $dbh, $rtags, $rsubfields, $rvalues, %indicators ) = @_;
-    my $prevtag = -1;
+    my $params = shift;
+    my $cgi    = shift;
+    
+    # creating a new record
     my $record  = MARC::Record->new();
-
-    #     my %subfieldlist=();
-    my $prevvalue;    # if tag <10
-    my $field;        # if tag >=10
-    for ( my $i = 0 ; $i < @$rtags ; $i++ ) {
-        next unless @$rvalues[$i];
-
- # rebuild MARC::Record
- #             warn "0=>".@$rtags[$i].@$rsubfields[$i]." = ".@$rvalues[$i].": ";
-        if ( @$rtags[$i] ne $prevtag ) {
-            if ( $prevtag < 10 ) {
-                if ($prevvalue) {
-
-                    if ( $prevtag ne '000' ) {
-                        $record->insert_fields_ordered(
-                            ( sprintf "%03s", $prevtag ), $prevvalue );
-                    }
-                    else {
-
-                        $record->leader($prevvalue);
-
-                    }
-                }
-            }
-            else {
-                if ($field) {
-                    $record->insert_fields_ordered($field);
-                }
-            }
-            $indicators{ @$rtags[$i] } .= '  ';
-            if ( @$rtags[$i] < 10 ) {
-                $prevvalue = @$rvalues[$i];
-                undef $field;
-            }
-            else {
-                undef $prevvalue;
-                $field = MARC::Field->new(
-                    ( sprintf "%03s", @$rtags[$i] ),
-                    substr( $indicators{ @$rtags[$i] }, 0, 1 ),
-                    substr( $indicators{ @$rtags[$i] }, 1, 1 ),
-                    @$rsubfields[$i] => @$rvalues[$i]
+    my $i=0;
+    my @fields;
+    while ($params->[$i]){ # browse all CGI params
+        my $param = $params->[$i];
+        my $newfield=0;
+        # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
+        if ($param eq 'biblionumber') {
+            my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
+                &GetMarcFromKohaField( "biblio.biblionumber", '' );
+            if ($biblionumbertagfield < 10) {
+                $newfield = MARC::Field->new(
+                    $biblionumbertagfield,
+                    $cgi->param($param),
+                );
+            } else {
+                $newfield = MARC::Field->new(
+                    $biblionumbertagfield,
+                    '',
+                    '',
+                    "$biblionumbertagsubfield" => $cgi->param($param),
                 );
             }
-            $prevtag = @$rtags[$i];
-        }
-        else {
-            if ( @$rtags[$i] < 10 ) {
-                $prevvalue = @$rvalues[$i];
-            }
-            else {
-                if ( length( @$rvalues[$i] ) > 0 ) {
-                    $field->add_subfields( @$rsubfields[$i] => @$rvalues[$i] );
+            push @fields,$newfield if($newfield);
+        } 
+        elsif ($param =~ /^tag_(\d*)_indicator_/){ # new field start when having 'input name="..._indicator_..."
+            my $tag  = $1;
+            
+            my $ind1 = substr($cgi->param($param),0,1);
+            my $ind2 = substr($cgi->param($param),1,1);
+            $newfield=0;
+            my $j=$i+1;
+            
+            if($tag < 10){ # no code for theses fields
+    # in MARC editor, 000 contains the leader.
+                if ($tag eq '000' ) {
+                    $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
+    # between 001 and 009 (included)
+                } else {
+                    $newfield = MARC::Field->new(
+                        $tag,
+                        $cgi->param($params->[$j+1]),
+                    );
+                }
+    # > 009, deal with subfields
+            } else {
+                while($params->[$j] =~ /_code_/){ # browse all it's subfield
+                    my $inner_param = $params->[$j];
+                    if ($newfield){
+                        if($cgi->param($params->[$j+1])){  # only if there is a value (code => value)
+                            $newfield->add_subfields(
+                                $cgi->param($inner_param) => $cgi->param($params->[$j+1])
+                            );
+                        }
+                    } else {
+                        if ( $cgi->param($params->[$j+1]) ) { # creating only if there is a value (code => value)
+                            $newfield = MARC::Field->new(
+                                $tag,
+                                ''.$ind1,
+                                ''.$ind2,
+                                $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
+                            );
+                        }
+                    }
+                    $j+=2;
                 }
             }
-            $prevtag = @$rtags[$i];
+            push @fields,$newfield if($newfield);
         }
+        $i++;
     }
-
-    # the last has not been included inside the loop... do it now !
-    $record->insert_fields_ordered($field) if $field;
-
-    $record->encoding('UTF-8');
-
-    #    $record->MARC::File::USMARC::update_leader();
+    
+    $record->append_fields(@fields);
     return $record;
 }
 
@@ -2203,7 +2389,7 @@ sub TransformHtmlToMarc {
 
 =over 4
 
-$result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
+       $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
 
 =back
 
@@ -2211,10 +2397,13 @@ $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
 
 sub TransformMarcToKoha {
     my ( $dbh, $record, $frameworkcode ) = @_;
-    my $sth =
-      $dbh->prepare(
-"select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?"
-      );
+    
+    #  FIXME :: This query is unused..
+    #    my $sth =
+    #      $dbh->prepare(
+    #"select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?"
+    #      );
+    
     my $result;
     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
     $sth2->execute;
@@ -2224,7 +2413,7 @@ sub TransformMarcToKoha {
           &TransformMarcToKohaOneField( "biblio", $field, $record, $result,
             $frameworkcode );
     }
-    $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
+    my $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
     $sth2->execute;
     while ( ($field) = $sth2->fetchrow ) {
         if ( $field eq 'notes' ) { $field = 'bnotes'; }
@@ -2277,9 +2466,9 @@ $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result,
 
 sub TransformMarcToKohaOneField {
 
-# FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
+    # FIXME ? if a field has a repeatable subfield that is used in old-db,
+    # only the 1st will be retrieved...
     my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
-
     my $res = "";
     my ( $tagfield, $subfield ) =
       GetMarcFromKohaField( $kohatable . "." . $kohafield,
@@ -2313,6 +2502,7 @@ sub TransformMarcToKohaOneField {
     }
     return $result;
 }
+
 =head1  OTHER FUNCTIONS
 
 =head2 char_decode
@@ -2748,6 +2938,7 @@ ModZebra( $biblionumber, $op, $server, $newRecord );
     $op is specialUpdate or delete, and is used to know what we want to do
     $server is the server that we want to update
     $newRecord is the MARC::Record containing the new record. It is usefull only when NoZebra=1, and is used to know what to add to the nozebra database. (the record in mySQL being, if it exist, the previous record, the one just before the modif. We need both : the previous and the new one.
+    
 =back
 
 =cut
@@ -2755,14 +2946,12 @@ ModZebra( $biblionumber, $op, $server, $newRecord );
 sub ModZebra {
 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
     my ( $biblionumber, $op, $server, $newRecord ) = @_;
-#     warn "ModZebra with : ( $biblionumber, $op, $server, $newRecord )";
     my $dbh=C4::Context->dbh;
-    #warn "SERVER:".$server;
-#
-# true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
-# at the same time
-# replaced by a zebraqueue table, that is filled with ModZebra to run.
-# the table is emptied by misc/cronjobs/zebraqueue_start.pl script
+
+    # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
+    # at the same time
+    # replaced by a zebraqueue table, that is filled with ModZebra to run.
+    # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
 
     if (C4::Context->preference("NoZebra")) {
         # lock the nozebra table : we will read index lines, update them in Perl process
@@ -2784,18 +2973,17 @@ sub ModZebra {
             %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
         } else {
             # it's a deletion, delete the record...
-#             warn "DELETE the record $biblionumber on $server".$record->as_formatted;
+            # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
             %result=_DelBiblioNoZebra($biblionumber,$record,$server);
         }
         # ok, now update the database...
         my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
         foreach my $key (keys %result) {
             foreach my $index (keys %{$result{$key}}) {
-#                 warn "UPDATING : $server $key , $index with :".$result{$key}->{$index};
                 $sth->execute($result{$key}->{$index}, $server, $key, $index);
             }
         }
-    $dbh->do('UNLOCK TABLES');
+        $dbh->do('UNLOCK TABLES');
 
     } else {
         #
@@ -2809,6 +2997,10 @@ sub ModZebra {
 
 =head2 GetNoZebraIndexes
 
+    %indexes = GetNoZebraIndexes;
+    
+    return the data from NoZebraIndexes syspref.
+
 =cut
 
 sub GetNoZebraIndexes {
@@ -2929,13 +3121,12 @@ sub _DelBiblioNoZebra {
     return %result;
 }
 
-=head2 _DelBiblioNoZebra($biblionumber,$record);
+=head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
 
-    function to delete a biblio in NoZebra indexes
+    function to add a biblio in NoZebra indexes
 
 =cut
 
-
 sub _AddBiblioNoZebra {
     my ($biblionumber, $record, $server, %result)=@_;
     my $dbh = C4::Context->dbh;
@@ -2948,14 +3139,14 @@ sub _AddBiblioNoZebra {
         my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
         $title = lc($record->subfield($titletag,$titlesubfield));
     } else {
-#     warn "server : $server";
+        # warn "server : $server";
         # for authorities, the "title" is the $a mainentry
         my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield(152,'b'));
         warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
         $title = $record->subfield($authref->{auth_tag_to_report},'a');
-        $index{'mainmainentry'}=$authref->{auth_tag_to_report}.'a';
-        $index{'mainentry'}    = $authref->{auth_tag_to_report}.'*';
-        $index{'auth_type'}    = '152b';
+        $index{'mainmainentry'} = $authref->{auth_tag_to_report}.'a';
+        $index{'mainentry'}     = $authref->{auth_tag_to_report}.'*';
+        $index{'auth_type'}     = '152b';
     }
 
     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
@@ -2985,25 +3176,25 @@ sub _AddBiblioNoZebra {
                         next unless $_; # skip  empty values (multiple spaces)
                         # if the entry is already here, improve weight
 #                         warn "managing $_";
-                        if ($result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) {
+                        if ($result{$key}->{"$_"} =~ /$biblionumber,$title\-(\d);/) {
                             my $weight=$1+1;
-                            $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
-                            $result{$key}->{$_} .= "$biblionumber,$title-$weight;";
+                            $result{$key}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
+                            $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
                         } else {
                             # get the value if it exist in the nozebra table, otherwise, create it
                             $sth2->execute($server,$key,$_);
                             my $existing_biblionumbers = $sth2->fetchrow;
                             # it exists
                             if ($existing_biblionumbers) {
-                                $result{$key}->{$_} =$existing_biblionumbers;
+                                $result{$key}->{"$_"} =$existing_biblionumbers;
                                 my $weight=$1+1;
-                                $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
-                                $result{$key}->{$_} .= "$biblionumber,$title-$weight;";
+                                $result{$key}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
+                                $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
                             # create a new ligne for this entry
                             } else {
 #                             warn "INSERT : $server / $key / $_";
                                 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
-                                $result{$key}->{$_}.="$biblionumber,$title-1;";
+                                $result{$key}->{"$_"}.="$biblionumber,$title-1;";
                             }
                         }
                     }
@@ -3017,24 +3208,24 @@ sub _AddBiblioNoZebra {
                 foreach (split / /,$line) {
                     next unless $_; # skip  empty values (multiple spaces)
                     # if the entry is already here, improve weight
-                    if ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
+                    if ($result{'__RAW__'}->{"$_"} =~ /$biblionumber,$title\-(\d);/) {
                         my $weight=$1+1;
-                        $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
-                        $result{'__RAW__'}->{$_} .= "$biblionumber,$title-$weight;";
+                        $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
+                        $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
                     } else {
                         # get the value if it exist in the nozebra table, otherwise, create it
                         $sth2->execute($server,'__RAW__',$_);
                         my $existing_biblionumbers = $sth2->fetchrow;
                         # it exists
                         if ($existing_biblionumbers) {
-                            $result{'__RAW__'}->{$_} =$existing_biblionumbers;
+                            $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
                             my $weight=$1+1;
-                            $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
-                            $result{'__RAW__'}->{$_} .= "$biblionumber,$title-$weight;";
+                            $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
+                            $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
                         # create a new ligne for this entry
                         } else {
                             $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).',  indexname="__RAW__",value='.$dbh->quote($_));
-                            $result{'__RAW__'}->{$_}.="$biblionumber,$title-1;";
+                            $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
                         }
                     }
                 }
@@ -3100,7 +3291,7 @@ sub _koha_add_biblio {
     $sth->finish;
     $sth = $dbh->prepare(
         "INSERT INTO biblio
-    SET biblionumber  = ?, title = ?, author = ?, copyrightdate = ?, serial = ?, seriestitle = ?, notes = ?, abstract = ?, unititle = ?, frameworkcode = ? "
+    SET datecreated=NOW(), biblionumber  = ?, title = ?, author = ?, copyrightdate = ?, serial = ?, seriestitle = ?, notes = ?, abstract = ?, unititle = ?, frameworkcode = ? "
     );
     $sth->execute(
         $biblionumber,         $biblio->{'title'},
@@ -3163,6 +3354,7 @@ sub _find_value {
 
 =over 4
 
+$biblionumber = _koha_modify_biblio($dbh,$biblio);
 Internal function for updating the biblio table
 
 =back
@@ -3171,20 +3363,36 @@ Internal function for updating the biblio table
 
 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 = ?"
-      );
+    # FIXME: this code could be made more portable by not hard-coding
+    #        the values that are supposed to be in biblio table
+    my $query = qq{
+        UPDATE biblio
+        SET    title = ?,
+               author = ?,
+               abstract = ?,
+               copyrightdate = ?,
+               seriestitle = ?,
+               serial = ?,
+               unititle = ?,
+               notes = ?
+        WHERE  biblionumber = ?
+    };
+    my $sth = $dbh->prepare($query);
+    
     $sth->execute(
-        $biblio->{'title'},       $biblio->{'author'},
-        $biblio->{'abstract'},    $biblio->{'copyrightdate'},
-        $biblio->{'seriestitle'}, $biblio->{'serial'},
-        $biblio->{'unititle'},    $biblio->{'notes'},
+        $biblio->{'title'},
+        $biblio->{'author'},
+        $biblio->{'abstract'},
+        $biblio->{'copyrightdate'},
+        $biblio->{'seriestitle'},
+        $biblio->{'serial'},
+        $biblio->{'unititle'},
+        $biblio->{'notes'},
         $biblio->{'biblionumber'}
-    );
-    $sth->finish;
+    ) if $biblio->{'biblionumber'};
+    
+    warn $sth->err if $sth->err;
+    warn "BIG ERROR :: No biblionumber for $biblio->{title}" if $biblio->{biblionumber} !~ /\d+/; # if it is not a number
     return ( $biblio->{'biblionumber'} );
 }
 
@@ -3221,11 +3429,16 @@ sub _koha_modify_biblioitem {
     $biblioitem->{'bnotes'}       = $dbh->quote( $biblioitem->{'bnotes'} );
     $biblioitem->{'size'}         = $dbh->quote( $biblioitem->{'size'} );
     $biblioitem->{'place'}        = $dbh->quote( $biblioitem->{'place'} );
+    $biblioitem->{'collectiontitle'}        = $dbh->quote( $biblioitem->{'collectiontitle'} );
+    $biblioitem->{'collectionissn'}         = $dbh->quote( $biblioitem->{'collectionissn'} );
+    $biblioitem->{'collectionvolume'}       = $dbh->quote( $biblioitem->{'collectionvolume'} );
+    $biblioitem->{'editionstatement'}       = $dbh->quote( $biblioitem->{'editionstatement'} );
+    $biblioitem->{'editionresponsibility'}  = $dbh->quote( $biblioitem->{'editionresponsibility'} );
     $biblioitem->{'ccode'}        = $dbh->quote( $biblioitem->{'ccode'} );
     $biblioitem->{'biblionumber'} =
       $dbh->quote( $biblioitem->{'biblionumber'} );
 
-    $query = "Update biblioitems set
+    $query = "UPDATE biblioitems SET
         itemtype        = $biblioitem->{'itemtype'},
         url             = $biblioitem->{'url'},
         isbn            = $biblioitem->{'isbn'},
@@ -3241,6 +3454,11 @@ sub _koha_modify_biblioitem {
         notes           = $biblioitem->{'bnotes'},
         size            = $biblioitem->{'size'},
         place           = $biblioitem->{'place'},
+        collectiontitle = $biblioitem->{'collectiontitle'},
+        collectionissn  = $biblioitem->{'collectionissn'},
+        collectionvolume= $biblioitem->{'collectionvolume'},
+        editionstatement= $biblioitem->{'editionstatement'},
+        editionresponsibility= $biblioitem->{'editionresponsibility'},
         ccode           = $biblioitem->{'ccode'}
         where biblionumber = $biblioitem->{'biblionumber'}";
 
@@ -3288,8 +3506,11 @@ sub _koha_add_biblioitem {
             volumeddesc      = ?, illus           = ?,
             pages            = ?, notes           = ?,
             size             = ?, lccn            = ?,
-            marc             = ?, lcsort          =?,
-            place            = ?, ccode           = ?
+            marc             = ?, lcsort          = ?,
+            place            = ?, ccode           = ?,
+            collectiontitle  = ?, collectionissn  = ?,
+            collectionvolume = ?, editionstatement= ?,
+            editionresponsibility= ?
           "
     );
     my ($lcsort) =
@@ -3307,7 +3528,10 @@ sub _koha_add_biblioitem {
         $biblioitem->{'pages'},          $biblioitem->{'bnotes'},
         $biblioitem->{'size'},           $biblioitem->{'lccn'},
         $biblioitem->{'marc'},           $biblioitem->{'place'},
-        $lcsort,                         $biblioitem->{'ccode'}
+        $lcsort,                         $biblioitem->{'ccode'},
+        $biblioitem->{'collectiontitle'},$biblioitem->{'collectionissn'},
+        $biblioitem->{'collectionvolume'},$biblioitem->{'editionstatement'},
+        $biblioitem->{'editionresponsibility'}
     );
     $sth->finish;
     return ($bibitemnum);
@@ -3354,13 +3578,12 @@ sub _koha_new_items {
         $sth = $dbh->prepare(
             "Insert into items set
             itemnumber           = ?,     biblionumber     = ?,
-            multivolumepart      = ?,
             biblioitemnumber     = ?,     barcode          = ?,
             booksellerid         = ?,     dateaccessioned  = NOW(),
             homebranch           = ?,     holdingbranch    = ?,
             price                = ?,     replacementprice = ?,
             replacementpricedate = NOW(), datelastseen     = NOW(),
-            multivolume          = ?,     stack            = ?,
+                       stack            = ?,
             itemlost             = ?,     wthdrawn         = ?,
             paidfor              = ?,     itemnotes        = ?,
             itemcallnumber       =?,      notforloan       = ?,
@@ -3369,11 +3592,11 @@ sub _koha_new_items {
         );
         $sth->execute(
             $itemnumber,                $item->{'biblionumber'},
-            $item->{'multivolumepart'}, $item->{'biblioitemnumber'},
+                       $item->{'biblioitemnumber'},
             $barcode,                   $item->{'booksellerid'},
             $item->{'homebranch'},      $item->{'holdingbranch'},
             $item->{'price'},           $item->{'replacementprice'},
-            $item->{multivolume},       $item->{stack},
+                       $item->{stack},
             $item->{itemlost},          $item->{wthdrawn},
             $item->{paidfor},           $item->{'itemnotes'},
             $item->{'itemcallnumber'},  $item->{'notforloan'},
@@ -3384,13 +3607,12 @@ sub _koha_new_items {
         $sth = $dbh->prepare(
             "INSERT INTO items SET
             itemnumber           = ?,     biblionumber     = ?,
-            multivolumepart      = ?,
             biblioitemnumber     = ?,     barcode          = ?,
             booksellerid         = ?,     dateaccessioned  = ?,
             homebranch           = ?,     holdingbranch    = ?,
             price                = ?,     replacementprice = ?,
             replacementpricedate = NOW(), datelastseen     = NOW(),
-            multivolume          = ?,     stack            = ?,
+                       stack            = ?,
             itemlost             = ?,     wthdrawn         = ?,
             paidfor              = ?,     itemnotes        = ?,
             itemcallnumber       = ?,     notforloan       = ?,
@@ -3400,11 +3622,11 @@ sub _koha_new_items {
         );
         $sth->execute(
             $itemnumber,                 $item->{'biblionumber'},
-            $item->{'multivolumepart'},  $item->{'biblioitemnumber'},
+                       $item->{'biblioitemnumber'},
             $barcode,                    $item->{'booksellerid'},
             $item->{'dateaccessioned'},  $item->{'homebranch'},
             $item->{'holdingbranch'},    $item->{'price'},
-            $item->{'replacementprice'}, $item->{multivolume},
+            $item->{'replacementprice'},
             $item->{stack},              $item->{itemlost},
             $item->{wthdrawn},           $item->{paidfor},
             $item->{'itemnotes'},        $item->{'itemcallnumber'},
@@ -3435,10 +3657,10 @@ sub _koha_modify_item {
     # if all we're doing is setting statuses, just update those and get out
     if ( $op eq "setstatus" ) {
         my $query =
-          "UPDATE items SET itemlost=?,wthdrawn=?,binding=? WHERE itemnumber=?";
+          "UPDATE items SET itemlost=?,wthdrawn=? WHERE itemnumber=?";
         my @bind = (
             $item->{'itemlost'}, $item->{'wthdrawn'},
-            $item->{'binding'},  $item->{'itemnumber'}
+                       $item->{'itemnumber'}
         );
         my $sth = $dbh->prepare($query);
         $sth->execute(@bind);
@@ -3450,32 +3672,29 @@ sub _koha_modify_item {
       itemcalculator( $dbh, $item->{'bibitemnum'}, $item->{'itemcallnumber'} );
 
     my $query = "UPDATE items SET
-barcode=?,itemnotes=?,itemcallnumber=?,notforloan=?,location=?,multivolumepart=?,multivolume=?,stack=?,wthdrawn=?,holdingbranch=?,homebranch=?,cutterextra=?, onloan=?, binding=?";
+barcode=?,itemnotes=?,itemcallnumber=?,notforloan=?,location=?,stack=?,wthdrawn=?,holdingbranch=?,homebranch=?,cutterextra=?, onloan=?";
 
     my @bind = (
         $item->{'barcode'},        $item->{'notes'},
         $item->{'itemcallnumber'}, $item->{'notforloan'},
-        $item->{'location'},       $item->{multivolumepart},
-        $item->{multivolume},      $item->{stack},
+        $item->{'location'},       $item->{stack},
         $item->{wthdrawn},         $item->{holdingbranch},
         $item->{homebranch},       $cutterextra,
-        $item->{onloan},           $item->{binding}
+        $item->{onloan},           
     );
     if ( $item->{'lost'} ne '' ) {
         $query =
 "update items set biblioitemnumber=?,barcode=?,itemnotes=?,homebranch=?,
                             itemlost=?,wthdrawn=?,itemcallnumber=?,notforloan=?,
-                             location=?,multivolumepart=?,multivolume=?,stack=?,wthdrawn=?,holdingbranch=?,cutterextra=?,onloan=?, binding=?";
+                             location=?,stack=?,wthdrawn=?,holdingbranch=?,cutterextra=?,onloan=?";
         @bind = (
             $item->{'bibitemnum'},     $item->{'barcode'},
             $item->{'notes'},          $item->{'homebranch'},
             $item->{'lost'},           $item->{'wthdrawn'},
             $item->{'itemcallnumber'}, $item->{'notforloan'},
-            $item->{'location'},       $item->{multivolumepart},
-            $item->{multivolume},      $item->{stack},
+            $item->{'location'},       $item->{stack},
             $item->{wthdrawn},         $item->{holdingbranch},
-            $cutterextra,              $item->{onloan},
-            $item->{binding}
+            $cutterextra,              $item->{onloan}
         );
         if ( $item->{homebranch} ) {
             $query .= ",homebranch=?";
@@ -3724,20 +3943,16 @@ sub itemcalculator {
 
 =head2 ModBiblioMarc
 
-=over 4
-
-&ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
-
-Add MARC data for a biblio to koha 
-
-Function exported, but should NOT be used, unless you really know what you're doing
-
-=back
+    &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
+    
+    Add MARC data for a biblio to koha 
+    
+    Function exported, but should NOT be used, unless you really know what you're doing
 
 =cut
 
 sub ModBiblioMarc {
-
+    
 # pass the MARC::Record to this function, and it will create the records in the marc field
     my ( $record, $biblionumber, $frameworkcode ) = @_;
     my $dbh = C4::Context->dbh;
@@ -3751,7 +3966,7 @@ sub ModBiblioMarc {
     $sth->finish;
     my $encoding = C4::Context->preference("marcflavour");
 
-# deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
+    # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
     if ( $encoding eq "UNIMARC" ) {
         my $string;
         if ( length($record->subfield( 100, "a" )) == 35 ) {
@@ -3770,14 +3985,12 @@ sub ModBiblioMarc {
                 MARC::Field->new( 100, "", "", "a" => $string ) );
         }
     }
-#     warn "biblionumber : ".$biblionumber;
     ModZebra($biblionumber,"specialUpdate","biblioserver",$record);
     $sth =
       $dbh->prepare(
         "update biblioitems set marc=?,marcxml=?  where biblionumber=?");
-    $sth->execute( $record->as_usmarc(), $record->as_xml_record(),
+    $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding),
         $biblionumber );
-#     warn $record->as_xml_record();
     $sth->finish;
     return $biblionumber;
 }
@@ -3798,7 +4011,7 @@ Function exported, but should NOT be used, unless you really know what you're do
 
 sub AddItemInMarc {
 
-# pass the MARC::Record to this function, and it will create the records in the marc tables
+    # pass the MARC::Record to this function, and it will create the records in the marc tables
     my ( $record, $biblionumber, $frameworkcode ) = @_;
     my $newrec = &GetMarcBiblio($biblionumber);
 
@@ -3959,8 +4172,13 @@ Joshua Ferraro jmf@liblime.com
 
 # $Id$
 # $Log$
-# Revision 1.219  2007/07/20 14:02:57  hdl
-# Adding biblio.biblionumber to GetItemsfor Inventory
+# Revision 1.221  2007/07/31 16:01:11  toins
+# Some new functions.
+# TransformHTMLtoMarc rewrited.
+#
+# Revision 1.220  2007/07/20 15:43:16  hdl
+# Bug Fixing GetMarcSubjects.
+# Links parameters were mixed.
 #
 # Revision 1.218  2007/07/19 07:40:08  hdl
 # Adding selection by location for inventory