]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
Tpac: Rename orig_loc to physical_loc, for clarity's sake.
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Record.pm
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';
9
10 # context additions: 
11 #   record : bre object
12 sub load_record {
13     my $self = shift;
14     my $ctx = $self->ctx;
15     $ctx->{page} = 'record';  
16
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);
21
22     my $rec_id = $ctx->{page_args}->[0]
23         or return Apache2::Const::HTTP_BAD_REQUEST;
24
25     $self->get_staff_search_settings;
26     if ($ctx->{staff_saved_search_size}) {
27         $ctx->{saved_searches} = ($self->staff_load_searches)[1];
28     }
29
30     $self->fetch_related_search_info($rec_id);
31
32     # run copy retrieval in parallel to bib retrieval
33     # XXX unapi
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)
38     );
39
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};
43
44     $ctx->{copies} = $copy_rec->gather(1);
45     $ctx->{copy_limit} = $copy_limit;
46     $ctx->{copy_offset} = $copy_offset;
47
48     $ctx->{have_holdings_to_show} = 0;
49     $ctx->{have_mfhd_to_show} = 0;
50     $self->get_hold_copy_summary($rec_id, $org);
51
52     $cstore->kill_me;
53
54     if (
55         $ctx->{get_org_setting}->
56             ($org, "opac.fully_compressed_serial_holdings")
57     ) {
58         $ctx->{holding_summaries} =
59             $self->get_holding_summaries($rec_id, $org, $depth);
60
61         $ctx->{have_holdings_to_show} =
62             scalar(@{$ctx->{holding_summaries}->{basic}}) ||
63             scalar(@{$ctx->{holding_summaries}->{index}}) ||
64             scalar(@{$ctx->{holding_summaries}->{supplement}});
65     } else {
66         $ctx->{mfhd_summaries} =
67             $self->get_mfhd_summaries($rec_id, $org, $depth);
68
69         if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
70         ) {
71             $ctx->{have_mfhd_to_show} = 1;
72         };
73     }
74
75     my %expandies = (
76         marchtml => sub {
77             $ctx->{marchtml} = $self->mk_marc_html($rec_id);
78         },
79         issues => sub {
80             $ctx->{expanded_holdings} =
81                 $self->get_expanded_holdings($rec_id, $org, $depth)
82                 if $ctx->{have_holdings_to_show};
83         },
84         cnbrowse => sub {
85             $self->prepare_browse_call_numbers();
86         }
87     );
88
89     my @expand = $self->cgi->param('expand');
90     if (grep {$_ eq 'all'} @expand) {
91         $ctx->{expand_all} = 1;
92         $expandies{$_}->() for keys %expandies;
93
94     } else {
95         for my $exp (@expand) {
96             $ctx->{"expand_$exp"} = 1;
97             $expandies{$exp}->() if exists $expandies{$exp};
98         }
99     }
100
101     return Apache2::Const::OK;
102 }
103
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 {
107     my $self = shift;
108     my $rec_id = shift;
109     my $ctx = $self->ctx;
110     $ctx->{search_result_index} = -1;
111
112     $self->load_rresults(internal => 1);
113
114     my @search_ids = @{$ctx->{ids}};
115     return unless @search_ids;
116
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;
122             last;
123         }
124     }
125
126     $ctx->{first_search_record} = $search_ids[0];
127     $ctx->{last_search_record} = $search_ids[-1];
128 }
129
130
131 sub mk_copy_query {
132     my $self = shift;
133     my $rec_id = shift;
134     my $org = shift;
135     my $depth = shift;
136     my $copy_limit = shift;
137     my $copy_offset = shift;
138
139     my $query = {
140         select => {
141             acp => ['id', 'barcode', 'circ_lib', 'create_date', 'age_protect', 'holdable'],
142             acpl => [
143                 {column => 'name', alias => 'copy_location'},
144                 {column => 'holdable', alias => 'location_holdable'}
145             ],
146             ccs => [
147                 {column => 'name', alias => 'copy_status'},
148                 {column => 'holdable', alias => 'status_holdable'}
149             ],
150             acn => [
151                 {column => 'label', alias => 'call_number_label'},
152                 {column => 'id', alias => 'call_number'}
153             ],
154             circ => ['due_date'],
155         },
156
157         from => {
158             acp => {
159                 acn => {
160                     join => {bre => {filter => {id => $rec_id }}},
161                     filter => {deleted => 'f'}
162                 },
163                 circ => { # If the copy is circulating, retrieve the open circ
164                     type => 'left',
165                     filter => {checkin_time => undef}
166                 },
167                 acpl => {},
168                 ccs => {},
169                 aou => {}
170             }
171         },
172
173         where => {'+acp' => {deleted => 'f' }},
174
175         order_by => [
176             {class => 'aou', field => 'name'}, 
177             {class => 'acn', field => 'label'}
178         ],
179
180         limit => $copy_limit,
181         offset => $copy_offset
182     };
183
184     # XXX In the future, $sort_org should be understood to be an abstration
185     # that refers to something configurable, not necessariyl physical_loc.
186
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'
191         };
192     }
193
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} = {
197             fkey => 'circ_lib',
198             field => 'id',
199             filter => {
200                 id => {
201                     in => {
202                         select => {aou => [{
203                             column => 'id', 
204                             transform => 'actor.org_unit_descendants', 
205                             result_field => 'id', 
206                             params => [$depth]
207                         }]},
208                         from => 'aou',
209                         where => {id => $org}
210                     }
211                 }
212             }
213         }
214     };
215
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'};
221     }
222
223     return $query;
224 }
225
226 sub mk_marc_html {
227     my($self, $rec_id) = @_;
228
229     # could be optimized considerably by performing the xslt on the already fetched record
230     return $U->simplereq(
231         'open-ils.search', 
232         'open-ils.search.biblio.record.html', $rec_id, 1);
233 }
234
235 sub get_holding_summaries {
236     my ($self, $rec_id, $org, $depth) = @_;
237
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}
242     )->gather(1);
243
244     $serial->kill_me;
245     return $result;
246 }
247
248 sub get_mfhd_summaries {
249     my ($self, $rec_id, $org, $depth) = @_;
250
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
255     )->gather(1);
256
257     $serial->kill_me;
258     return $result;
259 }
260
261 sub get_expanded_holdings {
262     my ($self, $rec_id, $org, $depth) = @_;
263
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");
267
268     my $serial =  create OpenSRF::AppSession("open-ils.serial");
269     my $result = $serial->request(
270         "open-ils.serial.received_siss.retrieve.by_bib.atomic",
271         $rec_id, {
272             "ou" => $org, "depth" => $depth,
273             "limit" => $holding_limit, "offset" => $holding_offset,
274             "type" => $type
275         }
276     )->gather(1);
277
278     $serial->kill_me;
279     return $result;
280 }
281
282 sub any_call_number_label {
283     my ($self) = @_;
284
285     if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
286         return $self->ctx->{copies}->[0]->{call_number_label};
287     } else {
288         return;
289     }
290 }
291
292 sub prepare_browse_call_numbers {
293     my ($self) = @_;
294
295     my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
296         return [];
297
298     my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
299         $self->ctx->{aou_tree}->();
300
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")
305     )->gather(1) || [];
306
307     $supercat->kill_me;
308
309     $self->ctx->{browsed_call_numbers} = [
310         map {
311             $_->record->marc(
312                 (new XML::LibXML)->parse_string($_->record->marc)
313             );
314             $_;
315         } @$results
316     ];
317     $self->ctx->{browsing_ou} = $org_unit;
318 }
319
320 sub get_hold_copy_summary {
321     my ($self, $rec_id, $org) = @_;
322     
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); 
326
327     $self->ctx->{record_hold_count} = $U->simplereq(
328         'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
329
330     $self->ctx->{copy_summary} = $req1->recv->content;
331
332     $search->kill_me;
333 }
334
335 1;