]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
TPAC: Give MFHD summaries OU / depth awareness
[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     # run copy retrieval in parallel to bib retrieval
31     # XXX unapi
32     my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
33     my $copy_rec = $cstore->request(
34         'open-ils.cstore.json_query.atomic', 
35         $self->mk_copy_query($rec_id, $org, $depth, $copy_limit, $copy_offset)
36     );
37
38     my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {flesh => '{holdings_xml,mra,acp}'});
39     $ctx->{bre_id} = $rec_data[0]->{id};
40     $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
41
42     $ctx->{copies} = $copy_rec->gather(1);
43     $ctx->{copy_limit} = $copy_limit;
44     $ctx->{copy_offset} = $copy_offset;
45
46     $ctx->{have_holdings_to_show} = 0;
47     $ctx->{have_mfhd_to_show} = 0;
48     $self->get_hold_copy_summary($rec_id, $org);
49
50     $cstore->kill_me;
51
52     if (
53         $ctx->{get_org_setting}->
54             ($org, "opac.fully_compressed_serial_holdings")
55     ) {
56         $ctx->{holding_summaries} =
57             $self->get_holding_summaries($rec_id, $org, $depth);
58
59         $ctx->{have_holdings_to_show} =
60             scalar(@{$ctx->{holding_summaries}->{basic}}) ||
61             scalar(@{$ctx->{holding_summaries}->{index}}) ||
62             scalar(@{$ctx->{holding_summaries}->{supplement}});
63     } else {
64         $ctx->{mfhd_summaries} =
65             $self->get_mfhd_summaries($rec_id, $org, $depth);
66
67         if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
68         ) {
69             $ctx->{have_mfhd_to_show} = 1;
70         };
71     }
72
73     my %expandies = (
74         marchtml => sub {
75             $ctx->{marchtml} = $self->mk_marc_html($rec_id);
76         },
77         issues => sub {
78             $ctx->{expanded_holdings} =
79                 $self->get_expanded_holdings($rec_id, $org, $depth)
80                 if $ctx->{have_holdings_to_show};
81         },
82         cnbrowse => sub {
83             $self->prepare_browse_call_numbers();
84         }
85     );
86
87     my @expand = $self->cgi->param('expand');
88     if (grep {$_ eq 'all'} @expand) {
89         $ctx->{expand_all} = 1;
90         $expandies{$_}->() for keys %expandies;
91
92     } else {
93         for my $exp (@expand) {
94             $ctx->{"expand_$exp"} = 1;
95             $expandies{$exp}->() if exists $expandies{$exp};
96         }
97     }
98
99     return Apache2::Const::OK;
100 }
101
102 sub mk_copy_query {
103     my $self = shift;
104     my $rec_id = shift;
105     my $org = shift;
106     my $depth = shift;
107     my $copy_limit = shift;
108     my $copy_offset = shift;
109
110     my $query = {
111         select => {
112             acp => ['id', 'barcode', 'circ_lib', 'create_date', 'age_protect', 'holdable'],
113             acpl => [
114                 {column => 'name', alias => 'copy_location'},
115                 {column => 'holdable', alias => 'location_holdable'}
116             ],
117             ccs => [
118                 {column => 'name', alias => 'copy_status'},
119                 {column => 'holdable', alias => 'status_holdable'}
120             ],
121             acn => [
122                 {column => 'label', alias => 'call_number_label'},
123                 {column => 'id', alias => 'call_number'}
124             ],
125             circ => ['due_date'],
126         },
127
128         from => {
129             acp => {
130                 acn => {
131                     join => {bre => {filter => {id => $rec_id }}},
132                     filter => {deleted => 'f'}
133                 },
134                 circ => { # If the copy is circulating, retrieve the open circ
135                     type => 'left',
136                     filter => {checkin_time => undef}
137                 },
138                 acpl => {},
139                 ccs => {},
140                 aou => {}
141             }
142         },
143
144         where => {'+acp' => {deleted => 'f' }},
145
146         order_by => [
147             {class => 'aou', field => 'name'}, 
148             {class => 'acn', field => 'label'}
149         ],
150
151         limit => $copy_limit,
152         offset => $copy_offset
153     };
154
155     # XXX In the future, $sort_org should be understood to be an abstration
156     # that refers to something configurable, not necessariyl orig_loc.
157
158     if (my $sort_org = $self->ctx->{orig_loc}) {
159         unshift @{$query->{order_by}}, {
160             class => 'acp', field => 'circ_lib', transform => 'numeric_eq',
161             params => [$sort_org], direction => 'desc'
162         };
163     }
164
165     if($org != $self->ctx->{aou_tree}->()->id) { 
166         # no need to add the org join filter if we're not actually filtering
167         $query->{from}->{acp}->{aou} = {
168             fkey => 'circ_lib',
169             field => 'id',
170             filter => {
171                 id => {
172                     in => {
173                         select => {aou => [{
174                             column => 'id', 
175                             transform => 'actor.org_unit_descendants', 
176                             result_field => 'id', 
177                             params => [$depth]
178                         }]},
179                         from => 'aou',
180                         where => {id => $org}
181                     }
182                 }
183             }
184         }
185     };
186
187     # Filter hidden items if this is the public catalog
188     unless($self->ctx->{is_staff}) { 
189         $query->{where}->{'+acp'}->{opac_visible} = 't';
190         $query->{from}->{'acp'}->{'acpl'}->{filter} = {opac_visible => 't'};
191         $query->{from}->{'acp'}->{'ccs'}->{filter} = {opac_visible => 't'};
192     }
193
194     return $query;
195 }
196
197 sub mk_marc_html {
198     my($self, $rec_id) = @_;
199
200     # could be optimized considerably by performing the xslt on the already fetched record
201     return $U->simplereq(
202         'open-ils.search', 
203         'open-ils.search.biblio.record.html', $rec_id, 1);
204 }
205
206 sub get_holding_summaries {
207     my ($self, $rec_id, $org, $depth) = @_;
208
209     my $serial = create OpenSRF::AppSession("open-ils.serial");
210     my $result = $serial->request(
211         "open-ils.serial.bib.summary_statements",
212         $rec_id, {"org_id" => $org, "depth" => $depth}
213     )->gather(1);
214
215     $serial->kill_me;
216     return $result;
217 }
218
219 sub get_mfhd_summaries {
220     my ($self, $rec_id, $org, $depth) = @_;
221
222     my $serial = create OpenSRF::AppSession("open-ils.search");
223     my $result = $serial->request(
224         "open-ils.search.serial.record.bib.retrieve",
225         $rec_id, $org, $depth
226     )->gather(1);
227
228     $serial->kill_me;
229     return $result;
230 }
231
232 sub get_expanded_holdings {
233     my ($self, $rec_id, $org, $depth) = @_;
234
235     my $holding_limit = int($self->cgi->param("holding_limit") || 10);
236     my $holding_offset = int($self->cgi->param("holding_offset") || 0);
237     my $type = $self->cgi->param("expand_holding_type");
238
239     my $serial =  create OpenSRF::AppSession("open-ils.serial");
240     my $result = $serial->request(
241         "open-ils.serial.received_siss.retrieve.by_bib.atomic",
242         $rec_id, {
243             "ou" => $org, "depth" => $depth,
244             "limit" => $holding_limit, "offset" => $holding_offset,
245             "type" => $type
246         }
247     )->gather(1);
248
249     $serial->kill_me;
250     return $result;
251 }
252
253 sub any_call_number_label {
254     my ($self) = @_;
255
256     if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
257         return $self->ctx->{copies}->[0]->{call_number_label};
258     } else {
259         return;
260     }
261 }
262
263 sub prepare_browse_call_numbers {
264     my ($self) = @_;
265
266     my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
267         return [];
268
269     my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
270         $self->ctx->{aou_tree}->();
271
272     my $supercat = create OpenSRF::AppSession("open-ils.supercat");
273     my $results = $supercat->request(
274         "open-ils.supercat.call_number.browse", 
275         $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
276     )->gather(1) || [];
277
278     $supercat->kill_me;
279
280     $self->ctx->{browsed_call_numbers} = [
281         map {
282             $_->record->marc(
283                 (new XML::LibXML)->parse_string($_->record->marc)
284             );
285             $_;
286         } @$results
287     ];
288     $self->ctx->{browsing_ou} = $org_unit;
289 }
290
291 sub get_hold_copy_summary {
292     my ($self, $rec_id, $org) = @_;
293     
294     my $search = OpenSRF::AppSession->create('open-ils.search');
295     my $req1 = $search->request(
296         'open-ils.search.biblio.record.copy_count', $org, $rec_id); 
297
298     $self->ctx->{record_hold_count} = $U->simplereq(
299         'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
300
301     $self->ctx->{copy_summary} = $req1->recv->content;
302
303     $search->kill_me;
304 }
305
306 1;