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 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;
73 return $self->load_home if $path =~ /opac\/home/;
74 return $self->load_login if $path =~ /opac\/login/;
75 return $self->load_logout if $path =~ /opac\/logout/;
76 return $self->load_rresults if $path =~ /opac\/results/;
77 return $self->load_record if $path =~ /opac\/record/;
78 return $self->load_place_hold if $path =~ /opac\/place_hold/;
80 return $self->load_myopac_holds if $path =~ /opac\/myopac\/holds/;
81 return $self->load_myopac_circs if $path =~ /opac\/myopac\/circs/;
82 return $self->load_myopac if $path =~ /opac\/myopac/;
84 return Apache2::Const::OK;
87 # general purpose utility functions added to the environment
90 my $e = $self->editor;
93 $cache{map} = {}; # public object maps
94 $cache{list} = {}; # public object lists
96 # fetch-on-demand-and-cache subs for commonly used public data
97 my @public_classes = qw/ccs aout/;
99 for my $hint (@public_classes) {
102 $Fieldmapper::fieldmap->{$_}->{hint} eq $hint
103 } keys %{ $Fieldmapper::fieldmap };
105 $class =~ s/Fieldmapper:://o;
109 my $list_key = $hint . '_list';
110 my $find_key = "find_$hint";
112 $ctx->{$list_key} = sub {
113 my $method = "retrieve_all_$class";
114 $cache{list}{$hint} = $e->$method() unless $cache{list}{$hint};
115 return $cache{list}{$hint};
118 $cache{map}{$hint} = {};
120 $ctx->{$find_key} = sub {
122 return $cache{map}{$hint}{$id} if $cache{map}{$hint}{$id};
123 ($cache{map}{$hint}{$id}) = grep { $_->id == $id } @{$ctx->{$list_key}->()};
124 return $cache{map}{$hint}{$id};
129 $ctx->{aou_tree} = sub {
131 # fetch the org unit tree
132 unless($cache{aou_tree}) {
133 my $tree = $e->search_actor_org_unit([
134 { parent_ou => undef},
136 flesh_fields => {aou => ['children']},
137 order_by => {aou => 'name'}
141 # flesh the org unit type for each org unit
142 # and simultaneously set the id => aou map cache
146 $node->ou_type( $ctx->{find_aout}->($node->ou_type) );
147 $cache{map}{aou}{$node->id} = $node;
148 flesh_aout($_, $ctx) foreach @{$node->children};
150 flesh_aout($tree, $ctx);
152 $cache{aou_tree} = $tree;
155 return $cache{aou_tree};
158 # Add a special handler for the tree-shaped org unit cache
159 $cache{map}{aou} = {};
160 $ctx->{find_aou} = sub {
162 $ctx->{aou_tree}->(); # force the org tree to load
163 return $cache{map}{aou}{$org_id};
166 # turns an ISO date into something TT can understand
167 $ctx->{parse_datetime} = sub {
169 $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
171 "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
185 # user_status : hash of user circ numbers
189 my $e = $self->editor;
190 my $ctx = $self->ctx;
192 if($e->authtoken($self->cgi->cookie('ses'))) {
196 $ctx->{authtoken} = $e->authtoken;
197 $ctx->{user} = $e->requestor;
198 $ctx->{user_stats} = $U->simplereq(
200 'open-ils.actor.user.opac.vital_stats',
201 $e->authtoken, $e->requestor->id);
205 return $self->load_logout;
209 return Apache2::Const::OK;
214 $self->ctx->{page} = 'home';
215 return Apache2::Const::OK;
221 my $cgi = $self->cgi;
223 $self->ctx->{page} = 'login';
225 my $username = $cgi->param('username');
226 my $password = $cgi->param('password');
228 return Apache2::Const::OK unless $username and $password;
230 my $seed = $U->simplereq(
232 'open-ils.auth.authenticate.init',
235 my $response = $U->simplereq(
237 'open-ils.auth.authenticate.complete',
238 { username => $username,
239 password => md5_hex($seed . md5_hex($password)),
244 # XXX check event, redirect as necessary
246 my $home = $self->apache->unparsed_uri;
247 $home =~ s/\/login/\/home/;
249 $self->apache->print(
251 -url => $cgi->param('origin') || $home,
252 -cookie => $cgi->cookie(
256 -value => $response->{payload}->{authtoken},
257 -expires => CORE::time + $response->{payload}->{authtime}
262 return Apache2::Const::REDIRECT;
268 my $path = $self->apache->uri;
269 $path =~ s/(\/[^\/]+$)/\/home/;
270 my $url = 'http://' . $self->apache->hostname . "$path";
272 $self->apache->print(
273 $self->cgi->redirect(
275 -cookie => $self->cgi->cookie(
284 return Apache2::Const::REDIRECT;
290 # records : list of bre's and copy-count objects
293 my $cgi = $self->cgi;
294 my $ctx = $self->ctx;
295 my $e = $self->editor;
297 $ctx->{page} = 'rresult';
298 my $page = $cgi->param('page') || 0;
299 my $facet = $cgi->param('facet');
300 my $query = $cgi->param('query');
301 my $limit = $cgi->param('limit') || 10; # XXX user settings
302 my $args = {limit => $limit, offset => $page * $limit};
303 $query = "$query $facet" if $facet;
307 $results = $U->simplereq(
309 'open-ils.search.biblio.multiclass.query.staff',
314 $logger->error("multiclass search error: $err");
315 $results = {count => 0, ids => []};
318 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
320 $ctx->{records} = [];
321 $ctx->{search_facets} = {};
322 $ctx->{page_size} = $limit;
323 $ctx->{hit_count} = $results->{count};
325 return Apache2::Const::OK if @$rec_ids == 0;
327 my $cstore1 = OpenSRF::AppSession->create('open-ils.cstore');
328 my $bre_req = $cstore1->request(
329 'open-ils.cstore.direct.biblio.record_entry.search', {id => $rec_ids});
331 my $search = OpenSRF::AppSession->create('open-ils.search');
332 my $facet_req = $search->request('open-ils.search.facet_cache.retrieve', $results->{facet_key}, 10);
334 unless($cache{cmf}) {
335 $cache{cmf} = $e->search_config_metabib_field({id => {'!=' => undef}});
336 $ctx->{metabib_field} = $cache{cmf};
337 #$cache{cmc} = $e->search_config_metabib_class({name => {'!=' => undef}});
338 #$ctx->{metabib_class} = $cache{cmc};
342 while(my $resp = $bre_req->recv) {
343 my $bre = $resp->content;
345 # XXX farm out to multiple cstore sessions before loop, then collect after
346 my $copy_counts = $e->json_query(
347 {from => ['asset.record_copy_count', 1, $bre->id, 0]})->[0];
352 marc_xml => XML::LibXML->new->parse_string($bre->marc),
353 copy_counts => $copy_counts
360 # shove recs into context in search results order
361 for my $rec_id (@$rec_ids) {
364 grep { $_->{bre}->id == $rec_id } @data
368 my $facets = $facet_req->gather(1);
370 for my $cmf_id (keys %$facets) { # quick-n-dirty
371 my ($cmf) = grep { $_->id eq $cmf_id } @{$cache{cmf}};
372 $facets->{$cmf_id} = {cmf => $cmf, data => $facets->{$cmf_id}};
374 $ctx->{search_facets} = $facets;
376 return Apache2::Const::OK;
380 # record : bre object
383 $self->ctx->{page} = 'record';
385 my $rec_id = $self->ctx->{page_args}->[0]
386 or return Apache2::Const::HTTP_BAD_REQUEST;
388 $self->ctx->{record} = $self->editor->retrieve_biblio_record_entry([
393 bre => ['call_numbers'],
394 acn => ['copies'] # limit, paging, etc.
399 $self->ctx->{marc_xml} = XML::LibXML->new->parse_string($self->ctx->{record}->marc);
401 return Apache2::Const::OK;
405 # user : au object, fleshed
408 $self->ctx->{page} = 'myopac';
410 $self->ctx->{user} = $self->editor->retrieve_actor_user([
411 $self->ctx->{user}->id,
421 return Apache2::Const::OK;
424 sub load_myopac_holds {
426 my $e = $self->editor;
427 my $ctx = $self->ctx;
429 my $limit = $self->cgi->param('limit') || 10;
430 my $offset = $self->cgi->param('offset') || 0;
432 my $circ = OpenSRF::AppSession->create('open-ils.circ');
433 my $hold_ids = $circ->request(
434 'open-ils.circ.holds.id_list.retrieve',
439 $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ];
441 my $req = $circ->request(
442 'open-ils.circ.hold.details.batch.retrieve',
446 suppress_notices => 1,
447 suppress_transits => 1,
449 suppress_patron_details => 1,
454 # any requests we can fire off here?
457 while(my $resp = $req->recv) {
458 my $hold = $resp->content;
459 push(@{$ctx->{holds}}, {
461 marc_xml => XML::LibXML->new->parse_string($hold->{bre}->marc)
466 return Apache2::Const::OK;
469 sub load_place_hold {
471 my $ctx = $self->ctx;
472 my $e = $self->editor;
473 $self->ctx->{page} = 'place_hold';
475 $ctx->{hold_target} = $self->cgi->param('hold_target');
476 $ctx->{hold_type} = $self->cgi->param('hold_type');
477 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
479 if($ctx->{hold_type} eq 'T') {
480 $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
484 $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
486 if(my $pickup_lib = $self->cgi->param('pickup_lib')) {
489 patronid => $e->requestor->id,
490 titleid => $ctx->{hold_target}, # XXX
491 pickup_lib => $pickup_lib,
495 my $allowed = $U->simplereq(
497 'open-ils.circ.title_hold.is_possible',
501 if($allowed->{success} == 1) {
502 my $hold = Fieldmapper::action::hold_request->new;
504 $hold->pickup_lib($pickup_lib);
505 $hold->requestor($e->requestor->id);
506 $hold->usr($e->requestor->id); # XXX staff
507 $hold->target($ctx->{hold_target});
508 $hold->hold_type($ctx->{hold_type});
509 # frozen, expired, etc..
511 my $stat = $U->simplereq(
513 'open-ils.circ.holds.create',
517 if($stat and $stat > 0) {
518 $ctx->{hold_success} = 1;
520 $ctx->{hold_failed} = 1; # XXX process the events, etc
524 # place the hold and deliver results
527 return Apache2::Const::OK;
531 sub load_myopac_circs {
533 my $e = $self->editor;
534 my $ctx = $self->ctx;
537 my $limit = $self->cgi->param('limit') || 10;
538 my $offset = $self->cgi->param('offset') || 0;
540 my $circ_data = $U->simplereq(
542 'open-ils.actor.user.checked_out',
547 my @circ_ids = ( @{$circ_data->{overdue}}, @{$circ_data->{out}} );
548 @circ_ids = grep { defined $_ } @circ_ids[0..($offset + $limit - 1)];
550 return Apache2::Const::OK unless @circ_ids;
552 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
553 my $req = $cstore->request(
554 'open-ils.cstore.direct.action.circulation.search',
559 circ => ['target_copy'],
560 acp => ['call_number'],
567 while(my $resp = $req->recv) {
568 my $circ = $resp->content;
571 marc_xml => ($circ->target_copy->call_number->id == -1) ?
572 undef : # pre-cat copy, use the dummy title/author instead
573 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc),
577 # make sure the final list is in the correct order
578 for my $id (@circ_ids) {
581 (grep { $_->{circ}->id == $id } @circs)
585 return Apache2::Const::OK;