Repository navigation
Expand file tree
/
Copy pathWebLayout.pm
More file actions
251 lines (194 loc) · 5.7 KB
/
Copy pathWebLayout.pm
File metadata and controls
251 lines (194 loc) · 5.7 KB
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
package WebLayout;
use strict;
use warnings;
use WebConfig;
use HTML::Template;
1;
=head3 new
my $layout = WebLayout->new($tmpl_data);
Create a layout object. The layout object contains all the information
needed to assemble a web page from the caller-specified templates.
=over 4
=item tmpl_data
Information about where to get the templates. If omitted, then default templates are used.
If a string, then the string should be the name of the template file for the page body.
If a hash reference, then the C<body> member must be the string for the body template and
the C<frame> member must be the string for the frame template.
=item RETURN
Returns a blessed layout object initialized using the specified template strings.
=back
=cut
sub new {
my ($class, $tmpl_path) = @_;
my ($body, $frame);
my $tmpl = $tmpl_path || TMPL_PATH.'WebLayoutDefault.tmpl';
if (ref $tmpl eq 'HASH') {
my $bodyString = $tmpl->{body};
my $frameString = $tmpl->{frame};
$body = HTML::Template->new(scalarref => \$bodyString, die_on_bad_params => 0);
$frame = HTML::Template->new(scalarref => \$frameString, die_on_bad_params => 0);
} else {
$body = HTML::Template->new(filename => $tmpl, die_on_bad_params => 0);
$frame = HTML::Template->new(filename => TMPL_PATH . 'WebLayoutFrame.tmpl',
die_on_bad_params => 0);
}
my $self = { 'templates' => [],
'default_template' => $body,
'frame' => $frame,
'title' => '',
'javascript' => [],
'css' => [],
'meta' => [],
'relocation' => "",
'page' => "",
'show_icon' => 0,
'icon_path' => '',
};
bless($self, $class);
return $self;
}
=head3 set_relocation
$layout->set_relocation($prefix);
Specify the relocation rule for relative URLs in links added to the
header. The rule will change the value C<./> at the beginning of a URL to
the specified prefix.
=over 4
=item prefix
Relative URL prefix used to get back to the normal location.
=back
=cut
sub set_relocation {
# Get the parameters.
my ($self, $prefix) = @_;
# Set the new relocation prefix.
$self->{relocation} = $prefix;
}
=head3 relocate
my $relocatedURL = $layout->relocate($url);
Relocate the specified URL using the stored relocation factor. The
relocation factor indicates the difference between the location of the
active CGI script and the expected CGI directory.
=over 4
=item url
URL to relocate.
=item RETURN
Returns a relocated URL. If the URL is absolute, it is unchanged. If it is relative, it
will be moved according to the instructions in the relocation prefix.
=back
=cut
sub relocate {
# Get the parameters.
my ($self, $url) = @_;
# Declare the return variable.
my $retVal;
# Determine the type of URL.
if ($url =~ m#^(http|/)#) {
# Here it's absolute, so we don't change it.
$retVal = $url;
} else {
# Here it's relative. We need to relocate it. Strip off a dot-slash. This
# is essentially a no-op.
$url =~ s#^\./##;
# Stash the relocation prefix in front.
$retVal = $self->{relocation} . $url;
}
# Return the result.
return $retVal;
}
sub set_content {
$_[0]->frame->param( TITLE => $_[1]->{'title'} );
my @warn = map { { MSG => $_ } } @{$_[1]->{'warnings'}};
my @info = map { { MSG => $_ } } @{$_[1]->{'info'}};
$_[0]->template->param( PAGETITLE => $_[1]->{'pagetitle'});
$_[0]->template->param( CONTENT => $_[1]->{'content'});
$_[0]->template->param( MENU => $_[1]->{'menu'});
$_[0]->template->param( USER => $_[1]->{'user'});
$_[0]->template->param( WARNINGS => \@warn );
$_[0]->template->param( INFO => \@info );
while (my($var, $val) = each %Conf::web_template_settings)
{
$_[0]->template->param($var => $val);
}
}
sub set_variable {
$_[0]->template->param( $_[1] => $_[2]);
}
sub set_page {
my ($self, $page) = @_;
$self->{page} = $page;
}
sub add_javascript {
if ($_[1]) {
push @{$_[0]->{'javascript'}}, { 'JSFILE' => $_[0]->relocate($_[1]) };
}
}
sub add_css {
if ($_[1]) {
unshift @{$_[0]->{'css'}}, { 'CSSFILE' => $_[0]->relocate($_[1]) };
}
}
sub add_css_reverse {
if ($_[1]) {
push @{$_[0]->{'css'}}, { 'CSSFILE' => $_[0]->relocate($_[1]) };
}
}
sub add_metatag {
if ($_[1]) {
push @{$_[0]->{'meta'}}, { 'METATAG' => $_[1] };
}
}
sub output {
my ($self) = @_;
my $retVal;
$self->frame->param( BODY => $self->template->output() );
$self->frame->param( JAVASCRIPT => $self->{'javascript'} );
$self->frame->param( CSS => $self->{'css'} );
$self->frame->param( META => $self->{'meta'} );
$self->frame->param( SHOW_ICON => $self->show_icon );
$self->frame->param( ICON_PATH => $self->icon_path );
$retVal = $self->frame->output();
return $retVal;
}
sub add_template {
my ($self, $tmpl, $pages) = @_;
my ($body);
if (ref $tmpl eq 'HASH') {
my $bodyString = $tmpl->{body};
$body = HTML::Template->new(scalarref => \$bodyString, die_on_bad_params => 0);
} else {
$body = HTML::Template->new(filename => $tmpl, die_on_bad_params => 0);
}
push @{$self->{templates}}, [$body, $pages];
}
sub template {
my ($self) = @_;
unless(defined $self->{page}){
return $self->{'default_template'};
} else {
foreach my $t (@{$self->{templates}}){
foreach (@{$t->[1]}){
if($self->{page} eq $_){
return $t->[0];
}
}
}
}
return $self->{'default_template'};
}
sub frame {
return $_[0]->{'frame'};
}
sub show_icon {
my ($self, $show) = @_;
if (defined($show)) {
$self->{show_icon} = $show;
}
return $self->{show_icon};
}
sub icon_path {
my ($self, $path) = @_;
if (defined($path)) {
$self->{icon_path} = $path;
}
return $self->{icon_path};
}