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) = @_;
16 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE);
20 $list = $U->simplereq(
22 'open-ils.actor.anon_cache.get_value',
23 $cache_key, (ref $self)->ANON_CACHE_MYLIST);
33 $marc_xml = $self->fetch_marc_xml_by_id($list);
36 # Leverage QueryParser to sort the items by values of config.metabib_fields
37 # from the items' marc records.
39 my ($sorter, $modifier) = $self->_get_bookbag_sort_params("anonsort");
40 my $query = $self->_prepare_anonlist_sorting_query($list, $sorter, $modifier);
41 $list = $U->bib_record_list_via_search($query) or
42 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
45 return ($cache_key, $list, $marc_xml);
49 # Adds a record (by id) to My List, creating a new anon cache + list if necessary.
52 my $rec_id = $self->cgi->param('record');
54 my ($cache_key, $list) = $self->fetch_mylist;
55 push(@$list, $rec_id);
57 $cache_key = $U->simplereq(
59 'open-ils.actor.anon_cache.set_value',
60 $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
62 return $self->mylist_action_redirect($cache_key);
65 sub load_mylist_delete {
67 my $rec_id = $self->cgi->param('record');
69 my ($cache_key, $list) = $self->fetch_mylist;
70 $list = [ grep { $_ ne $rec_id } @$list ];
72 $cache_key = $U->simplereq(
74 'open-ils.actor.anon_cache.set_value',
75 $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
77 return $self->mylist_action_redirect($cache_key);
80 sub load_mylist_move {
82 my @rec_ids = $self->cgi->param('record');
83 my $action = $self->cgi->param('action') || '';
85 return $self->load_myopac_bookbag_update('place_hold', undef, @rec_ids)
86 if $action eq 'place_hold';
88 my ($cache_key, $list) = $self->fetch_mylist;
89 return $self->mylist_action_redirect unless $cache_key;
92 foreach my $id (@$list) { push(@keep, $id) unless grep { $id eq $_ } @rec_ids; }
94 $cache_key = $U->simplereq(
96 'open-ils.actor.anon_cache.set_value',
97 $cache_key, (ref $self)->ANON_CACHE_MYLIST, \@keep
100 if ($self->ctx->{user} and $action =~ /^\d+$/) {
101 # in this case, action becomes list_id
102 $self->load_myopac_bookbag_update('add_rec', $self->cgi->param('action'));
103 # XXX TODO: test for failure of above
106 return $self->mylist_action_redirect($cache_key);
109 sub load_cache_clear {
111 $self->clear_anon_cache;
112 return $self->mylist_action_redirect;
115 # Wipes the entire anonymous cache, including My List
116 sub clear_anon_cache {
120 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE) or return;
124 'open-ils.actor.anon_cache.delete_session', $cache_key)
129 # Called after an anon-cache / My List action occurs. Redirect
130 # to the redirect_url (cgi param) or referrer or home.
131 sub mylist_action_redirect {
133 my $cache_key = shift;
136 if( my $anchor = $self->cgi->param('anchor') ) {
137 # on the results page, we want to redirect
138 # back to record that was affected
139 $url = $self->ctx->{referer};
140 $url =~ s/#.*|$/#$anchor/;
143 return $self->generic_redirect(
146 -name => (ref $self)->COOKIE_ANON_CACHE,
148 -value => ($cache_key) ? $cache_key : '',
149 -expires => ($cache_key) ? undef : '-1h'
156 (undef, $self->ctx->{mylist}, $self->ctx->{mylist_marc_xml}) =
157 $self->fetch_mylist(1);
159 return Apache2::Const::OK;