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