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