Bug 20538: Remove the need of writing [% KOHA_VERSION %] everywhere
[koha.git] / Koha / Template / Plugin / Asset.pm
blobcaf1786c5d3110aad23b9beae57e20917048a26d
1 package Koha::Template::Plugin::Asset;
3 # Copyright Marc VĂ©ron / marc veron ag, Switzerland
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Template::Plugin;
23 use base qw( Template::Plugin );
25 use File::Basename;
26 use File::Spec;
27 use C4::Context;
29 sub new {
30 my ($class, $context) = @_;
32 my $self = {
33 _CONTEXT => $context,
36 return bless $self, $class;
39 sub js {
40 my ( $self, $filename, $attributes ) = @_;
42 my $url = $self->url($filename);
43 unless ($url) {
44 warn "File not found : $filename";
45 return;
48 $attributes->{src} = $url;
50 return $self->tag('script', $attributes) . '</script>';
53 sub css {
54 my ( $self, $filename, $attributes ) = @_;
56 my $url = $self->url($filename);
57 unless ($url) {
58 warn "File not found : $filename";
59 return;
62 $attributes->{rel} = 'stylesheet';
63 $attributes->{type} = 'text/css';
64 $attributes->{href} = $url;
66 return $self->tag('link', $attributes);
69 sub url {
70 my ( $self, $filename ) = @_;
72 my $stash = $self->{_CONTEXT}->stash();
73 my $interface = $stash->get('interface');
74 my $theme = $stash->get('theme');
76 my $configkey = $interface =~ /opac/ ? 'opachtdocs' : 'intrahtdocs';
77 my $root = C4::Context->config($configkey);
79 my ($basename, $dirname, $suffix) = fileparse($filename, qr/\.[^.]*/);
81 my $type = substr $suffix, 1;
82 my @dirs = (
83 "$theme",
84 ".",
87 my $version = C4::Context->preference('Version');
88 foreach my $dir (@dirs) {
89 my $abspath = File::Spec->catfile($root, $dir, $filename);
90 if (-e $abspath) {
91 return File::Spec->catfile($interface, $dir, $dirname, "${basename}_${version}${suffix}");
96 sub tag {
97 my ($self, $name, $attributes) = @_;
99 my @attributes_strs;
100 if ($attributes) {
101 while (my ($key, $value) = each %$attributes) {
102 push @attributes_strs, qq{$key="$value"};
105 my $attributes_str = join ' ', @attributes_strs;
107 return "<$name $attributes_str>";