+ while (@_) {
+ my $key = shift;
+ my $val = shift;
+ if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
+ elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
+ if ( $key ) {
+ $self->{VARS}->{$key} = $val;
+ } else {
+ warn "Problem = a value of $val has been passed to param without key";
+ }
+ }
+}
+
+
+=head1 NAME
+
+C4::Templates - Functions for managing templates
+
+=head1 FUNCTIONS
+
+=cut
+
+# FIXME: this is a quick fix to stop rc1 installing broken
+# Still trying to figure out the correct fix.
+my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
+
+#---------------------------------------------------------------------------------------------------------
+# FIXME - POD
+
+sub _get_template_file {
+ my ($tmplbase, $interface, $query) = @_;
+
+ my $is_intranet = $interface eq 'intranet';
+ my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
+ my ($theme, $lang, $availablethemes) = themelanguage($htdocs, $tmplbase, $interface, $query);
+ $lang //= 'en';
+ $theme //= '';
+ $tmplbase = "$htdocs/$theme/$lang/modules/$tmplbase" if $tmplbase !~ /^\//;
+ # do not prefix an absolute path
+
+ return ( $htdocs, $theme, $lang, $tmplbase );
+}
+
+=head2 badtemplatecheck
+
+ badtemplatecheck( $template_path );
+
+ The sub will throw an exception if the template path is not allowed.
+
+ Note: At this moment the sub is actually a helper routine for
+ sub gettemplate.
+
+=cut
+
+sub badtemplatecheck {
+ my ( $template ) = @_;
+ if( !$template || $template !~ m/^[a-zA-Z0-9_\-\/]+\.(tt|pref)$/ ) {
+ # This also includes two dots
+ Koha::Exceptions::NoPermission->throw( 'bad template path' );
+ } else {
+ # Check allowed dirs
+ my $dirs = C4::Context->config("pluginsdir");
+ $dirs = [ $dirs ] if !ref($dirs);
+ unshift @$dirs, C4::Context->config('opachtdocs'), C4::Context->config('intrahtdocs');
+ my $found = 0;
+ foreach my $dir ( @$dirs ) {
+ $dir .= '/' if $dir !~ m/\/$/;
+ $found++ if $template =~ m/^$dir/;
+ last if $found;
+ }
+ Koha::Exceptions::NoPermission->throw( 'bad template path' ) if !$found;
+ }
+}
+
+sub gettemplate {
+ my ( $tmplbase, $interface, $query ) = @_;
+ ($query) or warn "no query in gettemplate";
+ my ($htdocs, $theme, $lang, $filename)
+ = _get_template_file($tmplbase, $interface, $query);
+ badtemplatecheck( $filename ); # single trip for bad templates
+ my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
+
+# NOTE: Commenting these out rather than deleting them so that those who need
+# to know how we previously shimmed these directories will be able to understand.
+# my $is_intranet = $interface eq 'intranet';
+# my $themelang =
+# ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
+# "/$theme/$lang";
+# $template->param(
+# themelang => $themelang,
+# interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
+# theme => $theme,
+# lang => $lang
+# );
+
+ # Bidirectionality, must be sent even if is the only language
+ my $current_lang = regex_lang_subtags($lang);
+ my $bidi;
+ $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
+ $template->param(
+ bidi => $bidi,
+ );
+ # Languages
+ my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
+ my $num_languages_enabled = 0;
+ foreach my $lang (@$languages_loop) {
+ foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
+ $num_languages_enabled++ if $sublang->{enabled};
+ }
+ }
+ my $one_language_enabled = ($num_languages_enabled <= 1) ? 1 : 0; # deal with zero enabled langs as well
+ $template->param(
+ languages_loop => $languages_loop,
+ one_language_enabled => $one_language_enabled,
+ ) unless $one_language_enabled;
+
+ return $template;
+}
+
+
+=head2 themelanguage
+
+ my ($theme,$lang,\@themes) = themelanguage($htdocs,$tmpl,$interface,query);
+
+This function returns the theme and language to be used for rendering the UI.
+It also returns the list of themes that should be applied as a fallback. This is
+used for the theme overlay feature (i.e. if a file doesn't exist on the requested
+theme, fallback to the configured fallback).
+
+Important: this function is used on the webinstaller too, so always consider
+the use case where the DB is not populated already when rewriting/fixing.
+
+=cut
+
+sub themelanguage {
+ my ($htdocs, $tmpl, $interface, $query) = @_;
+ ($query) or warn "no query in themelanguage";
+
+ # Select a language based on cookie, syspref available languages & browser
+ my $lang = C4::Languages::getlanguage($query);
+
+ # Get theme
+ my @themes;
+ my $theme_syspref = ($interface eq 'intranet') ? 'template' : 'opacthemes';
+ my $fallback_syspref = ($interface eq 'intranet') ? 'template' : 'OPACFallback';
+ # Yeah, hardcoded, last resort if the DB is not populated
+ my $hardcoded_theme = ($interface eq 'intranet') ? 'prog' : 'bootstrap';
+
+ # Configured theme is the first one
+ push @themes, C4::Context->preference( $theme_syspref )
+ if C4::Context->preference( $theme_syspref );
+ # Configured fallback next
+ push @themes, C4::Context->preference( $fallback_syspref )
+ if C4::Context->preference( $fallback_syspref );
+ # The hardcoded fallback theme is the last one
+ push @themes, $hardcoded_theme;
+
+ # Try to find first theme for the selected theme/lang, then for fallback/lang
+ my $where = $tmpl =~ /xsl$/ ? 'xslt' : 'modules';
+ for my $theme (@themes) {
+ if ( -e "$htdocs/$theme/$lang/$where/$tmpl" ) {
+ return ( $theme, $lang, [ uniq(@themes) ] );
+ }