]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
TPac: detail page distinct copy/holdings display depth
[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');
19     $depth = $ctx->{get_aou}->($org)->ou_type->depth 
20         unless defined $depth; # can be 0
21
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;
25
26     my $copy_limit = int($self->cgi->param('copy_limit') || 10);
27     my $copy_offset = int($self->cgi->param('copy_offset') || 0);
28
29     my $rec_id = $ctx->{page_args}->[0]
30         or return Apache2::Const::HTTP_BAD_REQUEST;
31
32     $self->get_staff_search_settings;
33     if ($ctx->{staff_saved_search_size}) {
34         $ctx->{saved_searches} = ($self->staff_load_searches)[1];
35     }
36
37     $self->fetch_related_search_info($rec_id);
38
39     # run copy retrieval in parallel to bib retrieval
40     # XXX unapi
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)
45     );
46
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};
50
51     $ctx->{copies} = $copy_rec->gather(1);
52     $ctx->{copy_limit} = $copy_limit;
53     $ctx->{copy_offset} = $copy_offset;
54
55     $ctx->{have_holdings_to_show} = 0;
56     $ctx->{have_mfhd_to_show} = 0;
57
58     $self->get_hold_copy_summary($rec_id, $org);
59
60     $cstore->kill_me;
61
62     if (
63         $ctx->{get_org_setting}->
64             ($org, "opac.fully_compressed_serial_holdings")
65     ) {
66         $ctx->{holding_summaries} =
67             $self->get_holding_summaries($rec_id, $org, $copy_depth);
68
69         $ctx->{have_holdings_to_show} =
70             scalar(@{$ctx->{holding_summaries}->{basic}}) ||
71             scalar(@{$ctx->{holding_summaries}->{index}}) ||
72             scalar(@{$ctx->{holding_summaries}->{supplement}});
73     } else {
74         $ctx->{mfhd_summaries} =
75             $self->get_mfhd_summaries($rec_id, $org, $copy_depth);
76
77         if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
78         ) {
79             $ctx->{have_mfhd_to_show} = 1;
80         };
81     }
82
83     my %expandies = (
84         marchtml => sub {
85             $ctx->{marchtml} = $self->mk_marc_html($rec_id);
86         },
87         issues => sub {
88             $ctx->{expanded_holdings} =
89                 $self->get_expanded_holdings($rec_id, $org, $copy_depth)
90                 if $ctx->{have_holdings_to_show};
91         },
92         cnbrowse => sub {
93             $self->prepare_browse_call_numbers();
94         }
95     );
96
97     my @expand = $self->cgi->param('expand');
98     if (grep {$_ eq 'all'} @expand) {
99         $ctx->{expand_all} = 1;
100         $expandies{$_}->() for keys %expandies;
101
102     } else {
103         for my $exp (@expand) {
104             $ctx->{"expand_$exp"} = 1;
105             $expandies{$exp}->() if exists $expandies{$exp};
106         }
107     }
108
109     return Apache2::Const::OK;
110 }
111
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 {
115     my $self = shift;
116     my $rec_id = shift;
117     my $ctx = $self->ctx;
118     $ctx->{search_result_index} = -1;
119
120     $self->load_rresults(internal => 1);
121
122     my @search_ids = @{$ctx->{ids}};
123     return unless @search_ids;
124
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;
130             last;
131         }
132     }
133
134     $ctx->{first_search_record} = $search_ids[0];
135     $ctx->{last_search_record} = $search_ids[-1];
136 }
137
138
139 sub mk_copy_query {
140     my $self = shift;
141     my $rec_id = shift;
142     my $org = shift;
143     my $depth = shift;
144     my $copy_limit = shift;
145     my $copy_offset = shift;
146
147     my $query = {
148         select => {
149             acp => ['id', 'barcode', 'circ_lib', 'create_date', 'age_protect', 'holdable'],
150             acpl => [
151                 {column => 'name', alias => 'copy_location'},
152                 {column => 'holdable', alias => 'location_holdable'}
153             ],
154             ccs => [
155                 {column => 'name', alias => 'copy_status'},
156                 {column => 'holdable', alias => 'status_holdable'}
157             ],
158             acn => [
159                 {column => 'label', alias => 'call_number_label'},
160                 {column => 'id', alias => 'call_number'}
161             ],
162             circ => ['due_date'],
163             acnp => [
164                 {column => 'label', alias => 'call_number_prefix_label'},
165                 {column => 'id', alias => 'call_number_prefix'}
166             ],
167             acns => [
168                 {column => 'label', alias => 'call_number_suffix_label'},
169                 {column => 'id', alias => 'call_number_suffix'}
170             ],
171             bmp => [
172                 {column => 'label', alias => 'part_label'},
173             ]
174         },
175
176         from => {
177             acp => {
178                 acn => { 
179                     join => { 
180                         acnp => { fkey => 'prefix' },
181                         acns => { fkey => 'suffix' }
182                     },
183                     filter => [{deleted => 'f'}, {record => $rec_id}],
184                 },
185                 circ => { # If the copy is circulating, retrieve the open circ
186                     type => 'left',
187                     filter => {checkin_time => undef}
188                 },
189                 acpl => {},
190                 ccs => {},
191                 aou => {},
192                 acpm => {
193                     type => 'left',
194                     join => {
195                         bmp => { type => 'left' }
196                     }
197                 }
198             }
199         },
200
201         where => {
202             '+acp' => {deleted => 'f' }
203         },
204
205         order_by => [
206             {class => 'aou', field => 'name'}, 
207             {class => 'acn', field => 'label'}
208         ],
209
210         limit => $copy_limit,
211         offset => $copy_offset
212     };
213
214     # XXX In the future, $sort_org should be understood to be an abstration
215     # that refers to something configurable, not necessariyl physical_loc.
216
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'
221         };
222     }
223
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} = {
227             fkey => 'circ_lib',
228             field => 'id',
229             filter => {
230                 id => {
231                     in => {
232                         select => {aou => [{
233                             column => 'id', 
234                             transform => 'actor.org_unit_descendants', 
235                             result_field => 'id', 
236                             params => [$depth]
237                         }]},
238                         from => 'aou',
239                         where => {id => $org}
240                     }
241                 }
242             }
243         }
244     };
245
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     }
252
253     return $query;
254 }
255
256 sub mk_marc_html {
257     my($self, $rec_id) = @_;
258
259     # could be optimized considerably by performing the xslt on the already fetched record
260     return $U->simplereq(
261         'open-ils.search', 
262         'open-ils.search.biblio.record.html', $rec_id, 1);
263 }
264
265 sub get_holding_summaries {
266     my ($self, $rec_id, $org, $depth) = @_;
267
268     my $serial = create OpenSRF::AppSession("open-ils.serial");
269     my $result = $serial->request(
270         "open-ils.serial.bib.summary_statements",
271         $rec_id, {"org_id" => $org, "depth" => $depth}
272     )->gather(1);
273
274     $serial->kill_me;
275     return $result;
276 }
277
278 sub get_mfhd_summaries {
279     my ($self, $rec_id, $org, $depth) = @_;
280
281     my $serial = create OpenSRF::AppSession("open-ils.search");
282     my $result = $serial->request(
283         "open-ils.search.serial.record.bib.retrieve",
284         $rec_id, $org, $depth
285     )->gather(1);
286
287     $serial->kill_me;
288     return $result;
289 }
290
291 sub get_expanded_holdings {
292     my ($self, $rec_id, $org, $depth) = @_;
293
294     my $holding_limit = int($self->cgi->param("holding_limit") || 10);
295     my $holding_offset = int($self->cgi->param("holding_offset") || 0);
296     my $type = $self->cgi->param("expand_holding_type");
297
298     my $serial =  create OpenSRF::AppSession("open-ils.serial");
299     my $result = $serial->request(
300         "open-ils.serial.received_siss.retrieve.by_bib.atomic",
301         $rec_id, {
302             "ou" => $org, "depth" => $depth,
303             "limit" => $holding_limit, "offset" => $holding_offset,
304             "type" => $type
305         }
306     )->gather(1);
307
308     $serial->kill_me;
309     return $result;
310 }
311
312 sub any_call_number_label {
313     my ($self) = @_;
314
315     if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
316         return $self->ctx->{copies}->[0]->{call_number_label};
317     } else {
318         return;
319     }
320 }
321
322 sub prepare_browse_call_numbers {
323     my ($self) = @_;
324
325     my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
326         return [];
327
328     my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
329         $self->ctx->{aou_tree}->();
330
331     my $supercat = create OpenSRF::AppSession("open-ils.supercat");
332     my $results = $supercat->request(
333         "open-ils.supercat.call_number.browse", 
334         $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
335     )->gather(1) || [];
336
337     $supercat->kill_me;
338
339     $self->ctx->{browsed_call_numbers} = [
340         map {
341             $_->record->marc(
342                 (new XML::LibXML)->parse_string($_->record->marc)
343             );
344             $_;
345         } @$results
346     ];
347     $self->ctx->{browsing_ou} = $org_unit;
348 }
349
350 sub get_hold_copy_summary {
351     my ($self, $rec_id, $org) = @_;
352     
353     my $search = OpenSRF::AppSession->create('open-ils.search');
354     my $req1 = $search->request(
355         'open-ils.search.biblio.record.copy_count', $org, $rec_id); 
356
357     $self->ctx->{record_hold_count} = $U->simplereq(
358         'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
359
360     $self->ctx->{copy_summary} = $req1->recv->content;
361
362     $search->kill_me;
363 }
364
365 1;