Browse Source
Having to write [% KOHA_VERSION %] for each url is bad because: - It's easily forgettable when adding new <script> or <link> - It prevents grep'ing for the full filename - It violates the DRY principle - If at some point we want to change the "force js and css reload" mechanism, it will be tedious This patch: - adds a Template::Toolkit plugin that generates <script> and <link> tags for JS and CSS files, and inserts automatically the Koha version in the filename - use the new plugin to remove all occurences of [% KOHA_VERSION %] - remove the code that was adding KOHA_VERSION as a template variable Test plan: 1. Apply patch 2. Go to several different pages in Koha (opac and intranet) while checking your browser's dev tools (there should be no 404 for JS and CSS files, and the Koha version should appear in filenames) and the server logs (there should be no "File not found") 3. `git grep KOHA_VERSION` should return nothing 4. prove t/db_dependent/Koha/Template/Plugin/Asset.t Signed-off-by: Josef Moravec <josef.moravec@gmail.com> Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>18.05.x
Julian Maurice
6 years ago
committed by
Jonathan Druart
246 changed files with 1020 additions and 571 deletions
@ -0,0 +1,110 @@ |
|||
package Koha::Template::Plugin::Asset; |
|||
|
|||
# Copyright Marc Véron / marc veron ag, Switzerland |
|||
|
|||
# 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 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. |
|||
# |
|||
# You should have received a copy of the GNU General Public License |
|||
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
|||
|
|||
use Modern::Perl; |
|||
|
|||
use Template::Plugin; |
|||
use base qw( Template::Plugin ); |
|||
|
|||
use File::Basename; |
|||
use File::Spec; |
|||
use C4::Context; |
|||
|
|||
sub new { |
|||
my ($class, $context) = @_; |
|||
|
|||
my $self = { |
|||
_CONTEXT => $context, |
|||
}; |
|||
|
|||
return bless $self, $class; |
|||
} |
|||
|
|||
sub js { |
|||
my ( $self, $filename, $attributes ) = @_; |
|||
|
|||
my $url = $self->url($filename); |
|||
unless ($url) { |
|||
warn "File not found : $filename"; |
|||
return; |
|||
} |
|||
|
|||
$attributes->{src} = $url; |
|||
|
|||
return $self->tag('script', $attributes) . '</script>'; |
|||
} |
|||
|
|||
sub css { |
|||
my ( $self, $filename, $attributes ) = @_; |
|||
|
|||
my $url = $self->url($filename); |
|||
unless ($url) { |
|||
warn "File not found : $filename"; |
|||
return; |
|||
} |
|||
|
|||
$attributes->{rel} = 'stylesheet'; |
|||
$attributes->{type} = 'text/css'; |
|||
$attributes->{href} = $url; |
|||
|
|||
return $self->tag('link', $attributes); |
|||
} |
|||
|
|||
sub url { |
|||
my ( $self, $filename ) = @_; |
|||
|
|||
my $stash = $self->{_CONTEXT}->stash(); |
|||
my $interface = $stash->get('interface'); |
|||
my $theme = $stash->get('theme'); |
|||
|
|||
my $configkey = $interface =~ /opac/ ? 'opachtdocs' : 'intrahtdocs'; |
|||
my $root = C4::Context->config($configkey); |
|||
|
|||
my ($basename, $dirname, $suffix) = fileparse($filename, qr/\.[^.]*/); |
|||
|
|||
my $type = substr $suffix, 1; |
|||
my @dirs = ( |
|||
"$theme", |
|||
".", |
|||
); |
|||
|
|||
my $version = C4::Context->preference('Version'); |
|||
foreach my $dir (@dirs) { |
|||
my $abspath = File::Spec->catfile($root, $dir, $filename); |
|||
if (-e $abspath) { |
|||
return File::Spec->catfile($interface, $dir, $dirname, "${basename}_${version}${suffix}"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
sub tag { |
|||
my ($self, $name, $attributes) = @_; |
|||
|
|||
my @attributes_strs; |
|||
if ($attributes) { |
|||
while (my ($key, $value) = each %$attributes) { |
|||
push @attributes_strs, qq{$key="$value"}; |
|||
} |
|||
} |
|||
my $attributes_str = join ' ', @attributes_strs; |
|||
|
|||
return "<$name $attributes_str>"; |
|||
} |
|||
|
|||
1; |
@ -1,4 +1,5 @@ |
|||
[% USE Asset %] |
|||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
|||
<link rel="shortcut icon" href="[% IF ( IntranetFavicon ) %][% IntranetFavicon %][% ELSE %][% interface %]/[% theme %]/img/favicon.ico[% END %]" type="image/x-icon" /> |
|||
<link rel="stylesheet" type="text/css" media="print" href="[% interface %]/[% theme %]/css/print_[% KOHA_VERSION %].css" /> |
|||
[% Asset.css("css/print.css", { media = "print" }) %] |
|||
|
|||
|
@ -1,7 +1,8 @@ |
|||
[% USE Asset %] |
|||
<script type="text/javascript"> |
|||
var GB_ROOT_DIR = "[% interface %]/lib/greybox/"; |
|||
</script> |
|||
<script type="text/javascript" src="[% interface %]/lib/greybox/AJS_[% KOHA_VERSION %].js"></script> |
|||
<script type="text/javascript" src="[% interface %]/lib/greybox/AJS_fx_[% KOHA_VERSION %].js"></script> |
|||
<script type="text/javascript" src="[% interface %]/lib/greybox/gb_scripts_[% KOHA_VERSION %].js"></script> |
|||
<link href="[% interface %]/lib/greybox/gb_styles_[% KOHA_VERSION %].css" rel="stylesheet" type="text/css" /> |
|||
[% Asset.js("lib/greybox/AJS.js") %] |
|||
[% Asset.js("lib/greybox/AJS_fx.js") %] |
|||
[% Asset.js("lib/greybox/gb_scripts.js") %] |
|||
[% Asset.css("lib/greybox/gb_styles.css") %] |
|||
|
@ -1,20 +1,21 @@ |
|||
[% USE Asset %] |
|||
[% INCLUDE 'doc-head-open.inc' %] |
|||
<title>Online help</title> |
|||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
|||
<link rel="shortcut icon" href="[% IF ( IntranetFavicon ) %][% IntranetFavicon %][% ELSE %][% interface %]/[% theme %]/img/favicon.ico[% END %]" type="image/x-icon" /> |
|||
<link rel="stylesheet" type="text/css" href="[% interface %]/lib/jquery/jquery-ui-1.11.4.min_[% KOHA_VERSION %].css" /> |
|||
<link rel="stylesheet" type="text/css" media="print" href="[% interface %]/[% theme %]/css/print_[% KOHA_VERSION %].css" /> |
|||
[% Asset.css("lib/jquery/jquery-ui-1.11.4.min.css") %] |
|||
[% Asset.css("css/print.css", { media = "print" }) %] |
|||
[% INCLUDE intranetstylesheet.inc %] |
|||
[% IF ( bidi ) %] |
|||
<link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/right-to-left_[% KOHA_VERSION %].css" /> |
|||
[% Asset.css("css/right-to-left.css") %] |
|||
[% END %] |
|||
<script type="text/javascript" src="[% interface %]/lib/jquery/jquery-2.2.3.min_[% KOHA_VERSION %].js"></script> |
|||
<script type="text/javascript" src="[% interface %]/lib/jquery/jquery-migrate-1.3.0.min_[% KOHA_VERSION %].js"></script> |
|||
<script type="text/javascript" src="[% interface %]/lib/jquery/jquery-ui-1.11.4.min_[% KOHA_VERSION %].js"></script> |
|||
<script type="text/javascript" src="[% interface %]/lib/shortcut/shortcut_[% KOHA_VERSION %].js"></script> |
|||
[% Asset.js("lib/jquery/jquery-2.2.3.min.js") %] |
|||
[% Asset.js("lib/jquery/jquery-migrate-1.3.0.min.js") %] |
|||
[% Asset.js("lib/jquery/jquery-ui-1.11.4.min.js") %] |
|||
[% Asset.js("lib/shortcut/shortcut.js") %] |
|||
<!-- koha core js --> |
|||
<script type="text/javascript" src="[% interface %]/[% theme %]/js/staff-global_[% KOHA_VERSION %].js"></script> |
|||
[% Asset.js("js/staff-global.js") %] |
|||
|
|||
<link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/help_[% KOHA_VERSION %].css" /> |
|||
[% Asset.css("css/help.css") %] |
|||
</head> |
|||
<body id="help" class="help"> |
|||
|
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue