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 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
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;
28 my $copy_limit = int($self->cgi->param('copy_limit') || 10);
29 my $copy_offset = int($self->cgi->param('copy_offset') || 0);
31 my $rec_id = $ctx->{page_args}->[0]
32 or return Apache2::Const::HTTP_BAD_REQUEST;
34 $self->get_staff_search_settings;
35 if ($ctx->{staff_saved_search_size}) {
36 $ctx->{saved_searches} = ($self->staff_load_searches)[1];
39 $self->fetch_related_search_info($rec_id);
41 # run copy retrieval in parallel to bib retrieval
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)
49 my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {
50 flesh => '{holdings_xml,bmp,mra,acp,acnp,acns}',
55 $ctx->{bre_id} = $rec_data[0]->{id};
56 $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
58 $ctx->{copies} = $copy_rec->gather(1);
59 $ctx->{copy_limit} = $copy_limit;
60 $ctx->{copy_offset} = $copy_offset;
62 $ctx->{have_holdings_to_show} = 0;
63 $ctx->{have_mfhd_to_show} = 0;
65 $self->get_hold_copy_summary($rec_id, $org);
70 $ctx->{get_org_setting}->
71 ($org, "opac.fully_compressed_serial_holdings")
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);
77 $ctx->{mfhd_summaries} =
78 $self->get_mfhd_summaries($rec_id, $org, $copy_depth);
80 if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
82 $ctx->{have_mfhd_to_show} = 1;
88 $ctx->{marchtml} = $self->mk_marc_html($rec_id);
95 $self->prepare_browse_call_numbers();
99 my @expand = $self->cgi->param('expand');
100 if (grep {$_ eq 'all'} @expand) {
101 $ctx->{expand_all} = 1;
102 $expandies{$_}->() for keys %expandies;
105 for my $exp (@expand) {
106 $ctx->{"expand_$exp"} = 1;
107 $expandies{$exp}->() if exists $expandies{$exp};
111 return Apache2::Const::OK;
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 {
119 my $ctx = $self->ctx;
120 $ctx->{search_result_index} = -1;
122 $self->load_rresults(internal => 1);
124 my @search_ids = @{$ctx->{ids}};
125 return unless @search_ids;
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;
136 $ctx->{first_search_record} = $search_ids[0];
137 $ctx->{last_search_record} = $search_ids[-1];
146 my $copy_limit = shift;
147 my $copy_offset = shift;
150 my $query = $U->basic_opac_copy_query(
151 $rec_id, undef, undef, $copy_limit, $copy_offset, $self->ctx->{is_staff}
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} = {
164 transform => 'actor.org_unit_descendants',
165 result_field => 'id',
169 where => {id => $org}
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]
182 push(@{$query->{order_by}},
183 { class => "acp", field => 'status',
184 transform => 'evergreen.rank_cp_status'
192 my($self, $rec_id) = @_;
194 # could be optimized considerably by performing the xslt on the already fetched record
195 return $U->simplereq(
197 'open-ils.search.biblio.record.html', $rec_id, 1);
200 sub load_serial_holding_summaries {
201 my ($self, $rec_id, $org, $depth) = @_;
203 my $limit = $self->cgi->param("slimit") || 10;
204 my $offset = $self->cgi->param("soffset") || 0;
206 my $serial = create OpenSRF::AppSession("open-ils.serial");
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
214 return if $self->apache_log_if_event(
215 $tree, "getting holding summary tree for record $rec_id"
218 # Next, if requested, get a list of individual holdings under a
219 # particular summary.
221 my $summary_id = int($self->cgi->param("sid") || 0);
222 my $summary_type = $self->cgi->param("stype");
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;
230 if (not @$expand_offsets) {
231 $expand_offsets = undef;
232 $auto_expand_first = 1;
235 $holdings = $serial->request(
236 "open-ils.serial.holdings.grouped_by_summary",
237 $summary_type, $summary_id,
238 $expand_path, $expand_limit, $expand_offsets,
240 1 + ($self->ctx->{is_staff} ? 1 : 0)
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"
250 $self->apache_log_if_event(
251 $holdings, "getting holdings grouped by summary $summary_id"
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);
262 $self->ctx->{holding_summary_tree} = $tree;
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) = @_;
272 foreach my $sum (@{$tree->{holding_summaries}}) {
273 if ($sum->{id} == $sid and $sum->{summary_type} eq $stype) {
274 $sum->{holdings} = $holdings;
279 foreach my $child (@{$tree->{children}}) {
280 return 1 if $self->place_holdings_with_summary(
281 $child, $holdings, $sid, $stype
288 sub get_mfhd_summaries {
289 my ($self, $rec_id, $org, $depth) = @_;
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
301 sub any_call_number_label {
304 if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
305 return $self->ctx->{copies}->[0]->{call_number_label};
311 sub prepare_browse_call_numbers {
314 my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
317 my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
318 $self->ctx->{aou_tree}->();
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")
328 $self->ctx->{browsed_call_numbers} = [
331 (new XML::LibXML)->parse_string($_->record->marc)
336 $self->ctx->{browsing_ou} = $org_unit;
339 sub get_hold_copy_summary {
340 my ($self, $rec_id, $org) = @_;
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);
346 $self->ctx->{record_hold_count} = $U->simplereq(
347 'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
349 $self->ctx->{copy_summary} = $req1->recv->content;
354 sub load_print_record {
357 my $rec_id = $self->ctx->{page_args}->[0]
358 or return Apache2::Const::HTTP_BAD_REQUEST;
360 $self->{ctx}->{bre_id} = $rec_id;
361 $self->{ctx}->{printable_record} = $U->simplereq(
363 'open-ils.search.biblio.record.print', $rec_id);
365 return Apache2::Const::OK;
368 sub load_email_record {
371 my $rec_id = $self->ctx->{page_args}->[0]
372 or return Apache2::Const::HTTP_BAD_REQUEST;
374 $self->{ctx}->{bre_id} = $rec_id;
377 'open-ils.search.biblio.record.email',
378 $self->ctx->{authtoken}, $rec_id);
380 return Apache2::Const::OK;