-#
-# Change this value to reflect where you will store your includes
-#
-my %configfile;
-open (KC, "/etc/koha.conf");
-while (<KC>) {
- chomp;
- (next) if (/^\s*#/);
- if (/(.*)\s*=\s*(.*)/) {
- my $variable=$1;
- my $value=$2;
-
- $variable =~ s/^\s*//g;
- $variable =~ s/\s*$//g;
- $value =~ s/^\s*//g;
- $value =~ s/\s*$//g;
- $configfile{$variable}=$value;
- } # if
-} # while
-close(KC);
-
-my $path=$configfile{'includes'};
-($path) || ($path="/usr/local/www/hdl/htdocs/includes");
-
-# make all your functions, whether exported or not;
-
-sub picktemplate {
- my ($includes, $base) = @_;
- my $dbh=C4Connect;
- my $templates;
- opendir (D, "$includes/templates");
- my @dirlist=readdir D;
- foreach (@dirlist) {
- (next) if (/^\./);
- #(next) unless (/\.tmpl$/);
- (next) unless (-e "$includes/templates/$_/$base");
- $templates->{$_}=1;
- }
- my $sth=$dbh->prepare("select value from systempreferences where
- variable='template'");
- $sth->execute;
- my ($preftemplate) = $sth->fetchrow;
- $sth->finish;
- $dbh->disconnect;
- if ($templates->{$preftemplate}) {
- return $preftemplate;
+C<&pathtotemplate> returns a hash with the following keys:
+
+=over 4
+
+=item C<path>
+
+The full pathname to the desired template.
+
+=item C<foundlanguage>
+
+The value is set to 1 if a template in the desired language was found,
+or 0 otherwise.
+
+=item C<foundtheme>
+
+The value is set to 1 if a template of the desired theme was found, or
+0 otherwise.
+
+=back
+
+If C<&pathtotemplate> cannot find an acceptable template, it returns 0.
+
+Note that if a template of the desired language or theme cannot be
+found, C<&pathtotemplate> will print a warning message. Unless you've
+set C<$SIG{__WARN__}>, though, this won't show up in the output HTML
+document.
+
+=cut
+#'
+# FIXME - Fix POD: it doesn't look in the directory given by the
+# 'includes' option in /etc/koha.conf.
+sub pathtotemplate {
+ my %params = @_;
+ my $template = $params{'template'};
+ my $themeor = $params{'theme'};
+ my $languageor = lc($params{'language'});
+ my $ptype = lc($params{'type'} or 'intranet');
+
+ # FIXME - Make sure $params{'template'} was given. Or else assume
+ # "default".
+ my $type;
+ if ($ptype eq 'opac') {$type = 'opac-tmpl/'; }
+ elsif ($ptype eq 'none') {$type = ''; }
+ elsif ($ptype eq 'intranet') {$type = 'intranet-tmpl/'; }
+ else {$type = $ptype . '/'; }
+
+ my %returns;
+ my $theme = C4::Context->preference("theme") || "default";
+ if ($themeor and
+ C4::Context->preference("allowthemeoverride") =~ qr/$themeor/i)
+ {
+ $theme = $themeor;
+ }
+ my @languageorder = getlanguageorder();
+ my $language = $languageor || shift(@languageorder);
+
+ #where to search for templates
+ my @tmpldirs = ("$path/templates", $path);
+ unshift (@tmpldirs, C4::Context->config('templatedirectory')) if C4::Context->config('templatedirectory');
+ unshift (@tmpldirs, $params{'path'}) if $params{'path'};
+
+ my ($etheme, $elanguage, $epath);
+
+ CHECK: foreach my $edir (@tmpldirs) {
+ foreach $etheme ($theme, 'all', 'default') {
+ foreach $elanguage ($language, @languageorder, 'all','en') {
+ # 'en' is the fallback-language
+ if (-e "$edir/$type$etheme/$elanguage/$template") {
+ $epath = "$edir/$type$etheme/$elanguage/$template";
+ last CHECK;
+ }
+ }
+ }
+ }
+
+ unless ($epath) {
+ warn "Could not find $template in @tmpldirs";
+ return 0;
+ }
+
+ if ($language eq $elanguage) {
+ $returns{'foundlanguage'} = 1;
+ } else {
+ $returns{'foundlanguage'} = 0;
+ warn "The language $language could not be found for $template of $theme.\nServing $elanguage instead.\n";
+ }
+ if ($theme eq $etheme) {
+ $returns{'foundtheme'} = 1;