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->cgi->param('loc') || $ctx->{aou_tree}->()->id;
18 my $depth = $self->cgi->param('depth') || 0;
19 my $copy_limit = int($self->cgi->param('copy_limit') || 10);
20 my $copy_offset = int($self->cgi->param('copy_offset') || 0);
22 my $rec_id = $ctx->{page_args}->[0]
23 or return Apache2::Const::HTTP_BAD_REQUEST;
25 $self->get_staff_search_settings;
26 if ($ctx->{staff_saved_search_size}) {
27 $ctx->{saved_searches} = ($self->staff_load_searches)[1];
30 $self->fetch_related_search_info($rec_id);
32 # run copy retrieval in parallel to bib retrieval
34 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
35 my $copy_rec = $cstore->request(
36 'open-ils.cstore.json_query.atomic',
37 $self->mk_copy_query($rec_id, $org, $depth, $copy_limit, $copy_offset)
40 my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {flesh => '{holdings_xml,mra,acp}'});
41 $ctx->{bre_id} = $rec_data[0]->{id};
42 $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
44 $ctx->{copies} = $copy_rec->gather(1);
45 $ctx->{copy_limit} = $copy_limit;
46 $ctx->{copy_offset} = $copy_offset;
48 $ctx->{have_holdings_to_show} = 0;
49 $ctx->{have_mfhd_to_show} = 0;
50 $self->get_hold_copy_summary($rec_id, $org);
55 $ctx->{get_org_setting}->
56 ($org, "opac.fully_compressed_serial_holdings")
58 $ctx->{holding_summaries} =
59 $self->get_holding_summaries($rec_id, $org, $depth);
61 $ctx->{have_holdings_to_show} =
62 scalar(@{$ctx->{holding_summaries}->{basic}}) ||
63 scalar(@{$ctx->{holding_summaries}->{index}}) ||
64 scalar(@{$ctx->{holding_summaries}->{supplement}});
66 $ctx->{mfhd_summaries} =
67 $self->get_mfhd_summaries($rec_id, $org, $depth);
69 if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
71 $ctx->{have_mfhd_to_show} = 1;
77 $ctx->{marchtml} = $self->mk_marc_html($rec_id);
80 $ctx->{expanded_holdings} =
81 $self->get_expanded_holdings($rec_id, $org, $depth)
82 if $ctx->{have_holdings_to_show};
85 $self->prepare_browse_call_numbers();
89 my @expand = $self->cgi->param('expand');
90 if (grep {$_ eq 'all'} @expand) {
91 $ctx->{expand_all} = 1;
92 $expandies{$_}->() for keys %expandies;
95 for my $exp (@expand) {
96 $ctx->{"expand_$exp"} = 1;
97 $expandies{$exp}->() if exists $expandies{$exp};
101 return Apache2::Const::OK;
104 # collect IDs and info on the search that lead to this details page
105 # If no search query, etc is present, we leave ctx.search_result_index == -1
106 sub fetch_related_search_info {
109 my $ctx = $self->ctx;
110 $ctx->{search_result_index} = -1;
112 $self->load_rresults(internal => 1);
114 my @search_ids = @{$ctx->{ids}};
115 return unless @search_ids;
117 for my $idx (0..$#search_ids) {
118 if ($search_ids[$idx] == $rec_id) {
119 $ctx->{prev_search_record} = $search_ids[$idx - 1] if $idx > 0;
120 $ctx->{next_search_record} = $search_ids[$idx + 1];
121 $ctx->{search_result_index} = $idx;
126 $ctx->{first_search_record} = $search_ids[0];
127 $ctx->{last_search_record} = $search_ids[-1];
136 my $copy_limit = shift;
137 my $copy_offset = shift;
141 acp => ['id', 'barcode', 'circ_lib', 'create_date', 'age_protect', 'holdable'],
143 {column => 'name', alias => 'copy_location'},
144 {column => 'holdable', alias => 'location_holdable'}
147 {column => 'name', alias => 'copy_status'},
148 {column => 'holdable', alias => 'status_holdable'}
151 {column => 'label', alias => 'call_number_label'},
152 {column => 'id', alias => 'call_number'}
154 circ => ['due_date'],
160 join => {bre => {filter => {id => $rec_id }}},
161 filter => {deleted => 'f'}
163 circ => { # If the copy is circulating, retrieve the open circ
165 filter => {checkin_time => undef}
173 where => {'+acp' => {deleted => 'f' }},
176 {class => 'aou', field => 'name'},
177 {class => 'acn', field => 'label'}
180 limit => $copy_limit,
181 offset => $copy_offset
184 # XXX In the future, $sort_org should be understood to be an abstration
185 # that refers to something configurable, not necessariyl physical_loc.
187 if (my $sort_org = $self->ctx->{physical_loc}) {
188 unshift @{$query->{order_by}}, {
189 class => 'acp', field => 'circ_lib', transform => 'numeric_eq',
190 params => [$sort_org], direction => 'desc'
194 if($org != $self->ctx->{aou_tree}->()->id) {
195 # no need to add the org join filter if we're not actually filtering
196 $query->{from}->{acp}->{aou} = {
204 transform => 'actor.org_unit_descendants',
205 result_field => 'id',
209 where => {id => $org}
216 # Filter hidden items if this is the public catalog
217 unless($self->ctx->{is_staff}) {
218 $query->{where}->{'+acp'}->{opac_visible} = 't';
219 $query->{from}->{'acp'}->{'acpl'}->{filter} = {opac_visible => 't'};
220 $query->{from}->{'acp'}->{'ccs'}->{filter} = {opac_visible => 't'};
227 my($self, $rec_id) = @_;
229 # could be optimized considerably by performing the xslt on the already fetched record
230 return $U->simplereq(
232 'open-ils.search.biblio.record.html', $rec_id, 1);
235 sub get_holding_summaries {
236 my ($self, $rec_id, $org, $depth) = @_;
238 my $serial = create OpenSRF::AppSession("open-ils.serial");
239 my $result = $serial->request(
240 "open-ils.serial.bib.summary_statements",
241 $rec_id, {"org_id" => $org, "depth" => $depth}
248 sub get_mfhd_summaries {
249 my ($self, $rec_id, $org, $depth) = @_;
251 my $serial = create OpenSRF::AppSession("open-ils.search");
252 my $result = $serial->request(
253 "open-ils.search.serial.record.bib.retrieve",
254 $rec_id, $org, $depth
261 sub get_expanded_holdings {
262 my ($self, $rec_id, $org, $depth) = @_;
264 my $holding_limit = int($self->cgi->param("holding_limit") || 10);
265 my $holding_offset = int($self->cgi->param("holding_offset") || 0);
266 my $type = $self->cgi->param("expand_holding_type");
268 my $serial = create OpenSRF::AppSession("open-ils.serial");
269 my $result = $serial->request(
270 "open-ils.serial.received_siss.retrieve.by_bib.atomic",
272 "ou" => $org, "depth" => $depth,
273 "limit" => $holding_limit, "offset" => $holding_offset,
282 sub any_call_number_label {
285 if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
286 return $self->ctx->{copies}->[0]->{call_number_label};
292 sub prepare_browse_call_numbers {
295 my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
298 my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
299 $self->ctx->{aou_tree}->();
301 my $supercat = create OpenSRF::AppSession("open-ils.supercat");
302 my $results = $supercat->request(
303 "open-ils.supercat.call_number.browse",
304 $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
309 $self->ctx->{browsed_call_numbers} = [
312 (new XML::LibXML)->parse_string($_->record->marc)
317 $self->ctx->{browsing_ou} = $org_unit;
320 sub get_hold_copy_summary {
321 my ($self, $rec_id, $org) = @_;
323 my $search = OpenSRF::AppSession->create('open-ils.search');
324 my $req1 = $search->request(
325 'open-ils.search.biblio.record.copy_count', $org, $rec_id);
327 $self->ctx->{record_hold_count} = $U->simplereq(
328 'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
330 $self->ctx->{copy_summary} = $req1->recv->content;