1 package OpenILS::WWW::EGKPacLoader;
2 use base 'OpenILS::WWW::EGCatLoader';
3 use strict; use warnings;
5 use Apache2::Const -compile => qw(OK HTTP_BAD_REQUEST);
6 use OpenSRF::Utils::Logger qw/$logger/;
7 use OpenILS::Application::AppUtils;
8 use OpenILS::Utils::CStoreEditor qw/:funcs/;
9 my $U = 'OpenILS::Application::AppUtils';
12 # -----------------------------------------------------------------------------
13 # Override our parent's load() sub so we can do kpac-specific path routing.
14 # -----------------------------------------------------------------------------
18 $self->init_ro_object_cache;
20 my $stat = $self->load_common;
21 return $stat unless $stat == Apache2::Const::OK;
23 $self->load_kpac_config;
25 my $path = $self->apache->path_info;
26 ($self->ctx->{page} = $path) =~ s#.*/(.*)#$1#g;
28 return $self->load_simple("home") if $path =~ m|kpac/home|;
29 return $self->load_simple("category") if $path =~ m|kpac/category|;
30 return $self->load_kpac_rresults if $path =~ m|kpac/results|;
31 return $self->load_record(no_search => 1) if $path =~ m|kpac/record|;
33 # ----------------------------------------------------------------
34 # Everything below here requires SSL
35 # ----------------------------------------------------------------
36 return $self->redirect_ssl unless $self->cgi->https;
38 return $self->load_getit_results if $path =~ m|kpac/getit_results|;
39 return $self->load_getit if $path =~ m|kpac/getit|;
41 # ----------------------------------------------------------------
42 # Everything below here requires authentication
43 # ----------------------------------------------------------------
44 return $self->redirect_auth unless $self->editor->requestor;
48 return Apache2::Const::OK;
51 sub load_kpac_rresults {
54 # The redirect-to-record-details-on-single-hit logic
55 # leverages the opac_root to determine the record detail
56 # page. Replace it temporarily for our purposes.
57 my $tpac_root = $self->ctx->{opac_root};
58 $self->ctx->{opac_root} = $self->ctx->{kpac_root};
60 my $stat = $self->load_rresults;
61 $self->ctx->{opac_root} = $tpac_root;
69 my $rec_id = $ctx->{page_args}->[0];
70 my $bbag_id = $self->cgi->param('bookbag');
71 my $action = $self->cgi->param('action') || '';
73 # first load the record
74 my $stat = $self->load_record(no_search => 1);
75 return $stat unless $stat == Apache2::Const::OK;
77 $self->ctx->{page} = 'getit'; # repair the page
79 return $self->save_item_to_bookbag($rec_id, $bbag_id) if $action eq 'save';
80 return $self->login_and_place_hold($rec_id) if $action eq 'hold';
82 # if the user is logged in, fetch his bookbags
84 $ctx->{bookbags} = $self->editor->search_container_biblio_record_entry_bucket([
85 { owner => $ctx->{user}->id,
88 { order_by => {cbreb => 'name'},
89 limit => $self->cgi->param('bbag_limit') || 100
94 $self->ctx->{page} = 'getit'; # repair the page
95 return Apache2::Const::OK;
98 sub login_and_place_hold {
101 my $ctx = $self->ctx;
102 my $username = $self->cgi->param('username');
103 my $password = $self->cgi->param('password');
104 my $pickup_lib = $self->cgi->param('pickup_lib');
106 return Apache2::Const::HTTP_BAD_REQUEST
107 unless $pickup_lib =~ /^\d+$/;
109 my $new_uri = $self->apache->unparsed_uri;
110 my $sep = ($new_uri =~ /\?/) ? '&' : '?';
113 # First, log the user in and return to
114 $self->apache->log->info("kpac: logging in $username");
116 # TODO: let user know username/password is required..
117 return Apache2::Const::OK unless $username and $password;
119 $new_uri .= "${sep}pickup_lib=$pickup_lib&action=hold";
120 $self->cgi->param('redirect_to', $new_uri);
121 return $self->load_login;
125 $self->apache->log->info("kpac: placing hold for $bre_id");
127 $new_uri =~ s/getit/getit_results/g;
128 $self->cgi->param('hold_target', $bre_id);
129 $self->cgi->param('hold_type', 'T');
130 $self->cgi->param('part', ''); # needed even if unused
132 my $stat = $self->load_place_hold;
134 $self->apache->log->info("kpac: place hold returned $stat");
136 return $stat unless $stat == Apache2::Const::OK;
138 my $hdata = $ctx->{hold_data}->[0]; # only 1 hold placed
139 if (my $hold_id = $hdata ? $hdata->{hold_success} : undef) {
141 $self->apache->log->info("kpac: place hold succeeded");
142 $new_uri .= "${sep}hold=$hold_id";
145 $self->apache->log->info("kpac: place hold failed : " . $ctx->{hold_failed_event});
146 $new_uri .= "${sep}hold_failed=1";
150 $self->apache->log->info("kpac: place hold redirecting to: $new_uri");
151 return $self->generic_redirect($new_uri);
154 sub save_item_to_bookbag {
157 my $bookbag_id = shift;
160 # save to existing bookbag
161 $self->cgi->param('record', $rec_id);
162 my $stat = $self->load_myopac_bookbag_update('add_rec', $bookbag_id);
163 # TODO: check for failure
164 (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
165 $new_uri .= ($new_uri =~ /\?/) ? "&list=$bookbag_id" : "?list=$bookbag_id";
166 return $self->generic_redirect($new_uri);
169 # save to anonymous list
171 # set some params assumed to exist for load_mylist_add
172 $self->cgi->param('record', $rec_id);
173 (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
174 $new_uri .= ($new_uri =~ /\?/) ? '&list=anon' : '?list=anon';
175 $self->cgi->param('redirect_to', $new_uri);
177 return $self->load_mylist_add;
180 return Apache2::Const::HTTP_BAD_REQUEST;
184 sub load_getit_results {
186 my $ctx = $self->ctx;
187 my $e = $self->editor;
188 my $list = $self->cgi->param('list');
189 my $hold_id = $self->cgi->param('hold');
190 my $rec_id = $ctx->{page_args}->[0];
192 my (undef, @rec_data) = $self->get_records_and_facets([$rec_id]);
193 $ctx->{bre_id} = $rec_data[0]->{id};
194 $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
197 if ($list eq 'anon') {
198 $ctx->{added_to_anon} = 1;
200 $ctx->{added_to_list} = $e->retrieve_container_biblio_record_entry_bucket($list);
204 $ctx->{hold} = $e->retrieve_action_hold_request($hold_id);
208 return Apache2::Const::OK;
211 sub load_kpac_config {
213 my $ctx = $self->ctx;
216 my $path = $self->apache->dir_config('KPacConfigFile');
219 $self->apache->log->error("KPacConfigFile required!");
223 $kpac_config = XMLin(
226 ForceArray => ['layout', 'page', 'cell'],
231 my $ou = $ctx->{physical_loc} || $self->_get_search_lib;
234 # Search up the org tree to find the nearest config for the context org unit
235 while (my $org = $ctx->{get_aou}->($ou)) {
236 ($layout) = grep {$_->{owner} eq $org->id} @{$kpac_config->{layout}};
238 $ou = $org->parent_ou;
241 $ctx->{kpac_layout} = $layout;
242 $ctx->{kpac_config} = $kpac_config;
243 $ctx->{kpac_root} = $ctx->{base_path} . "/kpac";
244 $ctx->{home_page} = 'http://' . $self->apache->hostname . $ctx->{kpac_root} . "/home";
245 $ctx->{global_search_filter} = $kpac_config->{global_filter};