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;
10 my $U = 'OpenILS::Application::AppUtils';
12 our $ac_types = ['toc', 'anotes', 'excerpt', 'summary', 'reviews'];
20 $ctx->{page} = 'record';
22 $self->timelog("load_record() began");
24 my $rec_id = $ctx->{page_args}->[0]
25 or return Apache2::Const::HTTP_BAD_REQUEST;
27 $self->added_content_stage1($rec_id);
28 $self->timelog("past added content stage 1");
30 my $org = $self->_get_search_lib();
31 my $org_name = $ctx->{get_aou}->($org)->shortname;
32 my $pref_ou = $self->_get_pref_lib();
33 my $depth = $self->cgi->param('depth');
34 $depth = $ctx->{get_aou}->($org)->ou_type->depth
35 unless defined $depth; # can be 0
37 my $copy_depth = $self->cgi->param('copy_depth');
38 $copy_depth = $depth unless defined $copy_depth; # can be 0
39 $self->ctx->{copy_depth} = $copy_depth;
41 my $copy_limit = int($self->cgi->param('copy_limit') || 10);
42 my $copy_offset = int($self->cgi->param('copy_offset') || 0);
44 $self->get_staff_search_settings;
45 if ($ctx->{staff_saved_search_size}) {
46 $ctx->{saved_searches} = ($self->staff_load_searches)[1];
48 $self->timelog("past staff saved searches");
50 $self->fetch_related_search_info($rec_id) unless $kwargs{no_search};
51 $self->timelog("past related search info");
53 # Check for user and load lists and prefs
54 if ($self->ctx->{user}) {
55 $self->_load_lists_and_settings;
56 $self->timelog("load user lists and settings");
59 # run copy retrieval in parallel to bib retrieval
61 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
62 my $copy_rec = $cstore->request(
63 'open-ils.cstore.json_query.atomic',
64 $self->mk_copy_query($rec_id, $org, $copy_depth, $copy_limit, $copy_offset, $pref_ou)
67 my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {
68 flesh => '{holdings_xml,bmp,mra,acp,acnp,acns}',
74 $self->timelog("past get_records_and_facets()");
75 $ctx->{bre_id} = $rec_data[0]->{id};
76 $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
78 $ctx->{copies} = $copy_rec->gather(1);
79 $self->timelog("past store copy retrieval call");
80 $ctx->{copy_limit} = $copy_limit;
81 $ctx->{copy_offset} = $copy_offset;
83 $ctx->{have_holdings_to_show} = 0;
84 $ctx->{have_mfhd_to_show} = 0;
86 $self->get_hold_copy_summary($rec_id, $org);
88 $self->timelog("past get_hold_copy_summary()");
89 $self->ctx->{bib_is_dead} = OpenILS::Application::AppUtils->is_true(
90 OpenILS::Utils::CStoreEditor->new->json_query({
91 select => { bre => [ 'deleted' ] },
93 where => { 'id' => $rec_id }
100 $ctx->{get_org_setting}->
101 ($org, "opac.fully_compressed_serial_holdings")
103 # We're loading this data here? Are we therefore assuming that we
104 # *are* going to display something in the "issues" expandy?
105 $self->load_serial_holding_summaries($rec_id, $org, $copy_depth);
107 $ctx->{mfhd_summaries} =
108 $self->get_mfhd_summaries($rec_id, $org, $copy_depth);
110 if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
112 $ctx->{have_mfhd_to_show} = 1;
116 $self->timelog("past serials holding stuff");
120 $ctx->{marchtml} = $self->mk_marc_html($rec_id);
127 $self->prepare_browse_call_numbers();
131 my @expand = $self->cgi->param('expand');
132 if (grep {$_ eq 'all'} @expand) {
133 $ctx->{expand_all} = 1;
134 $expandies{$_}->() for keys %expandies;
137 for my $exp (@expand) {
138 $ctx->{"expand_$exp"} = 1;
139 $expandies{$exp}->() if exists $expandies{$exp};
143 $self->timelog("past expandies");
145 $self->added_content_stage2($rec_id);
147 $self->timelog("past added content stage 2");
149 return Apache2::Const::OK;
152 # collect IDs and info on the search that lead to this details page
153 # If no search query, etc is present, we leave ctx.search_result_index == -1
154 sub fetch_related_search_info {
157 my $ctx = $self->ctx;
158 $ctx->{search_result_index} = -1;
160 $self->load_rresults(internal => 1);
162 my @search_ids = @{$ctx->{ids}};
163 return unless @search_ids;
165 for my $idx (0..$#search_ids) {
166 if ($search_ids[$idx] == $rec_id) {
167 $ctx->{prev_search_record} = $search_ids[$idx - 1] if $idx > 0;
168 $ctx->{next_search_record} = $search_ids[$idx + 1];
169 $ctx->{search_result_index} = $idx;
174 $ctx->{first_search_record} = $search_ids[0];
175 $ctx->{last_search_record} = $search_ids[-1];
184 my $copy_limit = shift;
185 my $copy_offset = shift;
188 my $query = $U->basic_opac_copy_query(
189 $rec_id, undef, undef, $copy_limit, $copy_offset, $self->ctx->{is_staff}
192 if($org != $self->ctx->{aou_tree}->()->id) {
193 # no need to add the org join filter if we're not actually filtering
194 $query->{from}->{acp}->{aou} = {
202 transform => 'actor.org_unit_descendants',
203 result_field => 'id',
207 where => {id => $org}
214 # Unsure if we want these in the shared function, leaving here for now
215 unshift(@{$query->{order_by}},
216 { class => "aou", field => 'id',
217 transform => 'evergreen.rank_ou', params => [$org, $pref_ou]
220 push(@{$query->{order_by}},
221 { class => "acp", field => 'status',
222 transform => 'evergreen.rank_cp_status'
230 my($self, $rec_id) = @_;
232 # could be optimized considerably by performing the xslt on the already fetched record
233 return $U->simplereq(
235 'open-ils.search.biblio.record.html', $rec_id, 1);
238 sub load_serial_holding_summaries {
239 my ($self, $rec_id, $org, $depth) = @_;
241 my $limit = $self->cgi->param("slimit") || 10;
242 my $offset = $self->cgi->param("soffset") || 0;
244 my $serial = create OpenSRF::AppSession("open-ils.serial");
246 # First, get the tree of /summaries/ of holdings.
247 my $tree = $serial->request(
248 "open-ils.serial.holding_summary_tree.by_bib",
249 $rec_id, $org, $depth, $limit, $offset
252 return if $self->apache_log_if_event(
253 $tree, "getting holding summary tree for record $rec_id"
256 # Next, if requested, get a list of individual holdings under a
257 # particular summary.
259 my $summary_id = int($self->cgi->param("sid") || 0);
260 my $summary_type = $self->cgi->param("stype");
262 if ($summary_id and $summary_type) {
263 my $expand_path = [ $self->cgi->param("sepath") ],
264 my $expand_limit = $self->cgi->param("selimit");
265 my $expand_offsets = [ $self->cgi->param("seoffset") ];
266 my $auto_expand_first = 0;
268 if (not @$expand_offsets) {
269 $expand_offsets = undef;
270 $auto_expand_first = 1;
273 $holdings = $serial->request(
274 "open-ils.serial.holdings.grouped_by_summary",
275 $summary_type, $summary_id,
276 $expand_path, $expand_limit, $expand_offsets,
278 1 + ($self->ctx->{is_staff} ? 1 : 0)
281 if ($holdings and ref $holdings eq "ARRAY") {
282 $self->place_holdings_with_summary(
283 $tree, $holdings, $summary_id, $summary_type
284 ) or $self->apache->log->warn(
285 "could not place holdings within summary tree"
288 $self->apache_log_if_event(
289 $holdings, "getting holdings grouped by summary $summary_id"
296 # The presence of any keys in the tree hash other than 'more' means that we
297 # must have /something/ we could show.
298 $self->ctx->{have_holdings_to_show} = grep { $_ ne 'more' } (keys %$tree);
300 $self->ctx->{holding_summary_tree} = $tree;
303 # This helper to load_serial_holding_summaries() recursively searches in
304 # $tree for a holding summary matching $sid and $stype, and places $holdings
305 # within the node for that summary. IOW, this is about showing expanded
306 # holdings under their "parent" summary.
307 sub place_holdings_with_summary {
308 my ($self, $tree, $holdings, $sid, $stype) = @_;
310 foreach my $sum (@{$tree->{holding_summaries}}) {
311 if ($sum->{id} == $sid and $sum->{summary_type} eq $stype) {
312 $sum->{holdings} = $holdings;
317 foreach my $child (@{$tree->{children}}) {
318 return 1 if $self->place_holdings_with_summary(
319 $child, $holdings, $sid, $stype
326 sub get_mfhd_summaries {
327 my ($self, $rec_id, $org, $depth) = @_;
329 my $serial = create OpenSRF::AppSession("open-ils.search");
330 my $result = $serial->request(
331 "open-ils.search.serial.record.bib.retrieve",
332 $rec_id, $org, $depth
339 sub any_call_number_label {
342 if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
343 return $self->ctx->{copies}->[0]->{call_number_label};
349 sub prepare_browse_call_numbers {
352 my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
355 my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
356 $self->ctx->{aou_tree}->();
358 my $supercat = create OpenSRF::AppSession("open-ils.supercat");
359 my $results = $supercat->request(
360 "open-ils.supercat.call_number.browse",
361 $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
366 $self->ctx->{browsed_call_numbers} = [
369 (new XML::LibXML)->parse_string($_->record->marc)
374 $self->ctx->{browsing_ou} = $org_unit;
377 sub get_hold_copy_summary {
378 my ($self, $rec_id, $org) = @_;
380 my $search = OpenSRF::AppSession->create('open-ils.search');
381 my $req1 = $search->request(
382 'open-ils.search.biblio.record.copy_count', $org, $rec_id);
384 $self->ctx->{record_hold_count} = $U->simplereq(
385 'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
387 $self->ctx->{copy_summary} = $req1->recv->content;
392 sub load_print_record {
395 my $rec_id = $self->ctx->{page_args}->[0]
396 or return Apache2::Const::HTTP_BAD_REQUEST;
398 $self->{ctx}->{bre_id} = $rec_id;
399 $self->{ctx}->{printable_record} = $U->simplereq(
401 'open-ils.search.biblio.record.print', $rec_id);
403 return Apache2::Const::OK;
406 sub load_email_record {
409 my $rec_id = $self->ctx->{page_args}->[0]
410 or return Apache2::Const::HTTP_BAD_REQUEST;
412 $self->{ctx}->{bre_id} = $rec_id;
415 'open-ils.search.biblio.record.email',
416 $self->ctx->{authtoken}, $rec_id);
418 return Apache2::Const::OK;
421 # for each type, fire off the reqeust to see if content is available
422 # ctx.added_content.$type.status:
426 sub added_content_stage1 {
429 my $ctx = $self->ctx;
430 my $sel_type = $self->cgi->param('ac') || '';
431 my $key = $self->get_ac_key($rec_id);
432 ($key = $key->{value}) =~ s/^\s+//g if $key;
434 $ctx->{added_content} = {};
435 for my $type (@$ac_types) {
436 $ctx->{added_content}->{$type} = {content => ''};
437 $ctx->{added_content}->{$type}->{status} = $key ? 3 : 2;
440 $logger->debug("tpac: starting added content request for $key => $type");
442 my $req = Net::HTTP::NB->new(Host => $self->apache->hostname);
445 $logger->warn("Unable to fetch added content from " . $self->apache->hostname . ": $@");
449 my $http_type = ($type eq $sel_type) ? 'GET' : 'HEAD';
450 $req->write_request($http_type => "/opac/extras/ac/$type/html/" . uri_escape($key));
451 $ctx->{added_content}->{$type}->{request} = $req;
456 # check each outstanding request. If it's ready, read the HTTP
457 # status and use it to determine if content is available. Otherwise,
458 # leave the status as unknown.
459 sub added_content_stage2 {
461 my $ctx = $self->ctx;
462 my $sel_type = $self->cgi->param('ac') || '';
464 for my $type (keys %{$ctx->{added_content}}) {
465 my $content = $ctx->{added_content}->{$type};
467 if ($content->{status} == 3) {
468 $logger->debug("tpac: finishing added content request for $type");
470 my $req = $content->{request};
471 my $sel = IO::Select->new($req);
473 # if we are requesting a specific type of content, give the
474 # backend code a little extra time to retrieve the content.
475 my $wait = $type eq $sel_type ? 3 : 0; # TODO: config?
477 if ($sel->can_read($wait)) {
478 my ($code) = $req->read_response_headers;
479 $content->{status} = $code eq '200' ? 1 : 2;
480 $logger->debug("tpac: added content request for $type returned $code");
482 if ($code eq '200' and $type eq $sel_type) {
485 my $n = $req->read_entity_body($buf, 1024);
487 $content->{content} .= $buf;
495 # XXX this is copied directly from AddedContent.pm in
496 # working/user/jeff/ac_by_record_id_rebase. When Jeff's
497 # branch is merged and Evergreen gets added content
498 # lookup by ID, this can be removed.
499 # returns [{tag => $tag, value => $value}, {tag => $tag2, value => $value2}]
503 my $key_data = $self->editor->json_query({
504 select => {mfr => ['tag', 'value']},
526 grep {$_->{tag} eq '020'} @$key_data,
527 grep {$_->{tag} eq '024'} @$key_data