1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::Output::Template::Document;
## no critic(Perl::Critic::Policy::OTRS::RequireCamelCase)
use strict;
use warnings;
use parent qw (Template::Document);
our $ObjectManagerDisabled = 1;
=head1 NAME
Kernel::Output::Template::Document - Template Toolkit document extension package
=head1 PUBLIC INTERFACE
=head2 process()
process this template document. This method is inherited from
Template::Document and used to perform some up-front initialization
and processing.
=cut
sub process {
my ( $Self, $Context ) = @_;
$Self->_InstallOTRSExtensions($Context);
$Self->_PrecalculateBlockStructure($Context);
$Self->_PrecalculateBlockHookSubscriptions($Context);
return $Self->SUPER::process($Context);
}
=begin Internal:
=head2 _InstallOTRSExtensions()
adds some OTRS specific extensions to Template::Toolkit.
=cut
sub _InstallOTRSExtensions {
my ( $Self, $Context ) = @_;
# Already installed, nothing to do.
return if $Context->stash()->get('OTRS');
#
# Load the OTRS plugin. This will register some filters and functions.
#
$Context->stash()->set( 'OTRS', $Context->plugin('OTRS') );
#
# The RenderBlock macro makes it possible to use the old dtl:block-Style block calls
# that are still used by OTRS with Template::Toolkit.
#
# The block data is passed to the template, and this macro processes it and calls the relevant
# blocks.
#
$Context->stash()->set(
'PerformRenderBlock',
sub {
my $output = '';
my ( %_tt_args, $_tt_params );
$_tt_args{'BlockName'} = shift;
$_tt_params = shift;
$_tt_params = {} if ref $_tt_params ne 'HASH';
$_tt_params = { %_tt_args, %$_tt_params };
my $stash = $Context->localise($_tt_params);
eval {
my $BlockName = $stash->get('BlockName');
my $ParentBlock = $stash->get('ParentBlock') || $stash->{_BlockTree};
return if !exists $ParentBlock->{Children};
return if !exists $ParentBlock->{Children}->{$BlockName};
my $TemplateName = $stash->get('template')->{name} // '';
$TemplateName = substr( $TemplateName, 0, -3 ); # remove .tt extension
my $GenerateBlockHook =
$Context->{LayoutObject}->{_BlockHookSubscriptions}->{$TemplateName}->{$BlockName};
for my $TargetBlock ( @{ $ParentBlock->{Children}->{$BlockName} } ) {
$output .= "<!--HookStart${BlockName}-->\n" if $GenerateBlockHook;
$output .= $Context->process(
$TargetBlock->{Path},
{
'Data' => $TargetBlock->{Data},
'ParentBlock' => $TargetBlock,
},
);
$output .= "<!--HookEnd${BlockName}-->\n" if $GenerateBlockHook;
}
delete $ParentBlock->{Children}->{$BlockName};
};
$stash = $Context->delocalise();
die $@ if $@;
return $output;
}
);
#
# This block is used to cut out JavaScript code from the templates and insert it in the
# footer of the page, all in one place.
#
# Usage:
# [% WRAPPER JSOnDocumentComplete -%]
# console.log();
# [%- END %]
#
$Self->{_DEFBLOCKS}->{JSOnDocumentComplete} //= sub {
my $context = shift || die "template sub called without context\n";
my $stash = $context->stash();
my $output = '';
my $_tt_error;
eval {
if ( $stash->get( [ 'global', 0, 'KeepScriptTags', 0 ] ) ) {
$output .= $stash->get('content');
}
else {
push @{ $context->{LayoutObject}->{_JSOnDocumentComplete} }, $stash->get('content');
}
};
if ($@) {
$_tt_error = $context->catch( $@, \$output );
die $_tt_error if $_tt_error->type() ne 'return';
}
return $output;
};
#
# This block is used to insert the collected JavaScript code in the page footer.
#
$Self->{_DEFBLOCKS}->{JSOnDocumentCompleteInsert} //= sub {
my $context = shift || die "template sub called without context\n";
my $stash = $context->stash();
my $output = '';
my $_tt_error;
eval {
my $Code = join "\n", @{ $context->{LayoutObject}->{_JSOnDocumentComplete} || [] };
# remove opening script tags
$Code =~ s{ \s* <script[^>]+> (?:\s*<!--)? (?:\s*//\s*<!\[CDATA\[)? \n? }{}smxg;
# remove closing script tags
$Code =~ s{ \s* (?:-->\s*)? (?://\s*\]\]>\s*)? </script> \n? }{\n\n}smxg;
# remove additional newlines at the end of the code block
$Code =~ s{ \n+ \z }{\n}smxg;
$output .= $Code;
delete $context->{LayoutObject}->{_JSOnDocumentComplete};
};
if ($@) {
$_tt_error = $context->catch( $@, \$output );
die $_tt_error if $_tt_error->type() ne 'return';
}
return $output;
};
#
# This block is used to cut out JavaScript data that needs to be inserted to Core.Config
# from the templates and insert it in the footer of the page, all in one place.
#
# Usage:
# [% Process JSData
# Key = 'Test.Key'
# Value = { ... }
# %]
#
#
$Self->{_DEFBLOCKS}->{JSData} //= sub {
my $context = shift || die "template sub called without context\n";
my $stash = $context->stash();
my $output = '';
my $_tt_error;
eval {
my $Key = $stash->get('Key');
my $Value = $stash->get('Value');
return $output if !$Key;
$context->{LayoutObject}->{_JSData} //= {};
$context->{LayoutObject}->{_JSData}->{$Key} = $Value;
};
if ($@) {
$_tt_error = $context->catch( $@, \$output );
die $_tt_error if $_tt_error->type() ne 'return';
}
return $output;
};
#
# This block is used to insert the collected JavaScript data in the page footer.
#
$Self->{_DEFBLOCKS}->{JSDataInsert} //= sub {
my $context = shift || die "template sub called without context\n";
my $stash = $context->stash();
my $output = '';
my $_tt_error;
eval {
my %Data = %{ $context->{LayoutObject}->{_JSData} // {} };
if (%Data) {
my $JSONString = $Kernel::OM->Get('Kernel::System::JSON')->Encode(
Data => \%Data,
SortKeys => 1,
);
# Escape closing script tags in the JSON content as they will confuse the
# browser's parser.
$JSONString =~ s{ </(?<ScriptTag>script)}{<\\/$+{ScriptTag}}ismxg;
$output .= "Core.Config.AddConfig($JSONString);\n";
}
delete $context->{LayoutObject}->{_JSData};
};
if ($@) {
$_tt_error = $context->catch( $@, \$output );
die $_tt_error if $_tt_error->type() ne 'return';
}
return $output;
};
return;
}
=head2 _PrecalculateBlockStructure()
pre-calculates the tree structure of the blocks so that it
can be used by PerformRenderBlock in an efficient way.
=cut
sub _PrecalculateBlockStructure {
my ( $Self, $Context ) = @_;
my $Defblocks = $Self->{_DEFBLOCKS} || {};
my $BlockData = $Context->stash()->get( [ 'global', 0, 'BlockData', 0 ] ) || [];
return if !@{$BlockData};
my $BlockParents = {};
my $BlockPaths = {};
BLOCKPATHIDENTIFIER:
for my $BlockIdentifier ( sort keys %{$Defblocks} ) {
my @BlockPath = split( m{/}, $BlockIdentifier );
my $BlockPathLength = scalar @BlockPath;
next BLOCKPATHIDENTIFIER if !$BlockPathLength;
$BlockPaths->{ $BlockPath[-1] } = $BlockIdentifier;
$BlockParents->{ $BlockPath[-1] } = [ splice( @BlockPath, 0, $BlockPathLength - 1 ) ];
}
$Context->stash()->{_BlockTree} = {};
my $BlockIndex = 0;
BLOCK:
while ( $BlockIndex < @{$BlockData} ) {
my $Block = $BlockData->[$BlockIndex];
my $BlockName = $Block->{Name};
if ( !exists $BlockPaths->{$BlockName} ) {
$BlockIndex++;
next BLOCK;
}
my $BlockPointer = $Context->stash()->{_BlockTree};
PARENTBLOCK:
for my $ParentBlock ( @{ $BlockParents->{$BlockName} // [] } ) {
# Check if a parent node can be found
if (
!exists $BlockPointer->{Children}
|| !exists $BlockPointer->{Children}->{$ParentBlock}
)
{
# Parent node was not found. That means we dan discard this block.
splice @{$BlockData}, $BlockIndex, 1;
next BLOCK;
}
# Ok, parent block found, update block pointer to last element
$BlockPointer = $BlockPointer->{Children}->{$ParentBlock}->[-1];
}
$Block->{Path} = $BlockPaths->{$BlockName};
# Ok, the parent block pointer was apparently set correctly.
# Now append the data of our current block.
push @{ $BlockPointer->{Children}->{$BlockName} }, $Block;
# Remove block data
splice @{$BlockData}, $BlockIndex, 1;
}
return;
}
=head2 _PrecalculateBlockHookSubscriptions()
=cut
sub _PrecalculateBlockHookSubscriptions {
my ( $Self, $Context ) = @_;
# Only calculate once per LayoutObject
return if defined $Context->{LayoutObject}->{_BlockHookSubscriptions};
my $Config = $Kernel::OM->Get('Kernel::Config')->Get('Frontend::Template::GenerateBlockHooks') // {};
my %BlockHooks;
for my $Key ( sort keys %{ $Config // {} } ) {
for my $Template ( sort keys %{ $Config->{$Key} // {} } ) {
for my $Block ( @{ $Config->{$Key}->{$Template} // [] } ) {
$BlockHooks{$Template}->{$Block} = 1;
}
}
}
$Context->{LayoutObject}->{_BlockHookSubscriptions} = \%BlockHooks;
return;
}
1;
=end Internal:
=head1 TERMS AND CONDITIONS
This software is part of the OTRS project (L<https://otrs.org/>).
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (GPL). If you
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
=cut
|