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_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;
54 my $rec_id = $ctx->{page_args}->[0];
55 my $bbag_id = $self->cgi->param('bookbag');
56 my $action = $self->cgi->param('action') || '';
58 # first load the record
59 my $stat = $self->load_record(no_search => 1);
60 return $stat unless $stat == Apache2::Const::OK;
62 $self->ctx->{page} = 'getit'; # repair the page
64 return $self->save_item_to_bookbag($rec_id, $bbag_id) if $action eq 'save';
65 return $self->login_and_place_hold($rec_id) if $action eq 'hold';
67 # if the user is logged in, fetch his bookbags
69 $ctx->{bookbags} = $self->editor->search_container_biblio_record_entry_bucket([
70 { owner => $ctx->{user}->id,
73 { order_by => {cbreb => 'name'},
74 limit => $self->cgi->param('bbag_limit') || 100
79 $self->ctx->{page} = 'getit'; # repair the page
80 return Apache2::Const::OK;
83 sub login_and_place_hold {
87 my $username = $self->cgi->param('username');
88 my $password = $self->cgi->param('password');
89 my $pickup_lib = $self->cgi->param('pickup_lib');
92 # First, log the user in and return to
93 # TODO: let user know username/password is required..
94 return Apache2::Const::OK unless $username and $password;
95 my $new_uri = $self->apache->unparsed_uri;
96 my $sep = ($new_uri =~ /\?/) ? '&' : '?';
97 $new_uri .= "${sep}pickup_lib=$pickup_lib&action=hold";
98 $self->cgi->param('redirect_to', $new_uri);
99 return $self->load_login;
105 # (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
106 # $new_uri .= ($new_uri =~ /\?/) ? "&hold=$hold_id" : "?hold=$hold_id";
107 # return $self->generic_redirect($new_uri);
109 return Apache2::Const::OK;
112 sub save_item_to_bookbag {
115 my $bookbag_id = shift;
118 # save to existing bookbag
119 $self->cgi->param('record', $rec_id);
120 my $stat = $self->load_myopac_bookbag_update('add_rec', $bookbag_id);
121 # TODO: check for failure
122 (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
123 $new_uri .= ($new_uri =~ /\?/) ? "&list=$bookbag_id" : "?list=$bookbag_id";
124 return $self->generic_redirect($new_uri);
127 # save to anonymous list
129 # set some params assumed to exist for load_mylist_add
130 $self->cgi->param('record', $rec_id);
131 (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
132 $new_uri .= ($new_uri =~ /\?/) ? '&list=anon' : '?list=anon';
133 $self->cgi->param('redirect_to', $new_uri);
135 return $self->load_mylist_add;
138 return Apache2::Const::HTTP_BAD_REQUEST;
142 sub load_getit_results {
144 my $ctx = $self->ctx;
145 my $e = $self->editor;
146 my $list = $self->cgi->param('list');
147 my $hold_id = $self->cgi->param('hold');
148 my $rec_id = $ctx->{page_args}->[0];
150 my (undef, @rec_data) = $self->get_records_and_facets([$rec_id]);
151 $ctx->{bre_id} = $rec_data[0]->{id};
152 $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
155 if ($list eq 'anon') {
156 $ctx->{anon_list} = 1;
158 $ctx->{list} = $e->retrieve_container_biblio_record_entry_bucket($list);
163 # new hold means potential for replication lag
169 return Apache2::Const::OK;
172 sub load_kpac_config {
176 my $path = $self->apache->dir_config('KPacConfigFile');
179 $self->apache->log->error("KPacConfigFile required!");
183 $kpac_config = XMLin(
186 ForceArray => ['layout', 'page', 'cell'],
191 my $ou = $self->ctx->{physical_loc} || $self->_get_search_lib;
194 # Search up the org tree to find the nearest config for the context org unit
195 while (my $org = $self->ctx->{get_aou}->($ou)) {
196 ($layout) = grep {$_->{owner} eq $org->id} @{$kpac_config->{layout}};
198 $ou = $org->parent_ou;
201 $self->ctx->{kpac_layout} = $layout;
202 $self->ctx->{kpac_config} = $kpac_config;
203 $self->ctx->{kpac_root} = $self->ctx->{base_path} . "/kpac";