]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGKPacLoader.pm
LP#1282783: Use patron hold notification defaults for KPAC
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGKPacLoader.pm
1 package OpenILS::WWW::EGKPacLoader;
2 use base 'OpenILS::WWW::EGCatLoader';
3 use strict; use warnings;
4 use XML::Simple;
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';
10 my $kpac_config;
11
12 # -----------------------------------------------------------------------------
13 # Override our parent's load() sub so we can do kpac-specific path routing.
14 # -----------------------------------------------------------------------------
15 sub load {
16     my $self = shift;
17
18     $self->init_ro_object_cache; 
19
20     my $stat = $self->load_common; 
21     return $stat unless $stat == Apache2::Const::OK;
22
23     $self->load_kpac_config;
24
25     my $path = $self->apache->path_info;
26     ($self->ctx->{page} = $path) =~ s#.*/(.*)#$1#g;
27
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|; 
32     return $self->load_library if $path =~ m|kpac/library|;
33
34     # ----------------------------------------------------------------
35     #  Everything below here requires SSL
36     # ----------------------------------------------------------------
37     return $self->redirect_ssl unless $self->cgi->https;
38
39     return $self->load_getit_results if $path =~ m|kpac/getit_results|;
40     return $self->load_getit if $path =~ m|kpac/getit|;
41
42     # ----------------------------------------------------------------
43     #  Everything below here requires authentication
44     # ----------------------------------------------------------------
45     return $self->redirect_auth unless $self->editor->requestor;
46
47     # AUTH pages
48
49     return Apache2::Const::OK;
50 }
51
52 sub load_kpac_rresults {
53     my $self = shift;
54
55     # The redirect-to-record-details-on-single-hit logic
56     # leverages the opac_root to determine the record detail
57     # page.  Replace it temporarily for our purposes.
58     my $tpac_root = $self->ctx->{opac_root};
59     $self->ctx->{opac_root} = $self->ctx->{kpac_root};
60
61     my $stat = $self->load_rresults;
62     $self->ctx->{opac_root} = $tpac_root;
63
64     return $stat;
65 }
66
67 sub load_getit {
68     my $self = shift;
69     my $ctx = $self->ctx;
70     my $rec_id = $ctx->{page_args}->[0];
71     my $bbag_id = $self->cgi->param('bookbag');
72     my $action = $self->cgi->param('action') || '';
73
74     # first load the record
75     my $stat = $self->load_record(no_search => 1);
76     return $stat unless $stat == Apache2::Const::OK;
77
78     $self->ctx->{page} = 'getit'; # repair the page
79
80     return $self->save_item_to_bookbag($rec_id, $bbag_id) if $action eq 'save';
81     return $self->login_and_place_hold($rec_id) if $action eq 'hold';
82
83     # if the user is logged in, fetch his bookbags
84     if ($ctx->{user}) {
85         $ctx->{bookbags} = $self->editor->search_container_biblio_record_entry_bucket([
86             {   owner => $ctx->{user}->id, 
87                 btype => 'bookbag' 
88             }, 
89             {   order_by => {cbreb => 'name'},
90                 limit => $self->cgi->param('bbag_limit') || 100 
91             }
92         ]);
93     }
94
95     # If user is logged in, get default hold pickup and notification info 
96     if ($ctx->{user}) {
97         my $set_map = $self->ctx->{user_setting_map};
98         if ($$set_map{'opac.default_pickup_location'}) {
99             $ctx->{default_pickup_lib} = $$set_map{'opac.default_pickup_location'};
100         }
101         if ($$set_map{'opac.default_phone'}) {
102             $ctx->{default_phone} = $$set_map{'opac.default_phone'};
103         }
104         if ($$set_map{'opac.hold_notify'}) {
105             $ctx->{notify_method} = $$set_map{'opac.hold_notify'};
106         }
107     }
108
109     $self->ctx->{page} = 'getit'; # repair the page
110     return Apache2::Const::OK;
111 }
112     
113 sub login_and_place_hold {
114     my $self = shift;
115     my $bre_id = shift;
116     my $ctx = $self->ctx;
117     my $username = $self->cgi->param('username');
118     my $password = $self->cgi->param('password');
119     my $pickup_lib = $self->cgi->param('pickup_lib');
120
121     return Apache2::Const::HTTP_BAD_REQUEST 
122         unless $pickup_lib =~ /^\d+$/;
123
124     #If a pickup library hasn't been selected, reload page
125     if ($pickup_lib == '0') {
126         return $self->load_login;
127     }
128
129     my $new_uri = $self->apache->unparsed_uri;
130     my $sep = ($new_uri =~ /\?/) ? '&' : '?';
131
132     if (!$ctx->{user}) {
133         # First, log the user in and return to 
134         $self->apache->log->info("kpac: logging in $username");
135
136         # TODO: let user know username/password is required..
137         return Apache2::Const::OK unless $username and $password;
138
139         $new_uri .= "${sep}pickup_lib=$pickup_lib&action=hold";
140         $self->cgi->param('redirect_to', $new_uri);
141         return $self->load_login;
142
143     } else {
144
145         $self->apache->log->info("kpac: placing hold for $bre_id");
146
147         $new_uri =~ s/getit/getit_results/g;
148         $self->cgi->param('hold_target', $bre_id);
149         $self->cgi->param('hold_type', 'T');
150         $self->cgi->param('part', ''); # needed even if unused
151
152         my $stat = $self->load_place_hold;
153
154         $self->apache->log->info("kpac: place hold returned $stat");
155
156         return $stat unless $stat == Apache2::Const::OK;
157
158         my $hdata = $ctx->{hold_data}->[0]; # only 1 hold placed
159         if (my $hold_id = $hdata ? $hdata->{hold_success} : undef) {
160
161             $self->apache->log->info("kpac: place hold succeeded");
162             $new_uri .= "${sep}hold=$hold_id";
163
164         } else {
165             $self->apache->log->info("kpac: place hold failed : " . $ctx->{hold_failed_event});
166             $new_uri .= "${sep}hold_failed=1";
167         }
168     }
169
170     $self->apache->log->info("kpac: place hold redirecting to: $new_uri");
171     return $self->generic_redirect($new_uri);
172 }
173
174 sub save_item_to_bookbag {
175     my $self = shift;
176     my $rec_id = shift;
177     my $bookbag_id = shift;
178
179     if ($bookbag_id) { 
180         # save to existing bookbag
181         $self->cgi->param('record', $rec_id);
182         my $stat = $self->load_myopac_bookbag_update('add_rec', $bookbag_id);
183         # TODO: check for failure
184         (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
185         $new_uri .= ($new_uri =~ /\?/) ? "&list=$bookbag_id" : "?list=$bookbag_id";
186         return $self->generic_redirect($new_uri);
187
188     } else { 
189         # save to anonymous list
190        
191         # set some params assumed to exist for load_mylist_add
192         $self->cgi->param('record', $rec_id);
193         (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
194         $new_uri .= ($new_uri =~ /\?/) ? '&list=anon' : '?list=anon';
195         $self->cgi->param('redirect_to', $new_uri);
196
197         return $self->load_mylist_add;
198     }
199
200     return Apache2::Const::HTTP_BAD_REQUEST;
201 }
202
203
204 sub load_getit_results {
205     my $self = shift;
206     my $ctx = $self->ctx;
207     my $e = $self->editor;
208     my $list = $self->cgi->param('list');
209     my $hold_id = $self->cgi->param('hold');
210     my $rec_id = $ctx->{page_args}->[0];
211
212     my (undef, @rec_data) = $self->get_records_and_facets([$rec_id]);
213     $ctx->{bre_id} = $rec_data[0]->{id};
214     $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
215
216     if ($list) {
217         if ($list eq 'anon') {
218             $ctx->{added_to_anon} = 1;
219         } else {
220             $ctx->{added_to_list} = $e->retrieve_container_biblio_record_entry_bucket($list);
221         }
222     } else { 
223         $e->xact_begin;
224         $ctx->{hold} = $e->retrieve_action_hold_request($hold_id);
225         $e->xact_rollback;
226     }
227
228     return Apache2::Const::OK;
229 }
230
231 sub load_kpac_config {
232     my $self = shift;
233     my $ctx = $self->ctx;
234
235     if (!$kpac_config) {
236         my $path = $self->apache->dir_config('KPacConfigFile');
237
238         if (!$path) {
239             $self->apache->log->error("KPacConfigFile required!");
240             return;
241         }
242         
243         $kpac_config = XMLin(
244             $path,
245             KeyAttr => ['id'],
246             ForceArray => ['layout', 'page', 'cell'],
247             NormaliseSpace => 2
248         );
249     }
250
251     my $ou = $ctx->{physical_loc} || $self->_get_search_lib;
252     my $layout;
253
254     # Search up the org tree to find the nearest config for the context org unit
255     while (my $org = $ctx->{get_aou}->($ou)) {
256         ($layout) = grep {$_->{owner} eq $org->id} @{$kpac_config->{layout}};
257         last if $layout;
258         $ou = $org->parent_ou;
259     }
260
261     $ctx->{kpac_layout} = $layout;
262     $ctx->{kpac_config} = $kpac_config;
263     $ctx->{kpac_root} = $ctx->{base_path} . "/kpac"; 
264     $ctx->{home_page} = $ctx->{proto} . '://' . $ctx->{hostname} . $ctx->{kpac_root} . "/home";
265     $ctx->{global_search_filter} = $kpac_config->{global_filter};
266 }
267
268
269 1;