Foundational work for temporary/anon lists and per-user lists (bookbags)
[evergreen-equinox.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Container.pm
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';
9
10 use constant COOKIE_ANON_CACHE => 'anoncache';
11 use constant ANON_CACHE_MYLIST => 'mylist';
12
13 # Retrieve the users cached records AKA 'My List'
14 # Returns an empty list if there are no cached records
15 sub fetch_mylist {
16     my ($self, $with_marc_xml) = @_;
17
18     my $list = [];
19     my $cache_key = $self->cgi->cookie(COOKIE_ANON_CACHE);
20
21     if($cache_key) {
22
23         $list = $U->simplereq(
24             'open-ils.actor',
25             'open-ils.actor.anon_cache.get_value', 
26             $cache_key, ANON_CACHE_MYLIST);
27
28         if(!$list) {
29             $cache_key = undef;
30             $list = [];
31         }
32     }
33
34     $self->apache->log->info("Found anon-cache list [@$list]");
35     my $marc_xml;
36     if ($with_marc_xml) {
37         $marc_xml = $self->fetch_marc_xml_by_id($list);
38     }
39
40     return ($cache_key, $list, $marc_xml);
41 }
42
43
44 # Adds a record (by id) to My List, creating a new anon cache + list if necessary.
45 sub load_mylist_add {
46     my $self = shift;
47     my $rec_id = $self->cgi->param('record');
48
49     my ($cache_key, $list) = $self->fetch_mylist;
50     push(@$list, $rec_id);
51
52     $cache_key = $U->simplereq(
53         'open-ils.actor',
54         'open-ils.actor.anon_cache.set_value', 
55         $cache_key, ANON_CACHE_MYLIST, $list);
56
57     return $self->mylist_action_redirect($cache_key);
58 }
59
60 # Removes a record ID from My List
61 sub load_mylist_del {
62     my $self = shift;
63     my $rec_id = $self->cgi->param('record');
64
65     my ($cache_key, $list) = $self->fetch_mylist;
66     return $self->mylist_action_redirect unless $cache_key;
67
68     $list = [ grep { $_ ne $rec_id } @$list ];
69
70     $cache_key = $U->simplereq(
71         'open-ils.actor',
72         'open-ils.actor.anon_cache.set_value', 
73         $cache_key, ANON_CACHE_MYLIST, $list);
74
75     return $self->mylist_action_redirect($cache_key);
76 }
77
78 sub load_cache_clear {
79     my $self = shift;
80     $self->clear_anon_cache;
81     return $self->mylist_action_redirect;
82 }
83
84 # Wipes the entire anonymous cache, including My List
85 sub clear_anon_cache {
86     my $self = shift;
87     my $field = shift;
88
89     my $cache_key = $self->cgi->cookie(COOKIE_ANON_CACHE) or return;
90
91     $U->simplereq(
92         'open-ils.actor',
93         'open-ils.actor.anon_cache.delete_session', $cache_key)
94         if $cache_key;
95
96 }
97
98 # Called after an anon-cache / My List action occurs.  Redirect
99 # to the redirect_url (cgi param) or referrer or home.
100 sub mylist_action_redirect {
101     my $self = shift;
102     my $cache_key = shift;
103
104     return $self->generic_redirect(
105         undef, 
106         $self->cgi->cookie(
107             -name => COOKIE_ANON_CACHE,
108             -path => '/',
109             -value => ($cache_key) ? $cache_key : '',
110             -expires => ($cache_key) ? undef : '-1h'
111         )
112     );
113 }
114
115 sub load_mylist {
116     my ($self) = shift;
117     (undef, $self->ctx->{mylist}, $self->ctx->{mylist_marc_xml}) =
118         $self->fetch_mylist(1);
119
120     return Apache2::Const::OK;
121 }
122
123 1;