Bug 18736: Calculate tax depending on rounding
[koha.git] / C4 / Languages.pm
index 78d54a9..1958c91 100644 (file)
@@ -5,18 +5,18 @@ package C4::Languages;
 # Portions Copyright 2009 Chris Cormack and the Koha Dev Team
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# 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;
@@ -26,21 +26,10 @@ use Carp;
 use CGI;
 use List::MoreUtils qw( any );
 use C4::Context;
-use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG);
-
-eval {
-    if (C4::Context->ismemcached) {
-        require Memoize::Memcached;
-        import Memoize::Memcached qw(memoize_memcached);
-
-        memoize_memcached('getTranslatedLanguages', memcached => C4::Context->memcached);
-        memoize_memcached('getFrameworkLanguages' , memcached => C4::Context->memcached);
-        memoize_memcached('getAllLanguages',        memcached => C4::Context->memcached);
-    }
-};
+use Koha::Cache::Memory::Lite;
+use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG);
 
 BEGIN {
-    $VERSION = 3.07.00.049;
     require Exporter;
     @ISA    = qw(Exporter);
     @EXPORT = qw(
@@ -249,7 +238,8 @@ sub getLanguages {
                 $language_subtag_registry->{language_description} = $language_descriptions->{description};
             }
         }
-        if ( !$language_list || index (  $language_list, $language_subtag_registry->{ iso639_2_code } ) >= 0) {
+        # Do not push unless valid iso639-2 code
+        if ( $language_subtag_registry->{ iso639_2_code } and ( !$language_list || index (  $language_list, $language_subtag_registry->{ iso639_2_code } ) >= 0) ) {
             push @languages_loop, $language_subtag_registry;
         }
     }
@@ -352,8 +342,18 @@ sub _build_languages_arrayref {
             push ( @{ $language_groups->{$language_subtags_hashref->{language}} }, $language_subtags_hashref );
         }
         # $key is a language subtag like 'en'
-        while( my ($key, $value) = each %$language_groups) {
 
+        my %idx = map { $enabled_languages->[$_] => $_ } reverse 0 .. @$enabled_languages-1;
+        my @ordered_keys = sort {
+            my $aa = $language_groups->{$a}->[0]->{rfc4646_subtag};
+            my $bb = $language_groups->{$b}->[0]->{rfc4646_subtag};
+            ( exists $idx{$aa} and exists $idx{$bb} and ( $idx{$aa} cmp $idx{$bb} ) )
+            || ( exists $idx{$aa} and exists $idx{$bb} )
+            || exists $idx{$bb}
+        } keys %$language_groups;
+
+        for my $key ( @ordered_keys ) {
+            my $value = $language_groups->{$key};
             # is this language group enabled? are any of the languages within it enabled?
             my $enabled;
             for my $enabled_language (@enabled_languages) {
@@ -569,43 +569,57 @@ sub accept_language {
 sub getlanguage {
     my ($cgi) = @_;
 
+    my $memory_cache = Koha::Cache::Memory::Lite->get_instance();
+    my $cache_key = "getlanguage";
+    unless ( $cgi and $cgi->param('language') ) {
+        my $cached = $memory_cache->get_from_cache($cache_key);
+        return $cached if $cached;
+    }
+
     $cgi //= new CGI;
     my $interface = C4::Context->interface;
+    my $theme = C4::Context->preference( ( $interface eq 'opac' ) ? 'opacthemes' : 'template' );
     my $language;
 
     my $preference_to_check =
       $interface eq 'intranet' ? 'language' : 'opaclanguages';
     # Get the available/valid languages list
-    my @languages = split /,/, C4::Context->preference($preference_to_check);
+    my @languages;
+    my $preference_value = C4::Context->preference($preference_to_check);
+    if ($preference_value) {
+        @languages = split /,/, $preference_value;
+    }
 
     # Chose language from the URL
-    $language = $cgi->param( 'language' );
-    if ( defined $language && any { $_ eq $language } @languages) {
-        return $language;
+    my $cgi_param_language = $cgi->param( 'language' );
+    if ( defined $cgi_param_language && any { $_ eq $cgi_param_language } @languages) {
+        $language = $cgi_param_language;
     }
 
     # cookie
-    if ($language = $cgi->cookie('KohaOpacLanguage') ) {
-        $language =~ s/[^a-zA-Z_-]*//; # sanitize cookie
+    if (not $language and my $cgi_cookie_language = $cgi->cookie('KohaOpacLanguage') ) {
+        ( $language = $cgi_cookie_language ) =~ s/[^a-zA-Z_-]*//; # sanitize cookie
     }
 
     # HTTP_ACCEPT_LANGUAGE
     if ( !$language && $ENV{HTTP_ACCEPT_LANGUAGE} ) {
         $language = accept_language( $ENV{HTTP_ACCEPT_LANGUAGE},
-            getTranslatedLanguages( $interface, 'prog' ) );
+            getTranslatedLanguages( $interface, $theme ) );
     }
 
     # Ignore a lang not selected in sysprefs
-    if ( $language && any { $_ eq $language } @languages ) {
-        return $language;
+    if ( $language && not any { $_ eq $language } @languages ) {
+        $language = undef;
     }
 
     # Pick the first selected syspref language
-    $language = shift @languages;
-    return $language if $language;
+    $language = shift @languages unless $language;
 
     # Fall back to English if necessary
-    return 'en';
+    $language ||= 'en';
+
+    $memory_cache->set_in_cache( $cache_key, $language );
+    return $language;
 }
 
 1;