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