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