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_preview/%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 .= '&clear_cart=1' if $clear_cart;
219 my $sort = $self->cgi->param('sort') || $self->cgi->param('anonsort');
220 my $sort_dir = $self->cgi->param('sort_dir');
221 $url .= '&sort='.$sort if $sort;
222 $url .= '&sort_dir='.$sort_dir if $sort_dir;
223 $url .= '&is_list=1';
225 return $self->generic_redirect($url);
228 sub load_mylist_email {
231 my $cache_key = shift // $self->cgi->cookie((ref $self)->COOKIE_CART_CACHE);
234 return $self->generic_redirect;
238 "%s://%s%s/record/email_preview/%s",
240 $self->ctx->{hostname},
241 $self->ctx->{opac_root},
245 my $redirect = $self->cgi->param('redirect_to');
246 $url .= '?redirect_to=' . uri_escape_utf8($redirect);
247 my $clear_cart = $self->cgi->param('clear_cart');
248 $url .= '&clear_cart=1' if $clear_cart;
249 my $sort = $self->cgi->param('sort') || $self->cgi->param('anonsort');
250 my $sort_dir = $self->cgi->param('sort_dir');
251 $url .= '&sort='.$sort if $sort;
252 $url .= '&sort_dir='.$sort_dir if $sort_dir;
253 $url .= '&is_list=1';
255 return $self->generic_redirect($url);
258 sub _stash_record_list_in_anon_cache {
262 my $cache_key = $U->simplereq(
264 'open-ils.actor.anon_cache.set_value',
265 undef, (ref $self)->CART_CACHE_MYLIST, [ @rec_ids ]);
269 sub load_mylist_move {
271 my @rec_ids = $self->cgi->param('record');
272 my $action = $self->cgi->param('action') || '';
274 my ($cache_key, $list) = $self->fetch_mylist;
276 unless ((scalar(@rec_ids) > 0) ||
277 ($self->cgi->param('entire_list') && scalar(@$list) > 0)) {
278 my $url = $self->ctx->{referer};
279 $url .= ($url =~ /\?/ ? '&' : '?') . 'cart_none_selected=1';
280 return $self->generic_redirect($url);
283 if ($action eq 'place_hold') {
284 if ($self->cgi->param('entire_list')) {
287 return $self->load_myopac_bookbag_update('place_hold', undef, @rec_ids);
289 if ($action eq 'print') {
290 if ($self->cgi->param('entire_list')) {
293 my $temp_cache_key = $self->_stash_record_list_in_anon_cache(@rec_ids);
294 return $self->load_mylist_print($temp_cache_key);
296 if ($action eq 'email') {
297 if ($self->cgi->param('entire_list')) {
300 my $temp_cache_key = $self->_stash_record_list_in_anon_cache(@rec_ids);
301 return $self->load_mylist_email($temp_cache_key);
303 if ($action eq 'new_list') {
304 my $url = $self->apache->unparsed_uri;
305 $url =~ s!/mylist/move!/myopac/lists!;
306 return $self->generic_redirect($url);
309 return $self->mylist_action_redirect unless $cache_key;
312 foreach my $id (@$list) { push(@keep, $id) unless grep { $id eq $_ } @rec_ids; }
314 $cache_key = $U->simplereq(
316 'open-ils.actor.anon_cache.set_value',
317 $cache_key, (ref $self)->CART_CACHE_MYLIST, \@keep
320 if ($action eq 'delete' && scalar(@keep) == 0) {
321 my $url = $self->cgi->param('orig_referrer') // $self->ctx->{referer};
322 return $self->generic_redirect($url);
325 if ($self->ctx->{user} and $action =~ /^\d+$/) {
326 # in this case, action becomes list_id
327 $self->load_myopac_bookbag_update('add_rec', $self->cgi->param('action'));
328 # XXX TODO: test for failure of above
331 return $self->mylist_action_redirect($cache_key);
334 sub load_cache_clear {
336 $self->clear_anon_cache;
337 return $self->mylist_action_redirect;
340 # Wipes the entire anonymous cache, including My List
341 sub clear_anon_cache {
345 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_CART_CACHE) or return;
349 'open-ils.actor.anon_cache.delete_session', $cache_key)
354 # Called after an anon-cache / My List action occurs. Redirect
355 # to the redirect_url (cgi param) or referrer or home.
356 sub mylist_action_redirect {
358 my $cache_key = shift;
361 if( my $anchor = $self->cgi->param('anchor') ) {
362 # on the results page, we want to redirect
363 # back to record that was affected
364 $url = $self->cgi->param('redirect_to') // $self->ctx->{referer};
365 $url =~ s/#.*|$/#$anchor/;
367 $url = $self->cgi->param('redirect_to') // $self->ctx->{referer};
370 return $self->generic_redirect(
373 -name => (ref $self)->COOKIE_CART_CACHE,
375 -value => ($cache_key) ? $cache_key : '',
376 -expires => ($cache_key) ? undef : '-1h'
381 # called after an anon-cache / my list addition when we are configured
382 # to show a warning to the user.
384 sub mylist_warning_redirect {
386 my $cache_key = shift;
388 my $base_url = sprintf(
389 "%s://%s%s/temp_warn",
391 $self->ctx->{hostname},
392 $self->ctx->{opac_root}
395 my $redirect = $self->ctx->{referer};
396 if (my $anchor = $self->cgi->param('anchor')) {
397 $redirect =~ s/#.*|$/#$anchor/;
400 $base_url .= '?redirect_to=' . uri_escape_utf8($redirect);
402 return $self->generic_redirect(
405 -name => (ref $self)->COOKIE_CART_CACHE,
407 -value => ($cache_key) ? $cache_key : '',
408 -expires => ($cache_key) ? undef : '-1h'
415 (undef, $self->ctx->{mylist}, $self->ctx->{mylist_marc_xml}) =
416 $self->fetch_mylist(1);
418 # get list of bookbags in case user wants to move cart contents to
420 if ($self->ctx->{user}) {
421 $self->_load_lists_and_settings;
424 return Apache2::Const::OK;
427 sub load_temp_warn_post {
429 my $url = $self->cgi->param('redirect_to');
430 return $self->generic_redirect(
433 -name => 'no_temp_list_warn',
435 -value => ($self->cgi->param('no_temp_list_warn')) ? '1' : '',
436 -expires => ($self->cgi->param('no_temp_list_warn')) ? undef : '-1h'
443 $self->ctx->{'redirect_to'} = $self->cgi->param('redirect_to');
444 return Apache2::Const::OK;