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'];
19 $ctx->{page} = 'record';
21 $self->timelog("load_record() began");
23 my $rec_id = $ctx->{page_args}->[0]
24 or return Apache2::Const::HTTP_BAD_REQUEST;
26 $self->added_content_stage1($rec_id);
27 $self->timelog("past added content stage 1");
29 my $org = $self->_get_search_lib();
30 my $org_name = $ctx->{get_aou}->($org)->shortname;
31 my $pref_ou = $self->_get_pref_lib();
32 my $depth = $self->cgi->param('depth');
33 $depth = $ctx->{get_aou}->($org)->ou_type->depth
34 unless defined $depth; # can be 0
36 my $copy_depth = $self->cgi->param('copy_depth');
37 $copy_depth = $depth unless defined $copy_depth; # can be 0
38 $self->ctx->{copy_depth} = $copy_depth;
40 my $copy_limit = int($self->cgi->param('copy_limit') || 10);
41 my $copy_offset = int($self->cgi->param('copy_offset') || 0);
43 $self->get_staff_search_settings;
44 if ($ctx->{staff_saved_search_size}) {
45 $ctx->{saved_searches} = ($self->staff_load_searches)[1];
47 $self->timelog("past staff saved searches");
49 $self->fetch_related_search_info($rec_id);
50 $self->timelog("past related search info");
52 # run copy retrieval in parallel to bib retrieval
54 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
55 my $copy_rec = $cstore->request(
56 'open-ils.cstore.json_query.atomic',
57 $self->mk_copy_query($rec_id, $org, $copy_depth, $copy_limit, $copy_offset, $pref_ou)
60 my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {
61 flesh => '{holdings_xml,bmp,mra,acp,acnp,acns}',
67 $self->timelog("past get_records_and_facets()");
68 $ctx->{bre_id} = $rec_data[0]->{id};
69 $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
71 $ctx->{copies} = $copy_rec->gather(1);
72 $self->timelog("past store copy retrieval call");
73 $ctx->{copy_limit} = $copy_limit;
74 $ctx->{copy_offset} = $copy_offset;
76 $ctx->{have_holdings_to_show} = 0;
77 $ctx->{have_mfhd_to_show} = 0;
79 $self->get_hold_copy_summary($rec_id, $org);
81 $self->timelog("past get_hold_copy_summary()");
82 $self->ctx->{bib_is_dead} = OpenILS::Application::AppUtils->is_true(
83 OpenILS::Utils::CStoreEditor->new->json_query({
84 select => { bre => [ 'deleted' ] },
86 where => { 'id' => $rec_id }
93 $ctx->{get_org_setting}->
94 ($org, "opac.fully_compressed_serial_holdings")
96 # We're loading this data here? Are we therefore assuming that we
97 # *are* going to display something in the "issues" expandy?
98 $self->load_serial_holding_summaries($rec_id, $org, $copy_depth);
100 $ctx->{mfhd_summaries} =
101 $self->get_mfhd_summaries($rec_id, $org, $copy_depth);
103 if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
105 $ctx->{have_mfhd_to_show} = 1;
109 $self->timelog("past serials holding stuff");
113 $ctx->{marchtml} = $self->mk_marc_html($rec_id);
120 $self->prepare_browse_call_numbers();
124 my @expand = $self->cgi->param('expand');
125 if (grep {$_ eq 'all'} @expand) {
126 $ctx->{expand_all} = 1;
127 $expandies{$_}->() for keys %expandies;
130 for my $exp (@expand) {
131 $ctx->{"expand_$exp"} = 1;
132 $expandies{$exp}->() if exists $expandies{$exp};
136 $self->timelog("past expandies");
138 $self->added_content_stage2($rec_id);
140 $self->timelog("past added content stage 2");
142 return Apache2::Const::OK;
145 # collect IDs and info on the search that lead to this details page
146 # If no search query, etc is present, we leave ctx.search_result_index == -1
147 sub fetch_related_search_info {
150 my $ctx = $self->ctx;
151 $ctx->{search_result_index} = -1;
153 $self->load_rresults(internal => 1);
155 my @search_ids = @{$ctx->{ids}};
156 return unless @search_ids;
158 for my $idx (0..$#search_ids) {
159 if ($search_ids[$idx] == $rec_id) {
160 $ctx->{prev_search_record} = $search_ids[$idx - 1] if $idx > 0;
161 $ctx->{next_search_record} = $search_ids[$idx + 1];
162 $ctx->{search_result_index} = $idx;
167 $ctx->{first_search_record} = $search_ids[0];
168 $ctx->{last_search_record} = $search_ids[-1];
177 my $copy_limit = shift;
178 my $copy_offset = shift;
181 my $query = $U->basic_opac_copy_query(
182 $rec_id, undef, undef, $copy_limit, $copy_offset, $self->ctx->{is_staff}
185 if($org != $self->ctx->{aou_tree}->()->id) {
186 # no need to add the org join filter if we're not actually filtering
187 $query->{from}->{acp}->{aou} = {
195 transform => 'actor.org_unit_descendants',
196 result_field => 'id',
200 where => {id => $org}
207 # Unsure if we want these in the shared function, leaving here for now
208 unshift(@{$query->{order_by}},
209 { class => "aou", field => 'id',
210 transform => 'evergreen.rank_ou', params => [$org, $pref_ou]
213 push(@{$query->{order_by}},
214 { class => "acp", field => 'status',
215 transform => 'evergreen.rank_cp_status'
223 my($self, $rec_id) = @_;
225 # could be optimized considerably by performing the xslt on the already fetched record
226 return $U->simplereq(
228 'open-ils.search.biblio.record.html', $rec_id, 1);
231 sub load_serial_holding_summaries {
232 my ($self, $rec_id, $org, $depth) = @_;
234 my $limit = $self->cgi->param("slimit") || 10;
235 my $offset = $self->cgi->param("soffset") || 0;
237 my $serial = create OpenSRF::AppSession("open-ils.serial");
239 # First, get the tree of /summaries/ of holdings.
240 my $tree = $serial->request(
241 "open-ils.serial.holding_summary_tree.by_bib",
242 $rec_id, $org, $depth, $limit, $offset
245 return if $self->apache_log_if_event(
246 $tree, "getting holding summary tree for record $rec_id"
249 # Next, if requested, get a list of individual holdings under a
250 # particular summary.
252 my $summary_id = int($self->cgi->param("sid") || 0);
253 my $summary_type = $self->cgi->param("stype");
255 if ($summary_id and $summary_type) {
256 my $expand_path = [ $self->cgi->param("sepath") ],
257 my $expand_limit = $self->cgi->param("selimit");
258 my $expand_offsets = [ $self->cgi->param("seoffset") ];
259 my $auto_expand_first = 0;
261 if (not @$expand_offsets) {
262 $expand_offsets = undef;
263 $auto_expand_first = 1;
266 $holdings = $serial->request(
267 "open-ils.serial.holdings.grouped_by_summary",
268 $summary_type, $summary_id,
269 $expand_path, $expand_limit, $expand_offsets,
271 1 + ($self->ctx->{is_staff} ? 1 : 0)
274 if ($holdings and ref $holdings eq "ARRAY") {
275 $self->place_holdings_with_summary(
276 $tree, $holdings, $summary_id, $summary_type
277 ) or $self->apache->log->warn(
278 "could not place holdings within summary tree"
281 $self->apache_log_if_event(
282 $holdings, "getting holdings grouped by summary $summary_id"
289 # The presence of any keys in the tree hash other than 'more' means that we
290 # must have /something/ we could show.
291 $self->ctx->{have_holdings_to_show} = grep { $_ ne 'more' } (keys %$tree);
293 $self->ctx->{holding_summary_tree} = $tree;
296 # This helper to load_serial_holding_summaries() recursively searches in
297 # $tree for a holding summary matching $sid and $stype, and places $holdings
298 # within the node for that summary. IOW, this is about showing expanded
299 # holdings under their "parent" summary.
300 sub place_holdings_with_summary {
301 my ($self, $tree, $holdings, $sid, $stype) = @_;
303 foreach my $sum (@{$tree->{holding_summaries}}) {
304 if ($sum->{id} == $sid and $sum->{summary_type} eq $stype) {
305 $sum->{holdings} = $holdings;
310 foreach my $child (@{$tree->{children}}) {
311 return 1 if $self->place_holdings_with_summary(
312 $child, $holdings, $sid, $stype
319 sub get_mfhd_summaries {
320 my ($self, $rec_id, $org, $depth) = @_;
322 my $serial = create OpenSRF::AppSession("open-ils.search");
323 my $result = $serial->request(
324 "open-ils.search.serial.record.bib.retrieve",
325 $rec_id, $org, $depth
332 sub any_call_number_label {
335 if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
336 return $self->ctx->{copies}->[0]->{call_number_label};
342 sub prepare_browse_call_numbers {
345 my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
348 my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
349 $self->ctx->{aou_tree}->();
351 my $supercat = create OpenSRF::AppSession("open-ils.supercat");
352 my $results = $supercat->request(
353 "open-ils.supercat.call_number.browse",
354 $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
359 $self->ctx->{browsed_call_numbers} = [
362 (new XML::LibXML)->parse_string($_->record->marc)
367 $self->ctx->{browsing_ou} = $org_unit;
370 sub get_hold_copy_summary {
371 my ($self, $rec_id, $org) = @_;
373 my $search = OpenSRF::AppSession->create('open-ils.search');
374 my $req1 = $search->request(
375 'open-ils.search.biblio.record.copy_count', $org, $rec_id);
377 $self->ctx->{record_hold_count} = $U->simplereq(
378 'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
380 $self->ctx->{copy_summary} = $req1->recv->content;
385 sub load_print_record {
388 my $rec_id = $self->ctx->{page_args}->[0]
389 or return Apache2::Const::HTTP_BAD_REQUEST;
391 $self->{ctx}->{bre_id} = $rec_id;
392 $self->{ctx}->{printable_record} = $U->simplereq(
394 'open-ils.search.biblio.record.print', $rec_id);
396 return Apache2::Const::OK;
399 sub load_email_record {
402 my $rec_id = $self->ctx->{page_args}->[0]
403 or return Apache2::Const::HTTP_BAD_REQUEST;
405 $self->{ctx}->{bre_id} = $rec_id;
408 'open-ils.search.biblio.record.email',
409 $self->ctx->{authtoken}, $rec_id);
411 return Apache2::Const::OK;
414 # for each type, fire off the reqeust to see if content is available
415 # ctx.added_content.$type.status:
419 sub added_content_stage1 {
422 my $ctx = $self->ctx;
423 my $sel_type = $self->cgi->param('ac') || '';
424 my $key = $self->get_ac_key($rec_id);
425 ($key = $key->{value}) =~ s/^\s+//g if $key;
427 $ctx->{added_content} = {};
428 for my $type (@$ac_types) {
429 $ctx->{added_content}->{$type} = {content => ''};
430 $ctx->{added_content}->{$type}->{status} = $key ? 3 : 2;
433 $logger->debug("tpac: starting added content request for $key => $type");
435 my $req = Net::HTTP::NB->new(Host => $self->apache->hostname);
438 $logger->warn("Unable to fetch added content from " . $self->apache->hostname . ": $@");
442 my $http_type = ($type eq $sel_type) ? 'GET' : 'HEAD';
443 $req->write_request($http_type => "/opac/extras/ac/$type/html/" . uri_escape($key));
444 $ctx->{added_content}->{$type}->{request} = $req;
449 # check each outstanding request. If it's ready, read the HTTP
450 # status and use it to determine if content is available. Otherwise,
451 # leave the status as unknown.
452 sub added_content_stage2 {
454 my $ctx = $self->ctx;
455 my $sel_type = $self->cgi->param('ac') || '';
457 for my $type (keys %{$ctx->{added_content}}) {
458 my $content = $ctx->{added_content}->{$type};
460 if ($content->{status} == 3) {
461 $logger->debug("tpac: finishing added content request for $type");
463 my $req = $content->{request};
464 my $sel = IO::Select->new($req);
466 # if we are requesting a specific type of content, give the
467 # backend code a little extra time to retrieve the content.
468 my $wait = $type eq $sel_type ? 3 : 0; # TODO: config?
470 if ($sel->can_read($wait)) {
471 my ($code) = $req->read_response_headers;
472 $content->{status} = $code eq '200' ? 1 : 2;
473 $logger->debug("tpac: added content request for $type returned $code");
475 if ($code eq '200' and $type eq $sel_type) {
478 my $n = $req->read_entity_body($buf, 1024);
480 $content->{content} .= $buf;
488 # XXX this is copied directly from AddedContent.pm in
489 # working/user/jeff/ac_by_record_id_rebase. When Jeff's
490 # branch is merged and Evergreen gets added content
491 # lookup by ID, this can be removed.
492 # returns [{tag => $tag, value => $value}, {tag => $tag2, value => $value2}]
496 my $key_data = $self->editor->json_query({
497 select => {mfr => ['tag', 'value']},
519 grep {$_->{tag} eq '020'} @$key_data,
520 grep {$_->{tag} eq '024'} @$key_data