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 $depth = $self->cgi->param('depth');
19 $depth = $ctx->{get_aou}->($org)->ou_type->depth
20 unless defined $depth; # can be 0
22 my $copy_depth = $self->cgi->param('copy_depth');
23 $copy_depth = $depth unless defined $copy_depth; # can be 0
24 $self->ctx->{copy_depth} = $copy_depth;
26 my $copy_limit = int($self->cgi->param('copy_limit') || 10);
27 my $copy_offset = int($self->cgi->param('copy_offset') || 0);
29 my $rec_id = $ctx->{page_args}->[0]
30 or return Apache2::Const::HTTP_BAD_REQUEST;
32 $self->get_staff_search_settings;
33 if ($ctx->{staff_saved_search_size}) {
34 $ctx->{saved_searches} = ($self->staff_load_searches)[1];
37 $self->fetch_related_search_info($rec_id);
39 # run copy retrieval in parallel to bib retrieval
41 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
42 my $copy_rec = $cstore->request(
43 'open-ils.cstore.json_query.atomic',
44 $self->mk_copy_query($rec_id, $org, $copy_depth, $copy_limit, $copy_offset)
47 my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {flesh => '{holdings_xml,bmp,mra,acp,acnp,acns}'});
48 $ctx->{bre_id} = $rec_data[0]->{id};
49 $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
51 $ctx->{copies} = $copy_rec->gather(1);
52 $ctx->{copy_limit} = $copy_limit;
53 $ctx->{copy_offset} = $copy_offset;
55 $ctx->{have_holdings_to_show} = 0;
56 $ctx->{have_mfhd_to_show} = 0;
58 $self->get_hold_copy_summary($rec_id, $org);
63 $ctx->{get_org_setting}->
64 ($org, "opac.fully_compressed_serial_holdings")
66 $ctx->{holding_summaries} =
67 $self->get_holding_summaries($rec_id, $org, $copy_depth);
69 $ctx->{have_holdings_to_show} =
70 scalar(@{$ctx->{holding_summaries}->{basic}}) ||
71 scalar(@{$ctx->{holding_summaries}->{index}}) ||
72 scalar(@{$ctx->{holding_summaries}->{supplement}});
74 $ctx->{mfhd_summaries} =
75 $self->get_mfhd_summaries($rec_id, $org, $copy_depth);
77 if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
79 $ctx->{have_mfhd_to_show} = 1;
85 $ctx->{marchtml} = $self->mk_marc_html($rec_id);
88 $ctx->{expanded_holdings} =
89 $self->get_expanded_holdings($rec_id, $org, $copy_depth)
90 if $ctx->{have_holdings_to_show};
93 $self->prepare_browse_call_numbers();
97 my @expand = $self->cgi->param('expand');
98 if (grep {$_ eq 'all'} @expand) {
99 $ctx->{expand_all} = 1;
100 $expandies{$_}->() for keys %expandies;
103 for my $exp (@expand) {
104 $ctx->{"expand_$exp"} = 1;
105 $expandies{$exp}->() if exists $expandies{$exp};
109 return Apache2::Const::OK;
112 # collect IDs and info on the search that lead to this details page
113 # If no search query, etc is present, we leave ctx.search_result_index == -1
114 sub fetch_related_search_info {
117 my $ctx = $self->ctx;
118 $ctx->{search_result_index} = -1;
120 $self->load_rresults(internal => 1);
122 my @search_ids = @{$ctx->{ids}};
123 return unless @search_ids;
125 for my $idx (0..$#search_ids) {
126 if ($search_ids[$idx] == $rec_id) {
127 $ctx->{prev_search_record} = $search_ids[$idx - 1] if $idx > 0;
128 $ctx->{next_search_record} = $search_ids[$idx + 1];
129 $ctx->{search_result_index} = $idx;
134 $ctx->{first_search_record} = $search_ids[0];
135 $ctx->{last_search_record} = $search_ids[-1];
144 my $copy_limit = shift;
145 my $copy_offset = shift;
149 acp => ['id', 'barcode', 'circ_lib', 'create_date', 'age_protect', 'holdable'],
151 {column => 'name', alias => 'copy_location'},
152 {column => 'holdable', alias => 'location_holdable'}
155 {column => 'name', alias => 'copy_status'},
156 {column => 'holdable', alias => 'status_holdable'}
159 {column => 'label', alias => 'call_number_label'},
160 {column => 'id', alias => 'call_number'}
162 circ => ['due_date'],
164 {column => 'label', alias => 'call_number_prefix_label'},
165 {column => 'id', alias => 'call_number_prefix'}
168 {column => 'label', alias => 'call_number_suffix_label'},
169 {column => 'id', alias => 'call_number_suffix'}
172 {column => 'label', alias => 'part_label'},
180 acnp => { fkey => 'prefix' },
181 acns => { fkey => 'suffix' }
183 filter => [{deleted => 'f'}, {record => $rec_id}],
185 circ => { # If the copy is circulating, retrieve the open circ
187 filter => {checkin_time => undef}
195 bmp => { type => 'left' }
202 '+acp' => {deleted => 'f' }
206 {class => 'aou', field => 'name'},
207 {class => 'acn', field => 'label'}
210 limit => $copy_limit,
211 offset => $copy_offset
214 # XXX In the future, $sort_org should be understood to be an abstration
215 # that refers to something configurable, not necessariyl physical_loc.
217 if (my $sort_org = $self->ctx->{physical_loc}) {
218 unshift @{$query->{order_by}}, {
219 class => 'acp', field => 'circ_lib', transform => 'numeric_eq',
220 params => [$sort_org], direction => 'desc'
224 if($org != $self->ctx->{aou_tree}->()->id) {
225 # no need to add the org join filter if we're not actually filtering
226 $query->{from}->{acp}->{aou} = {
234 transform => 'actor.org_unit_descendants',
235 result_field => 'id',
239 where => {id => $org}
246 # Filter hidden items if this is the public catalog
247 unless($self->ctx->{is_staff}) {
248 $query->{where}->{'+acp'}->{opac_visible} = 't';
249 $query->{from}->{'acp'}->{'acpl'}->{filter} = {opac_visible => 't'};
250 $query->{from}->{'acp'}->{'ccs'}->{filter} = {opac_visible => 't'};
251 $query->{from}->{'acp'}->{'aou'}->{filter} = {opac_visible => 't'};
258 my($self, $rec_id) = @_;
260 # could be optimized considerably by performing the xslt on the already fetched record
261 return $U->simplereq(
263 'open-ils.search.biblio.record.html', $rec_id, 1);
266 sub get_holding_summaries {
267 my ($self, $rec_id, $org, $depth) = @_;
269 my $serial = create OpenSRF::AppSession("open-ils.serial");
270 my $result = $serial->request(
271 "open-ils.serial.bib.summary_statements",
272 $rec_id, {"org_id" => $org, "depth" => $depth}
279 sub get_mfhd_summaries {
280 my ($self, $rec_id, $org, $depth) = @_;
282 my $serial = create OpenSRF::AppSession("open-ils.search");
283 my $result = $serial->request(
284 "open-ils.search.serial.record.bib.retrieve",
285 $rec_id, $org, $depth
292 sub get_expanded_holdings {
293 my ($self, $rec_id, $org, $depth) = @_;
295 my $holding_limit = int($self->cgi->param("holding_limit") || 10);
296 my $holding_offset = int($self->cgi->param("holding_offset") || 0);
297 my $type = $self->cgi->param("expand_holding_type");
299 my $serial = create OpenSRF::AppSession("open-ils.serial");
300 my $result = $serial->request(
301 "open-ils.serial.received_siss.retrieve.by_bib.atomic",
303 "ou" => $org, "depth" => $depth,
304 "limit" => $holding_limit, "offset" => $holding_offset,
313 sub any_call_number_label {
316 if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
317 return $self->ctx->{copies}->[0]->{call_number_label};
323 sub prepare_browse_call_numbers {
326 my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
329 my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
330 $self->ctx->{aou_tree}->();
332 my $supercat = create OpenSRF::AppSession("open-ils.supercat");
333 my $results = $supercat->request(
334 "open-ils.supercat.call_number.browse",
335 $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
340 $self->ctx->{browsed_call_numbers} = [
343 (new XML::LibXML)->parse_string($_->record->marc)
348 $self->ctx->{browsing_ou} = $org_unit;
351 sub get_hold_copy_summary {
352 my ($self, $rec_id, $org) = @_;
354 my $search = OpenSRF::AppSession->create('open-ils.search');
355 my $req1 = $search->request(
356 'open-ils.search.biblio.record.copy_count', $org, $rec_id);
358 $self->ctx->{record_hold_count} = $U->simplereq(
359 'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
361 $self->ctx->{copy_summary} = $req1->recv->content;