Bug 12478: more authorities
authorRobin Sheat <robin@catalyst.net.nz>
Tue, 3 Mar 2015 01:02:19 +0000 (14:02 +1300)
committerTomas Cohen Arazi <tomascohen@theke.io>
Tue, 26 Apr 2016 12:55:17 +0000 (09:55 -0300)
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Jesse Weaver <jweaver@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Koha/SearchEngine/Elasticsearch/Search.pm
Koha/SearchEngine/QueryBuilder.pm

index c1d3adc..33644a4 100644 (file)
@@ -150,26 +150,56 @@ sub search_auth_compat {
     my $self = shift;
 
     my $database = Koha::Database->new();
-    my $schema = $database->schema();
-    my $res = $self->search(@_);
+    my $schema   = $database->schema();
+    my $res      = $self->search(@_);
     my @records;
-    $res->each(sub {
+    $res->each(
+        sub {
             my %result;
-            my $record = @_[0];
+            my $record    = @_[0];
             my $marc_json = $record->{record};
+
             # I wonder if these should be real values defined in the mapping
             # rather than hard-coded conversions.
-            $result{authid} = $record{Local-Number};
+            $result{authid} = $record->{ Local-Number };
+
             # TODO put all this info into the record at index time so we
             # don't have to go and sort it all out now.
-            my $rs = $schema->resultset('auth_types')->search({ authtypecode => $authtypecode });
-            my $authtype = $rs->first;
-            my $authtypecode = $record{authtype};
-            my $marc = $self->json2marc($marc_json);
-            die Dumper(\@_);
+            my $authtypecode = $record->{authtype};
+            my $rs           = $schema->resultset('AuthType')
+              ->search( { authtypecode => $authtypecode } );
+
+            # FIXME there's an assumption here that we will get a result.
+            # the original code also makes an assumption that some provided
+            # authtypecode may sometimes be used instead of the one stored
+            # with the record. It's not documented why this is the case, so
+            # it's not reproduced here.
+            my $authtype           = $rs->single;
+            my $auth_tag_to_report = $authtype->auth_tag_to_report;
+            my $marc               = $self->json2marc($marc_json);
+            my $mainentry          = $marc->field($auth_tag_to_report);
+            my $reported_tag;
+            if ($mainentry) {
+                foreach ( $mainentry->subfields() ) {
+                    $reported_tag .= '$' . $_->[0] . $_->[1];
+                }
+            }
+            # Turn the resultset into a hash
+            my %authtype_cols;
+            foreach my $col (@{ $authtype->result_source->columns }) {
+                $authtype_cols{$col} = $authtype->get_column($col);
+            }
+            $result{authtype}     = $authtype_cols;
+            $result{reported_tag} = $reported_tag;
+
+            # Reimplementing BuildSummary is out of scope because it'll be hard
+            $result{summary} =
+              C4::AuthoritiesMarc::BuildSummary( $marc, $result{authid},
+                $authtypecode );
             push @records, $marc;
-        });
-    return (\@records, $res->total);
+        }
+    );
+    return ( \@records, $res->total );
 }
 
 =head2 json2marc
index aaaba01..9a9b712 100644 (file)
@@ -45,11 +45,13 @@ Creates a new C<QueryBuilder> of whatever the relevant type is.
 
 use C4::Context;
 use Modern::Perl;
+use Carp;
 
 sub new {
     my $engine = C4::Context->preference("SearchEngine");
     my $file = "Koha/SearchEngine/${engine}/QueryBuilder.pm";
     my $class = "Koha::SearchEngine::${engine}::QueryBuilder";
+    croak "SearchEngine system preference not set" unless $engine;
     require $file;
     shift @_;
     return $class->new(@_);