]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
3d1d6c819e6367d50dc4a09af340592ceae84fa2
[working/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->_get_search_lib();
18     my $depth = $self->cgi->param('depth') || $ctx->{get_aou}->($org)->ou_type->depth;
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,bmp,mra,acp,acnp,acns}'});
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             acnp => [
156                 {column => 'label', alias => 'call_number_prefix_label'},
157                 {column => 'id', alias => 'call_number_prefix'}
158             ],
159             acns => [
160                 {column => 'label', alias => 'call_number_suffix_label'},
161                 {column => 'id', alias => 'call_number_suffix'}
162             ],
163             bmp => [
164                 {column => 'label', alias => 'part_label'},
165             ]
166         },
167
168         from => {
169             acp => {
170                 acn => { 
171                     join => { 
172                         acnp => { fkey => 'prefix' },
173                         acns => { fkey => 'suffix' }
174                     },
175                     filter => [{deleted => 'f'}, {record => $rec_id}],
176                 },
177                 circ => { # If the copy is circulating, retrieve the open circ
178                     type => 'left',
179                     filter => {checkin_time => undef}
180                 },
181                 acpl => {},
182                 ccs => {},
183                 aou => {},
184                 acpm => {
185                     type => 'left',
186                     join => {
187                         bmp => { type => 'left' }
188                     }
189                 }
190             }
191         },
192
193         where => {
194             '+acp' => {deleted => 'f' }
195         },
196
197         order_by => [
198             {class => 'aou', field => 'name'}, 
199             {class => 'acn', field => 'label'}
200         ],
201
202         limit => $copy_limit,
203         offset => $copy_offset
204     };
205
206     # XXX In the future, $sort_org should be understood to be an abstration
207     # that refers to something configurable, not necessariyl physical_loc.
208
209     if (my $sort_org = $self->ctx->{physical_loc}) {
210         unshift @{$query->{order_by}}, {
211             class => 'acp', field => 'circ_lib', transform => 'numeric_eq',
212             params => [$sort_org], direction => 'desc'
213         };
214     }
215
216     if($org != $self->ctx->{aou_tree}->()->id) { 
217         # no need to add the org join filter if we're not actually filtering
218         $query->{from}->{acp}->{aou} = {
219             fkey => 'circ_lib',
220             field => 'id',
221             filter => {
222                 id => {
223                     in => {
224                         select => {aou => [{
225                             column => 'id', 
226                             transform => 'actor.org_unit_descendants', 
227                             result_field => 'id', 
228                             params => [$depth]
229                         }]},
230                         from => 'aou',
231                         where => {id => $org}
232                     }
233                 }
234             }
235         }
236     };
237
238     # Filter hidden items if this is the public catalog
239     unless($self->ctx->{is_staff}) { 
240         $query->{where}->{'+acp'}->{opac_visible} = 't';
241         $query->{from}->{'acp'}->{'acpl'}->{filter} = {opac_visible => 't'};
242         $query->{from}->{'acp'}->{'ccs'}->{filter} = {opac_visible => 't'};
243     }
244
245     return $query;
246 }
247
248 sub mk_marc_html {
249     my($self, $rec_id) = @_;
250
251     # could be optimized considerably by performing the xslt on the already fetched record
252     return $U->simplereq(
253         'open-ils.search', 
254         'open-ils.search.biblio.record.html', $rec_id, 1);
255 }
256
257 sub get_holding_summaries {
258     my ($self, $rec_id, $org, $depth) = @_;
259
260     my $serial = create OpenSRF::AppSession("open-ils.serial");
261     my $result = $serial->request(
262         "open-ils.serial.bib.summary_statements",
263         $rec_id, {"org_id" => $org, "depth" => $depth}
264     )->gather(1);
265
266     $serial->kill_me;
267     return $result;
268 }
269
270 sub get_mfhd_summaries {
271     my ($self, $rec_id, $org, $depth) = @_;
272
273     my $serial = create OpenSRF::AppSession("open-ils.search");
274     my $result = $serial->request(
275         "open-ils.search.serial.record.bib.retrieve",
276         $rec_id, $org, $depth
277     )->gather(1);
278
279     $serial->kill_me;
280     return $result;
281 }
282
283 sub get_expanded_holdings {
284     my ($self, $rec_id, $org, $depth) = @_;
285
286     my $holding_limit = int($self->cgi->param("holding_limit") || 10);
287     my $holding_offset = int($self->cgi->param("holding_offset") || 0);
288     my $type = $self->cgi->param("expand_holding_type");
289
290     my $serial =  create OpenSRF::AppSession("open-ils.serial");
291     my $result = $serial->request(
292         "open-ils.serial.received_siss.retrieve.by_bib.atomic",
293         $rec_id, {
294             "ou" => $org, "depth" => $depth,
295             "limit" => $holding_limit, "offset" => $holding_offset,
296             "type" => $type
297         }
298     )->gather(1);
299
300     $serial->kill_me;
301     return $result;
302 }
303
304 sub any_call_number_label {
305     my ($self) = @_;
306
307     if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
308         return $self->ctx->{copies}->[0]->{call_number_label};
309     } else {
310         return;
311     }
312 }
313
314 sub prepare_browse_call_numbers {
315     my ($self) = @_;
316
317     my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
318         return [];
319
320     my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
321         $self->ctx->{aou_tree}->();
322
323     my $supercat = create OpenSRF::AppSession("open-ils.supercat");
324     my $results = $supercat->request(
325         "open-ils.supercat.call_number.browse", 
326         $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
327     )->gather(1) || [];
328
329     $supercat->kill_me;
330
331     $self->ctx->{browsed_call_numbers} = [
332         map {
333             $_->record->marc(
334                 (new XML::LibXML)->parse_string($_->record->marc)
335             );
336             $_;
337         } @$results
338     ];
339     $self->ctx->{browsing_ou} = $org_unit;
340 }
341
342 sub get_hold_copy_summary {
343     my ($self, $rec_id, $org) = @_;
344     
345     my $search = OpenSRF::AppSession->create('open-ils.search');
346     my $req1 = $search->request(
347         'open-ils.search.biblio.record.copy_count', $org, $rec_id); 
348
349     $self->ctx->{record_hold_count} = $U->simplereq(
350         'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
351
352     $self->ctx->{copy_summary} = $req1->recv->content;
353
354     $search->kill_me;
355 }
356
357 1;