]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/EGCatLoader.pm
protect against empty search results
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / EGCatLoader.pm
1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use CGI;
4 use XML::LibXML;
5 use Digest::MD5 qw(md5_hex);
6 use Apache2::Const -compile => qw(OK DECLINED HTTP_INTERNAL_SERVER_ERROR REDIRECT);
7 use OpenSRF::AppSession;
8 use OpenSRF::Utils::Logger qw/$logger/;
9 use OpenILS::Application::AppUtils;
10 use OpenILS::Utils::CStoreEditor qw/:funcs/;
11 use OpenILS::Utils::Fieldmapper;
12 my $U = 'OpenILS::Application::AppUtils';
13
14 sub new {
15     my($class, $apache, $ctx) = @_;
16
17     my $self = bless({}, ref($class) || $class);
18
19     $self->apache($apache);
20     $self->ctx($ctx);
21     $self->cgi(CGI->new);
22
23     OpenILS::Utils::CStoreEditor->init; # just in case
24     $self->editor(new_editor());
25
26     return $self;
27 }
28
29
30 # current Apache2::RequestRec;
31 sub apache {
32     my($self, $apache) = @_;
33     $self->{apache} = $apache if $apache;
34     return $self->{apache};
35 }
36
37 # runtime context
38 sub ctx {
39     my($self, $ctx) = @_;
40     $self->{ctx} = $ctx if $ctx;
41     return $self->{ctx};
42 }
43
44 # cstore editor
45 sub editor {
46     my($self, $editor) = @_;
47     $self->{editor} = $editor if $editor;
48     return $self->{editor};
49 }
50
51 # CGI handle
52 sub cgi {
53     my($self, $cgi) = @_;
54     $self->{cgi} = $cgi if $cgi;
55     return $self->{cgi};
56 }
57
58
59 # load common data, then load page data
60 sub load {
61     my $self = shift;
62
63     my $path = $self->apache->path_info;
64     $self->load_helpers;
65
66     my $stat = $self->load_common;
67     return $stat unless $stat == Apache2::Const::OK;
68
69     return $self->load_home if $path =~ /opac\/home/;
70     return $self->load_login if $path =~ /opac\/login/;
71     return $self->load_logout if $path =~ /opac\/logout/;
72     return $self->load_rresults if $path =~ /opac\/results/;
73     return $self->load_rdetail if $path =~ /opac\/rdetail/;
74     return $self->load_myopac if $path =~ /opac\/myopac/;
75     return $self->load_place_hold if $path =~ /opac\/place_hold/;
76
77     return Apache2::Const::OK;
78 }
79
80 # general purpose utility functions added to the environment
81 # context additions: 
82 #   find_org_unit : function(id) => aou object
83 #   org_tree : function(id) => aou object, top of tree, fleshed
84 my $cached_org_tree;
85 my %org_unit_map;
86 sub load_helpers {
87     my $self = shift;
88
89     # pull the org unit from the cached org tree
90     $self->ctx->{find_org_unit} = sub {
91         my $org_id = shift;
92         return undef unless defined $org_id;
93         return $org_unit_map{$org_id} if defined $org_unit_map{$org_id};
94         my $tree = shift || $self->ctx->{org_tree}->();
95         return $org_unit_map{$org_id} = $tree if $tree->id == $org_id;
96         for my $child (@{$tree->children}) {
97             my $node = $self->ctx->{find_org_unit}->($org_id, $child);
98             return $node if $node;
99         }
100         return undef;
101     };
102
103     $self->ctx->{org_tree} = sub {
104         unless($cached_org_tree) {
105             $cached_org_tree = $self->editor->search_actor_org_unit([
106                             {   parent_ou => undef},
107                             {   flesh            => -1,
108                                     flesh_fields    => {aou =>  ['children', 'ou_type']},
109                                     order_by        => {aou => 'name'}
110                             }
111                     ])->[0];
112         }
113         return $cached_org_tree;
114     }
115 }
116
117 # context additions: 
118 #   authtoken : string
119 #   user : au object
120 #   user_status : hash of user circ numbers
121 sub load_common {
122     my $self = shift;
123
124     my $e = $self->editor;
125     my $ctx = $self->ctx;
126
127     if($e->authtoken($self->cgi->cookie('ses'))) {
128
129         if($e->checkauth) {
130
131             $ctx->{authtoken} = $e->authtoken;
132             $ctx->{user} = $e->requestor;
133             $ctx->{user_stats} = $U->simplereq(
134                 'open-ils.actor', 
135                 'open-ils.actor.user.opac.vital_stats', 
136                 $e->authtoken, $e->requestor->id);
137
138         } else {
139
140             return $self->load_logout;
141         }
142     }
143
144     return Apache2::Const::OK;
145 }
146
147 sub load_home {
148     my $self = shift;
149     $self->ctx->{page} = 'home';
150     return Apache2::Const::OK;
151 }
152
153
154 sub load_login {
155     my $self = shift;
156     my $cgi = $self->cgi;
157
158     $self->ctx->{page} = 'login';
159
160     my $username = $cgi->param('username');
161     my $password = $cgi->param('password');
162
163     return Apache2::Const::OK unless $username and $password;
164
165         my $seed = $U->simplereq(
166         'open-ils.auth', 
167                 'open-ils.auth.authenticate.init',
168         $username);
169
170         my $response = $U->simplereq(
171         'open-ils.auth', 
172                 'open-ils.auth.authenticate.complete', 
173                 {       username => $username, 
174                         password => md5_hex($seed . md5_hex($password)), 
175                         type => 'opac' 
176         }
177     );
178
179     # XXX check event, redirect as necessary
180
181     my $home = $self->apache->unparsed_uri;
182     $home =~ s/\/login/\/home/;
183
184     $self->apache->print(
185         $cgi->redirect(
186             -url => $cgi->param('origin') || $home,
187             -cookie => $cgi->cookie(
188                 -name => 'ses',
189                 -path => '/',
190                 -secure => 1,
191                 -value => $response->{payload}->{authtoken},
192                 -expires => CORE::time + $response->{payload}->{authtime}
193             )
194         )
195     );
196
197     return Apache2::Const::REDIRECT;
198 }
199
200 sub load_logout {
201     my $self = shift;
202
203     my $path = $self->apache->uri;
204     $path =~ s/(\/[^\/]+$)/\/home/;
205     my $url = 'http://' . $self->apache->hostname . "$path";
206
207     $self->apache->print(
208         $self->cgi->redirect(
209             -url => $url,
210             -cookie => $self->cgi->cookie(
211                 -name => 'ses',
212                 -path => '/',
213                 -value => '',
214                 -expires => '-1h'
215             )
216         )
217     );
218
219     return Apache2::Const::REDIRECT;
220 }
221
222 # context additions: 
223 #   page_size
224 #   hit_count
225 #   records : list of bre's and copy-count objects
226 my $cmf_cache;
227 my $cmc_cache;
228 sub load_rresults {
229     my $self = shift;
230
231     my $cgi = $self->cgi;
232     my $ctx = $self->ctx;
233     my $e = $self->editor;
234
235     $ctx->{page} = 'rresult';
236
237     unless($cmf_cache) {
238         $cmf_cache = $e->search_config_metabib_field({id => {'!=' => undef}});
239         $cmc_cache = $e->search_config_metabib_class({name => {'!=' => undef}});
240         $ctx->{metabib_field} = $cmf_cache;
241         $ctx->{metabib_class} = $cmc_cache;
242     }
243
244
245     my $page = $cgi->param('page') || 0;
246     my $query = $cgi->param('query');
247     my $limit = $cgi->param('limit') || 10; # XXX user settings
248
249     my $args = {limit => $limit, offset => $page * $limit}; 
250     my $results = $U->simplereq('open-ils.search',
251         'open-ils.search.biblio.multiclass.query.staff', $args, $query, 1);
252
253     my $search = OpenSRF::AppSession->create('open-ils.search');
254     my $facet_req = $search->request('open-ils.search.facet_cache.retrieve', $results->{facet_key}, 10);
255
256     my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
257
258     $ctx->{page_size} = $limit;
259     $ctx->{hit_count} = $results->{count};
260     $ctx->{records} = [];
261     $ctx->{search_facets} = {};
262
263     return Apache2::Const::OK if @$rec_ids == 0;
264     
265     my $cstore1 = OpenSRF::AppSession->create('open-ils.cstore');
266
267     my $bre_req = $cstore1->request(
268         'open-ils.cstore.direct.biblio.record_entry.search', {id => $rec_ids});
269
270     my @data;
271     while(my $resp = $bre_req->recv) {
272         my $bre = $resp->content; 
273
274         my $copy_counts = $e->json_query(
275             {from => ['asset.record_copy_count', 1, $bre->id, 0]})->[0];
276
277         push(@data,
278             {
279                 bre => $bre,
280                 marc_xml => XML::LibXML->new->parse_string($bre->marc),
281                 copy_counts => $copy_counts
282             }
283         );
284     }
285
286     $cstore1->kill_me;
287
288     # shove recs into context in search results order
289     for my $rec_id (@$rec_ids) { 
290         push(
291             @{$ctx->{records}},
292             grep { $_->{bre}->id == $rec_id } @data
293         );
294     }
295
296     my $facets = $facet_req->gather(1);
297
298     for my $cmf_id (keys %$facets) {  # quick-n-dirty
299         my ($cmf) = grep { $_->id eq $cmf_id } @$cmf_cache;
300         $facets->{$cmf->label} = $facets->{$cmf_id};
301         delete $facets->{$cmf_id};
302     }
303     $ctx->{search_facets} = $facets;
304
305     return Apache2::Const::OK;
306 }
307
308 # context additions: 
309 #   record : bre object
310 sub load_rdetail {
311     my $self = shift;
312
313     $self->ctx->{record} = $self->editor->retrieve_biblio_record_entry([
314         $self->cgi->param('record'),
315         {
316             flesh => 2, 
317             flesh_fields => {
318                 bre => ['call_numbers'],
319                 acn => ['copies'] # limit, paging, etc.
320             }
321         }
322     ]);
323
324     $self->ctx->{marc_xml} = XML::LibXML->new->parse_string($self->ctx->{record}->marc);
325
326     return Apache2::Const::OK;
327 }
328
329 # context additions: 
330 #   user : au object, fleshed
331 sub load_myopac {
332     my $self = shift;
333
334     $self->ctx->{user} = $self->editor->retrieve_actor_user([
335         $self->ctx->{user}->id,
336         {
337             flesh => 1,
338             flesh_fields => {
339                 au => ['card']
340                 # ...
341             }
342         }
343     ]);
344
345     return Apache2::Const::OK;
346 }
347
348 # context additions: 
349 sub load_place_hold {
350     my $self = shift;
351     my $ctx = $self->ctx;
352     my $e = $self->editor;
353
354     $ctx->{hold_target} = $self->cgi->param('hold_target');
355     $ctx->{hold_type} = $self->cgi->param('hold_type');
356     $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
357
358     if($ctx->{hold_type} eq 'T') {
359         $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
360     }
361     # ...
362
363     $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
364
365     if(my $pickup_lib = $self->cgi->param('pickup_lib')) {
366
367         my $args = {
368             patronid => $e->requestor->id,
369             titleid => $ctx->{hold_target}, # XXX
370             pickup_lib => $pickup_lib,
371             depth => 0, # XXX
372         };
373
374         my $allowed = $U->simplereq(
375             'open-ils.circ',
376             'open-ils.circ.title_hold.is_possible',
377             $e->authtoken, $args
378         );
379
380         if($allowed->{success} == 1) {
381             my $hold = Fieldmapper::action::hold_request->new;
382
383             $hold->pickup_lib($pickup_lib);
384             $hold->requestor($e->requestor->id);
385             $hold->usr($e->requestor->id); # XXX staff
386             $hold->target($ctx->{hold_target});
387             $hold->hold_type($ctx->{hold_type});
388             # frozen, expired, etc..
389
390             my $stat = $U->simplereq(
391                 'open-ils.circ',
392                 'open-ils.circ.holds.create',
393                 $e->authtoken, $hold
394             );
395
396             if($stat and $stat > 0) {
397                 $ctx->{hold_success} = 1;
398             } else {
399                 $ctx->{hold_failed} = 1; # XXX process the events, etc
400             }
401         }
402
403         # place the hold and deliver results
404     }
405
406     return Apache2::Const::OK;
407 }
408
409 1;