OPAC Browse: Fix authority counting
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Thu, 9 May 2013 22:29:10 +0000 (18:29 -0400)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Thu, 9 May 2013 22:29:10 +0000 (18:29 -0400)
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Browse.pm
Open-ILS/src/templates/opac/browse.tt2
Open-ILS/src/templates/opac/css/style.css.tt2

index f98645b..98cfb98 100644 (file)
@@ -17,6 +17,7 @@ use UUID::Tiny;
 use Encode;
 use DateTime;
 use DateTime::Format::ISO8601;
+use List::MoreUtils qw/uniq/;
 
 # ---------------------------------------------------------------------------
 # Pile of utilty methods used accross applications.
@@ -2119,6 +2120,18 @@ sub strip_marc_fields {
        return $class->entityize($marcdoc->documentElement->toString);
 }
 
+# Given a list of PostgreSQL arrays of numbers,
+# unnest the numbers and return a unique set, skipping any list elements
+# that are just '{NULL}'.
+sub unique_unnested_numbers {
+    return uniq(
+        map(
+            int,
+            map { $_ eq 'NULL' ? undef : (split /,/, $_) }
+                map { substr($_, 1, -1) } @_
+        )
+    );
+}
 
 1;
 
index 755687d..f35cabd 100644 (file)
@@ -109,9 +109,9 @@ sub find_authority_headings {
             # points to another record (not that it makes much sense for
             # one authority record to have links to multiple others, but I can't
             # say there definitely aren't cases for that).
-            ($field->subfield('0') || "") =~ /(^|\))(\d+)$/;
+            $h->{target} = $2
+                if ($field->subfield('0') || "") =~ /(^|\))(\d+)$/;
 
-            $h->{target} = $2 if defined $2;
             push @headings, $h;
         }
 
@@ -166,12 +166,22 @@ sub flesh_browse_results {
         # Get all linked authority records themselves
         my $linked = $self->editor->json_query({
             select => {
-                are => [qw/id marc control_set/]
+                are => [qw/id marc control_set/],
+                aalink => [{column => "target", transform => "array_agg",
+                    aggregate => 1}]
+            },
+            from => {
+                are => {
+                    aalink => {
+                        type => "left",
+                        fkey => "id", field => "source"
+                    }
+                }
             },
-            from => { are => { } },
             where => {"+are" => {id => \@auth_ids}}
         }) or return;
 
+
         $self->map_authority_headings_to_results($linked, $results);
 
         # Get use counts of authority records, i.e. number of bibs linked to
@@ -185,16 +195,20 @@ sub flesh_browse_results {
                 ]
             },
             from => {abl => {}},
-            where => {"+abl" => {authority => \@auth_ids}}
+            where => {
+                "+abl" => {
+                    authority => [
+                        @auth_ids, $U->unique_unnested_numbers(
+                            map { $_->{target} } @$linked
+                        )
+                    ]
+                }
+            }
         }) or return;
 
-        my %counts_by_authority =
-            map { $_->{authority} => $_->{count} } @$counts;
-        foreach my $row(@$results) {
-            foreach my $auth (@{$row->{authorities}}) {
-                $auth->{count} = $counts_by_authority{$auth->{id}};
-            }
-        }
+        $self->ctx->{authority_counts} = {
+            map { $_->{authority} => $_->{count} } @$counts
+        };
     }
 
     return 1;
index 8e4b1d6..6e806a6 100644 (file)
                                         field_id = field_group.keys.0;
                                         field = acs.$field_id;
                                         headings = field_group.values.0;
-                                        FOR h IN headings %]
+                                        FOR h IN headings;
+                                            target_auth_id = h.target %]
 
-                                            <li>[% field.name %]
-                                            [% IF h.target %]
-                                            <a href="[% mkurl(ctx.opac_root _ '/results', {query => 'identifier|authority_id[' _ h.target _ ']'}) %]">[% h.heading | html %]</a>
+                                            <li><span class="browse-result-authority-field-name">[% field.name %]</span>
+                                            [% IF target_auth_id %]
+                                            <a href="[% mkurl(ctx.opac_root _ '/results', {query => 'identifier|authority_id[' _ target_auth_id _ ']'}) %]">[% h.heading | html %]</a>
+                                            <span class="browse-result-authority-bib-links">([% ctx.authority_counts.$target_auth_id %])</span>
                                             [% ELSE;
                                                 h.heading | html;
-                                            END;
-                                        END %]
-
-                                            <span class="browse-result-authority-bib-links">([% a.count %])</span></li>
+                                            END %]
+                                            </li>
+                                        [% END %]
                                     [% END %]
                                 [% END %]
                             </ul>
index f645b21..ef0ef64 100644 (file)
@@ -1531,6 +1531,10 @@ a.preflib_change {
 .browse-shortcuts {
     font-size: 120%;
 }
+.browse-result-authority-field-name {
+    font-style: italic;
+    margin-right: 1em;
+}
 .browse-leading-article-warning {
     font-style: italic;
     font-size: 110%;