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 FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
7 use OpenSRF::AppSession;
8 use OpenSRF::EX qw/:try/;
9 use OpenSRF::Utils qw/:datetime/;
10 use OpenSRF::Utils::Logger qw/$logger/;
11 use OpenILS::Application::AppUtils;
12 use OpenILS::Utils::CStoreEditor qw/:funcs/;
13 use OpenILS::Utils::Fieldmapper;
14 use DateTime::Format::ISO8601;
15 my $U = 'OpenILS::Application::AppUtils';
17 my %cache; # proc-level cache
20 my($class, $apache, $ctx) = @_;
22 my $self = bless({}, ref($class) || $class);
24 $self->apache($apache);
28 OpenILS::Utils::CStoreEditor->init; # just in case
29 $self->editor(new_editor());
35 # current Apache2::RequestRec;
37 my($self, $apache) = @_;
38 $self->{apache} = $apache if $apache;
39 return $self->{apache};
42 # runtime / template context
45 $self->{ctx} = $ctx if $ctx;
51 my($self, $editor) = @_;
52 $self->{editor} = $editor if $editor;
53 return $self->{editor};
59 $self->{cgi} = $cgi if $cgi;
64 # load common data, then load page data
69 my $stat = $self->load_common;
70 return $stat unless $stat == Apache2::Const::OK;
72 my $path = $self->apache->path_info;
74 return $self->load_home if $path =~ /opac\/home/;
75 return $self->load_login if $path =~ /opac\/login/;
76 return $self->load_logout if $path =~ /opac\/logout/;
77 return $self->load_rresults if $path =~ /opac\/results/;
78 return $self->load_record if $path =~ /opac\/record/;
80 # ----------------------------------------------------------------
81 # These pages require authentication
82 # ----------------------------------------------------------------
83 return Apache2::Const::FORBIDDEN unless $self->cgi->https;
84 return $self->load_logout unless $self->editor->requestor;
86 return $self->load_place_hold if $path =~ /opac\/place_hold/;
87 return $self->load_myopac_holds if $path =~ /opac\/myopac\/holds/;
88 return $self->load_myopac_circs if $path =~ /opac\/myopac\/circs/;
89 return $self->load_myopac if $path =~ /opac\/myopac/;
90 # ----------------------------------------------------------------
92 return Apache2::Const::OK;
95 # general purpose utility functions added to the environment
98 my $e = $self->editor;
101 $cache{map} = {}; # public object maps
102 $cache{list} = {}; # public object lists
104 # fetch-on-demand-and-cache subs for commonly used public data
105 my @public_classes = qw/ccs aout/;
107 for my $hint (@public_classes) {
110 $Fieldmapper::fieldmap->{$_}->{hint} eq $hint
111 } keys %{ $Fieldmapper::fieldmap };
113 $class =~ s/Fieldmapper:://o;
117 my $list_key = $hint . '_list';
118 my $find_key = "find_$hint";
120 $ctx->{$list_key} = sub {
121 my $method = "retrieve_all_$class";
122 $cache{list}{$hint} = $e->$method() unless $cache{list}{$hint};
123 return $cache{list}{$hint};
126 $cache{map}{$hint} = {};
128 $ctx->{$find_key} = sub {
130 return $cache{map}{$hint}{$id} if $cache{map}{$hint}{$id};
131 ($cache{map}{$hint}{$id}) = grep { $_->id == $id } @{$ctx->{$list_key}->()};
132 return $cache{map}{$hint}{$id};
137 $ctx->{aou_tree} = sub {
139 # fetch the org unit tree
140 unless($cache{aou_tree}) {
141 my $tree = $e->search_actor_org_unit([
142 { parent_ou => undef},
144 flesh_fields => {aou => ['children']},
145 order_by => {aou => 'name'}
149 # flesh the org unit type for each org unit
150 # and simultaneously set the id => aou map cache
154 $node->ou_type( $ctx->{find_aout}->($node->ou_type) );
155 $cache{map}{aou}{$node->id} = $node;
156 flesh_aout($_, $ctx) foreach @{$node->children};
158 flesh_aout($tree, $ctx);
160 $cache{aou_tree} = $tree;
163 return $cache{aou_tree};
166 # Add a special handler for the tree-shaped org unit cache
167 $cache{map}{aou} = {};
168 $ctx->{find_aou} = sub {
170 $ctx->{aou_tree}->(); # force the org tree to load
171 return $cache{map}{aou}{$org_id};
174 # turns an ISO date into something TT can understand
175 $ctx->{parse_datetime} = sub {
177 $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
179 "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
193 # user_status : hash of user circ numbers
197 my $e = $self->editor;
198 my $ctx = $self->ctx;
200 if($e->authtoken($self->cgi->cookie('ses'))) {
204 $ctx->{authtoken} = $e->authtoken;
205 $ctx->{user} = $e->requestor;
206 $ctx->{user_stats} = $U->simplereq(
208 'open-ils.actor.user.opac.vital_stats',
209 $e->authtoken, $e->requestor->id);
213 return $self->load_logout;
217 return Apache2::Const::OK;
222 $self->ctx->{page} = 'home';
223 return Apache2::Const::OK;
229 my $cgi = $self->cgi;
231 $self->ctx->{page} = 'login';
233 my $username = $cgi->param('username');
234 my $password = $cgi->param('password');
236 return Apache2::Const::OK unless $username and $password;
238 my $seed = $U->simplereq(
240 'open-ils.auth.authenticate.init',
243 my $response = $U->simplereq(
245 'open-ils.auth.authenticate.complete',
246 { username => $username,
247 password => md5_hex($seed . md5_hex($password)),
252 # XXX check event, redirect as necessary
254 my $home = $self->apache->unparsed_uri;
255 $home =~ s/\/login/\/home/;
257 $self->apache->print(
259 -url => $cgi->param('origin') || $home,
260 -cookie => $cgi->cookie(
264 -value => $response->{payload}->{authtoken},
265 -expires => CORE::time + $response->{payload}->{authtime}
270 return Apache2::Const::REDIRECT;
276 my $url = 'http://' . $self->apache->hostname . $self->ctx->{base_path} . "/opac/home";
278 $self->apache->print(
279 $self->cgi->redirect(
281 -cookie => $self->cgi->cookie(
290 return Apache2::Const::REDIRECT;
296 # records : list of bre's and copy-count objects
299 my $cgi = $self->cgi;
300 my $ctx = $self->ctx;
301 my $e = $self->editor;
303 $ctx->{page} = 'rresult';
304 my $page = $cgi->param('page') || 0;
305 my $facet = $cgi->param('facet');
306 my $query = $cgi->param('query');
307 my $limit = $cgi->param('limit') || 10; # XXX user settings
308 my $args = {limit => $limit, offset => $page * $limit};
309 $query = "$query $facet" if $facet;
313 $results = $U->simplereq(
315 'open-ils.search.biblio.multiclass.query.staff',
320 $logger->error("multiclass search error: $err");
321 $results = {count => 0, ids => []};
324 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
326 $ctx->{records} = [];
327 $ctx->{search_facets} = {};
328 $ctx->{page_size} = $limit;
329 $ctx->{hit_count} = $results->{count};
331 return Apache2::Const::OK if @$rec_ids == 0;
333 my $cstore1 = OpenSRF::AppSession->create('open-ils.cstore');
334 my $bre_req = $cstore1->request(
335 'open-ils.cstore.direct.biblio.record_entry.search', {id => $rec_ids});
337 my $search = OpenSRF::AppSession->create('open-ils.search');
338 my $facet_req = $search->request('open-ils.search.facet_cache.retrieve', $results->{facet_key}, 10);
340 unless($cache{cmf}) {
341 $cache{cmf} = $e->search_config_metabib_field({id => {'!=' => undef}});
342 $ctx->{metabib_field} = $cache{cmf};
343 #$cache{cmc} = $e->search_config_metabib_class({name => {'!=' => undef}});
344 #$ctx->{metabib_class} = $cache{cmc};
348 while(my $resp = $bre_req->recv) {
349 my $bre = $resp->content;
351 # XXX farm out to multiple cstore sessions before loop, then collect after
352 my $copy_counts = $e->json_query(
353 {from => ['asset.record_copy_count', 1, $bre->id, 0]})->[0];
358 marc_xml => XML::LibXML->new->parse_string($bre->marc),
359 copy_counts => $copy_counts
366 # shove recs into context in search results order
367 for my $rec_id (@$rec_ids) {
370 grep { $_->{bre}->id == $rec_id } @data
374 my $facets = $facet_req->gather(1);
376 for my $cmf_id (keys %$facets) { # quick-n-dirty
377 my ($cmf) = grep { $_->id eq $cmf_id } @{$cache{cmf}};
378 $facets->{$cmf_id} = {cmf => $cmf, data => $facets->{$cmf_id}};
380 $ctx->{search_facets} = $facets;
382 return Apache2::Const::OK;
386 # record : bre object
389 $self->ctx->{page} = 'record';
391 my $rec_id = $self->ctx->{page_args}->[0]
392 or return Apache2::Const::HTTP_BAD_REQUEST;
394 $self->ctx->{record} = $self->editor->retrieve_biblio_record_entry([
399 bre => ['call_numbers'],
400 acn => ['copies'] # limit, paging, etc.
405 $self->ctx->{marc_xml} = XML::LibXML->new->parse_string($self->ctx->{record}->marc);
407 return Apache2::Const::OK;
411 # user : au object, fleshed
414 $self->ctx->{page} = 'myopac';
416 $self->ctx->{user} = $self->editor->retrieve_actor_user([
417 $self->ctx->{user}->id,
427 return Apache2::Const::OK;
430 sub load_myopac_holds {
432 my $e = $self->editor;
433 my $ctx = $self->ctx;
435 my $limit = $self->cgi->param('limit') || 10;
436 my $offset = $self->cgi->param('offset') || 0;
438 my $circ = OpenSRF::AppSession->create('open-ils.circ');
439 my $hold_ids = $circ->request(
440 'open-ils.circ.holds.id_list.retrieve',
445 $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ];
447 my $req = $circ->request(
448 'open-ils.circ.hold.details.batch.retrieve',
452 suppress_notices => 1,
453 suppress_transits => 1,
455 suppress_patron_details => 1,
460 # any requests we can fire off here?
463 while(my $resp = $req->recv) {
464 my $hold = $resp->content;
465 push(@{$ctx->{holds}}, {
467 marc_xml => XML::LibXML->new->parse_string($hold->{bre}->marc)
472 return Apache2::Const::OK;
475 sub load_place_hold {
477 my $ctx = $self->ctx;
478 my $e = $self->editor;
479 $self->ctx->{page} = 'place_hold';
481 $ctx->{hold_target} = $self->cgi->param('hold_target');
482 $ctx->{hold_type} = $self->cgi->param('hold_type');
483 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
485 if($ctx->{hold_type} eq 'T') {
486 $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
490 $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
492 if(my $pickup_lib = $self->cgi->param('pickup_lib')) {
495 patronid => $e->requestor->id,
496 titleid => $ctx->{hold_target}, # XXX
497 pickup_lib => $pickup_lib,
501 my $allowed = $U->simplereq(
503 'open-ils.circ.title_hold.is_possible',
507 if($allowed->{success} == 1) {
508 my $hold = Fieldmapper::action::hold_request->new;
510 $hold->pickup_lib($pickup_lib);
511 $hold->requestor($e->requestor->id);
512 $hold->usr($e->requestor->id); # XXX staff
513 $hold->target($ctx->{hold_target});
514 $hold->hold_type($ctx->{hold_type});
515 # frozen, expired, etc..
517 my $stat = $U->simplereq(
519 'open-ils.circ.holds.create',
523 if($stat and $stat > 0) {
524 $ctx->{hold_success} = 1;
526 $ctx->{hold_failed} = 1; # XXX process the events, etc
530 # place the hold and deliver results
533 return Apache2::Const::OK;
537 sub load_myopac_circs {
539 my $e = $self->editor;
540 my $ctx = $self->ctx;
543 my $limit = $self->cgi->param('limit') || 10;
544 my $offset = $self->cgi->param('offset') || 0;
546 my $circ_data = $U->simplereq(
548 'open-ils.actor.user.checked_out',
553 my @circ_ids = ( @{$circ_data->{overdue}}, @{$circ_data->{out}} );
554 @circ_ids = grep { defined $_ } @circ_ids[0..($offset + $limit - 1)];
556 return Apache2::Const::OK unless @circ_ids;
558 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
559 my $req = $cstore->request(
560 'open-ils.cstore.direct.action.circulation.search',
565 circ => ['target_copy'],
566 acp => ['call_number'],
573 while(my $resp = $req->recv) {
574 my $circ = $resp->content;
577 marc_xml => ($circ->target_copy->call_number->id == -1) ?
578 undef : # pre-cat copy, use the dummy title/author instead
579 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc),
583 # make sure the final list is in the correct order
584 for my $id (@circ_ids) {
587 (grep { $_->{circ}->id == $id } @circs)
591 return Apache2::Const::OK;