1
# -*- Mode: perl; indent-tabs-mode: nil -*-
3
# The contents of this file are subject to the Mozilla Public
4
# License Version 1.1 (the "License"); you may not use this file
5
# except in compliance with the License. You may obtain a copy of
6
# the License at http://www.mozilla.org/MPL/
8
# Software distributed under the License is distributed on an "AS
9
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
# implied. See the License for the specific language governing
11
# rights and limitations under the License.
13
# The Original Code is the Bugzilla Bug Tracking System.
15
# The Initial Developer of the Original Code is Netscape Communications
16
# Corporation. Portions created by Netscape are
17
# Copyright (C) 1998 Netscape Communications Corporation. All
20
# Contributor(s): Myk Melez <myk@mozilla.org>
21
# Zach Lipton <zach@zachlipton.com>
24
package Bugzilla::Template::Plugin::Hook;
28
use Bugzilla::Constants;
29
use Bugzilla::Template;
34
use base qw(Template::Plugin);
37
my ($class, $context) = @_;
42
my ($class, $context) = @_;
43
return bless { _CONTEXT => $context }, $class;
47
my ($self, $hook_name) = @_;
49
my $paths = $self->{_CONTEXT}->{LOAD_TEMPLATES}->[0]->paths;
50
my $template = $self->{_CONTEXT}->stash->{component}->{name};
54
if (!$template =~ /[\w\.\/\-_\\]+/) {
55
ThrowCodeError('template_invalid', { name => $template});
58
# also get extension hook files that live in extensions/:
59
# parse out the parts of the template name
60
my ($vol, $subpath, $filename) = File::Spec->splitpath($template);
61
$subpath = $subpath || '';
62
$filename =~ m/(.*)\.(.*)\.tmpl/;
63
my $templatename = $1;
65
# munge the filename to create the extension hook filename:
66
my $extensiontemplate = $subpath.'/'.$templatename.'-'.$hook_name.'.'.$type.'.tmpl';
67
my @extensions = glob(bz_locations()->{'extensionsdir'} . "/*");
68
my @usedlanguages = getLanguages();
69
foreach my $extension (@extensions) {
70
foreach my $language (@usedlanguages) {
71
my $file = $extension.'/template/'.$language.'/'.$extensiontemplate;
73
# tt is stubborn and won't take a template file not in its
74
# include path, so we open a filehandle and give it to process()
75
# so the hook gets invoked:
82
# we keep this too since you can still put hook templates in
83
# template/en/custom/hook
84
foreach my $path (@$paths) {
85
my @files = glob("$path/hook/$template/$hook_name/*.tmpl");
87
# Have to remove the templates path (INCLUDE_PATH) from the
88
# file path since the template processor auto-adds it back.
89
@files = map($_ =~ /^$path\/(.*)$/ ? $1 : {}, @files);
91
# Add found files to the list of hooks, but removing duplicates,
92
# which can happen when there are identical hooks or duplicate
93
# directories in the INCLUDE_PATH (the latter probably being a TT bug).
94
foreach my $file (@files) {
95
push(@hooks, $file) unless grep($file eq $_, @hooks);
100
foreach my $hook (@hooks) {
101
$output .= $self->{_CONTEXT}->process($hook);
106
# get a list of languages we accept so we can find the hook
107
# that corresponds to our desired languages:
109
my $languages = trim(Bugzilla->params->{'languages'});
110
if (not ($languages =~ /,/)) { # only one language
113
my @languages = Bugzilla::Template::sortAcceptLanguage($languages);
114
my @accept_language = Bugzilla::Template::sortAcceptLanguage($ENV{'HTTP_ACCEPT_LANGUAGE'} || "" );
116
foreach my $lang (@accept_language) {
117
if(my @found = grep /^\Q$lang\E(-.+)?$/i, @languages) {
118
push (@usedlanguages, @found);
121
return @usedlanguages;
130
Bugzilla::Template::Plugin::Hook
134
Template Toolkit plugin to process hooks added into templates by extensions.
139
L<http:E<sol>E<sol>www.bugzilla.orgE<sol>docsE<sol>tipE<sol>htmlE<sol>customization.html>
140
L<http:E<sol>E<sol>bugzilla.mozilla.orgE<sol>show_bug.cgi?id=229658>
141
L<http:E<sol>E<sol>bugzilla.mozilla.orgE<sol>show_bug.cgi?id=298341>