From c27fe2a2abad8aeacba65d5d96a2b54f3de1279c Mon Sep 17 00:00:00 2001
From: Jesse Weaver <jweaver@bywatersolutions.com>
Date: Fri, 29 Jan 2016 15:56:20 -0700
Subject: [PATCH] Bug 19269: Rancor - Add LCCN, pubdate to indexing

This patch fills the two new columns in the import_biblios table:
lccn, pubdate
It also fixes update of ocntrol number in the import_biblios table

Because 010 fields can have leading and trailing whitespace, added trim
before INSERT into import_biblios table. -cori lynn

To test:
1 - Apply and update database
2 - Import some records
3 - Check the import table to see new columns are populated correctly
4 - check control number is correct in table too
5 - Open advanced cataloging editor
6 - Set your batch as searchable (settings->import batches)
7 - You should now be able to search batches for lccn or pubdate or
control number
8 - Verify records are found as expected and can be saved to catalog or
batch
---
 C4/ImportBatch.pm    | 27 ++++++++++++++++-----------
 Koha/MetaSearcher.pm |  2 ++
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm
index b2d53ae6b7..4595abe1ce 100644
--- a/C4/ImportBatch.pm
+++ b/C4/ImportBatch.pm
@@ -1660,16 +1660,13 @@ sub _add_auth_fields {
 sub _add_biblio_fields {
     my ($import_record_id, $marc_record) = @_;
 
-    my $controlnumber;
-    if ($marc_record->field('001')) {
-        $controlnumber = $marc_record->field('001')->data();
-    }
-    my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
+    my ($title, $author, $isbn, $issn, $controlnumber, $lccn, $pubdate) = _parse_biblio_fields($marc_record);
     my $dbh = C4::Context->dbh;
     # FIXME no originalsource
     $isbn = C4::Koha::GetNormalizedISBN($isbn);
-    my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn, control_number) VALUES (?, ?, ?, ?, ?, ?)");
-    $sth->execute($import_record_id, $title, $author, $isbn, $issn, $controlnumber);
+    $lccn =~ s/^\s+|\s+$//g;
+    my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn, control_number, lccn, pubdate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
+    $sth->execute($import_record_id, $title, $author, $isbn, $issn, $controlnumber, $lccn, $pubdate);
     $sth->finish();
 
 }
@@ -1677,14 +1674,14 @@ sub _add_biblio_fields {
 sub _update_biblio_fields {
     my ($import_record_id, $marc_record) = @_;
 
-    my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
+    my ($title, $author, $isbn, $issn, $controlnumber, $lccn, $pubdate) = _parse_biblio_fields($marc_record);
     my $dbh = C4::Context->dbh;
     # FIXME no originalsource
     # FIXME 2 - should regularize normalization of ISBN wherever it is done
     $isbn = C4::Koha::GetNormalizedISBN($isbn);
-    my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
+    my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?, control_number = ?, lccn = ?, pubdate = ?
                              WHERE  import_record_id = ?");
-    $sth->execute($title, $author, $isbn, $issn, $import_record_id);
+    $sth->execute($title, $author, $isbn, $issn, $controlnumber, $lccn, $pubdate, $import_record_id);
     $sth->finish();
 }
 
@@ -1693,7 +1690,15 @@ sub _parse_biblio_fields {
 
     my $dbh = C4::Context->dbh;
     my $bibliofields = TransformMarcToKoha($marc_record, '');
-    return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
+    my $controlnumber;
+    if ($marc_record->field('001')) {
+        $controlnumber = $marc_record->field('001')->data();
+    }
+    my $pubdate;
+    if ($marc_record->field('008')) {
+        $pubdate = substr( $marc_record->field('008')->data(), 7, 4 );
+    }
+    return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'}, $controlnumber, $bibliofields->{'lccn'}, $pubdate);
 
 }
 
diff --git a/Koha/MetaSearcher.pm b/Koha/MetaSearcher.pm
index 53c645279c..2ee056f975 100644
--- a/Koha/MetaSearcher.pm
+++ b/Koha/MetaSearcher.pm
@@ -205,6 +205,8 @@ our $_batch_db_mapping = {
     issn => 'import_biblios.issn',
     'local-number' => 'import_biblios.control_number',
     title => 'import_biblios.title',
+    lccn => 'import_biblios.lccn',
+    date => 'import_biblios.pubdate',
 };
 
 our $_batch_db_text_columns = {
-- 
2.11.0