Bug 20538: Fix copyright notice in Asset.pm
[koha.git] / Koha / Template / Plugin / Asset.pm
1 package Koha::Template::Plugin::Asset;
2
3 # Copyright 2018 BibLibre
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Template::Plugin;
23 use base qw( Template::Plugin );
24
25 use File::Basename;
26 use File::Spec;
27 use C4::Context;
28
29 sub new {
30     my ($class, $context) = @_;
31
32     my $self = {
33         _CONTEXT => $context,
34     };
35
36     return bless $self, $class;
37 }
38
39 sub js {
40     my ( $self, $filename, $attributes ) = @_;
41
42     my $url = $self->url($filename);
43     unless ($url) {
44         warn "File not found : $filename";
45         return;
46     }
47
48     $attributes->{src} = $url;
49
50     return $self->tag('script', $attributes) . '</script>';
51 }
52
53 sub css {
54     my ( $self, $filename, $attributes ) = @_;
55
56     my $url = $self->url($filename);
57     unless ($url) {
58         warn "File not found : $filename";
59         return;
60     }
61
62     $attributes->{rel} = 'stylesheet';
63     $attributes->{type} = 'text/css';
64     $attributes->{href} = $url;
65
66     return $self->tag('link', $attributes);
67 }
68
69 sub url {
70     my ( $self, $filename ) = @_;
71
72     my $stash = $self->{_CONTEXT}->stash();
73     my $interface = $stash->get('interface');
74     my $theme = $stash->get('theme');
75
76     my $configkey = $interface =~ /opac/ ? 'opachtdocs' : 'intrahtdocs';
77     my $root = C4::Context->config($configkey);
78
79     my ($basename, $dirname, $suffix) = fileparse($filename, qr/\.[^.]*/);
80
81     my $type = substr $suffix, 1;
82     my @dirs = (
83         "$theme",
84         ".",
85     );
86
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}");
92         }
93     }
94 }
95
96 sub tag {
97     my ($self, $name, $attributes) = @_;
98
99     my @attributes_strs;
100     if ($attributes) {
101         while (my ($key, $value) = each %$attributes) {
102             push @attributes_strs, qq{$key="$value"};
103         }
104     }
105     my $attributes_str = join ' ', @attributes_strs;
106
107     return "<$name $attributes_str>";
108 }
109
110 1;