removing use MARC::Charset
[koha_fer] / acqui.simple / marcimport.pl
index 874f8ff..b8bae89 100755 (executable)
@@ -9,6 +9,24 @@
 
 # Licensed under the GPL
 
+
+# Copyright 2000-2002 Katipo Communications
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA  02111-1307 USA
+
 use strict;
 
 # standard or CPAN modules used
@@ -16,37 +34,21 @@ use CGI;
 use DBI;
 
 # Koha modules used
-use C4::Database;
-use C4::Acquisitions;
+use C4::Context;
 use C4::Output;
+use C4::Charset;
 use C4::Input;
 use C4::Biblio;
-use C4::SimpleMarc;
-use C4::Z3950;
 use MARC::File::USMARC;
 use HTML::Template;
+use C4::Output;
+use C4::Auth;
 
 #------------------
 # Constants
 
-my %configfile;
-open (KC, "/etc/koha.conf");
-while (<KC>) {
- chomp;
- (next) if (/^\s*#/);
- if (/(.*)\s*=\s*(.*)/) {
-   my $variable=$1;
-   my $value=$2;
-   # Clean up white space at beginning and end
-   $variable=~s/^\s*//g;
-   $variable=~s/\s*$//g;
-   $value=~s/^\s*//g;
-   $value=~s/\s*$//g;
-   $configfile{$variable}=$value;
- }
-}
-my $includes=$configfile{'includes'};
-($includes) || ($includes="/usr/local/www/hdl/htdocs/includes");
+my $includes = C4::Context->config('includes') ||
+       "/usr/local/www/hdl/htdocs/includes";
 
 # HTML colors for alternating lines
 my $lc1='#dddddd';
@@ -59,61 +61,101 @@ my $lc2='#ddaaaa';
 my $userid=$ENV{'REMOTE_USER'};
 
 my $input = new CGI;
-my $dbh=C4Connect;
-
-#-------------
-# Display output
-#print $input->header;
-#print startpage();
-#print startmenu('acquisitions');
-
-#-------------
-# Process input parameters
-
-my $file=$input->param('file');
-my $menu = $input->param('menu');
-
-#
-#
-# TODO : parameter decoding and function call is quite dirty.
-# should be rewritten...
-#
-#
-if ($input->param('z3950queue')) {
-       AcceptZ3950Queue($dbh,$input);
-} 
-
-if ($input->param('uploadmarc')) {
-       AcceptMarcUpload($dbh,$input)
-}
-
-if ($input->param('insertnewrecord')) {
-    # Add biblio item, and set up menu for adding item copies
-    my ($biblionumber,$biblioitemnumber)=AcceptBiblioitem($dbh,$input);
-    exit;
-}
+my $dbh = C4::Context->dbh;
+
+my $uploadmarc=$input->param('uploadmarc');
+my $overwrite_biblio = $input->param('overwrite_biblio');
+my $filename = $input->param('filename');
+my ($template, $loggedinuser, $cookie)
+       = get_template_and_user({template_name => "acqui.simple/marcimport.tmpl",
+                                       query => $input,
+                                       type => "intranet",
+                                       authnotrequired => 0,
+                                       flagsrequired => {parameters => 1},
+                                       debug => 1,
+                                       });
+
+$template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
+                                               uploadmarc => $uploadmarc);
+if ($uploadmarc && length($uploadmarc)>0) {
+       my $marcrecord='';
+       while (<$uploadmarc>) {
+               $marcrecord.=$_;
+       }
+       my @marcarray = split /\x1D/, $marcrecord;
+       my $dbh = C4::Context->dbh;
+       my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?");
+       my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?");
+       my $searchbreeding = $dbh->prepare("select id from marc_breeding where isbn=?");
+       my $insertsql = $dbh->prepare("insert into marc_breeding (file,isbn,title,author,marc) values(?,?,?,?,?)");
+       my $replacesql = $dbh->prepare("update marc_breeding set file=?,isbn=?,title=?,author=?,marc=? where id=?");
+       # fields used for import results
+       my $imported=0;
+       my $alreadyindb = 0;
+       my $alreadyinfarm = 0;
+       my $notmarcrecord = 0;
+       for (my $i=0;$i<=$#marcarray;$i++) {
+               my $marcrecord = MARC::File::USMARC::decode($marcarray[$i]."\x1D");
+               if (ref($marcrecord) eq undef) {
+                       $notmarcrecord++;
+               } else {
+                       my $oldbiblio = MARCmarc2koha($dbh,$marcrecord);
+                       $oldbiblio->{title} = char_decode($oldbiblio->{title});
+                       $oldbiblio->{author} = char_decode($oldbiblio->{author});
+                       # if isbn found and biblio does not exist, add it. If isbn found and biblio exists, overwrite or ignore depending on user choice
+                       # drop every "special" char : spaces, - ...
+                       $oldbiblio->{isbn} =~ s/ |-|\.//g,
+                       # search if biblio exists
+                       my $biblioitemnumber;
+                       if ($oldbiblio->{isbn}) {
+                               $searchisbn->execute($oldbiblio->{isbn});
+                               ($biblioitemnumber) = $searchisbn->fetchrow;
+                       } else {
+                               $searchissn->execute($oldbiblio->{issn});
+                               ($biblioitemnumber) = $searchissn->fetchrow;
+                       }
+                       if ($biblioitemnumber) {
+                               $alreadyindb++;
+                       } else {
+                               # search in breeding farm
+                               my $breedingid;
+                               if ($oldbiblio->{isbn}) {
+                                       $searchbreeding->execute($oldbiblio->{isbn});
+                                       ($breedingid) = $searchbreeding->fetchrow;
+                               } else {
+                                       $searchbreeding->execute($oldbiblio->{issn});
+                                       ($breedingid) = $searchbreeding->fetchrow;
+                               }
+                               if (!$breedingid || $overwrite_biblio) {
+                                       my $recoded;
+                                       $recoded = $marcrecord->as_usmarc();
+                                               if ($breedingid) {
+                                                       $replacesql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded,$breedingid);
+                                               } else {
+                                                       $insertsql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded);
+                                               }
+                                       $imported++;
+                               } else {
+                                       $alreadyinfarm++;
+                               }
+                       }
+               }
+       }
+       $template->param(imported => $imported,
+                                                       alreadyindb => $alreadyindb,
+                                                       alreadyinfarm => $alreadyinfarm,
+                                                       notmarcrecord => $notmarcrecord,
+                                                       total => $imported+$alreadyindb+$alreadyinfarm+$notmarcrecord,
+                                                       );
 
