Bug 5529 Absence or Presence of lists not being reliably returned
[koha.git] / C4 / Auth.pm
index cecf8f8..7a0c13e 100644 (file)
@@ -34,7 +34,7 @@ use C4::VirtualShelves;
 use POSIX qw/strftime/;
 
 # use utf8;
-use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug $ldap $cas $caslogout);
+use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug $ldap $cas $caslogout $servers $memcached);
 
 BEGIN {
     $VERSION = 3.02;        # set version for version checking
@@ -54,7 +54,17 @@ BEGIN {
         require C4::Auth_with_cas;             # no import
         import  C4::Auth_with_cas qw(checkpw_cas login_cas logout_cas login_cas_url);
     }
-
+    $servers = C4::Context->config('memcached_servers');
+    $memcached;
+    if ($servers) {
+       require Cache::Memcached;
+        $memcached = Cache::Memcached->new({
+                                              servers => [ $servers ],
+                                              debug   => 0,
+                                              compress_threshold => 10_000,
+                                              namespace => C4::Context->config('memcached_namespace') || 'koha',
+                                          });
+    }
 }
 
 =head1 NAME
@@ -154,19 +164,19 @@ sub get_template_and_user {
         $template->param( loggedinusername => $user );
         $template->param( sessionID        => $sessionID );
 
-               my ($total, $pubshelves, $barshelves) = C4::Context->get_shelves_userenv();
-               if (defined($pubshelves)) {
-               $template->param(       pubshelves      => scalar (@$pubshelves),
-                                                       pubshelvesloop  => $pubshelves,
-                                                       );
-                       $template->param(       pubtotal                => $total->{'pubtotal'}, ) if ($total->{'pubtotal'} > scalar (@$pubshelves));
-               }
-               if (defined($barshelves)) {
-               $template->param(       barshelves      => scalar (@$barshelves),
-                                                       barshelvesloop  => $barshelves,
-                                                       );
-                       $template->param(       bartotal                => $total->{'bartotal'}, ) if ($total->{'bartotal'} > scalar (@$barshelves));
-               }
+        my ($total, $pubshelves, $barshelves) = C4::Context->get_shelves_userenv();
+        if (defined($pubshelves)) {
+            $template->param( pubshelves     => scalar @{$pubshelves},
+                              pubshelvesloop => $pubshelves,
+            );
+            $template->param( pubtotal   => $total->{'pubtotal'}, ) if ($total->{'pubtotal'} > scalar @{$pubshelves});
+        }
+        if (defined($barshelves)) {
+            $template->param( barshelves      => scalar @{$barshelves},
+                              barshelvesloop  => $barshelves,
+            );
+            $template->param( bartotal  => $total->{'bartotal'}, ) if ($total->{'bartotal'} > scalar @{$barshelves});
+        }
 
         $borrowernumber = getborrowernumber($user) if defined($user);
 
@@ -287,11 +297,11 @@ sub get_template_and_user {
         $template->param( sessionID        => $sessionID );
         
         my ($total, $pubshelves) = C4::Context->get_shelves_userenv();  # an anonymous user has no 'barshelves'...
-        if (defined(($pubshelves))) {
-            $template->param(   pubshelves      => scalar (@$pubshelves),
+        if (defined $pubshelves) {
+            $template->param(   pubshelves      => scalar @{$pubshelves},
                                 pubshelvesloop  => $pubshelves,
                             );
-            $template->param(   pubtotal        => $total->{'pubtotal'}, ) if ($total->{'pubtotal'} > scalar (@$pubshelves));
+            $template->param(   pubtotal        => $total->{'pubtotal'}, ) if ($total->{'pubtotal'} > scalar @{$pubshelves});
         }
 
     }
@@ -842,12 +852,12 @@ sub checkauth {
                                $total->{'bartotal'} = $totshelves;
                                ($pubshelves, $totshelves) = C4::VirtualShelves::GetRecentShelves(2, $row_count, undef);
                                $total->{'pubtotal'} = $totshelves;
-                               $session->param('barshelves', $barshelves->[0]);
-                               $session->param('pubshelves', $pubshelves->[0]);
+                               $session->param('barshelves', $barshelves);
+                               $session->param('pubshelves', $pubshelves);
                                $session->param('totshelves', $total);
 
-                               C4::Context::set_shelves_userenv('bar',$barshelves->[0]);
-                               C4::Context::set_shelves_userenv('pub',$pubshelves->[0]);
+                               C4::Context::set_shelves_userenv('bar',$barshelves);
+                               C4::Context::set_shelves_userenv('pub',$pubshelves);
                                C4::Context::set_shelves_userenv('tot',$total);
                        }
                else {
@@ -867,9 +877,9 @@ sub checkauth {
                        my ($total, $totshelves, $pubshelves);
                        ($pubshelves, $totshelves) = C4::VirtualShelves::GetRecentShelves(2, $row_count, undef);
                        $total->{'pubtotal'} = $totshelves;
-                       $session->param('pubshelves', $pubshelves->[0]);
+                       $session->param('pubshelves', $pubshelves);
                        $session->param('totshelves', $total);
-                       C4::Context::set_shelves_userenv('pub',$pubshelves->[0]);
+                       C4::Context::set_shelves_userenv('pub',$pubshelves);
                        C4::Context::set_shelves_userenv('tot',$total);
 
                        # setting a couple of other session vars...
@@ -1346,6 +1356,9 @@ sub get_session {
     elsif ($storage_method eq 'Pg') {
         $session = new CGI::Session("driver:PostgreSQL;serializer:yaml;id:md5", $sessionID, {Handle=>$dbh});
     }
+    elsif ($storage_method eq 'memcached' && $servers){
+       $session = new CGI::Session("driver:memcached;serializer:yaml;id:md5", $sessionID, { Memcached => $memcached } );
+    }
     else {
         # catch all defaults to tmp should work on all systems
         $session = new CGI::Session("driver:File;serializer:yaml;id:md5", $sessionID, {Directory=>'/tmp'});