Repository navigation
Expand file tree
/
Copy pathWebPage.pm
More file actions
455 lines (306 loc) · 9.01 KB
/
Copy pathWebPage.pm
File metadata and controls
455 lines (306 loc) · 9.01 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package WebPage;
use strict;
use warnings;
1;
=pod
=head1 NAME
WebPage - an abstract object for web pages used by WebApplication.
Instances of this object each represent a distinct page.
=head1 DESCRIPTION
TODO
=head1 METHODS
=over 4
=item * B<new> ()
Creates a new instance of the WebPage object.
=cut
sub new {
my ($class, $application) = @_;
my $self = { application => $application,
title => '',
components => {},
css => [],
omit_from_session => undef,
javascript => [],
data => {},
childComponents => [],
_orderedCSS => [],
_orderedJS => [],
};
bless($self, $class);
return $self;
}
=pod
=item * B<title> ()
Get/set the title of a page. By default the title is empty.
=cut
sub title {
my ($self, $title) = @_;
if (defined $title) {
$self->{'title'} = $title;
}
return $self->{'title'};
}
=pod
=item * B<init> ()
This method is called immediately after the new page object was created.
It should be used to perform all initialisations (requesting web components,
registering actions, getting PPO connections) and include required css or
javascript files (rf. to B<require_javascript> and B<require_css>).
The default init does nothing.
=cut
sub init {
}
=pod
=item * B<output> ()
Returns the html output of the page. This method is abstract and must be
implemented.
=cut
sub output {
my ($self) = @_;
die 'Abstract method "output" must be implemented in __PACKAGE__.\n';
}
=pod
=item * B<application> ()
Returns the reference to the WebApplication object which called this WebPage
=cut
sub application {
return $_[0]->{application};
}
sub app {
return $_[0]->{application};
}
=pod
=item * B<name> ()
Returns the page name which is used to retrieve this page using the
cgi param 'page';
=cut
sub name {
my $name = '';
if(ref($_[0]) =~ /^\w+\:\:\w+\:\:(\w+)$/) {
$name = $1;
} elsif (ref($_[0]) =~ /^\w+\:\:(\w+)$/) {
$name = $1;
}
return $name;
}
=pod
=item * B<url> ()
Returns the name of the cgi script of this page;
this is used as a relative url
=cut
sub url {
my ($self) = @_;
return $self->application->url . "?page=" . $self->name . "&";
}
=pod
=item * B<require_javascript> (I<js_file>)
Returns a reference to an array of javascript files to include into the output of
the html page. If the optional parameter I<js_file> is given, the page will require
that javascript file. To add a list of files, let I<js_file> be an array reference
of file names.
Call this method in the init method of your derived WebPage to include javascript
files into your page.
=cut
sub require_javascript {
if ($_[1]) {
if (ref $_[1] eq 'ARRAY') {
$_[0]->{'javascript'} = $_[1];
}
else {
push @{$_[0]->{'javascript'}}, $_[1];
}
}
return $_[0]->{'javascript'};
}
=item * B<require_javascript_ordered> ([filenames]) || (filename)
Adds a list of javascript files or a single file to an ordered list that
are required for this component. These files are always added after the
files in the component's child-components.
=cut
sub require_javascript_ordered {
my ($self, $files) = @_;
if (ref($files) eq 'ARRAY') {
push(@{$self->{"_orderedJS"}}, @$files);
} else {
push(@{$self->{"_orderedJS"}}, $files);
}
return $self->{"_orderedJS"};
}
=pod
=item * B<require_css> (I<css_file>)
Returns a reference to an array of css files to include into the output of the
html page. If the optional parameter I<css_file> is given, the page will require
that css file. To add a list of files, let I<css_file> be an array reference
of file names.
Call this method in the init method of your derived WebPage to include css files
into your page.
=cut
sub require_css {
if ($_[1]) {
if (ref $_[1] eq 'ARRAY') {
$_[0]->{'css'} = $_[1];
}
else {
push @{$_[0]->{'css'}}, $_[1];
}
}
return $_[0]->{'css'};
}
=item * B<require_css_ordered> ([filenames]) || (filename)
Adds a list of css files or a single file to an ordered list that
are required for this component. These files are always added after the
files in the component's child-components.
=cut
sub require_css_ordered {
my ($self, $files) = @_;
if (ref($files) eq 'ARRAY') {
push(@{$self->{"_orderedCSS"}}, @$files);
} else {
push(@{$self->{"_orderedCSS"}}, $files);
}
return $self->{"_orderedCSS"};
}
=pod
=item * B<start_form> (I<id>, I<state>, I<target>)
Returns the start of a form
Parameters:
id - (optional) an html id that can be referenced by javascript
state - (optional) a hashref whose keys will be turned into the names of hidden
variables with the according values set as values. If this is 1 and not a hashref,
all key/value pairs of the CGI object of the previous invocation of the script
are preserved.
target - (optional) the name of the target window for this form
=cut
sub start_form {
my ($self, $id, $state, $target) = @_;
my $id_string = ($id) ? " id='$id'" : '';
my $target_string = "";
if (defined($target)) {
$target_string = " target=$target";
}
my $start_form = "<form method='post'$id_string enctype='multipart/form-data' action='".
$self->application->url . "' style='margin: 0px; padding: 0px;'$target_string>\n";
my $cgi = $self->application->cgi;
if (ref($state) eq 'HASH') {
foreach my $key (keys(%$state)) {
if ( ref($state->{$key}) eq 'ARRAY' ) {
foreach my $val ( @{$state->{$key}} ) {
$start_form .= $self->application->cgi->hidden(-name=>$key, -id=>$key, -value=>$val, -override=>1) . "\n";
}
} else {
if ($key && defined($state->{$key})) {
$start_form .= $self->application->cgi->hidden(-name=>$key, -id=>$key, -value=>$state->{$key}, -override=>1) . "\n";
}
}
}
unless (exists $state->{page}) {
$start_form .= $self->application->cgi->hidden(-name=>'page', -id=>'page', -value=>$self->name, -overrride=>1) . "\n";
}
}
elsif ($state) {
my $cgi = $self->application->cgi;
my @names = $cgi->param;
foreach my $name (@names) {
next if ($name eq 'action');
$start_form .= $cgi->hidden(-name=>$name, -id=>$name, -value=>$cgi->param($name), -overrride=>1) . "\n";
}
}
else {
$start_form .= $cgi->hidden(-name=>'page', -id=>'page', -value=>$self->name, -overrride=>1) . "\n";
}
return $start_form;
}
=pod
=item * B<end_form> ()
Returns the end of a form
=cut
sub end_form {
my ($self) = @_;
return "</form>";
}
=pod
=item * B<required_rights> ()
Returns an empty array, should be overwritten by subclass if rights
are required to view the page.
=cut
sub required_rights {
return [];
}
=pod
=item * B<omit_from_session> (I<boolean>)
Returns true if a page should not be stored in the history.
=cut
sub omit_from_session {
my $self = shift;
if (scalar(@_)) {
$self->{omit_from_session} = shift;
}
return $self->{omit_from_session};
}
=pod
=item * B<supported_rights> ()
Returns a reference to an array of right object this page supports.
This method should be overwritten for any page that supports rights.
=cut
sub supported_rights {
return [];
}
=pod
=item * B<data> (I<id>, I<value>)
Method to store and retrieve data within the page object. The parameter I<id>
is the key used to store/retrieve the data. If you provide I<value> the method
will store the data, if not the stored data will returned.
=cut
sub data {
my $self = shift;
my $id = shift;
unless ($id) {
die "No id key for data given.";
}
# if ( $id and scalar(@_) == 0 and
# !exists($self->{data}->{$id}) ) {
# die "Retrieving unknown id key: $id.";
# }
if (scalar(@_)) {
$self->{data}->{$id} = shift;
}
return $self->{data}->{$id};
}
=pod
=item * B<robot_content> ()
Returns the html which will only be printed if the user agent is a robot.
This should be overwritten if you want special content to be seen by bots
like i.e. GoogleBot only.
=cut
sub robot_content {
return "";
}
=pod
=item * B<button> ($value, %options)
Returns the html for a submit button. The position parameter is the button value
(default C<Submit>). Any other properties can be added as part of the options hash.
No leading C<-> is necessary on the option name. Thus,
$page->button('OK', name => 'frog')
will generate a button with a value of C<OK> and a name of C<frog>. Use this method
instead of CGI methods or raw literals in order to automatically include the button
style class.
To generate a pure button (as opposed to a submit button), specify
type => 'button'
in the options.
=cut
sub button {
my ($self, $value, %options) = @_;
my $app = $self->{application};
return $app->button($value, %options);
}
=pod
=item * B<register_component> ($Class, $id)
Registers WebComponent with the page. This is identical to the
WebApplication call, but preserves component dependencies.
=cut
sub register_component {
my ($self, $component, $id) = @_;
my $web_component = $self->application->register_component($component, $id);
push(@{$self->{"childComponents"}}, $web_component);
return $web_component;
}