-if ($input->param('newitem')) {
-    # Add item copy
-    &AcceptItemCopy($dbh,$input);
-    exit;
-} # if newitem
-
-
-if ($file) {
-    ProcessFile($dbh,$input);
-} else {
-  SWITCH:
-    {
-       if ($menu eq 'z3950') { z3950menu($dbh,$input); last SWITCH; }
-       if ($menu eq 'uploadmarc') { uploadmarc($dbh); last SWITCH; }
-       if ($menu eq 'manual') { manual(); last SWITCH; }
-       mainmenu();
-    }
 }
-#print endmenu();
-#print endpage();
 
+print $input->header(
+    -type => guesstype($template->output),
+    -cookie => $cookie
+),$template->output;
+my $menu;
+my $file;
 
 # Process a MARC file : show list of records, of 1 record detail, if numrecord exists
 sub ProcessFile {
@@ -121,7 +163,6 @@ sub ProcessFile {
     use strict;
     # Input params
     my (
-       $dbh,
        $input,
     )=@_;
 
@@ -133,8 +174,6 @@ sub ProcessFile {
 
     my $debug=0;
 
-    requireDBI($dbh,"ProcessFile");
-
     # See if a particular result item was specified
     my $numrecord = $input->param('numrecord');
     if ($numrecord) {
@@ -154,7 +193,7 @@ sub ProcessRecord {
        $record,
        $data,
     );
-       
+
     if ($file=~/Z-(\d+)/) {
        my $id=$1;
        my $resultsid=$input->param('resultsid');
@@ -166,18 +205,16 @@ sub ProcessRecord {
        $sth->execute;
        ($data) = $sth->fetchrow;
     }
-    
+
     my $file=MARC::File::USMARC->indata ($data);
     my $oldkoha;
-    for (my $i==1;$i<$numrecord;$i++) {
+    for (my $i=1;$i<$numrecord;$i++) {
        $record = $file->next;
     }
     if ($record) {
        $oldkoha=MARCmarc2koha($dbh,$record);
     }
-    my $templatebase="marcimport/marcimportdetail.tmpl";
-    my $theme=picktemplate($includes, $templatebase);
-    my $template = HTML::Template->new(filename => "$includes/templates/$theme/$templatebase", die_on_bad_params => 0, path => [$includes]);
+    my $template=gettemplate('marcimport/marcimportdetail.tmpl');
     $oldkoha->{additionalauthors} =~ s/ \| /\n/g;
     $oldkoha =~ s/\|/\n/g;
     $template->param($oldkoha);
@@ -214,7 +251,7 @@ sub ProcessRecord {
     $template->param(numrecord => $numrecord);
     $template->param(file => $data);
     print "Content-Type: text/html\n\n", $template->output;
-}    
+}
 
 # lists all records from the MARC file
 sub ListFileRecords {
@@ -222,7 +259,7 @@ sub ListFileRecords {
 
     # Input parameters
     my (
-       $dbh,
+       $dbh,           # FIXME - Unused argument
        $input,
     )=@_;
 
@@ -240,15 +277,13 @@ sub ListFileRecords {
     my $recordsource;
     my $record;
     my ($numrecords,$resultsid,$data,$startdate,$enddate);
-    
-    requireDBI($dbh,"ListFileRecords");
+               # FIXME - there's already a $data a few lines above.
 
-    my $templatebase="marcimport/ListFileRecords.tmpl";
-    my $theme=picktemplate($includes, $templatebase);
-    my $template = HTML::Template->new(filename => "$includes/templates/$theme/$templatebase", die_on_bad_params => 0, path => [$includes]);
+    $dbh = C4::Context->dbh;
 
+    my $template=gettemplate('marcimport/ListFileRecords.tmpl');
     # File can be z3950 search query or uploaded MARC data
-    
+
     # if z3950 results
     if (not $file=~/Z-(\d+)/) {
        # This is a Marc upload
@@ -260,19 +295,19 @@ sub ListFileRecords {
     }
 
     if ($file=~/Z-(\d+)/) {
-       # This is a z3950 search 
+       # This is a z3950 search
        $template->param(IS_Z3950 =>1);
        my $id=$1;              # search query id number
        my $serverstring;
        my $starttimer=time();
-       
+
        $sth=$dbh->prepare("
                select z3950results.numrecords,z3950results.id,z3950results.results,
-                       z3950results.startdate,z3950results.enddate,server 
-               from z3950queue left outer join z3950results 
-                    on z3950queue.id=z3950results.queryid 
+                       z3950results.startdate,z3950results.enddate,server
+               from z3950queue left outer join z3950results
+                    on z3950queue.id=z3950results.queryid
                where z3950queue.id=?
-               order by server  
+               order by server
            ");
        $sth->execute($id);
        if ( $sth->rows ) {
@@ -282,7 +317,7 @@ sub ListFileRecords {
                my ($srvid, $server, $database, $auth) = split(/\//, $serverstring, 4);
                if ( $server ) {
                        my $srvname=&z3950servername($dbh,$srvid,"$server/$database");
-                       $template->parram(srvid => $srvid);
+                       $template->param(srvid => $srvid);
                        $template->param(srvname => $srvname);
                } # if $server
                my $startrecord=$input->param("ST-$srvid");
@@ -313,7 +348,7 @@ sub ListFileRecords {
                    $template->param(numrecords => $numrecords);
                    $template->param(previous => $previous);
                    $template->param(next => $next);
-                   my $stj=$dbh->prepare("update z3950results 
+                   my $stj=$dbh->prepare("update z3950results
                        set highestseen=? where id=?");
                    $stj->execute($startrecord+10,$resultsid);
                }
@@ -352,7 +387,7 @@ sub ListFileRecords {
 
        } else {
 #
-# This is an uploaded Marc record   
+# This is an uploaded Marc record
 #
            my @loop = ();
            my $MARCfile = MARC::File::USMARC->indata($data);
@@ -373,6 +408,7 @@ sub ListFileRecords {
 sub ResultRecordLink {
     use strict;
     my ($dbh,$oldkoha,$resultsid, $num)=@_;    # input
+               # FIXME - $dbh as argument is no longer used
     my (
        $sth,
        $bib,   # hash ref to named fields
@@ -381,12 +417,13 @@ sub ResultRecordLink {
        $fieldname,
        );
     my %row = ();
-    requireDBI($dbh,"PrintResultRecordLink");
+
+    $dbh = C4::Context->dbh;
 
 #    $bib=extractmarcfields($record);
 
-    $sth=$dbh->prepare("select * 
-         from biblioitems 
+    $sth=$dbh->prepare("select *
+         from biblioitems
          where (isbn=? and isbn!='')  or (issn=? and issn!='')  or (lccn=? and lccn!='') ");
     $sth->execute($oldkoha->{isbn},$oldkoha->{issn},$oldkoha->{lccn});
     if ($sth->rows) {
@@ -395,7 +432,7 @@ sub ResultRecordLink {
        $donetext="";
     }
     ($oldkoha->{author}) && ($oldkoha->{author}="by $oldkoha->{author}");
-    
+
     $searchfield="";
     foreach $fieldname ( "controlnumber", "lccn", "issn", "isbn") {
        if ( defined $oldkoha->{$fieldname} && $oldkoha->{$fieldname} ) {
@@ -421,161 +458,14 @@ sub ResultRecordLink {
 
 #---------------------------------
 
-sub z3950menu {
-    use strict;
-    my (
-       $dbh,
-       $input,
-    )=@_;
-
-    my (
-       $sth, $sti,
-       $processing,
-       $realenddate,
-       $totalrecords,
-       $elapsed,
-       $elapsedtime,
-       $resultstatus, $statuscolor,
-       $id, $term, $type, $done, 
-       $startdate, $enddate, $servers,
-       $record,$bib,$title,
-    );
-
-    requireDBI($dbh,"z3950menu");
-
-    print "<a href=$ENV{'SCRIPT_NAME'}>Main Menu</a><hr>\n";
-    print "<table border=0><tr><td valign=top>\n";
-    print "<h2>Results of Z39.50 searches</h2>\n";
-    print "<a href=$ENV{'SCRIPT_NAME'}?menu=z3950>Refresh</a><br>\n" .
-         "<ul>\n";
-
-    # Check queued queries
-    $sth=$dbh->prepare("select id,term,type,done,
-               startdate,enddate,servers 
-       from z3950queue 
-       order by id desc 
-       limit 20 ");
-    $sth->execute;
-    while ( ($id, $term, $type, $done, 
-               $startdate, $enddate, $servers) = $sth->fetchrow) {
-       $type=uc($type);
-       $term=~s/</&lt;/g;
-       $term=~s/>/&gt;/g;
-
-       $title="";
-       # See if query produced results
-       $sti=$dbh->prepare("select id,server,startdate,enddate,numrecords,results
-               from z3950results 
-               where queryid=?");
-       $sti->execute($id);
-       if ($sti->rows) {
-           $processing=0;
-           $realenddate=0;
-           $totalrecords=0;
-           while (my ($r_id,$r_server,$r_startdate,$r_enddate,$r_numrecords,$r_marcdata) 
-               = $sti->fetchrow) {
-               if ($r_enddate==0) {
-                   # It hasn't finished yet
-                   $processing=1;
-               } else {
-                   # It finished, see how long it took.
-                   if ($r_enddate>$realenddate) {
-                       $realenddate=$r_enddate;
-                   }
-                   # Snag any title from the results if there were any
-                   if ( ! $title && $r_marcdata ) {
-                       ($record)=parsemarcfileformat($r_marcdata);
-                       $bib=extractmarcfields($record);
-                       if ( $bib->{title} ) { $title=$bib->{title} };
-                   } # if no title yet
-               } # if finished
-
-               $totalrecords+=$r_numrecords;
-           } # while results
-
-           if ($processing) {
-               $elapsed=time()-$startdate;
-               $resultstatus="Processing...";
-               $statuscolor="red";
-           } else {
-               $elapsed=$realenddate-$startdate;
-               $resultstatus="Done.";
-               $statuscolor="black";
-               }
-
-               if ($elapsed>60) {
-                   $elapsedtime=sprintf "%d minutes",($elapsed/60);
-               } else {
-                   $elapsedtime=sprintf "%d seconds",$elapsed;
-               }
-               if ($totalrecords) {
-                   $totalrecords="$totalrecords found.";
-               } else {
-                   $totalrecords='';
-               }
-               print "<li><a href=$ENV{'SCRIPT_NAME'}?file=Z-$id&menu=$menu>".
-               "$type=$term</a>" .
-               "<font size=-1 color=$statuscolor>$resultstatus $totalrecords " .
-               "($elapsedtime) $title </font><br>\n";
-       } else {
-           print "<li><a href=$ENV{'SCRIPT_NAME'}?file=Z-$id&menu=$menu>
-               $type=$term</a> <font size=-1>Pending</font><br>\n";
-       } # if results done
-    } # while queries
-    print "</ul> </td>\n";
-    # End of query listing
-
-    #------------------------------
-    # Search input form
-    print "<td valign=top width=30%>\n";
-
-    my $sth=$dbh->prepare("select id,name,checked 
-       from z3950servers 
-       order by rank");
-    $sth->execute;
-    my $serverlist='';
-    while (my ($id, $name, $checked) = $sth->fetchrow) {
-       ($checked) ? ($checked='checked') : ($checked='');
-       $serverlist.="<input type=checkbox name=S-$id $checked> $name<br>\n";
-    }
-    $serverlist.="<input type=checkbox name=S-MAN> <input name=manualz3950server size=25 value=otherserver:210/DATABASE>\n";
-    
-    my $rand=rand(1000000000);
-print << "EOF";
-    <form action=$ENV{'SCRIPT_NAME'} method=GET>
-    <input type=hidden name=z3950queue value=1>
-    <input type=hidden name=menu value=$menu>
-    <p>
-    <input type=hidden name=test value=testvalue>
-    <input type=hidden name=rand value=$rand>
-        <table border=1 bgcolor=#dddddd>
-           <tr><th bgcolor=#bbbbbb colspan=2>Search for MARC records</th></tr>
-    <tr><td>Query Term</td><td><input name=query></td></tr>
-    <tr><td colspan=2 align=center>
-               <input type=radio name=type value=isbn checked>&nbsp;ISBN 
-               <input type=radio name=type value=lccn        >&nbsp;LCCN<br>
-               <input type=radio name=type value=author      >&nbsp;Author 
-               <input type=radio name=type value=title       >&nbsp;Title 
-               <input type=radio name=type value=keyword     >&nbsp;Keyword</td></tr>
-            <tr><td colspan=2> $serverlist </td></tr>
-            <tr><td colspan=2 align=center> <input type=submit> </td></tr>
-    </table>
-
-    </form>
-EOF
-    print "</td></tr></table>\n";
-} # sub z3950menu
-#---------------------------------
 
 sub uploadmarc {
     use strict;
-    my ($dbh)=@_;
+    my ($dbh)=@_;              # FIXME - Unused argument
 
-    requireDBI($dbh,"uploadmarc");
+    $dbh = C4::Context->dbh;
 
-    my $templatebase="marcimport/uploadmarc.tmpl";
-    my $theme=picktemplate($includes, $templatebase);
-    my $template = HTML::Template->new(filename => "$includes/templates/$theme/$templatebase", die_on_bad_params => 0, path => [$includes]);
+    my $template=gettemplate('marcimport/uploadmarc.tmpl');
     $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'});
 #    print "<a href=$ENV{'SCRIPT_NAME'}>Main Menu</a><hr>\n";
     my $sth=$dbh->prepare("select id,name from uploadedmarc");
@@ -599,90 +489,21 @@ sub manual {
 
 
 sub mainmenu {
-       my $templatebase="marcimport/mainmenu.tmpl";
-       my $theme=picktemplate($includes, $templatebase);
-       my $template = HTML::Template->new(filename => "$includes/templates/$theme/$templatebase", die_on_bad_params => 0, path => [$includes]);
+       my $template=gettemplate('marcimport/mainmenu.tmpl');
        $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'});
        print "Content-Type: text/html\n\n", $template->output;
 } # sub mainmenu
 
-#----------------------------
-# Accept form results to add query to z3950 queue
-sub AcceptZ3950Queue {
-    use strict;
-
-    # input parameters
-    my (
-       $dbh,           # DBI handle
-       $input,         # CGI parms
-    )=@_;
-
-    my @serverlist;
-    my $error;
-
-    requireDBI($dbh,"AcceptZ3950Queue");
-
-    my $query=$input->param('query');
-
-    my $isbngood=1;
-    if ($input->param('type') eq 'isbn') {
-       $isbngood=checkvalidisbn($query);
-    }
-    if ($isbngood) {
-    foreach ($input->param) {
-       if (/S-(.*)/) {
-           my $server=$1;
-           if ($server eq 'MAN') {
-                push @serverlist, "MAN/".$input->param('manualz3950server')."//"
-;
-           } else {
-                push @serverlist, $server;
-            }
-          }
-        }
-
-       $error=addz3950queue($dbh,$input->param('query'), $input->param('type'), 
-               $input->param('rand'), @serverlist);
-       if ( $error ) {
-           print qq|
-<table border=1 cellpadding=5 cellspacing=0 align=center>
-<tr><td bgcolor=#99cc33 background=/images/background-acq.gif colspan=2><font color=red><b>Error</b></font></td></tr>
-<tr><td colspan=2>
-<b>$error</b><p>
-|;
-           if ( $error =~ /daemon/i ) {
-               print qq|
-There is a launcher for the Z39.50 client daemon in your intranet installation<br>
-directory under <b>./scripts/z3950daemon/z3950-daemon-launch.sh</b>.  This<br>
-script should be run as root, and it will start up the program running with the<br>
-privileges of your apache user.  Ideally, this script should be started from a<br>
-system init directory so that is running after the machine starts up.
-|;
-       
-           } # if daemon
-           print qq|
-</td></tr>
-</table>
-
-<table border
-
-|;
-       } # if error
-    } else {
-       print "<font color=red size=+1>$query is not a valid ISBN
-       Number</font><p>\n";
-    }
-} # sub AcceptZ3950Queue
-
 #---------------------------------------------
 sub AcceptMarcUpload {
     use strict;
     my (
        $dbh,           # DBI handle
+                       # FIXME - Unused argument
        $input,         # CGI parms
     )=@_;
 
-    requireDBI($dbh,"AcceptMarcUpload");
+    $dbh = C4::Context->dbh;
 
     my $name=$input->param('name');
     my $data=$input->param('uploadmarc');
@@ -696,8 +517,8 @@ sub AcceptMarcUpload {
     }
     my $q_marcrecord=$dbh->quote($marcrecord);
     my $q_name=$dbh->quote($name);
-    my $sth=$dbh->prepare("insert into uploadedmarc 
-               (marc,name) 
+    my $sth=$dbh->prepare("insert into uploadedmarc
+               (marc,name)
        values ($q_marcrecord, $q_name)");
     $sth->execute;
 } # sub AcceptMarcUpload
@@ -706,7 +527,7 @@ sub AcceptMarcUpload {
 sub AcceptBiblioitem {
     use strict;
     my (
-       $dbh,
+       $dbh,                   # FIXME - Unused argument
        $input,
     )=@_;
 
@@ -715,7 +536,7 @@ sub AcceptBiblioitem {
     my $sth;
     my $record;
 
-    requireDBI($dbh,"AcceptBiblioitem");
+    $dbh = C4::Context->dbh;
 
 #    my $isbn=$input->param('isbn');
 #    my $issn=$input->param('issn');
@@ -732,21 +553,20 @@ sub AcceptBiblioitem {
     my $file= MARC::File::USMARC->indata($input->param('file'));
     my $numrecord = $input->param('numrecord');
     if ($numrecord) {
-       for (my $i==1;$i<$numrecord;$i++) {
+       for (my $i=1;$i<$numrecord;$i++) {
            $record=$file->next;
        }
     } else {
        print STDERR "Error in marcimport.pl/Acceptbiblioitem : numrecord not defined\n";
        print "Error in marcimport.pl/Acceptbiblioitem : numrecord not defined : contact administrator\n";
     }
-    my $templatebase="marcimport/AcceptBiblioitem.tmpl";
-    my $theme=picktemplate($includes, $templatebase);
-    my $template = HTML::Template->new(filename => "$includes/templates/$theme/$templatebase", die_on_bad_params => 0, path => [$includes]);
+    my $template=gettemplate('marcimport/AcceptBiblioitem.tmpl');
 
     my $oldkoha = MARCmarc2koha($dbh,$record);
     # See if it already exists
-    my $sth=$dbh->prepare("select biblionumber,biblioitemnumber 
-       from biblioitems 
+    # FIXME - There's already a $sth in this context.
+    my $sth=$dbh->prepare("select biblionumber,biblioitemnumber
+       from biblioitems
        where isbn=? or issn=? or lccn=?");
     $sth->execute($oldkoha->{isbn},$oldkoha->{issn},$oldkoha->{lccn});
     if ($sth->rows) {
@@ -764,14 +584,16 @@ sub AcceptBiblioitem {
        my $error;
        my %biblio;
        my %biblioitem;
-  
+
        # convert to upper case and split on lines
        my $subjectheadings=$input->param('subject');
        my @subjectheadings=split(/[\r\n]+/,$subjectheadings);
-  
+
        my $additionalauthors=$input->param('additionalauthors');
        my @additionalauthors=split(/[\r\n]+|\|/,uc($additionalauthors));
-  
+                       # FIXME - WTF are the additional authors
+                       # converted to upper case?
+
        # Use individual assignments to hash buckets, in case
        #  any of the input parameters are empty or don't exist
        $biblio{title}          =$input->param('title');
@@ -781,7 +603,7 @@ sub AcceptBiblioitem {
        $biblio{notes}          =$input->param('notes');
        $biblio{abstract}       =$input->param('abstract');
        $biblio{subtitle}       =$input->param('subtitle');
-  
+
        $biblioitem{volume}             =$input->param('volume');
        $biblioitem{number}             =$input->param('number');
        $biblioitem{itemtype}           =$input->param('itemtype');
@@ -811,10 +633,10 @@ sub AcceptBiblioitem {
 #              \@subjectheadings,
 #              \@additionalauthors
 #      );
-  
+
        if ( $error ) {
            print "<H2>Error adding biblio item</H2> $error\n";
-       } else { 
+       } else {
            $template->param(title => $title);
            $template->param(biblionumber => $biblionumber);
            $template->param(biblioitemnumber => $biblioitemnumber);
@@ -844,20 +666,19 @@ sub AcceptBiblioitem {
 sub AcceptItemCopy {
     use strict;
     my ( $dbh, $input )=@_;
+                       # FIXME - $dbh argument unused
 
-    my $templatebase="marcimport/AcceptItemCopy.tmpl";
-    my $theme=picktemplate($includes, $templatebase);
-    my $template = HTML::Template->new(filename => "$includes/templates/$theme/$templatebase", die_on_bad_params => 0, path => [$includes]);
+    my $template=gettemplate('marcimport/AcceptItemCopy.tmpl');
 
     my $error;
 
-    requireDBI($dbh,"AcceptItemCopy");
+    $dbh = C4::Context->dbh;
 
     my $barcode=$input->param('barcode');
     my $replacementprice=($input->param('replacementprice') || 0);
 
-    my $sth=$dbh->prepare("select barcode 
-       from items 
+    my $sth=$dbh->prepare("select barcode
+       from items
        where barcode=?");
     $sth->execute($barcode);
     if ($sth->rows) {
@@ -986,52 +807,60 @@ sub FormatMarcText {
 
 
 #---------------
+# log cleared, as marcimport is (almost) rewritten from scratch.
 # $Log$
-# Revision 1.12  2002/07/24 16:24:20  tipaul
-# Now, the acqui.simple system...
-# marcimport.pl has been almost completly rewritten, so LOT OF BUGS TO COME !!! You've been warned. It seems to work, but...
+# Revision 1.29  2003/01/28 15:28:31  tipaul
+# removing use MARC::Charset
+# Was a buggy test
+#
+# Revision 1.28  2003/01/28 15:00:31  tipaul
+# user can now search in breeding farm with isbn/issn or title. Title/name are stored in breeding farm and showed when a search is done
 #
-# As with my former messages, nothing seems to have been changed... but ...
-# * marcimport now uses HTML::Template.
-# * marcimport now uses MARC::Record. that means that when you import a record, the old-DB is populated with the data as in version 1.2, but the MARC-DB part is filled with full MARC::Record.
+# Revision 1.27  2003/01/26 23:21:49  acli
+# Handle non-latin1 charsets
 #
-# <IMPORTANT NOTE>
-# to get correct response times, you MUST add an index on isbn, issn and lccn rows in biblioitem table. Note this should be done in 1.2 too...
-# </IMPORTANT NOTE>
+# Revision 1.26  2003/01/23 12:26:41  tipaul
+# upgrading import in breeding farm (you can now search on ISBN or on title) AND character encoding.
 #
-# <IMPORTANT NOTE2>
-# acqui.simple manage biblio, biblioitems and items tables quite properly. Normal acquisition system manages biblio, biblioitems BUT NOT items. That will be done in the near future...
-# </IMPORTANT NOTE2>
+# Revision 1.25  2003/01/21 08:13:50  tipaul
+# character encoding ISO646 => 8859-1, first draft
 #
-# what's next now ?
-# * bug tracking, of course... Surely a dozen of dozens...
-# * LOT of developpments, i'll surely write a mail to koha-devel tomorrow (as it's time for dinner in France, and i plan to play NeverwinterNights after dinner ;-) ...
+# Revision 1.24  2003/01/14 16:41:17  tipaul
+# bugfix : use gettemplate_and_user instead of gettemplate.
+# fix a blank screen in 1.3.3 in "import in breeding farm"
 #
-# Revision 1.6.2.32  2002/06/29 17:33:47  amillar
-# Allow DEFAULT as input to addz3950search.
-# Check for existence of pid file (cat crashed otherwise).
-# Return error messages in addz3950search.
+# Revision 1.23  2003/01/06 13:06:28  tipaul
+# removing trailing #
 #
-# Revision 1.6.2.31  2002/06/28 18:50:46  tonnesen
-# Got rid of white text on black, replaced with black on background-acq.gif
+# Revision 1.22  2002/11/12 15:58:43  tipaul
+# road to 1.3.2 :
+# * many bugfixes
+# * adding value_builder : you can map a subfield in the marc_subfield_structure to a sub stored in "value_builder" directory. In this directory you can create screen used to build values with any method. In this commit is a 1st draft of the builder for 100$a unimarc french subfield, which is composed of 35 digits, with 12 differents values (only the 4th first are provided for instance)
 #
-# Revision 1.6.2.30  2002/06/28 18:07:27  tonnesen
-# marcimport.pl will print an error message if it can not signal the
-# processz3950queue program.  The message contains instructions for starting the
-# daemon.
+# Revision 1.21  2002/10/22 15:50:23  tipaul
+# road to 1.3.2 : adding a biblio in MARC format.
+# seems to work a few.
+# still to do :
+# * manage html checks (mandatory subfields...)
+# * add list of acceptable values (authorities)
+# * manage ## in MARC format
+# * manage correctly repeatable fields
+# and probably a LOT of bugfixes
 #
-# Revision 1.6.2.29  2002/06/27 18:35:01  tonnesen
-# $deweyinput was always defined (it's an HTML input field).  Check against
-# $bib->{dewey} instead.
+# Revision 1.20  2002/10/16 12:46:19  arensb
+# Added a FIXME comment.
 #
-# Revision 1.6.2.28  2002/06/27 17:41:26  tonnesen
-# Applying patch from Matt Kraai to pick F or NF based on presense of a dewey
-# number when adding a book via marcimport.pl
+# Revision 1.19  2002/10/15 10:14:44  tipaul
+# road to 1.3.2. Full rewrite of marcimport.pl.
+# The acquisition system in MARC version will work like this :
+# * marcimport will put marc records into a "breeding farm" table.
+# * when the user want to add a biblio, he enters first the ISBN/ISSN of the biblio. koha searches into breeding farm and if the record exists, it is shown to the user to help him adding the biblio. When the biblio is added, it's deleted from the breeding farm.
 #
-# Revision 1.6.2.27  2002/06/26 15:52:55  amillar
-# Fix display of marc tag labels and indicators
+# This commit :
+# * modify acqui.simple home page  (addbooks.pl)
+# * adds import into breeding farm
 #
-# Revision 1.6.2.26  2002/06/26 14:28:35  amillar
-# Removed subroutines now existing in modules: extractmarcfields,
-#  parsemarcfileformat, addz3950queue, getkeytableselectoptions
+# Please note that :
+# * z3950 functionnality is dropped from "marcimport" will be added somewhere else.
+# * templates are in a new acqui.simple sub directory, and the marcimport template directory will become obsolete soon.I think this is more logic
 #