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);
67 $self->ctx->{bib_is_dead} = OpenILS::Application::AppUtils->is_true(
68 OpenILS::Utils::CStoreEditor->new->json_query({
69 select => { bre => [ 'deleted' ] },
71 where => { 'id' => $rec_id }
78 $ctx->{get_org_setting}->
79 ($org, "opac.fully_compressed_serial_holdings")
81 # We're loading this data here? Are we therefore assuming that we
82 # *are* going to display something in the "issues" expandy?
83 $self->load_serial_holding_summaries($rec_id, $org, $copy_depth);
85 $ctx->{mfhd_summaries} =
86 $self->get_mfhd_summaries($rec_id, $org, $copy_depth);
88 if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
90 $ctx->{have_mfhd_to_show} = 1;
96 $ctx->{marchtml} = $self->mk_marc_html($rec_id);
103 $self->prepare_browse_call_numbers();
107 my @expand = $self->cgi->param('expand');
108 if (grep {$_ eq 'all'} @expand) {
109 $ctx->{expand_all} = 1;
110 $expandies{$_}->() for keys %expandies;
113 for my $exp (@expand) {
114 $ctx->{"expand_$exp"} = 1;
115 $expandies{$exp}->() if exists $expandies{$exp};
119 return Apache2::Const::OK;
122 # collect IDs and info on the search that lead to this details page
123 # If no search query, etc is present, we leave ctx.search_result_index == -1
124 sub fetch_related_search_info {
127 my $ctx = $self->ctx;
128 $ctx->{search_result_index} = -1;
130 $self->load_rresults(internal => 1);
132 my @search_ids = @{$ctx->{ids}};
133 return unless @search_ids;
135 for my $idx (0..$#search_ids) {
136 if ($search_ids[$idx] == $rec_id) {
137 $ctx->{prev_search_record} = $search_ids[$idx - 1] if $idx > 0;
138 $ctx->{next_search_record} = $search_ids[$idx + 1];
139 $ctx->{search_result_index} = $idx;
144 $ctx->{first_search_record} = $search_ids[0];
145 $ctx->{last_search_record} = $search_ids[-1];
154 my $copy_limit = shift;
155 my $copy_offset = shift;
158 my $query = $U->basic_opac_copy_query(
159 $rec_id, undef, undef, $copy_limit, $copy_offset, $self->ctx->{is_staff}
162 if($org != $self->ctx->{aou_tree}->()->id) {
163 # no need to add the org join filter if we're not actually filtering
164 $query->{from}->{acp}->{aou} = {
172 transform => 'actor.org_unit_descendants',
173 result_field => 'id',
177 where => {id => $org}
184 # Unsure if we want these in the shared function, leaving here for now
185 unshift(@{$query->{order_by}},
186 { class => "aou", field => 'id',
187 transform => 'evergreen.rank_ou', params => [$org, $pref_ou]
190 push(@{$query->{order_by}},
191 { class => "acp", field => 'status',
192 transform => 'evergreen.rank_cp_status'
200 my($self, $rec_id) = @_;
202 # could be optimized considerably by performing the xslt on the already fetched record
203 return $U->simplereq(
205 'open-ils.search.biblio.record.html', $rec_id, 1);
208 sub load_serial_holding_summaries {
209 my ($self, $rec_id, $org, $depth) = @_;
211 my $limit = $self->cgi->param("slimit") || 10;
212 my $offset = $self->cgi->param("soffset") || 0;
214 my $serial = create OpenSRF::AppSession("open-ils.serial");
216 # First, get the tree of /summaries/ of holdings.
217 my $tree = $serial->request(
218 "open-ils.serial.holding_summary_tree.by_bib",
219 $rec_id, $org, $depth, $limit, $offset
222 return if $self->apache_log_if_event(
223 $tree, "getting holding summary tree for record $rec_id"
226 # Next, if requested, get a list of individual holdings under a
227 # particular summary.
229 my $summary_id = int($self->cgi->param("sid") || 0);
230 my $summary_type = $self->cgi->param("stype");
232 if ($summary_id and $summary_type) {
233 my $expand_path = [ $self->cgi->param("sepath") ],
234 my $expand_limit = $self->cgi->param("selimit");
235 my $expand_offsets = [ $self->cgi->param("seoffset") ];
236 my $auto_expand_first = 0;
238 if (not @$expand_offsets) {
239 $expand_offsets = undef;
240 $auto_expand_first = 1;
243 $holdings = $serial->request(
244 "open-ils.serial.holdings.grouped_by_summary",
245 $summary_type, $summary_id,
246 $expand_path, $expand_limit, $expand_offsets,
248 1 + ($self->ctx->{is_staff} ? 1 : 0)
251 if ($holdings and ref $holdings eq "ARRAY") {
252 $self->place_holdings_with_summary(
253 $tree, $holdings, $summary_id, $summary_type
254 ) or $self->apache->log->warn(
255 "could not place holdings within summary tree"
258 $self->apache_log_if_event(
259 $holdings, "getting holdings grouped by summary $summary_id"
266 # The presence of any keys in the tree hash other than 'more' means that we
267 # must have /something/ we could show.
268 $self->ctx->{have_holdings_to_show} = grep { $_ ne 'more' } (keys %$tree);
270 $self->ctx->{holding_summary_tree} = $tree;
273 # This helper to load_serial_holding_summaries() recursively searches in
274 # $tree for a holding summary matching $sid and $stype, and places $holdings
275 # within the node for that summary. IOW, this is about showing expanded
276 # holdings under their "parent" summary.
277 sub place_holdings_with_summary {
278 my ($self, $tree, $holdings, $sid, $stype) = @_;
280 foreach my $sum (@{$tree->{holding_summaries}}) {
281 if ($sum->{id} == $sid and $sum->{summary_type} eq $stype) {
282 $sum->{holdings} = $holdings;
287 foreach my $child (@{$tree->{children}}) {
288 return 1 if $self->place_holdings_with_summary(
289 $child, $holdings, $sid, $stype
296 sub get_mfhd_summaries {
297 my ($self, $rec_id, $org, $depth) = @_;
299 my $serial = create OpenSRF::AppSession("open-ils.search");
300 my $result = $serial->request(
301 "open-ils.search.serial.record.bib.retrieve",
302 $rec_id, $org, $depth
309 sub any_call_number_label {
312 if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
313 return $self->ctx->{copies}->[0]->{call_number_label};
319 sub prepare_browse_call_numbers {
322 my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
325 my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
326 $self->ctx->{aou_tree}->();
328 my $supercat = create OpenSRF::AppSession("open-ils.supercat");
329 my $results = $supercat->request(
330 "open-ils.supercat.call_number.browse",
331 $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
336 $self->ctx->{browsed_call_numbers} = [
339 (new XML::LibXML)->parse_string($_->record->marc)
344 $self->ctx->{browsing_ou} = $org_unit;
347 sub get_hold_copy_summary {
348 my ($self, $rec_id, $org) = @_;
350 my $search = OpenSRF::AppSession->create('open-ils.search');
351 my $req1 = $search->request(
352 'open-ils.search.biblio.record.copy_count', $org, $rec_id);
354 $self->ctx->{record_hold_count} = $U->simplereq(
355 'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
357 $self->ctx->{copy_summary} = $req1->recv->content;
362 sub load_print_record {
365 my $rec_id = $self->ctx->{page_args}->[0]
366 or return Apache2::Const::HTTP_BAD_REQUEST;
368 $self->{ctx}->{bre_id} = $rec_id;
369 $self->{ctx}->{printable_record} = $U->simplereq(
371 'open-ils.search.biblio.record.print', $rec_id);
373 return Apache2::Const::OK;
376 sub load_email_record {
379 my $rec_id = $self->ctx->{page_args}->[0]
380 or return Apache2::Const::HTTP_BAD_REQUEST;
382 $self->{ctx}->{bre_id} = $rec_id;
385 'open-ils.search.biblio.record.email',
386 $self->ctx->{authtoken}, $rec_id);
388 return Apache2::Const::OK;