~ubuntu-branches/debian/sid/bugzilla/sid

« back to all changes in this revision

Viewing changes to Bugzilla/Template/Plugin/Hook.pm

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Bossek
  • Date: 2008-06-27 22:34:34 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080627223434-0ib57vstn43bb4a3
Tags: 3.0.4.1-1
* Update of French, Russian and German translations. (closes: #488251)
* Added Bulgarian and Belarusian translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- Mode: perl; indent-tabs-mode: nil -*-
2
 
#
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/
7
 
#
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.
12
 
#
13
 
# The Original Code is the Bugzilla Bug Tracking System.
14
 
#
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
18
 
# Rights Reserved.
19
 
#
20
 
# Contributor(s): Myk Melez <myk@mozilla.org>
21
 
#                 Zach Lipton <zach@zachlipton.com>
22
 
#
23
 
 
24
 
package Bugzilla::Template::Plugin::Hook;
25
 
 
26
 
use strict;
27
 
 
28
 
use Bugzilla::Constants;
29
 
use Bugzilla::Template;
30
 
use Bugzilla::Util;
31
 
use Bugzilla::Error;
32
 
use File::Spec;
33
 
 
34
 
use base qw(Template::Plugin);
35
 
 
36
 
sub load {
37
 
    my ($class, $context) = @_;
38
 
    return $class;
39
 
}
40
 
 
41
 
sub new {
42
 
    my ($class, $context) = @_;
43
 
    return bless { _CONTEXT => $context }, $class;
44
 
}
45
 
 
46
 
sub process {
47
 
    my ($self, $hook_name) = @_;
48
 
 
49
 
    my $paths = $self->{_CONTEXT}->{LOAD_TEMPLATES}->[0]->paths;
50
 
    my $template = $self->{_CONTEXT}->stash->{component}->{name};
51
 
    my @hooks = ();
52
 
    
53
 
    # sanity check:
54
 
    if (!$template =~ /[\w\.\/\-_\\]+/) {
55
 
        ThrowCodeError('template_invalid', { name => $template});
56
 
    }
57
 
 
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;
64
 
    my $type = $2;
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;
72
 
            if (-e $file) {
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:
76
 
                open (my $fh, $file);
77
 
                push(@hooks, $fh);
78
 
            }
79
 
        }
80
 
    }
81
 
    
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");
86
 
 
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);
90
 
 
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);
96
 
        }
97
 
    }
98
 
 
99
 
    my $output;
100
 
    foreach my $hook (@hooks) {
101
 
        $output .= $self->{_CONTEXT}->process($hook);
102
 
    }
103
 
    return $output;
104
 
}
105
 
 
106
 
# get a list of languages we accept so we can find the hook 
107
 
# that corresponds to our desired languages:
108
 
sub getLanguages() {
109
 
    my $languages = trim(Bugzilla->params->{'languages'});
110
 
    if (not ($languages =~ /,/)) { # only one language
111
 
        return $languages;
112
 
    }
113
 
    my @languages       = Bugzilla::Template::sortAcceptLanguage($languages);
114
 
    my @accept_language = Bugzilla::Template::sortAcceptLanguage($ENV{'HTTP_ACCEPT_LANGUAGE'} || "" );
115
 
    my @usedlanguages;
116
 
    foreach my $lang (@accept_language) {
117
 
        if(my @found = grep /^\Q$lang\E(-.+)?$/i, @languages) {
118
 
            push (@usedlanguages, @found);
119
 
        }
120
 
    }
121
 
    return @usedlanguages;
122
 
}
123
 
 
124
 
1;
125
 
 
126
 
__END__
127
 
 
128
 
=head1 NAME
129
 
 
130
 
Bugzilla::Template::Plugin::Hook
131
 
 
132
 
=head1 DESCRIPTION
133
 
 
134
 
Template Toolkit plugin to process hooks added into templates by extensions.
135
 
 
136
 
=head1 SEE ALSO
137
 
 
138
 
L<Template::Plugin>
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>