Bug 18025: Simplify logic and avoid 1 call to ValidateBorrowernumber
[koha.git] / C4 / Context.pm
index 728964e..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;
@@ -90,7 +91,6 @@ BEGIN {
 
 use Encode;
 use ZOOM;
-use XML::Simple;
 use Koha::Caches;
 use POSIX ();
 use DateTime::TimeZone;
@@ -100,6 +100,7 @@ use Carp;
 use C4::Boolean;
 use C4::Debug;
 use Koha;
+use Koha::Config;
 use Koha::Config::SysPref;
 use Koha::Config::SysPrefs;
 
@@ -168,66 +169,9 @@ environment variable to the pathname of a configuration file to use.
 # Zconn
 #     A connection object for the Zebra server
 
-# Koha's main configuration file koha-conf.xml
-# is searched for according to this priority list:
-#
-# 1. Path supplied via use C4::Context '/path/to/koha-conf.xml'
-# 2. Path supplied in KOHA_CONF environment variable.
-# 3. Path supplied in INSTALLED_CONFIG_FNAME, as long
-#    as value has changed from its default of 
-#    '__KOHA_CONF_DIR__/koha-conf.xml', as happens
-#    when Koha is installed in 'standard' or 'single'
-#    mode.
-# 4. Path supplied in CONFIG_FNAME.
-#
-# The first entry that refers to a readable file is used.
-
-use constant CONFIG_FNAME => "/etc/koha/koha-conf.xml";
-                # Default config file, if none is specified
-                
-my $INSTALLED_CONFIG_FNAME = '__KOHA_CONF_DIR__/koha-conf.xml';
-                # path to config file set by installer
-                # __KOHA_CONF_DIR__ is set by rewrite-confg.PL
-                # when Koha is installed in 'standard' or 'single'
-                # mode.  If Koha was installed in 'dev' mode, 
-                # __KOHA_CONF_DIR__ is *not* rewritten; instead
-                # developers should set the KOHA_CONF environment variable 
-
 $context = undef;        # Initially, no context is set
 @context_stack = ();        # Initially, no saved contexts
 
-
-=head2 read_config_file
-
-Reads the specified Koha config file. 
-
-Returns an object containing the configuration variables. The object's
-structure is a bit complex to the uninitiated ... take a look at the
-koha-conf.xml file as well as the XML::Simple documentation for details. Or,
-here are a few examples that may give you what you need:
-
-The simple elements nested within the <config> element:
-
-    my $pass = $koha->{'config'}->{'pass'};
-
-The <listen> elements:
-
-    my $listen = $koha->{'listen'}->{'biblioserver'}->{'content'};
-
-The elements nested within the <server> element:
-
-    my $ccl2rpn = $koha->{'server'}->{'biblioserver'}->{'cql2rpn'};
-
-Returns undef in case of error.
-
-=cut
-
-sub read_config_file {         # Pass argument naming config file to read
-    my $koha = XMLin(shift, keyattr => ['id'], forcearray => ['listen', 'server', 'serverinfo'], suppressempty => '');
-
-    return $koha;                      # Return value: ref-to-hash holding the configuration
-}
-
 =head2 db_scheme2dbi
 
     my $dbd_driver_name = C4::Context::db_schema2dbi($scheme);
@@ -292,38 +236,29 @@ sub new {
     undef $conf_fname unless 
         (defined $conf_fname && -s $conf_fname);
     # Figure out a good config file to load if none was specified.
-    if (!defined($conf_fname))
-    {
-        # If the $KOHA_CONF environment variable is set, use
-        # that. Otherwise, use the built-in default.
-        if (exists $ENV{"KOHA_CONF"} and $ENV{'KOHA_CONF'} and -s  $ENV{"KOHA_CONF"}) {
-            $conf_fname = $ENV{"KOHA_CONF"};
-        } elsif ($INSTALLED_CONFIG_FNAME !~ /__KOHA_CONF_DIR/ and -s $INSTALLED_CONFIG_FNAME) {
-            # NOTE: be careful -- don't change __KOHA_CONF_DIR in the above
-            # regex to anything else -- don't want installer to rewrite it
-            $conf_fname = $INSTALLED_CONFIG_FNAME;
-        } elsif (-s CONFIG_FNAME) {
-            $conf_fname = CONFIG_FNAME;
-        } else {
+    unless ( defined $conf_fname ) {
+        $conf_fname = Koha::Config->guess_koha_conf;
+        unless ( $conf_fname ) {
             warn "unable to locate Koha configuration file koha-conf.xml";
             return;
         }
     }
 
     my $conf_cache = Koha::Caches->get_instance('config');
-    if ( $conf_cache ) {
-        $self = $conf_cache->get_from_cache('kohaconf');
-    }
-    unless ( %$self ) {
-        $self = read_config_file($conf_fname);
+    my $config_from_cache;
+    if ( $conf_cache->cache ) {
+        $self = $conf_cache->get_from_cache('koha_conf');
     }
-    if ( $conf_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 ( $self and %$self ) {
+        $self = Koha::Config->read_from_file($conf_fname);
+        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 "read_config_file($conf_fname) returned undef";
+        warn "The config file ($conf_fname) has not been parsed correctly";
         return;
     }
 
@@ -466,21 +401,22 @@ with this method.
 
 =cut
 
-my $syspref_cache = Koha::Caches->get_instance();
+my $syspref_cache = Koha::Caches->get_instance('syspref');
 my $use_syspref_cache = 1;
 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 ) };
@@ -911,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.
 
@@ -922,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"} || '';
@@ -938,7 +874,6 @@ sub set_userenv {
         "flags"      => $userflags,
         "emailaddress"     => $emailaddress,
         "branchprinter"    => $branchprinter,
-        "persona"    => $persona,
         "shibboleth" => $shibboleth,
     };
     $context->{userenv}->{$var} = $cell;
@@ -1095,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