]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
TPAC: Show deleted record warning for deleted records
[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 $org_name = $ctx->{get_aou}->($org)->shortname;
19     my $pref_ou = $self->_get_pref_lib();
20     my $depth = $self->cgi->param('depth');
21     $depth = $ctx->{get_aou}->($org)->ou_type->depth 
22         unless defined $depth; # can be 0
23
24     my $copy_depth = $self->cgi->param('copy_depth');
25     $copy_depth = $depth unless defined $copy_depth; # can be 0
26     $self->ctx->{copy_depth} = $copy_depth;
27
28     my $copy_limit = int($self->cgi->param('copy_limit') || 10);
29     my $copy_offset = int($self->cgi->param('copy_offset') || 0);
30
31     my $rec_id = $ctx->{page_args}->[0]
32         or return Apache2::Const::HTTP_BAD_REQUEST;
33
34     $self->get_staff_search_settings;
35     if ($ctx->{staff_saved_search_size}) {
36         $ctx->{saved_searches} = ($self->staff_load_searches)[1];
37     }
38
39     $self->fetch_related_search_info($rec_id);
40
41     # run copy retrieval in parallel to bib retrieval
42     # XXX unapi
43     my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
44     my $copy_rec = $cstore->request(
45         'open-ils.cstore.json_query.atomic', 
46         $self->mk_copy_query($rec_id, $org, $copy_depth, $copy_limit, $copy_offset, $pref_ou)
47     );
48
49     my (undef, @rec_data) = $self->get_records_and_facets([$rec_id], undef, {
50         flesh => '{holdings_xml,bmp,mra,acp,acnp,acns}',
51         site => $org_name,
52         depth => $depth,
53         pref_lib => $pref_ou
54     });
55     $ctx->{bre_id} = $rec_data[0]->{id};
56     $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
57
58     $ctx->{copies} = $copy_rec->gather(1);
59     $ctx->{copy_limit} = $copy_limit;
60     $ctx->{copy_offset} = $copy_offset;
61
62     $ctx->{have_holdings_to_show} = 0;
63     $ctx->{have_mfhd_to_show} = 0;
64
65     $self->get_hold_copy_summary($rec_id, $org);
66
67     $self->ctx->{bib_is_dead} = OpenILS::Application::AppUtils->is_true(
68         OpenILS::Utils::CStoreEditor->new->json_query({
69             select => { bre => [ 'deleted' ] },
70             from => 'bre',
71             where => { 'id' => $rec_id }
72         })->[0]->{deleted}
73     );
74
75     $cstore->kill_me;
76
77     if (
78         $ctx->{get_org_setting}->
79             ($org, "opac.fully_compressed_serial_holdings")
80     ) {
81         # We're loading this data here? Are we therefore assuming that we
82         # *are* going to display something in the "issues" expandy?
83         $self->load_serial_holding_summaries($rec_id, $org, $copy_depth);
84     } else {
85         $ctx->{mfhd_summaries} =
86             $self->get_mfhd_summaries($rec_id, $org, $copy_depth);
87
88         if ($ctx->{mfhd_summaries} && scalar(@{$ctx->{mfhd_summaries}})
89         ) {
90             $ctx->{have_mfhd_to_show} = 1;
91         };
92     }
93
94     my %expandies = (
95         marchtml => sub {
96             $ctx->{marchtml} = $self->mk_marc_html($rec_id);
97         },
98         issues => sub {
99             return;
100             # XXX this needed?
101         },
102         cnbrowse => sub {
103             $self->prepare_browse_call_numbers();
104         }
105     );
106
107     my @expand = $self->cgi->param('expand');
108     if (grep {$_ eq 'all'} @expand) {
109         $ctx->{expand_all} = 1;
110         $expandies{$_}->() for keys %expandies;
111
112     } else {
113         for my $exp (@expand) {
114             $ctx->{"expand_$exp"} = 1;
115             $expandies{$exp}->() if exists $expandies{$exp};
116         }
117     }
118
119     return Apache2::Const::OK;
120 }
121
122 # collect IDs and info on the search that lead to this details page
123 # If no search query, etc is present, we leave ctx.search_result_index == -1
124 sub fetch_related_search_info {
125     my $self = shift;
126     my $rec_id = shift;
127     my $ctx = $self->ctx;
128     $ctx->{search_result_index} = -1;
129
130     $self->load_rresults(internal => 1);
131
132     my @search_ids = @{$ctx->{ids}};
133     return unless @search_ids;
134
135     for my $idx (0..$#search_ids) {
136         if ($search_ids[$idx] == $rec_id) {
137             $ctx->{prev_search_record} = $search_ids[$idx - 1] if $idx > 0;
138             $ctx->{next_search_record} = $search_ids[$idx + 1];
139             $ctx->{search_result_index} = $idx;
140             last;
141         }
142     }
143
144     $ctx->{first_search_record} = $search_ids[0];
145     $ctx->{last_search_record} = $search_ids[-1];
146 }
147
148
149 sub mk_copy_query {
150     my $self = shift;
151     my $rec_id = shift;
152     my $org = shift;
153     my $depth = shift;
154     my $copy_limit = shift;
155     my $copy_offset = shift;
156     my $pref_ou = shift;
157
158     my $query = $U->basic_opac_copy_query(
159         $rec_id, undef, undef, $copy_limit, $copy_offset, $self->ctx->{is_staff}
160     );
161
162     if($org != $self->ctx->{aou_tree}->()->id) { 
163         # no need to add the org join filter if we're not actually filtering
164         $query->{from}->{acp}->{aou} = {
165             fkey => 'circ_lib',
166             field => 'id',
167             filter => {
168                 id => {
169                     in => {
170                         select => {aou => [{
171                             column => 'id', 
172                             transform => 'actor.org_unit_descendants',
173                             result_field => 'id', 
174                             params => [$depth]
175                         }]},
176                         from => 'aou',
177                         where => {id => $org}
178                     }
179                 }
180             }
181         };
182     };
183
184     # Unsure if we want these in the shared function, leaving here for now
185     unshift(@{$query->{order_by}},
186         { class => "aou", field => 'id',
187           transform => 'evergreen.rank_ou', params => [$org, $pref_ou]
188         }
189     );
190     push(@{$query->{order_by}},
191         { class => "acp", field => 'status',
192           transform => 'evergreen.rank_cp_status'
193         }
194     );
195
196     return $query;
197 }
198
199 sub mk_marc_html {
200     my($self, $rec_id) = @_;
201
202     # could be optimized considerably by performing the xslt on the already fetched record
203     return $U->simplereq(
204         'open-ils.search', 
205         'open-ils.search.biblio.record.html', $rec_id, 1);
206 }
207
208 sub load_serial_holding_summaries {
209     my ($self, $rec_id, $org, $depth) = @_;
210
211     my $limit = $self->cgi->param("slimit") || 10;
212     my $offset = $self->cgi->param("soffset") || 0;
213
214     my $serial = create OpenSRF::AppSession("open-ils.serial");
215
216     # First, get the tree of /summaries/ of holdings.
217     my $tree = $serial->request(
218         "open-ils.serial.holding_summary_tree.by_bib",
219         $rec_id, $org, $depth, $limit, $offset
220     )->gather(1);
221
222     return if $self->apache_log_if_event(
223         $tree, "getting holding summary tree for record $rec_id"
224     );
225
226     # Next, if requested, get a list of individual holdings under a
227     # particular summary.
228     my $holdings;
229     my $summary_id = int($self->cgi->param("sid") || 0);
230     my $summary_type = $self->cgi->param("stype");
231
232     if ($summary_id and $summary_type) {
233         my $expand_path = [ $self->cgi->param("sepath") ],
234         my $expand_limit = $self->cgi->param("selimit");
235         my $expand_offsets = [ $self->cgi->param("seoffset") ];
236         my $auto_expand_first = 0;
237
238         if (not @$expand_offsets) {
239             $expand_offsets = undef;
240             $auto_expand_first = 1;
241         }
242
243         $holdings = $serial->request(
244             "open-ils.serial.holdings.grouped_by_summary",
245             $summary_type, $summary_id,
246             $expand_path, $expand_limit, $expand_offsets,
247             $auto_expand_first,
248             1 + ($self->ctx->{is_staff} ? 1 : 0)
249         )->gather(1);
250
251         if ($holdings and ref $holdings eq "ARRAY") {
252             $self->place_holdings_with_summary(
253                     $tree, $holdings, $summary_id, $summary_type
254             ) or $self->apache->log->warn(
255                 "could not place holdings within summary tree"
256             );
257         } else {
258             $self->apache_log_if_event(
259                 $holdings, "getting holdings grouped by summary $summary_id"
260             );
261         }
262     }
263
264     $serial->kill_me;
265
266     # The presence of any keys in the tree hash other than 'more' means that we
267     # must have /something/ we could show.
268     $self->ctx->{have_holdings_to_show} = grep { $_ ne 'more' } (keys %$tree);
269
270     $self->ctx->{holding_summary_tree} = $tree;
271 }
272
273 # This helper to load_serial_holding_summaries() recursively searches in
274 # $tree for a holding summary matching $sid and $stype, and places $holdings
275 # within the node for that summary. IOW, this is about showing expanded
276 # holdings under their "parent" summary.
277 sub place_holdings_with_summary {
278     my ($self, $tree, $holdings, $sid, $stype) = @_;
279
280     foreach my $sum (@{$tree->{holding_summaries}}) {
281         if ($sum->{id} == $sid and $sum->{summary_type} eq $stype) {
282             $sum->{holdings} = $holdings;
283             return 1;
284         }
285     }
286
287     foreach my $child (@{$tree->{children}}) {
288         return 1 if $self->place_holdings_with_summary(
289             $child, $holdings, $sid, $stype
290         );
291     }
292
293     return;
294 }
295
296 sub get_mfhd_summaries {
297     my ($self, $rec_id, $org, $depth) = @_;
298
299     my $serial = create OpenSRF::AppSession("open-ils.search");
300     my $result = $serial->request(
301         "open-ils.search.serial.record.bib.retrieve",
302         $rec_id, $org, $depth
303     )->gather(1);
304
305     $serial->kill_me;
306     return $result;
307 }
308
309 sub any_call_number_label {
310     my ($self) = @_;
311
312     if ($self->ctx->{copies} and @{$self->ctx->{copies}}) {
313         return $self->ctx->{copies}->[0]->{call_number_label};
314     } else {
315         return;
316     }
317 }
318
319 sub prepare_browse_call_numbers {
320     my ($self) = @_;
321
322     my $cn = ($self->cgi->param("cn") || $self->any_call_number_label) or
323         return [];
324
325     my $org_unit = $self->ctx->{get_aou}->($self->cgi->param('loc')) ||
326         $self->ctx->{aou_tree}->();
327
328     my $supercat = create OpenSRF::AppSession("open-ils.supercat");
329     my $results = $supercat->request(
330         "open-ils.supercat.call_number.browse", 
331         $cn, $org_unit->shortname, 9, $self->cgi->param("cnoffset")
332     )->gather(1) || [];
333
334     $supercat->kill_me;
335
336     $self->ctx->{browsed_call_numbers} = [
337         map {
338             $_->record->marc(
339                 (new XML::LibXML)->parse_string($_->record->marc)
340             );
341             $_;
342         } @$results
343     ];
344     $self->ctx->{browsing_ou} = $org_unit;
345 }
346
347 sub get_hold_copy_summary {
348     my ($self, $rec_id, $org) = @_;
349     
350     my $search = OpenSRF::AppSession->create('open-ils.search');
351     my $req1 = $search->request(
352         'open-ils.search.biblio.record.copy_count', $org, $rec_id); 
353
354     $self->ctx->{record_hold_count} = $U->simplereq(
355         'open-ils.circ', 'open-ils.circ.bre.holds.count', $rec_id);
356
357     $self->ctx->{copy_summary} = $req1->recv->content;
358
359     $search->kill_me;
360 }
361
362 sub load_print_record {
363     my $self = shift;
364
365     my $rec_id = $self->ctx->{page_args}->[0] 
366         or return Apache2::Const::HTTP_BAD_REQUEST;
367
368     $self->{ctx}->{bre_id} = $rec_id;
369     $self->{ctx}->{printable_record} = $U->simplereq(
370         'open-ils.search',
371         'open-ils.search.biblio.record.print', $rec_id);
372
373     return Apache2::Const::OK;
374 }
375
376 sub load_email_record {
377     my $self = shift;
378
379     my $rec_id = $self->ctx->{page_args}->[0] 
380         or return Apache2::Const::HTTP_BAD_REQUEST;
381
382     $self->{ctx}->{bre_id} = $rec_id;
383     $U->simplereq(
384         'open-ils.search',
385         'open-ils.search.biblio.record.email', 
386         $self->ctx->{authtoken}, $rec_id);
387
388     return Apache2::Const::OK;
389 }
390
391 1;