1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
4 use OpenSRF::Utils::Logger qw/$logger/;
5 use OpenILS::Utils::CStoreEditor qw/:funcs/;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Application::AppUtils;
8 my $U = 'OpenILS::Application::AppUtils';
10 # Retrieve the users cached records AKA 'My List'
11 # Returns an empty list if there are no cached records
13 my ($self, $with_marc_xml, $skip_sort) = @_;
16 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_CART_CACHE);
20 $list = $U->simplereq(
22 'open-ils.actor.anon_cache.get_value',
23 $cache_key, (ref $self)->CART_CACHE_MYLIST);
32 no warnings qw/numeric/;
33 $list = [map { int $_ } @$list];
34 $list = [grep { $_ > 0} @$list];
41 # capture pref_ou for callnumber filter/display
42 $ctx->{pref_ou} = $self->_get_pref_lib() || $ctx->{search_ou};
44 # search for local callnumbers for display
45 my $focus_ou = $ctx->{physical_loc} || $ctx->{pref_ou};
47 my (undef, @recs) = $self->get_records_and_facets(
49 flesh => '{mra,holdings_xml,acp,exclude_invisible_acn}',
51 site => $ctx->{get_aou}->($focus_ou)->shortname,
52 pref_lib => $ctx->{pref_ou}
56 # make it look like the caller is expecting
57 $marc_xml = { map {$_->{id} => $_->{marc_xml}} @recs };
60 # Leverage QueryParser to sort the items by values of config.metabib_fields
61 # from the items' marc records.
62 if (@$list && !$skip_sort) {
63 my ($sorter, $modifier) = $self->_get_bookbag_sort_params("anonsort");
64 my $query = $self->_prepare_anonlist_sorting_query($list, $sorter, $modifier);
69 $list = $U->bib_record_list_via_search($query, $args) or
70 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
73 return ($cache_key, $list, $marc_xml);
76 sub load_api_mylist_retrieve {
79 # this has the effect of instantiating an empty one if need be
80 my ($cache_key, $list) = $self->fetch_mylist(0, 1);
82 $self->ctx->{json_response} = {
83 mylist => [ map { 0 + $_ } @$list ], # force integers
85 $self->ctx->{json_reponse_cookie} =
87 -name => (ref $self)->COOKIE_CART_CACHE,
89 -value => ($cache_key) ? $cache_key : '',
90 -expires => ($cache_key) ? undef : '-1h'
93 return Apache2::Const::OK;
96 sub load_api_mylist_clear {
99 $self->clear_anon_cache;
101 # and return fresh, empty cart
102 return $self->load_api_mylist_retrieve();
105 # Adds a record (by id) to My List, creating a new anon cache + list if necessary.
106 sub load_mylist_add {
109 my ($cache_key, $list) = $self->_do_mylist_add();
111 # Check if we need to warn patron about adding to a "temporary"
113 if ($self->check_for_temp_list_warning) {
114 return $self->mylist_warning_redirect($cache_key);
117 return $self->mylist_action_redirect($cache_key);
120 sub load_api_mylist_add {
123 my ($cache_key, $list) = $self->_do_mylist_add();
125 $self->ctx->{json_response} = {
126 mylist => [ map { 0 + $_ } @$list ], # force integers
128 $self->ctx->{json_reponse_cookie} =
130 -name => (ref $self)->COOKIE_CART_CACHE,
132 -value => ($cache_key) ? $cache_key : '',
133 -expires => ($cache_key) ? undef : '-1h'
136 return Apache2::Const::OK;
141 my @rec_ids = $self->cgi->param('record');
143 my ($cache_key, $list) = $self->fetch_mylist(0, 1);
144 push(@$list, @rec_ids);
146 $cache_key = $U->simplereq(
148 'open-ils.actor.anon_cache.set_value',
149 $cache_key, (ref $self)->CART_CACHE_MYLIST, $list);
151 return ($cache_key, $list);
154 sub load_mylist_delete {
157 my ($cache_key, $list) = $self->_do_mylist_delete;
159 return $self->mylist_action_redirect($cache_key);
162 sub load_api_mylist_delete {
165 my ($cache_key, $list) = $self->_do_mylist_delete();
167 $self->ctx->{json_response} = {
168 mylist => [ map { 0 + $_ } @$list ], # force integers
170 $self->ctx->{json_reponse_cookie} =
172 -name => (ref $self)->COOKIE_CART_CACHE,
174 -value => ($cache_key) ? $cache_key : '',
175 -expires => ($cache_key) ? undef : '-1h'
178 return Apache2::Const::OK;
181 sub _do_mylist_delete {
183 my @rec_ids = $self->cgi->param('record');
185 my ($cache_key, $list) = $self->fetch_mylist(0, 1);
186 foreach my $rec_id (@rec_ids) {
187 $list = [ grep { $_ ne $rec_id } @$list ];
190 $cache_key = $U->simplereq(
192 'open-ils.actor.anon_cache.set_value',
193 $cache_key, (ref $self)->CART_CACHE_MYLIST, $list);
195 return ($cache_key, $list);
198 sub load_mylist_print {
201 my $cache_key = shift // $self->cgi->cookie((ref $self)->COOKIE_CART_CACHE);
204 return $self->generic_redirect;
208 "%s://%s%s/record/print/%s",
210 $self->ctx->{hostname},
211 $self->ctx->{opac_root},
215 my $redirect = $self->cgi->param('redirect_to');
216 $url .= '?redirect_to=' . uri_escape_utf8($redirect);
217 my $clear_cart = $self->cgi->param('clear_cart');
218 $url .= '&is_list=1';
219 $url .= '&clear_cart=1' if $clear_cart;
221 return $self->generic_redirect($url);
224 sub load_mylist_email {
227 my $cache_key = shift // $self->cgi->cookie((ref $self)->COOKIE_CART_CACHE);
230 return $self->generic_redirect;
234 "%s://%s%s/record/email/%s",
236 $self->ctx->{hostname},
237 $self->ctx->{opac_root},
241 my $redirect = $self->cgi->param('redirect_to');
242 $url .= '?redirect_to=' . uri_escape_utf8($redirect);
243 my $clear_cart = $self->cgi->param('clear_cart');
244 $url .= '&is_list=1';
245 $url .= '&clear_cart=1' if $clear_cart;
247 return $self->generic_redirect($url);
250 sub _stash_record_list_in_anon_cache {
254 my $cache_key = $U->simplereq(
256 'open-ils.actor.anon_cache.set_value',
257 undef, (ref $self)->CART_CACHE_MYLIST, [ @rec_ids ]);
261 sub load_mylist_move {
263 my @rec_ids = $self->cgi->param('record');
264 my $action = $self->cgi->param('action') || '';
266 my ($cache_key, $list) = $self->fetch_mylist;
268 unless ((scalar(@rec_ids) > 0) ||
269 ($self->cgi->param('entire_list') && scalar(@$list) > 0)) {
270 my $url = $self->ctx->{referer};
271 $url .= ($url =~ /\?/ ? '&' : '?') . 'cart_none_selected=1';
272 return $self->generic_redirect($url);
275 if ($action eq 'place_hold') {
276 if ($self->cgi->param('entire_list')) {
279 return $self->load_myopac_bookbag_update('place_hold', undef, @rec_ids);
281 if ($action eq 'print') {
282 my $temp_cache_key = $self->_stash_record_list_in_anon_cache(@rec_ids);
283 return $self->load_mylist_print($temp_cache_key);
285 if ($action eq 'email') {
286 my $temp_cache_key = $self->_stash_record_list_in_anon_cache(@rec_ids);
287 return $self->load_mylist_email($temp_cache_key);
289 if ($action eq 'new_list') {
290 my $url = $self->apache->unparsed_uri;
291 $url =~ s!/mylist/move!/myopac/lists!;
292 return $self->generic_redirect($url);
295 return $self->mylist_action_redirect unless $cache_key;
298 foreach my $id (@$list) { push(@keep, $id) unless grep { $id eq $_ } @rec_ids; }
300 $cache_key = $U->simplereq(
302 'open-ils.actor.anon_cache.set_value',
303 $cache_key, (ref $self)->CART_CACHE_MYLIST, \@keep
306 if ($action eq 'delete' && scalar(@keep) == 0) {
307 my $url = $self->cgi->param('orig_referrer') // $self->ctx->{referer};
308 return $self->generic_redirect($url);
311 if ($self->ctx->{user} and $action =~ /^\d+$/) {
312 # in this case, action becomes list_id
313 $self->load_myopac_bookbag_update('add_rec', $self->cgi->param('action'));
314 # XXX TODO: test for failure of above
317 return $self->mylist_action_redirect($cache_key);
320 sub load_cache_clear {
322 $self->clear_anon_cache;
323 return $self->mylist_action_redirect;
326 # Wipes the entire anonymous cache, including My List
327 sub clear_anon_cache {
331 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_CART_CACHE) or return;
335 'open-ils.actor.anon_cache.delete_session', $cache_key)
340 # Called after an anon-cache / My List action occurs. Redirect
341 # to the redirect_url (cgi param) or referrer or home.
342 sub mylist_action_redirect {
344 my $cache_key = shift;
347 if( my $anchor = $self->cgi->param('anchor') ) {
348 # on the results page, we want to redirect
349 # back to record that was affected
350 $url = $self->cgi->param('redirect_to') // $self->ctx->{referer};
351 $url =~ s/#.*|$/#$anchor/;
353 $url = $self->cgi->param('redirect_to') // $self->ctx->{referer};
356 return $self->generic_redirect(
359 -name => (ref $self)->COOKIE_CART_CACHE,
361 -value => ($cache_key) ? $cache_key : '',
362 -expires => ($cache_key) ? undef : '-1h'
367 # called after an anon-cache / my list addition when we are configured
368 # to show a warning to the user.
370 sub mylist_warning_redirect {
372 my $cache_key = shift;
374 my $base_url = sprintf(
375 "%s://%s%s/temp_warn",
377 $self->ctx->{hostname},
378 $self->ctx->{opac_root}
381 my $redirect = $self->ctx->{referer};
382 if (my $anchor = $self->cgi->param('anchor')) {
383 $redirect =~ s/#.*|$/#$anchor/;
386 $base_url .= '?redirect_to=' . uri_escape_utf8($redirect);
388 return $self->generic_redirect(
391 -name => (ref $self)->COOKIE_CART_CACHE,
393 -value => ($cache_key) ? $cache_key : '',
394 -expires => ($cache_key) ? undef : '-1h'
401 (undef, $self->ctx->{mylist}, $self->ctx->{mylist_marc_xml}) =
402 $self->fetch_mylist(1);
404 # get list of bookbags in case user wants to move cart contents to
406 if ($self->ctx->{user}) {
407 $self->_load_lists_and_settings;
410 return Apache2::Const::OK;
413 sub load_temp_warn_post {
415 my $url = $self->cgi->param('redirect_to');
416 return $self->generic_redirect(
419 -name => 'no_temp_list_warn',
421 -value => ($self->cgi->param('no_temp_list_warn')) ? '1' : '',
422 -expires => ($self->cgi->param('no_temp_list_warn')) ? undef : '-1h'
429 $self->ctx->{'redirect_to'} = $self->cgi->param('redirect_to');
430 return Apache2::Const::OK;