Bug 32426: (follow-up) Fix api/v1/patrons.t
[srvgit] / C4 / Breeding.pm
index c3e6c7c..7b3dbc5 100644 (file)
@@ -21,23 +21,22 @@ package C4::Breeding;
 use strict;
 use warnings;
 
-use C4::Biblio;
-use C4::Koha;
-use C4::Charset;
+use C4::Biblio qw(TransformMarcToKoha);
+use C4::Koha qw( GetVariationsOfISBN );
+use C4::Charset qw( MarcToUTF8Record SetUTF8Flag );
 use MARC::File::USMARC;
 use MARC::Field;
-use C4::ImportBatch;
-use C4::AuthoritiesMarc; #GuessAuthTypeCode, FindDuplicateAuthority
+use C4::ImportBatch qw( GetZ3950BatchId AddBiblioToBatch AddAuthToBatch );
+use C4::AuthoritiesMarc qw( GuessAuthTypeCode );
 use C4::Languages;
 use Koha::Database;
-use Koha::XSLT_Handler;
-
-use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
+use Koha::XSLT::Base;
 
+our (@ISA, @EXPORT_OK);
 BEGIN {
-       require Exporter;
-       @ISA = qw(Exporter);
-    @EXPORT = qw(&BreedingSearch &Z3950Search &Z3950SearchAuth);
+    require Exporter;
+    @ISA       = qw(Exporter);
+    @EXPORT_OK = qw(BreedingSearch Z3950Search Z3950SearchAuth);
 }
 
 =head1 NAME
@@ -57,9 +56,8 @@ cataloguing reservoir features.
 
 =head2 BreedingSearch
 
-($count, @results) = &BreedingSearch($title,$isbn);
-C<$title> contains the title,
-C<$isbn> contains isbn or issn,
+($count, @results) = &BreedingSearch($term);
+C<$term> contains the term to search, it will be searched as title,author, or isbn
 
 C<$count> is the number of items in C<@results>. C<@results> is an
 array of references-to-hash; the keys are the items from the C<import_records> and
