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';
15 $ctx->{page} = 'record';
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
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;
29 my $copy_limit = int($self->cgi->param('copy_limit') || 10);
30 my $copy_offset = int($self->cgi->param('copy_offset') || 0);
32 my $rec_id = $ctx->{page_args}->[0]
33 or return Apache2::Const::HTTP_BAD_REQUEST;
35 $self->get_staff_search_settings;
36 if ($ctx->{staff_saved_search_size}) {
37 $ctx->{saved_searches} = ($self->staff_load_searches)[1];
39 $self->timelog("past staff saved searches");
41 $self->fetch_related_search_info($rec_id);
42 $self->timelog("past related search info");
44 # run copy retrieval in parallel to bib retrieval
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)
52 my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {
53 flesh => '{holdings_xml,bmp,mra,acp,acnp,acns}',
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};
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;
68 $ctx->{have_holdings_to_show} = 0;
69 $ctx->{have_mfhd_to_show} = 0;
71 $self->get_hold_copy_summary($rec_id, $org);
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' ] },
78 where => { 'id' => $rec_id }
85 $ctx->{get_org_setting}->
86 ($org, "opac.fully_compressed_serial_holdings")
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);
92 $ctx->{mfhd_summaries} =
93 $self->get_mfhd_summaries($rec_id, $org, $copy_depth);
95 if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
97 $ctx->{have_mfhd_to_show} = 1;
101 $self->timelog("past serials holding stuff");
105 $ctx->{marchtml} = $self->mk_marc_html($rec_id);
112 $self->prepare_browse_call_numbers();
116 my @expand = $self->cgi->param('expand');
117 if (grep {$_ eq 'all'} @expand) {
118 $ctx->{expand_all} = 1;
119 $expandies{$_}->() for keys %expandies;
122 for my $exp (@expand) {
123 $ctx->{"expand_$exp"} = 1;
124 $expandies{$exp}->() if exists $expandies{$exp};
128 $self->timelog("past expandies");
129 return Apache2::Const::OK;
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 {
137 my $ctx = $self->ctx;
138 $ctx->{search_result_index} = -1;
140 $self->load_rresults(internal => 1);
142 my @search_ids = @{$ctx->{ids}};
143 return unless @search_ids;
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;
154 $ctx->{first_search_record} = $search_ids[0];
155 $ctx->{last_search_record} = $search_ids[-1];
164 my $copy_limit = shift;
165 my $copy_offset = shift;
168 my $query = $U->basic_opac_copy_query(
169 $rec_id, undef, undef, $copy_limit, $copy_offset, $self->ctx->{is_staff}
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} = {
182 transform => 'actor.org_unit_descendants',
183 result_field => 'id',
187 where => {id => $org}
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]
200 push(@{$query->{order_by}},
201 { class => "acp", field => 'status',
202 transform => 'evergreen.rank_cp_status'
210 my($self, $rec_id) = @_;
212 # could be optimized considerably by performing the xslt on the already fetched record
213 return $U->simplereq(
215 'open-ils.search.biblio.record.html', $rec_id, 1);
218 sub load_serial_holding_summaries {
219 my ($self, $rec_id, $org, $depth) = @_;
221 my $limit = $self->cgi->param("slimit") || 10;
222 my $offset = $self->cgi->param("soffset") || 0;
224 my $serial = create OpenSRF::AppSession("open-ils.serial");
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
232 return if $self->apache_log_if_event(
233 $tree, "getting holding summary tree for record $rec_id"
236 # Next, if requested, get a list of individual holdings under a
237 # particular summary.
239 my $summary_id = int($self->cgi->param("sid") || 0);
240 my $summary_type = $self->cgi->param("stype");
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;
248 if (not @$expand_offsets) {
249 $expand_offsets = undef;
250 $auto_expand_first = 1;
253 $holdings = $serial->request(
254 "open-ils.serial.holdings.grouped_by_summary",
255 $summary_type, $summary_id,
256 $expand_path, $expand_limit, $expand_offsets,
258 1 + ($self->ctx->{is_staff} ? 1 : 0)
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"
268 $self->apache_log_if_event(
269 $holdings, "getting holdings grouped by summary $summary_id"
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);
280 $self->ctx->{holding_summary_tree} = $tree;
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) = @_;
290 foreach my $sum (@{$tree->{holding_summaries}}) {
291 if ($sum->{id} == $sid and $sum->{summary_type} eq $stype) {
292 $sum->{holdings} = $holdings;
297 foreach my $child (@{$tree->{children}}) {
298 return 1 if $self->place_holdings_with_summary(
299 $child, $holdings, $sid, $stype
306 sub get_mfhd_summaries {
307 my ($self, $rec_id, $org, $depth) = @_;
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
319 sub any_call_number_label {
322 if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
323 return $self->ctx->{copies}->[0]->{call_number_label};
329 sub prepare_browse_call_numbers {
332 my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
335 my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
336 $self->ctx->{aou_tree}->();
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")
346 $self->ctx->{browsed_call_numbers} = [
349 (new XML::LibXML)->parse_string($_->record->marc)
354 $self->ctx->{browsing_ou} = $org_unit;
357 sub get_hold_copy_summary {
358 my ($self, $rec_id, $org) = @_;
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);
364 $self->ctx->{record_hold_count} = $U->simplereq(
365 'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
367 $self->ctx->{copy_summary} = $req1->recv->content;
372 sub load_print_record {
375 my $rec_id = $self->ctx->{page_args}->[0]
376 or return Apache2::Const::HTTP_BAD_REQUEST;
378 $self->{ctx}->{bre_id} = $rec_id;
379 $self->{ctx}->{printable_record} = $U->simplereq(
381 'open-ils.search.biblio.record.print', $rec_id);
383 return Apache2::Const::OK;
386 sub load_email_record {
389 my $rec_id = $self->ctx->{page_args}->[0]
390 or return Apache2::Const::HTTP_BAD_REQUEST;
392 $self->{ctx}->{bre_id} = $rec_id;
395 'open-ils.search.biblio.record.email',
396 $self->ctx->{authtoken}, $rec_id);
398 return Apache2::Const::OK;