Bug 18025: Simplify logic and avoid 1 call to ValidateBorrowernumber
[koha.git] / C4 / Context.pm
index 1f1e9a1..a50ddfd 100644 (file)
@@ -1,4 +1,5 @@
 package C4::Context;
+
 # Copyright 2002 Katipo Communications
 #
 # This file is part of Koha.
@@ -16,9 +17,9 @@ package C4::Context;
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
-use vars qw($AUTOLOAD $context @context_stack $servers);
+use Modern::Perl;
+
+use vars qw($AUTOLOAD $context @context_stack);
 BEGIN {
        if ($ENV{'HTTP_USER_AGENT'})    {
                require CGI::Carp;
@@ -246,18 +247,15 @@ sub new {
     my $conf_cache = Koha::Caches->get_instance('config');
     my $config_from_cache;
     if ( $conf_cache->cache ) {
-        $config_from_cache = $conf_cache->get_from_cache('koha_conf');
+        $self = $conf_cache->get_from_cache('koha_conf');
     }
-    unless ( %$self ) {
+    unless ( $self and %$self ) {
         $self = Koha::Config->read_from_file($conf_fname);
-    }
-
-    if ( $config_from_cache ) {
-        $self = $config_from_cache;
-    } elsif ( $conf_cache->memcached_cache ) {
-        # FIXME it may be better to use the memcached servers from the config file
-        # to cache it
-        $conf_cache->set_in_cache('koha_conf', $self)
+        if ( $conf_cache->memcached_cache ) {
+            # FIXME it may be better to use the memcached servers from the config file
+            # to cache it
+            $conf_cache->set_in_cache('koha_conf', $self)
+        }
     }
     unless ( exists $self->{config} or defined $self->{config} ) {
         warn "The config file ($conf_fname) has not been parsed correctly";
@@ -409,15 +407,16 @@ sub preference {
     my $self = shift;
     my $var  = shift;    # The system preference to return
 
-    $var = lc $var;
-
     return $ENV{"OVERRIDE_SYSPREF_$var"}
         if defined $ENV{"OVERRIDE_SYSPREF_$var"};
 
-    my $cached_var = $use_syspref_cache
-        ? $syspref_cache->get_from_cache("syspref_$var")
-        : undef;
-    return $cached_var if defined $cached_var;
+    $var = lc $var;
+
+    if ($use_syspref_cache) {
+        $syspref_cache = Koha::Caches->get_instance('syspref') unless $syspref_cache;
+        my $cached_var = $syspref_cache->get_from_cache("syspref_$var");
+        return $cached_var if defined $cached_var;
+    }
 
     my $syspref;
     eval { $syspref = Koha::Config::SysPrefs->find( lc $var ) };
@@ -848,7 +847,7 @@ sub userenv {
   C4::Context->set_userenv($usernum, $userid, $usercnum,
                            $userfirstname, $usersurname,
                            $userbranch, $branchname, $userflags,
-                           $emailaddress, $branchprinter, $persona);
+                           $emailaddress, $branchprinter, $shibboleth);
 
 Establish a hash of user environment variables.
 
@@ -859,7 +858,7 @@ set_userenv is called in Auth.pm
 #'
 sub set_userenv {
     shift @_;
-    my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter, $persona, $shibboleth)=
+    my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter, $shibboleth)=
     map { Encode::is_utf8( $_ ) ? $_ : Encode::decode('UTF-8', $_) } # CGI::Session doesn't handle utf-8, so we decode it here
     @_;
     my $var=$context->{"activeuser"} || '';
@@ -875,7 +874,6 @@ sub set_userenv {
         "flags"      => $userflags,
         "emailaddress"     => $emailaddress,
         "branchprinter"    => $branchprinter,
-        "persona"    => $persona,
         "shibboleth" => $shibboleth,
     };
     $context->{userenv}->{$var} = $cell;
@@ -1032,7 +1030,31 @@ sub interface {
     return $context->{interface} // 'opac';
 }
 
+# always returns a string for OK comparison via "eq" or "ne"
+sub mybranch {
+    C4::Context->userenv           or return '';
+    return C4::Context->userenv->{branch} || '';
+}
+
+=head2 only_my_library
+
+    my $test = C4::Context->only_my_library;
+
+    Returns true if you enabled IndependentBranches and the current user
+    does not have superlibrarian permissions.
+
+=cut
+
+sub only_my_library {
+    return
+         C4::Context->preference('IndependentBranches')
+      && C4::Context->userenv
+      && !C4::Context->IsSuperLibrarian()
+      && C4::Context->userenv->{branch};
+}
+
 1;
+
 __END__
 
 =head1 ENVIRONMENT