1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
5 use Digest::MD5 qw(md5_hex);
6 use Apache2::Const -compile => qw(OK DECLINED HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
7 use OpenSRF::AppSession;
8 use OpenSRF::EX qw/:try/;
9 use OpenSRF::Utils::Logger qw/$logger/;
10 use OpenILS::Application::AppUtils;
11 use OpenILS::Utils::CStoreEditor qw/:funcs/;
12 use OpenILS::Utils::Fieldmapper;
13 my $U = 'OpenILS::Application::AppUtils';
15 my %cache; # proc-level cache
18 my($class, $apache, $ctx) = @_;
20 my $self = bless({}, ref($class) || $class);
22 $self->apache($apache);
26 OpenILS::Utils::CStoreEditor->init; # just in case
27 $self->editor(new_editor());
33 # current Apache2::RequestRec;
35 my($self, $apache) = @_;
36 $self->{apache} = $apache if $apache;
37 return $self->{apache};
43 $self->{ctx} = $ctx if $ctx;
49 my($self, $editor) = @_;
50 $self->{editor} = $editor if $editor;
51 return $self->{editor};
57 $self->{cgi} = $cgi if $cgi;
62 # load common data, then load page data
66 my $path = $self->apache->path_info;
69 my $stat = $self->load_common;
70 return $stat unless $stat == Apache2::Const::OK;
72 return $self->load_home if $path =~ /opac\/home/;
73 return $self->load_login if $path =~ /opac\/login/;
74 return $self->load_logout if $path =~ /opac\/logout/;
75 return $self->load_rresults if $path =~ /opac\/results/;
76 return $self->load_record if $path =~ /opac\/record/;
77 return $self->load_myopac if $path =~ /opac\/myopac/;
78 return $self->load_place_hold if $path =~ /opac\/place_hold/;
80 return Apache2::Const::OK;
83 # general purpose utility functions added to the environment
85 # find_org_unit : function(id) => aou object
86 # org_tree : function(id) => aou object, top of tree, fleshed
89 $cache{org_unit_map} = {};
91 # pull the org unit from the cached org tree
92 $self->ctx->{find_org_unit} = sub {
94 return undef unless defined $org_id;
95 return $cache{org_unit_map}{$org_id} if defined $cache{org_unit_map}{$org_id};
96 my $tree = shift || $self->ctx->{org_tree}->();
97 return $cache{org_unit_map}{$org_id} = $tree if $tree->id == $org_id;
98 for my $child (@{$tree->children}) {
99 my $node = $self->ctx->{find_org_unit}->($org_id, $child);
100 return $node if $node;
105 $self->ctx->{org_tree} = sub {
106 unless($cache{org_tree}) {
107 $cache{org_tree} = $self->editor->search_actor_org_unit([
108 { parent_ou => undef},
110 flesh_fields => {aou => ['children', 'ou_type']},
111 order_by => {aou => 'name'}
115 return $cache{org_tree};
122 # user_status : hash of user circ numbers
126 my $e = $self->editor;
127 my $ctx = $self->ctx;
129 if($e->authtoken($self->cgi->cookie('ses'))) {
133 $ctx->{authtoken} = $e->authtoken;
134 $ctx->{user} = $e->requestor;
135 $ctx->{user_stats} = $U->simplereq(
137 'open-ils.actor.user.opac.vital_stats',
138 $e->authtoken, $e->requestor->id);
142 return $self->load_logout;
146 return Apache2::Const::OK;
151 $self->ctx->{page} = 'home';
152 return Apache2::Const::OK;
158 my $cgi = $self->cgi;
160 $self->ctx->{page} = 'login';
162 my $username = $cgi->param('username');
163 my $password = $cgi->param('password');
165 return Apache2::Const::OK unless $username and $password;
167 my $seed = $U->simplereq(
169 'open-ils.auth.authenticate.init',
172 my $response = $U->simplereq(
174 'open-ils.auth.authenticate.complete',
175 { username => $username,
176 password => md5_hex($seed . md5_hex($password)),
181 # XXX check event, redirect as necessary
183 my $home = $self->apache->unparsed_uri;
184 $home =~ s/\/login/\/home/;
186 $self->apache->print(
188 -url => $cgi->param('origin') || $home,
189 -cookie => $cgi->cookie(
193 -value => $response->{payload}->{authtoken},
194 -expires => CORE::time + $response->{payload}->{authtime}
199 return Apache2::Const::REDIRECT;
205 my $path = $self->apache->uri;
206 $path =~ s/(\/[^\/]+$)/\/home/;
207 my $url = 'http://' . $self->apache->hostname . "$path";
209 $self->apache->print(
210 $self->cgi->redirect(
212 -cookie => $self->cgi->cookie(
221 return Apache2::Const::REDIRECT;
227 # records : list of bre's and copy-count objects
230 my $cgi = $self->cgi;
231 my $ctx = $self->ctx;
232 my $e = $self->editor;
234 $ctx->{page} = 'rresult';
235 my $page = $cgi->param('page') || 0;
236 my $facet = $cgi->param('facet');
237 my $query = $cgi->param('query');
238 my $limit = $cgi->param('limit') || 10; # XXX user settings
239 my $args = {limit => $limit, offset => $page * $limit};
240 $query = "$query $facet" if $facet;
244 $results = $U->simplereq(
246 'open-ils.search.biblio.multiclass.query.staff',
251 $logger->error("multiclass search error: $err");
252 $results = {count => 0, ids => []};
255 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
257 $ctx->{records} = [];
258 $ctx->{search_facets} = {};
259 $ctx->{page_size} = $limit;
260 $ctx->{hit_count} = $results->{count};
262 return Apache2::Const::OK if @$rec_ids == 0;
264 my $cstore1 = OpenSRF::AppSession->create('open-ils.cstore');
265 my $bre_req = $cstore1->request(
266 'open-ils.cstore.direct.biblio.record_entry.search', {id => $rec_ids});
268 my $search = OpenSRF::AppSession->create('open-ils.search');
269 my $facet_req = $search->request('open-ils.search.facet_cache.retrieve', $results->{facet_key}, 10);
271 unless($cache{cmf}) {
272 $cache{cmf} = $e->search_config_metabib_field({id => {'!=' => undef}});
273 $ctx->{metabib_field} = $cache{cmf};
274 #$cache{cmc} = $e->search_config_metabib_class({name => {'!=' => undef}});
275 #$ctx->{metabib_class} = $cache{cmc};
279 while(my $resp = $bre_req->recv) {
280 my $bre = $resp->content;
282 # XXX farm out to multiple cstore sessions before loop, then collect after
283 my $copy_counts = $e->json_query(
284 {from => ['asset.record_copy_count', 1, $bre->id, 0]})->[0];
289 marc_xml => XML::LibXML->new->parse_string($bre->marc),
290 copy_counts => $copy_counts
297 # shove recs into context in search results order
298 for my $rec_id (@$rec_ids) {
301 grep { $_->{bre}->id == $rec_id } @data
305 my $facets = $facet_req->gather(1);
307 for my $cmf_id (keys %$facets) { # quick-n-dirty
308 my ($cmf) = grep { $_->id eq $cmf_id } @{$cache{cmf}};
309 $facets->{$cmf_id} = {cmf => $cmf, data => $facets->{$cmf_id}};
311 $ctx->{search_facets} = $facets;
313 return Apache2::Const::OK;
317 # record : bre object
321 my $rec_id = $self->ctx->{page_args}->[0]
322 or return Apache2::Const::HTTP_BAD_REQUEST;
324 $self->ctx->{record} = $self->editor->retrieve_biblio_record_entry([
329 bre => ['call_numbers'],
330 acn => ['copies'] # limit, paging, etc.
335 $self->ctx->{marc_xml} = XML::LibXML->new->parse_string($self->ctx->{record}->marc);
337 return Apache2::Const::OK;
341 # user : au object, fleshed
345 $self->ctx->{user} = $self->editor->retrieve_actor_user([
346 $self->ctx->{user}->id,
356 return Apache2::Const::OK;
360 sub load_place_hold {
362 my $ctx = $self->ctx;
363 my $e = $self->editor;
365 $ctx->{hold_target} = $self->cgi->param('hold_target');
366 $ctx->{hold_type} = $self->cgi->param('hold_type');
367 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
369 if($ctx->{hold_type} eq 'T') {
370 $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
374 $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
376 if(my $pickup_lib = $self->cgi->param('pickup_lib')) {
379 patronid => $e->requestor->id,
380 titleid => $ctx->{hold_target}, # XXX
381 pickup_lib => $pickup_lib,
385 my $allowed = $U->simplereq(
387 'open-ils.circ.title_hold.is_possible',
391 if($allowed->{success} == 1) {
392 my $hold = Fieldmapper::action::hold_request->new;
394 $hold->pickup_lib($pickup_lib);
395 $hold->requestor($e->requestor->id);
396 $hold->usr($e->requestor->id); # XXX staff
397 $hold->target($ctx->{hold_target});
398 $hold->hold_type($ctx->{hold_type});
399 # frozen, expired, etc..
401 my $stat = $U->simplereq(
403 'open-ils.circ.holds.create',
407 if($stat and $stat > 0) {
408 $ctx->{hold_success} = 1;
410 $ctx->{hold_failed} = 1; # XXX process the events, etc
414 # place the hold and deliver results
417 return Apache2::Const::OK;