]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
TPAC: Improvement to serials display (under the "issues held" label)
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Record.pm
1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
4 use OpenSRF::Utils::Logger qw/$logger/;
5 use OpenILS::Utils::CStoreEditor qw/:funcs/;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Application::AppUtils;
8 my $U = 'OpenILS::Application::AppUtils';
9
10 # context additions: 
11 #   record : bre object
12 sub load_record {
13     my $self = shift;
14     my $ctx = $self->ctx;
15     $ctx->{page} = 'record';  
16
17     my $org = $self->_get_search_lib();
18     my $org_name = $ctx->{get_aou}->($org)->shortname;
19     my $pref_ou = $self->_get_pref_lib();
20     my $depth = $self->cgi->param('depth');
21     $depth = $ctx->{get_aou}->($org)->ou_type->depth 
22         unless defined $depth; # can be 0
23
24     my $copy_depth = $self->cgi->param('copy_depth');
25     $copy_depth = $depth unless defined $copy_depth; # can be 0
26     $self->ctx->{copy_depth} = $copy_depth;
27
28     my $copy_limit = int($self->cgi->param('copy_limit') || 10);
29     my $copy_offset = int($self->cgi->param('copy_offset') || 0);
30
31     my $rec_id = $ctx->{page_args}->[0]
32         or return Apache2::Const::HTTP_BAD_REQUEST;
33
34     $self->get_staff_search_settings;
35     if ($ctx->{staff_saved_search_size}) {
36         $ctx->{saved_searches} = ($self->staff_load_searches)[1];
37     }
38
39     $self->fetch_related_search_info($rec_id);
40
41     # run copy retrieval in parallel to bib retrieval
42     # XXX unapi
43     my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
44     my $copy_rec = $cstore->request(
45         'open-ils.cstore.json_query.atomic', 
46         $self->mk_copy_query($rec_id, $org, $copy_depth, $copy_limit, $copy_offset, $pref_ou)
47     );
48
49     my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {
50         flesh => '{holdings_xml,bmp,mra,acp,acnp,acns}',
51         site => $org_name,
52         depth => $depth,
53         pref_lib => $pref_ou
54     });
55     $ctx->{bre_id} = $rec_data[0]->{id};
56     $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
57
58     $ctx->{copies} = $copy_rec->gather(1);
59     $ctx->{copy_limit} = $copy_limit;
60     $ctx->{copy_offset} = $copy_offset;
61
62     $ctx->{have_holdings_to_show} = 0;
63     $ctx->{have_mfhd_to_show} = 0;
64
65     $self->get_hold_copy_summary($rec_id, $org);
66
67     $cstore->kill_me;
68
69     if (
70         $ctx->{get_org_setting}->
71             ($org, "opac.fully_compressed_serial_holdings")
72     ) {
73         # We're loading this data here? Are we therefore assuming that we
74         # *are* going to display something in the "issues" expandy?
75         $self->load_serial_holding_summaries($rec_id, $org, $copy_depth);
76     } else {
77         $ctx->{mfhd_summaries} =
78             $self->get_mfhd_summaries($rec_id, $org, $copy_depth);
79
80         if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
81         ) {
82             $ctx->{have_mfhd_to_show} = 1;
83         };
84     }
85
86     my %expandies = (
87         marchtml => sub {
88             $ctx->{marchtml} = $self->mk_marc_html($rec_id);
89         },
90         issues => sub {
91             return;
92             # XXX this needed?
93         },
94         cnbrowse => sub {
95             $self->prepare_browse_call_numbers();
96         }
97     );
98
99     my @expand = $self->cgi->param('expand');
100     if (grep {$_ eq 'all'} @expand) {
101         $ctx->{expand_all} = 1;
102         $expandies{$_}->() for keys %expandies;
103
104     } else {
105         for my $exp (@expand) {
106             $ctx->{"expand_$exp"} = 1;
107             $expandies{$exp}->() if exists $expandies{$exp};
108         }
109     }
110
111     return Apache2::Const::OK;
112 }
113
114 # collect IDs and info on the search that lead to this details page
115 # If no search query, etc is present, we leave ctx.search_result_index == -1
116 sub fetch_related_search_info {
117     my $self = shift;
118     my $rec_id = shift;
119     my $ctx = $self->ctx;
120     $ctx->{search_result_index} = -1;
121
122     $self->load_rresults(internal => 1);
123
124     my @search_ids = @{$ctx->{ids}};
125     return unless @search_ids;
126
127     for my $idx (0..$#search_ids) {
128         if ($search_ids[$idx] == $rec_id) {
129             $ctx->{prev_search_record} = $search_ids[$idx - 1] if $idx > 0;
130             $ctx->{next_search_record} = $search_ids[$idx + 1];
131             $ctx->{search_result_index} = $idx;
132             last;
133         }
134     }
135
136     $ctx->{first_search_record} = $search_ids[0];
137     $ctx->{last_search_record} = $search_ids[-1];
138 }
139
140
141 sub mk_copy_query {
142     my $self = shift;
143     my $rec_id = shift;
144     my $org = shift;
145     my $depth = shift;
146     my $copy_limit = shift;
147     my $copy_offset = shift;
148     my $pref_ou = shift;
149
150     my $query = $U->basic_opac_copy_query(
151         $rec_id, undef, undef, $copy_limit, $copy_offset, $self->ctx->{is_staff}
152     );
153
154     if($org != $self->ctx->{aou_tree}->()->id) { 
155         # no need to add the org join filter if we're not actually filtering
156         $query->{from}->{acp}->{aou} = {
157             fkey => 'circ_lib',
158             field => 'id',
159             filter => {
160                 id => {
161                     in => {
162                         select => {aou => [{
163                             column => 'id', 
164                             transform => 'actor.org_unit_descendants',
165                             result_field => 'id', 
166                             params => [$depth]
167                         }]},
168                         from => 'aou',
169                         where => {id => $org}
170                     }
171                 }
172             }
173         };
174     };
175
176     # Unsure if we want these in the shared function, leaving here for now
177     unshift(@{$query->{order_by}},
178         { class => "aou", field => 'id',
179           transform => 'evergreen.rank_ou', params => [$org, $pref_ou]
180         }
181     );
182     push(@{$query->{order_by}},
183         { class => "acp", field => 'status',
184           transform => 'evergreen.rank_cp_status'
185         }
186     );
187
188     return $query;
189 }
190
191 sub mk_marc_html {
192     my($self, $rec_id) = @_;
193
194     # could be optimized considerably by performing the xslt on the already fetched record
195     return $U->simplereq(
196         'open-ils.search', 
197         'open-ils.search.biblio.record.html', $rec_id, 1);
198 }
199
200 sub load_serial_holding_summaries {
201     my ($self, $rec_id, $org, $depth) = @_;
202
203     my $limit = $self->cgi->param("slimit") || 10;
204     my $offset = $self->cgi->param("soffset") || 0;
205
206     my $serial = create OpenSRF::AppSession("open-ils.serial");
207
208     # First, get the tree of /summaries/ of holdings.
209     my $tree = $serial->request(
210         "open-ils.serial.holding_summary_tree.by_bib",
211         $rec_id, $org, $depth, $limit, $offset
212     )->gather(1);
213
214     return if $self->apache_log_if_event(
215         $tree, "getting holding summary tree for record $rec_id"
216     );
217
218     # Next, if requested, get a list of individual holdings under a
219     # particular summary.
220     my $holdings;
221     my $summary_id = int($self->cgi->param("sid") || 0);
222     my $summary_type = $self->cgi->param("stype");
223
224     if ($summary_id and $summary_type) {
225         my $expand_path = [ $self->cgi->param("sepath") ],
226         my $expand_limit = $self->cgi->param("selimit");
227         my $expand_offsets = [ $self->cgi->param("seoffset") ];
228         my $auto_expand_first = 0;
229
230         if (not @$expand_offsets) {
231             $expand_offsets = undef;
232             $auto_expand_first = 1;
233         }
234
235         $holdings = $serial->request(
236             "open-ils.serial.holdings.grouped_by_summary",
237             $summary_type, $summary_id,
238             $expand_path, $expand_limit, $expand_offsets,
239             $auto_expand_first,
240             1 + ($self->ctx->{is_staff} ? 1 : 0)
241         )->gather(1);
242
243         if ($holdings and ref $holdings eq "ARRAY") {
244             $self->place_holdings_with_summary(
245                     $tree, $holdings, $summary_id, $summary_type
246             ) or $self->apache->log->warn(
247                 "could not place holdings within summary tree"
248             );
249         } else {
250             $self->apache_log_if_event(
251                 $holdings, "getting holdings grouped by summary $summary_id"
252             );
253         }
254     }
255
256     $serial->kill_me;
257
258     # The presence of any keys in the tree hash other than 'more' means that we
259     # must have /something/ we could show.
260     $self->ctx->{have_holdings_to_show} = grep { $_ ne 'more' } (keys %$tree);
261
262     $self->ctx->{holding_summary_tree} = $tree;
263 }
264
265 # This helper to load_serial_holding_summaries() recursively searches in
266 # $tree for a holding summary matching $sid and $stype, and places $holdings
267 # within the node for that summary. IOW, this is about showing expanded
268 # holdings under their "parent" summary.
269 sub place_holdings_with_summary {
270     my ($self, $tree, $holdings, $sid, $stype) = @_;
271
272     foreach my $sum (@{$tree->{holding_summaries}}) {
273         if ($sum->{id} == $sid and $sum->{summary_type} eq $stype) {
274             $sum->{holdings} = $holdings;
275             return 1;
276         }
277     }
278
279     foreach my $child (@{$tree->{children}}) {
280         return 1 if $self->place_holdings_with_summary(
281             $child, $holdings, $sid, $stype
282         );
283     }
284
285     return;
286 }
287
288 sub get_mfhd_summaries {
289     my ($self, $rec_id, $org, $depth) = @_;
290
291     my $serial = create OpenSRF::AppSession("open-ils.search");
292     my $result = $serial->request(
293         "open-ils.search.serial.record.bib.retrieve",
294         $rec_id, $org, $depth
295     )->gather(1);
296
297     $serial->kill_me;
298     return $result;
299 }
300
301 sub any_call_number_label {
302     my ($self) = @_;
303
304     if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
305         return $self->ctx->{copies}->[0]->{call_number_label};
306     } else {
307         return;
308     }
309 }
310
311 sub prepare_browse_call_numbers {
312     my ($self) = @_;
313
314     my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
315         return [];
316
317     my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
318         $self->ctx->{aou_tree}->();
319
320     my $supercat = create OpenSRF::AppSession("open-ils.supercat");
321     my $results = $supercat->request(
322         "open-ils.supercat.call_number.browse", 
323         $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
324     )->gather(1) || [];
325
326     $supercat->kill_me;
327
328     $self->ctx->{browsed_call_numbers} = [
329         map {
330             $_->record->marc(
331                 (new XML::LibXML)->parse_string($_->record->marc)
332             );
333             $_;
334         } @$results
335     ];
336     $self->ctx->{browsing_ou} = $org_unit;
337 }
338
339 sub get_hold_copy_summary {
340     my ($self, $rec_id, $org) = @_;
341     
342     my $search = OpenSRF::AppSession->create('open-ils.search');
343     my $req1 = $search->request(
344         'open-ils.search.biblio.record.copy_count', $org, $rec_id); 
345
346     $self->ctx->{record_hold_count} = $U->simplereq(
347         'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
348
349     $self->ctx->{copy_summary} = $req1->recv->content;
350
351     $search->kill_me;
352 }
353
354 sub load_print_record {
355     my $self = shift;
356
357     my $rec_id = $self->ctx->{page_args}->[0] 
358         or return Apache2::Const::HTTP_BAD_REQUEST;
359
360     $self->{ctx}->{bre_id} = $rec_id;
361     $self->{ctx}->{printable_record} = $U->simplereq(
362         'open-ils.search',
363         'open-ils.search.biblio.record.print', $rec_id);
364
365     return Apache2::Const::OK;
366 }
367
368 sub load_email_record {
369     my $self = shift;
370
371     my $rec_id = $self->ctx->{page_args}->[0] 
372         or return Apache2::Const::HTTP_BAD_REQUEST;
373
374     $self->{ctx}->{bre_id} = $rec_id;
375     $U->simplereq(
376         'open-ils.search',
377         'open-ils.search.biblio.record.email', 
378         $self->ctx->{authtoken}, $rec_id);
379
380     return Apache2::Const::OK;
381 }
382
383 1;