initial cut of anonymoust cache record lists (aka My List) management; minor cleanup
authorberick <berick@esilibrary.com>
Fri, 18 Feb 2011 19:58:57 +0000 (14:58 -0500)
committerberick <berick@esilibrary.com>
Fri, 18 Feb 2011 19:58:57 +0000 (14:58 -0500)
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Container.pm [new file with mode: 0644]
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm

index f1e6d48..40d867b 100644 (file)
@@ -20,9 +20,12 @@ use OpenILS::WWW::EGCatLoader::Util;
 use OpenILS::WWW::EGCatLoader::Account;
 use OpenILS::WWW::EGCatLoader::Search;
 use OpenILS::WWW::EGCatLoader::Record;
+use OpenILS::WWW::EGCatLoader::Container;
 
 my $U = 'OpenILS::Application::AppUtils';
 
+use constant COOKIE_SES => 'ses';
+
 sub new {
     my($class, $apache, $ctx) = @_;
 
@@ -85,6 +88,9 @@ sub load {
     return $self->load_simple("advanced") if $path =~ /opac\/advanced/;
     return $self->load_rresults if $path =~ /opac\/results/;
     return $self->load_record if $path =~ /opac\/record/;
+    return $self->load_mylist_add if $path =~ /opac\/mylist\/add/;
+    return $self->load_mylist_del if $path =~ /opac\/mylist\/del/;
+    return $self->load_cache_clear if $path =~ /opac\/cache\/clear/;
 
     # ----------------------------------------------------------------
     # Logout and login require SSL
@@ -168,7 +174,7 @@ sub load_common {
     $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
     $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
 
-    if($e->authtoken($self->cgi->cookie('ses'))) {
+    if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
 
         if($e->checkauth) {
 
@@ -246,7 +252,7 @@ sub load_login {
         $cgi->redirect(
             -url => $cgi->param('redirect_to') || $acct,
             -cookie => $cgi->cookie(
-                -name => 'ses',
+                -name => COOKIE_SES,
                 -path => '/',
                 -secure => 1,
                 -value => $response->{payload}->{authtoken},
@@ -264,11 +270,15 @@ sub load_login {
 sub load_logout {
     my $self = shift;
 
+    # If the user was adding anyting to an anonymous cache 
+    # while logged in, go ahead and clear it out.
+    $self->clear_anon_cache;
+
     $self->apache->print(
         $self->cgi->redirect(
             -url => $self->ctx->{home_page},
             -cookie => $self->cgi->cookie(
-                -name => 'ses',
+                -name => COOKIE_SES,
                 -path => '/',
                 -value => '',
                 -expires => '-1h'
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Container.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Container.pm
new file mode 100644 (file)
index 0000000..934714a
--- /dev/null
@@ -0,0 +1,115 @@
+package OpenILS::WWW::EGCatLoader;
+use strict; use warnings;
+use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
+use OpenSRF::Utils::Logger qw/$logger/;
+use OpenILS::Utils::CStoreEditor qw/:funcs/;
+use OpenILS::Utils::Fieldmapper;
+use OpenILS::Application::AppUtils;
+my $U = 'OpenILS::Application::AppUtils';
+
+use constant COOKIE_ANON_CACHE => 'anoncache';
+use constant ANON_CACHE_MYLIST => 'mylist';
+
+# Retrieve the users cached records AKA 'My List'
+# Returns an empty list if there are no cached records
+sub fetch_mylist {
+    my $self = shift;
+
+    my $list = [];
+    my $cache_key = $self->cgi->cookie(COOKIE_ANON_CACHE);
+
+    if($cache_key) {
+
+        $list = $U->simplereq(
+            'open-ils.actor',
+            'open-ils.actor.anon_cache.get_value', 
+            $cache_key, ANON_CACHE_MYLIST);
+
+        if(!$list) {
+            $cache_key = undef;
+            $list = [];
+        }
+    }
+
+    $self->apache->log->info("Found anon-cache list [@$list]");
+
+    return ($cache_key, $list);
+}
+
+
+# Adds a record (by id) to My List, creating a new anon cache + list if necessary.
+sub load_mylist_add {
+    my $self = shift;
+    my $rec_id = $self->cgi->param('record');
+
+    my ($cache_key, $list) = $self->fetch_mylist;
+    push(@$list, $rec_id);
+
+    $cache_key = $U->simplereq(
+        'open-ils.actor',
+        'open-ils.actor.anon_cache.set_value', 
+        $cache_key, ANON_CACHE_MYLIST, $list);
+
+    return $self->mylist_action_redirect($cache_key);
+}
+
+# Removes a record ID from My List
+sub load_mylist_del {
+    my $self = shift;
+    my $rec_id = $self->cgi->param('record');
+
+    my ($cache_key, $list) = $self->fetch_mylist;
+    return $self->mylist_action_redirect unless $cache_key;
+
+    $list = [ grep { $_ ne $rec_id } @$list ];
+
+    $cache_key = $U->simplereq(
+        'open-ils.actor',
+        'open-ils.actor.anon_cache.set_value', 
+        $cache_key, ANON_CACHE_MYLIST, $list);
+
+    return $self->mylist_action_redirect($cache_key);
+}
+
+sub load_cache_clear {
+    my $self = shift;
+    $self->clear_anon_cache;
+    return $self->mylist_action_redirect;
+}
+
+# Wipes the entire anonymous cache, including My List
+sub clear_anon_cache {
+    my $self = shift;
+    my $field = shift;
+
+    my $cache_key = $self->cgi->cookie(COOKIE_ANON_CACHE) or return;
+
+    $U->simplereq(
+        'open-ils.actor',
+        'open-ils.actor.anon_cache.delete_session', $cache_key)
+        if $cache_key;
+
+}
+
+# Called after an anon-cache / My List action occurs.  Redirect
+# to the redirect_url (cgi param) or referrer or home.
+sub mylist_action_redirect {
+    my $self = shift;
+    my $cache_key = shift;
+
+    $self->apache->print(
+        $self->cgi->redirect(
+            -url => $self->cgi->param('redirect_to') || $self->ctx->{referer} || $self->ctx->{home_page},
+            -cookie => $self->cgi->cookie(
+                -name => COOKIE_ANON_CACHE,
+                -path => '/',
+                -value => ($cache_key) ? $cache_key : '',
+                -expires => ($cache_key) ? undef : '-1h'
+            )
+        )
+    );
+
+    return Apache2::Const::REDIRECT;
+}
+
+1;
index 87ff608..e37fc7c 100644 (file)
@@ -107,12 +107,16 @@ sub init_ro_object_cache {
     # retrieve and cache org unit setting values
     $ctx->{get_org_setting} = sub {
         my($org_id, $setting) = @_;
-        $cache{org_settings}{$org_id} = {} unless $cache{org_settings}{$org_id};
-        $cache{org_settings}{$org_id}{$setting} = $U->ou_ancestor_setting_value($org_id, $setting)
-            unless exists $cache{org_settings}{$org_id}{$setting};
+
+        $cache{org_settings}{$org_id} = {} 
+            unless $cache{org_settings}{$org_id};
+
+        $cache{org_settings}{$org_id}{$setting} = 
+            $U->ou_ancestor_setting_value($org_id, $setting)
+                unless exists $cache{org_settings}{$org_id}{$setting};
+
         return $cache{org_settings}{$org_id}{$setting};
     };
 }
 
-
 1;