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