@@ -68,34 +66,30 @@ C<import_biblios> tables of the Koha database.
 =cut
 
 sub BreedingSearch {
-    my ($search,$isbn) = @_;
+    my ($term) = @_;
     my $dbh   = C4::Context->dbh;
     my $count = 0;
     my ($query,@bind);
     my $sth;
     my @results;
 
+    my $authortitle = $term;
+    $authortitle =~ s/(\s+)/\%/g; #Replace spaces with wildcard
+    $authortitle = "%" . $authortitle . "%"; #Add wildcard to start and end of string
     # normalise ISBN like at import
-    $isbn = C4::Koha::GetNormalizedISBN($isbn);
-
-    $query = "SELECT import_record_id, file_name, isbn, title, author
-              FROM  import_biblios 
+    my @isbns = C4::Koha::GetVariationsOfISBN($term);
+
+    $query = "SELECT import_biblios.import_record_id,
+                import_batches.file_name,
+                import_biblios.isbn,
+                import_biblios.title,
+                import_biblios.author,
+                import_batches.upload_timestamp
+              FROM  import_biblios
               JOIN import_records USING (import_record_id)
               JOIN import_batches USING (import_batch_id)
-              WHERE ";
-    @bind=();
-    if (defined($search) && length($search)>0) {
-        $search =~ s/(\s+)/\%/g;
-        $query .= "title like ? OR author like ?";
-        push(@bind,"%$search%", "%$search%");
-    }
-    if ($#bind!=-1 && defined($isbn) && length($isbn)>0) {
-        $query .= " and ";
-    }
-    if (defined($isbn) && length($isbn)>0) {
-        $query .= "isbn like ?";
-        push(@bind,"$isbn%");
-    }
+              WHERE title LIKE ? OR author LIKE ? OR isbn IN (" . join(',',('?') x @isbns) . ")";
+    @bind=( $authortitle, $authortitle, @isbns );
     $sth = $dbh->prepare($query);
     $sth->execute(@bind);
     while (my $data = $sth->fetchrow_hashref) {
@@ -159,11 +153,11 @@ sub Z3950Search {
         $oResult[$s] =
             $server->{servertype} eq 'zed'?
                 $oConnection[$s]->search_pqf( $server_zquery ):
-                $oConnection[$s]->search(new ZOOM::Query::CQL(
+                $oConnection[$s]->search(ZOOM::Query::CQL->new(
                     _translate_query( $server, $squery )));
         $s++;
     }
-    my $xslh = Koha::XSLT_Handler->new;
+    my $xslh = Koha::XSLT::Base->new;
 
     my $nremaining = $s;
     while ( $nremaining-- ) {
@@ -298,6 +292,7 @@ sub _handle_one_result {
     if( $servhref->{servertype} eq 'sru' ) {
         $marcrecord= MARC::Record->new_from_xml( $raw, 'UTF-8',
             $servhref->{syntax} );
+        $marcrecord->encoding('UTF-8');
     } else {
         ($marcrecord) = MarcToUTF8Record($raw, C4::Context->preference('marcflavour'), $servhref->{encoding} // "iso-5426" ); #ignores charset return values
     }
@@ -309,16 +304,17 @@ sub _handle_one_result {
     my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0);
         #Last zero indicates: no update for batch record counts
 
-
-    #call to TransformMarcToKoha replaced by next call
-    #we only need six fields from the marc record
     my $row;
-    $row = _add_rowdata(
-        {
-            biblionumber => $bib,
-            server       => $servhref->{servername},
-            breedingid   => $breedingid,
-        }, $marcrecord) if $breedingid;
+    if( $breedingid ){
+        my @kohafields = ('biblio.title','biblio.author','biblioitems.isbn','biblioitems.lccn','biblioitems.editionstatement');
+        push @kohafields, C4::Context->preference('marcflavour') eq "MARC21" ? 'biblio.copyrightdate' : 'biblioitems.publicationyear';
+        $row = C4::Biblio::TransformMarcToKoha({ record => $marcrecord, kohafields => \@kohafields, limit_table => 'no_items' });
+        $row->{biblionumber} = $bib;
+        $row->{server}       = $servhref->{servername};
+        $row->{breedingid}   = $breedingid;
+        $row->{isbn}=_isbn_replace($row->{isbn});
+        $row = _add_custom_field_rowdata($row, $marcrecord);
+    }
     return ( $row, $error );
 }
 
@@ -346,28 +342,6 @@ sub _do_xslt_proc {
     }
 }
 
-sub _add_rowdata {
-    my ($row, $record)=@_;
-    my %fetch= (
-        title => 'biblio.title',
-        author => 'biblio.author',
-        isbn =>'biblioitems.isbn',
-        lccn =>'biblioitems.lccn', #LC control number (not call number)
-        edition =>'biblioitems.editionstatement'
-    );
-    $fetch{date} = C4::Context->preference('marcflavour') eq "MARC21" ? 'biblio.copyrightdate' : 'biblioitems.publicationyear';
-
-    foreach my $k (keys %fetch) {
-        $row->{$k} = C4::Biblio::TransformMarcToKohaOneField( $fetch{$k}, $record );
-    }
-    $row->{date}//= $row->{date2};
-    $row->{isbn}=_isbn_replace($row->{isbn});
-
-    $row = _add_custom_field_rowdata($row, $record);
-
-    return $row;
-}
-
 sub _add_custom_field_rowdata
 {
     my ( $row, $record ) = @_;
@@ -429,7 +403,7 @@ sub _isbn_replace {
 
 sub _create_connection {
     my ( $server ) = @_;
-    my $option1= new ZOOM::Options();
+    my $option1= ZOOM::Options->new();
     $option1->option( 'async' => 1 );
     $option1->option( 'elementSetName', 'F' );
     $option1->option( 'preferredRecordSyntax', $server->{syntax} );
@@ -567,7 +541,7 @@ sub Z3950SearchAuth {
             }
             else {
                 $oResult[$s] = $oConnection[$s]->search(
-                    new ZOOM::Query::CQL(_translate_query( $server, $squery ))
+                    ZOOM::Query::CQL->new(_translate_query( $server, $squery ))
                 );
             }
             $encoding[$s]   = ($server->{encoding}?$server->{encoding}:"iso-5426");
@@ -610,12 +584,15 @@ sub Z3950SearchAuth {
                             my ($charset_result, $charset_errors);
                             if( $servers[$k]->{servertype} eq 'sru' ) {
                                 $marcrecord = MARC::Record->new_from_xml( $marcdata, 'UTF-8', $servers[$k]->{syntax} );
+                                $marcrecord->encoding('UTF-8');
                             } else {
                                 ( $marcrecord, $charset_result, $charset_errors ) = MarcToUTF8Record( $marcdata, $marc_type, $encoding[$k] );
                             }
                             my $heading;
                             my $heading_authtype_code;
                             $heading_authtype_code = GuessAuthTypeCode($marcrecord);
+                            next if ( not defined $heading_authtype_code ) ;
+
                             $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord });
 
                             my $breedingid = ImportBreedingAuth( $marcrecord, $serverhost[$k], $encoding[$k], $heading );