BUG: fix patron search
[koha.git] / C4 / Context.pm
index d6a31bf..9b3f74d 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;
@@ -79,6 +80,7 @@ BEGIN {
 
         # Redefine multi_param if cgi version is < 4.08
         # Remove the "CGI::param called in list context" warning in this case
+        require CGI; # Can't check version without the require.
         if (!defined($CGI::VERSION) || $CGI::VERSION < 4.08) {
             no warnings 'redefine';
             *CGI::multi_param = \&CGI::param;
@@ -88,20 +90,21 @@ BEGIN {
     }          # else there is no browser to send fatals to!
 }
 
-use Encode;
-use ZOOM;
-use Koha::Caches;
-use POSIX ();
+use Carp;
 use DateTime::TimeZone;
+use Encode;
+use File::Spec;
 use Module::Load::Conditional qw(can_load);
-use Carp;
+use POSIX ();
+use ZOOM;
 
 use C4::Boolean;
 use C4::Debug;
-use Koha;
-use Koha::Config;
+use Koha::Caches;
 use Koha::Config::SysPref;
 use Koha::Config::SysPrefs;
+use Koha::Config;
+use Koha;
 
 =head1 NAME
 
@@ -406,15 +409,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 ) };
@@ -493,6 +497,7 @@ preference.
 sub set_preference {
     my ( $self, $variable, $value, $explanation, $type, $options ) = @_;
 
+    my $variable_case = $variable;
     $variable = lc $variable;
 
     my $syspref = Koha::Config::SysPrefs->find($variable);
@@ -518,7 +523,7 @@ sub set_preference {
         )->store;
     } else {
         $syspref = Koha::Config::SysPref->new(
-            {   variable    => $variable,
+            {   variable    => $variable_case,
                 value       => $value,
                 explanation => $explanation || undef,
                 type        => $type,
@@ -603,27 +608,12 @@ sub _new_Zconn {
     my $tried=0; # first attempt
     my $Zconn; # connection object
     my $elementSetName;
-    my $index_mode;
     my $syntax;
 
     $server //= "biblioserver";
 
-    if ( $server eq 'biblioserver' ) {
-        $index_mode = $context->{'config'}->{'zebra_bib_index_mode'} // 'dom';
-    } elsif ( $server eq 'authorityserver' ) {
-        $index_mode = $context->{'config'}->{'zebra_auth_index_mode'} // 'dom';
-    }
-
-    if ( $index_mode eq 'grs1' ) {
-        $elementSetName = 'F';
-        $syntax = ( $context->preference("marcflavour") eq 'UNIMARC' )
-                ? 'unimarc'
-                : 'usmarc';
-
-    } else { # $index_mode eq 'dom'
-        $syntax = 'xml';
-        $elementSetName = 'marcxml';
-    }
+    $syntax = 'xml';
+    $elementSetName = 'marcxml';
 
     my $host = $context->{'listen'}->{$server}->{'content'};
     my $user = $context->{"serverinfo"}->{$server}->{"user"};
@@ -845,7 +835,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.
 
@@ -856,7 +846,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"} || '';
@@ -872,7 +862,6 @@ sub set_userenv {
         "flags"      => $userflags,
         "emailaddress"     => $emailaddress,
         "branchprinter"    => $branchprinter,
-        "persona"    => $persona,
         "shibboleth" => $shibboleth,
     };
     $context->{userenv}->{$var} = $cell;
@@ -966,6 +955,25 @@ sub get_versions {
     return %versions;
 }
 
+=head2 timezone
+
+  my $C4::Context->timzone
+
+  Returns a timezone code for the instance of Koha
+
+=cut
+
+sub timezone {
+    my $self = shift;
+
+    my $timezone = C4::Context->config('timezone') || $ENV{TZ} || 'local';
+    if ( !DateTime::TimeZone->is_valid_name( $timezone ) ) {
+        warn "Invalid timezone in koha-conf.xml ($timezone)";
+        $timezone = 'local';
+    }
+
+    return $timezone;
+}
 
 =head2 tz
 
@@ -978,7 +986,8 @@ sub get_versions {
 sub tz {
     my $self = shift;
     if (!defined $context->{tz}) {
-        $context->{tz} = DateTime::TimeZone->new(name => 'local');
+        my $timezone = $self->timezone;
+        $context->{tz} = DateTime::TimeZone->new(name => $timezone);
     }
     return $context->{tz};
 }
@@ -1029,7 +1038,43 @@ 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};
+}
+
+=head3 temporary_directory
+
+Returns root directory for temporary storage
+
+=cut
+
+sub temporary_directory {
+    my ( $class ) = @_;
+    return C4::Context->config('tmp_path') || File::Spec->tmpdir;
+}
+
+
 1;
+
 __END__
 
 =head1 ENVIRONMENT