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 return ($cache_key, $list, $marc_xml);
40 # Adds a record (by id) to My List, creating a new anon cache + list if necessary.
43 my $rec_id = $self->cgi->param('record');
45 my ($cache_key, $list) = $self->fetch_mylist;
46 push(@$list, $rec_id);
48 $cache_key = $U->simplereq(
50 'open-ils.actor.anon_cache.set_value',
51 $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
53 return $self->mylist_action_redirect($cache_key);
56 sub load_mylist_delete {
58 my $rec_id = $self->cgi->param('record');
60 my ($cache_key, $list) = $self->fetch_mylist;
61 $list = [ grep { $_ ne $rec_id } @$list ];
63 $cache_key = $U->simplereq(
65 'open-ils.actor.anon_cache.set_value',
66 $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
68 return $self->mylist_action_redirect($cache_key);
71 sub load_mylist_move {
73 my @rec_ids = $self->cgi->param('record');
74 my $action = $self->cgi->param('action') || '';
76 return $self->load_myopac_bookbag_update('place_hold', undef, @rec_ids)
77 if $action eq 'place_hold';
79 my ($cache_key, $list) = $self->fetch_mylist;
80 return $self->mylist_action_redirect unless $cache_key;
83 foreach my $id (@$list) { push(@keep, $id) unless grep { $id eq $_ } @rec_ids; }
85 $cache_key = $U->simplereq(
87 'open-ils.actor.anon_cache.set_value',
88 $cache_key, (ref $self)->ANON_CACHE_MYLIST, \@keep
91 if ($self->ctx->{user} and $action =~ /^\d+$/) {
92 # in this case, action becomes list_id
93 $self->load_myopac_bookbag_update('add_rec', $self->cgi->param('action'));
94 # XXX TODO: test for failure of above
97 return $self->mylist_action_redirect($cache_key);
100 sub load_cache_clear {
102 $self->clear_anon_cache;
103 return $self->mylist_action_redirect;
106 # Wipes the entire anonymous cache, including My List
107 sub clear_anon_cache {
111 my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE) or return;
115 'open-ils.actor.anon_cache.delete_session', $cache_key)
120 # Called after an anon-cache / My List action occurs. Redirect
121 # to the redirect_url (cgi param) or referrer or home.
122 sub mylist_action_redirect {
124 my $cache_key = shift;
127 if( my $anchor = $self->cgi->param('anchor') ) {
128 # on the results page, we want to redirect
129 # back to record that was affected
130 $url = $self->ctx->{referer};
131 $url =~ s/#.*|$/#$anchor/g;
134 return $self->generic_redirect(
137 -name => (ref $self)->COOKIE_ANON_CACHE,
139 -value => ($cache_key) ? $cache_key : '',
140 -expires => ($cache_key) ? undef : '-1h'
147 (undef, $self->ctx->{mylist}, $self->ctx->{mylist_marc_xml}) =
148 $self->fetch_mylist(1);
150 return Apache2::Const::OK;