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