]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
TPAC: Prevent OU-invisible copies from being displayed to public
[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         $query->{from}->{'acp'}->{'aou'}->{filter} = {opac_visible => 't'};
252     }
253
254     return $query;
255 }
256
257 sub mk_marc_html {
258     my($self, $rec_id) = @_;
259
260     # could be optimized considerably by performing the xslt on the already fetched record
261     return $U->simplereq(
262         'open-ils.search', 
263         'open-ils.search.biblio.record.html', $rec_id, 1);
264 }
265
266 sub get_holding_summaries {
267     my ($self, $rec_id, $org, $depth) = @_;
268
269     my $serial = create OpenSRF::AppSession("open-ils.serial");
270     my $result = $serial->request(
271         "open-ils.serial.bib.summary_statements",
272         $rec_id, {"org_id" => $org, "depth" => $depth}
273     )->gather(1);
274
275     $serial->kill_me;
276     return $result;
277 }
278
279 sub get_mfhd_summaries {
280     my ($self, $rec_id, $org, $depth) = @_;
281
282     my $serial = create OpenSRF::AppSession("open-ils.search");
283     my $result = $serial->request(
284         "open-ils.search.serial.record.bib.retrieve",
285         $rec_id, $org, $depth
286     )->gather(1);
287
288     $serial->kill_me;
289     return $result;
290 }
291
292 sub get_expanded_holdings {
293     my ($self, $rec_id, $org, $depth) = @_;
294
295     my $holding_limit = int($self->cgi->param("holding_limit") || 10);
296     my $holding_offset = int($self->cgi->param("holding_offset") || 0);
297     my $type = $self->cgi->param("expand_holding_type");
298
299     my $serial =  create OpenSRF::AppSession("open-ils.serial");
300     my $result = $serial->request(
301         "open-ils.serial.received_siss.retrieve.by_bib.atomic",
302         $rec_id, {
303             "ou" => $org, "depth" => $depth,
304             "limit" => $holding_limit, "offset" => $holding_offset,
305             "type" => $type
306         }
307     )->gather(1);
308
309     $serial->kill_me;
310     return $result;
311 }
312
313 sub any_call_number_label {
314     my ($self) = @_;
315
316     if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
317         return $self->ctx->{copies}->[0]->{call_number_label};
318     } else {
319         return;
320     }
321 }
322
323 sub prepare_browse_call_numbers {
324     my ($self) = @_;
325
326     my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
327         return [];
328
329     my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
330         $self->ctx->{aou_tree}->();
331
332     my $supercat = create OpenSRF::AppSession("open-ils.supercat");
333     my $results = $supercat->request(
334         "open-ils.supercat.call_number.browse", 
335         $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
336     )->gather(1) || [];
337
338     $supercat->kill_me;
339
340     $self->ctx->{browsed_call_numbers} = [
341         map {
342             $_->record->marc(
343                 (new XML::LibXML)->parse_string($_->record->marc)
344             );
345             $_;
346         } @$results
347     ];
348     $self->ctx->{browsing_ou} = $org_unit;
349 }
350
351 sub get_hold_copy_summary {
352     my ($self, $rec_id, $org) = @_;
353     
354     my $search = OpenSRF::AppSession->create('open-ils.search');
355     my $req1 = $search->request(
356         'open-ils.search.biblio.record.copy_count', $org, $rec_id); 
357
358     $self->ctx->{record_hold_count} = $U->simplereq(
359         'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
360
361     $self->ctx->{copy_summary} = $req1->recv->content;
362
363     $search->kill_me;
364 }
365
366 1;