bugfix for 2451 : serial recieve & barcode does exist complaint
[koha.git] / cataloguing / z3950_search.pl
index 87f7712..aa16706 100755 (executable)
@@ -27,6 +27,7 @@ use C4::Biblio;
 use C4::Context;
 use C4::Breeding;
 use C4::Koha;
+use C4::Charset;
 use ZOOM;
 
 my $input        = new CGI;
@@ -39,6 +40,9 @@ my $title         = $input->param('title');
 my $author        = $input->param('author');
 my $isbn          = $input->param('isbn');
 my $issn          = $input->param('issn');
+my $lccn          = $input->param('lccn');
+my $subject= $input->param('subject');
+my $dewey = $input->param('dewey');
 my $random        = $input->param('random');
 my $op            = $input->param('op');
 my $noconnection;
@@ -59,9 +63,10 @@ my $oldbiblio;
 my $errmsg;
 my @serverloop = ();
 my @serverhost;
+my @servername;
 my @breeding_loop = ();
 
-my $DEBUG = 1;    # if set to 1, many debug message are send on syslog.
+my $DEBUG = 0;    # if set to 1, many debug message are send on syslog.
 
 unless ($random)
 {    # this var is not useful anymore just kept to keep rel2_2 compatibility
@@ -82,12 +87,13 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 $template->param( frameworkcode => $frameworkcode, );
 
 if ( $op ne "do_search" ) {
-    my $sth = $dbh->prepare("select id,host,checked from z3950servers  order by host");
+    my $sth = $dbh->prepare("select id,host,name,checked from z3950servers  order by host");
     $sth->execute();
     my $serverloop = $sth->fetchall_arrayref( {} );
     $template->param(
         isbn         => $isbn,
         issn         => $issn,
+        lccn         => $lccn,
         title        => $title,
         author       => $author,
         serverloop   => $serverloop,
@@ -101,29 +107,47 @@ else {
     my @oConnection;
     my @oResult;
     my $s = 0;
-
-    if ( $isbn || $issn ) {
-        $attr = '1=7';
-        $term = $isbn if ($isbn);
-        $term = $issn if ($issn);
+    my $query;
+    my $nterms;
+    if ($isbn || $issn) {
+        $term=$isbn if ($isbn);
+        $term=$issn if ($issn);
+        $query .= " \@or \@attr 1=8 \"$term\" \@attr 1=7 \"$term\" ";
+        $nterms++;
     }
-    elsif ($title) {
-        $attr = '1=4 ';
+    if ($title) {
         utf8::decode($title);
-        $term = $title;
+        $query .= " \@attr 1=4 \"$title\" ";
+        $nterms++;
     }
-    elsif ($author) {
-        $attr = '1=1003';
+    if ($author) {
         utf8::decode($author);
-        $term = $author;
+        $query .= " \@attr 1=1003 \"$author\" ";
+        $nterms++;
+    }
+    if ($dewey) {
+        $query .= " \@attr 1=16 \"$dewey\" ";
+        $nterms++;
+    }
+    if ($subject) {
+        utf8::decode($subject);
+        $query .= " \@attr 1=21 \"$subject\" ";
+        $nterms++;
     }
+       if ($lccn) {    
+        $query .= " \@attr 1=9 $lccn ";
+        $nterms++;
+    }
+for my $i (1..$nterms-1) {
+    $query = "\@and " . $query;
+}
+warn "query ".$query  if $DEBUG;
 
-    my $query = "\@attr $attr \"$term\"";
-    warn "query " . $query if $DEBUG;
     foreach my $servid (@id) {
         my $sth = $dbh->prepare("select * from z3950servers where id=?");
         $sth->execute($servid);
         while ( $server = $sth->fetchrow_hashref ) {
+            warn "serverinfo ".join(':',%$server) if $DEBUG;
             my $noconnection = 0;
             my $option1      = new ZOOM::Options();
             $option1->option( 'async' => 1 );
@@ -135,13 +159,14 @@ else {
             $option1->option( 'preferredRecordSyntax', $server->{syntax} );
             $oConnection[$s] = create ZOOM::Connection($option1)
               || $DEBUG
-              && warn( "something went wrong: " . $oConnection[$s]->errmsg() );
+              && warn( "" . $oConnection[$s]->errmsg() );
             warn( "server data", $server->{name}, $server->{port} ) if $DEBUG;
             $oConnection[$s]->connect( $server->{host}, $server->{port} )
               || $DEBUG
-              && warn( "something went wrong: " . $oConnection[$s]->errmsg() );
+              && warn( "" . $oConnection[$s]->errmsg() );
             $serverhost[$s] = $server->{host};
-            $encoding[$s]   = $server->{syntax};
+            $servername[$s] = $server->{name};
+            $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
             $s++;
         }    ## while fetch
     }    # foreach
@@ -154,7 +179,7 @@ else {
           || $DEBUG
           && warn( "somthing went wrong: " . $oConnection[$s]->errmsg() );
 
-        #$oResult[$z] = $oConnection[$z]->search_pqf($query);
+        # $oResult[$z] = $oConnection[$z]->search_pqf($query);
     }
 
   AGAIN:
@@ -190,37 +215,45 @@ else {
                   )
                 {
                     my $rec = $oResult[$k]->record($i);
-                    my $marcrecord;
-                    $marcdata   = $rec->raw();
-                    $marcrecord = FixEncoding($marcdata);
+                    if ($rec) {
+                        my $marcrecord;
+                        $marcdata   = $rec->raw();
+
+                        my ($charset_result, $charset_errors);
+                        ($marcrecord, $charset_result, $charset_errors) = 
+                          MarcToUTF8Record($marcdata, C4::Context->preference('marcflavour'), $encoding[$k]);
 ####WARNING records coming from Z3950 clients are in various character sets MARC8,UTF8,UNIMARC etc
 ## In HEAD i change everything to UTF-8
 # In rel2_2 i am not sure what encoding is so no character conversion is done here
 ##Add necessary encoding changes to here -TG
-                    my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
-                    $oldbiblio->{isbn}   =~ s/ |-|\.//g,
-                      $oldbiblio->{issn} =~ s/ |-|\.//g,
-                      my (
-                        $notmarcrecord, $alreadyindb, $alreadyinfarm,
-                        $imported,      $breedingid
-                      )
-                      = ImportBreeding( $marcdata, 1, $serverhost[$k], $encoding[$k], $random );
-
-                    my %row_data;
-                    if ( $i % 2 ) {
-                        $toggle = "#ffffcc";
-                    }
-                    else {
-                        $toggle = "white";
-                    }
-                    $row_data{toggle}       = $toggle;
-                    $row_data{server}       = $serverhost[$k];
-                    $row_data{isbn}         = $oldbiblio->{isbn};
-                    $row_data{title}        = $oldbiblio->{title};
-                    $row_data{author}       = $oldbiblio->{author};
-                    $row_data{breedingid}   = $breedingid;
-                    $row_data{biblionumber} = $biblionumber;
-                    push( @breeding_loop, \%row_data );
+                        my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, "" );
+                        $oldbiblio->{isbn}   =~ s/ |-|\.//g,
+                          $oldbiblio->{issn} =~ s/ |-|\.//g,
+                          my (
+                            $notmarcrecord, $alreadyindb, $alreadyinfarm,
+                            $imported,      $breedingid
+                          )
+                          = ImportBreeding( $marcdata, 2, $serverhost[$k], $encoding[$k], $random, 'z3950' );
+                        my %row_data;
+                        if ( $i % 2 ) {
+                            $toggle = 1;
+                        }
+                        else {
+                            $toggle = 0;
+                        }
+                        $row_data{toggle}       = $toggle;
+                        $row_data{server}       = $servername[$k];
+                        $row_data{isbn}         = $oldbiblio->{isbn};
+                        $row_data{lccn}         = $oldbiblio->{lccn};
+                        $row_data{title}        = $oldbiblio->{title};
+                        $row_data{author}       = $oldbiblio->{author};
+                        $row_data{breedingid}   = $breedingid;
+                        $row_data{biblionumber} = $biblionumber;
+                        push( @breeding_loop, \%row_data );
+                           
+                    } else {
+                        push(@breeding_loop,{'toggle'=>($i % 2)?1:0,'server'=>$servername[$k],'title'=>join(': ',$oConnection[$k]->error_x()),'breedingid'=>-1,'biblionumber'=>-1});
+                    } # $rec
                 }    # upto 5 results
             }    #$numresults
         }
@@ -228,7 +261,7 @@ else {
     $numberpending = $nremaining - 1;
     $template->param(
         breeding_loop => \@breeding_loop,
-        server        => $serverhost[$k],
+        server        => $servername[$k],
         numberpending => $numberpending,
     );