]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Container.pm
a80549fab2c3db70eaf02cbf14831970fc8ffd0e
[Evergreen.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 # Retrieve the users cached records AKA 'My List'
11 # Returns an empty list if there are no cached records
12 sub fetch_mylist {
13     my ($self, $with_marc_xml) = @_;
14
15     my $list = [];
16     my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE);
17
18     if($cache_key) {
19
20         $list = $U->simplereq(
21             'open-ils.actor',
22             'open-ils.actor.anon_cache.get_value', 
23             $cache_key, (ref $self)->ANON_CACHE_MYLIST);
24
25         if(!$list) {
26             $cache_key = undef;
27             $list = [];
28         }
29     }
30
31     my $marc_xml;
32     if ($with_marc_xml) {
33         $marc_xml = $self->fetch_marc_xml_by_id($list);
34     }
35
36     return ($cache_key, $list, $marc_xml);
37 }
38
39
40 # Adds a record (by id) to My List, creating a new anon cache + list if necessary.
41 sub load_mylist_add {
42     my $self = shift;
43     my $rec_id = $self->cgi->param('record');
44
45     my ($cache_key, $list) = $self->fetch_mylist;
46     push(@$list, $rec_id);
47
48     $cache_key = $U->simplereq(
49         'open-ils.actor',
50         'open-ils.actor.anon_cache.set_value', 
51         $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
52
53     return $self->mylist_action_redirect($cache_key);
54 }
55
56 sub load_mylist_delete {
57     my $self = shift;
58     my $rec_id = $self->cgi->param('record');
59
60     my ($cache_key, $list) = $self->fetch_mylist;
61     $list = [ grep { $_ ne $rec_id } @$list ];
62
63     $cache_key = $U->simplereq(
64         'open-ils.actor',
65         'open-ils.actor.anon_cache.set_value', 
66         $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
67
68     return $self->mylist_action_redirect($cache_key);
69 }
70
71 sub load_mylist_move {
72     my $self = shift;
73     my @rec_ids = $self->cgi->param('record');
74     my $action = $self->cgi->param('action') || '';
75
76     return $self->load_myopac_bookbag_update('place_hold', undef, @rec_ids)
77         if $action eq 'place_hold';
78
79     my ($cache_key, $list) = $self->fetch_mylist;
80     return $self->mylist_action_redirect unless $cache_key;
81
82     my @keep;
83     foreach my $id (@$list) { push(@keep, $id) unless grep { $id eq $_ } @rec_ids; }
84
85     $cache_key = $U->simplereq(
86         'open-ils.actor',
87         'open-ils.actor.anon_cache.set_value', 
88         $cache_key, (ref $self)->ANON_CACHE_MYLIST, \@keep
89     );
90
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
95     }
96
97     return $self->mylist_action_redirect($cache_key);
98 }
99
100 sub load_cache_clear {
101     my $self = shift;
102     $self->clear_anon_cache;
103     return $self->mylist_action_redirect;
104 }
105
106 # Wipes the entire anonymous cache, including My List
107 sub clear_anon_cache {
108     my $self = shift;
109     my $field = shift;
110
111     my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE) or return;
112
113     $U->simplereq(
114         'open-ils.actor',
115         'open-ils.actor.anon_cache.delete_session', $cache_key)
116         if $cache_key;
117
118 }
119
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 {
123     my $self = shift;
124     my $cache_key = shift;
125
126     my $url;
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;
132     } 
133
134     return $self->generic_redirect(
135         $url,
136         $self->cgi->cookie(
137             -name => (ref $self)->COOKIE_ANON_CACHE,
138             -path => '/',
139             -value => ($cache_key) ? $cache_key : '',
140             -expires => ($cache_key) ? undef : '-1h'
141         )
142     );
143 }
144
145 sub load_mylist {
146     my ($self) = shift;
147     (undef, $self->ctx->{mylist}, $self->ctx->{mylist_marc_xml}) =
148         $self->fetch_mylist(1);
149
150     return Apache2::Const::OK;
151 }
152
153 1;