]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/EGCatLoader.pm
make better use of media_prefix; only apply ses cookie in secure context; updated...
[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     
261     my $cstore1 = OpenSRF::AppSession->create('open-ils.cstore');
262
263     my $bre_req = $cstore1->request(
264         'open-ils.cstore.direct.biblio.record_entry.search', {id => $rec_ids});
265
266     my @data;
267     while(my $resp = $bre_req->recv) {
268         my $bre = $resp->content; 
269
270         my $copy_counts = $e->json_query(
271             {from => ['asset.record_copy_count', 1, $bre->id, 0]})->[0];
272
273         push(@data,
274             {
275                 bre => $bre,
276                 marc_xml => XML::LibXML->new->parse_string($bre->marc),
277                 copy_counts => $copy_counts
278             }
279         );
280     }
281
282     $cstore1->kill_me;
283
284     # shove recs into context in search results order
285     $ctx->{records} = [];
286     for my $rec_id (@$rec_ids) { 
287         push(
288             @{$ctx->{records}},
289             grep { $_->{bre}->id == $rec_id } @data
290         );
291     }
292
293     my $facets = $facet_req->gather(1);
294
295     for my $cmf_id (keys %$facets) {  # quick-n-dirty
296         my ($cmf) = grep { $_->id eq $cmf_id } @$cmf_cache;
297         $facets->{$cmf->label} = $facets->{$cmf_id};
298         delete $facets->{$cmf_id};
299     }
300     $ctx->{search_facets} = $facets;
301
302     return Apache2::Const::OK;
303 }
304
305 # context additions: 
306 #   record : bre object
307 sub load_rdetail {
308     my $self = shift;
309
310     $self->ctx->{record} = $self->editor->retrieve_biblio_record_entry([
311         $self->cgi->param('record'),
312         {
313             flesh => 2, 
314             flesh_fields => {
315                 bre => ['call_numbers'],
316                 acn => ['copies'] # limit, paging, etc.
317             }
318         }
319     ]);
320
321     $self->ctx->{marc_xml} = XML::LibXML->new->parse_string($self->ctx->{record}->marc);
322
323     return Apache2::Const::OK;
324 }
325
326 # context additions: 
327 #   user : au object, fleshed
328 sub load_myopac {
329     my $self = shift;
330
331     $self->ctx->{user} = $self->editor->retrieve_actor_user([
332         $self->ctx->{user}->id,
333         {
334             flesh => 1,
335             flesh_fields => {
336                 au => ['card']
337                 # ...
338             }
339         }
340     ]);
341
342     return Apache2::Const::OK;
343 }
344
345 # context additions: 
346 sub load_place_hold {
347     my $self = shift;
348     my $ctx = $self->ctx;
349     my $e = $self->editor;
350
351     $ctx->{hold_target} = $self->cgi->param('hold_target');
352     $ctx->{hold_type} = $self->cgi->param('hold_type');
353     $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
354
355     if($ctx->{hold_type} eq 'T') {
356         $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
357     }
358     # ...
359
360     $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
361
362     if(my $pickup_lib = $self->cgi->param('pickup_lib')) {
363
364         my $args = {
365             patronid => $e->requestor->id,
366             titleid => $ctx->{hold_target}, # XXX
367             pickup_lib => $pickup_lib,
368             depth => 0, # XXX
369         };
370
371         my $allowed = $U->simplereq(
372             'open-ils.circ',
373             'open-ils.circ.title_hold.is_possible',
374             $e->authtoken, $args
375         );
376
377         if($allowed->{success} == 1) {
378             my $hold = Fieldmapper::action::hold_request->new;
379
380             $hold->pickup_lib($pickup_lib);
381             $hold->requestor($e->requestor->id);
382             $hold->usr($e->requestor->id); # XXX staff
383             $hold->target($ctx->{hold_target});
384             $hold->hold_type($ctx->{hold_type});
385             # frozen, expired, etc..
386
387             my $stat = $U->simplereq(
388                 'open-ils.circ',
389                 'open-ils.circ.holds.create',
390                 $e->authtoken, $hold
391             );
392
393             if($stat and $stat > 0) {
394                 $ctx->{hold_success} = 1;
395             } else {
396                 $ctx->{hold_failed} = 1; # XXX process the events, etc
397             }
398         }
399
400         # place the hold and deliver results
401     }
402
403     return Apache2::Const::OK;
404 }
405
406 1;