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