CGI/Portable/AppMultiScreen.pm

filter: Statementstmt 40.9% 25 / 61 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 55.5% 5 / 9 Podpod 100% 4 / 4 totaltotal 36.9% 34 / 92 CC 7 SCAR 22.0
CC
7
Cov
33%
set_multi_screen_navigate_delegate_list
20.0
main
12.0
set_multi_screen_dispatch_one_delegate
6.0
CRAP 9.0
« DemoMailForm.pm ↑ Summary CGI/Portable/AppSplitScreen.pm »
1 =head1 NAME
2  
3 CGI::Portable::AppMultiScreen - Delegate construction, navigation of hierarchical screens
4  
5 =cut
6  
7 ######################################################################
8  
9 package CGI::Portable::AppMultiScreen;
10 1 require 5.004;
11  
12 # Copyright (c) 1999-2004, Darren R. Duncan. All rights reserved. This module
13 # is free software; you can redistribute it and/or modify it under the same terms
14 # as Perl itself. However, I do request that this copyright information and
15 # credits remain attached to the file. If you modify this module and
16 # redistribute a changed version then please attach a note listing the
17 # modifications. This module is available "as-is" and the author can not be held
18 # accountable for any problems resulting from its use.
19  
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 $VERSION = '0.50';
24  
25 ######################################################################
26  
27 =head1 DEPENDENCIES
28  
29 =head2 Perl Version
30  
31 5.004
32  
33 =head2 Standard Modules
34  
35 I<none>
36  
37 =head2 Nonstandard Modules
38  
39 CGI::Portable 0.50
40 CGI::Portable::AppStatic 0.50 (a superclass)
41  
42 =cut
43  
44 ######################################################################
45  
46 1 ▶use CGI::Portable 0.50;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use CGI::Portable::AppStatic 0.50;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 @ISA = qw( CGI::Portable::AppStatic );
49  
50 ######################################################################
51  
52 =head1 SYNOPSIS
53  
54 =head2 Simple program with multiple screens from different modules:
55  
56 #!/usr/bin/perl
57 use strict;
58 use warnings;
59  
60 require CGI::Portable;
61 my $globals = CGI::Portable->new();
62  
63 use Cwd;
64 $globals->file_path_root( cwd() ); # let us default to current working dir
65 $globals->file_path_delimiter( $^O=~/Mac/i ? ":" : $^O=~/Win/i ? "\\" : "/" );
66  
67 require CGI::Portable::AdapterCGI;
68 my $io = CGI::Portable::AdapterCGI->new();
69 $io->fetch_user_input( $globals );
70  
71 $globals->default_application_title( 'Demo Application' );
72 $globals->default_maintainer_name( 'Tony Simons' );
73 $globals->default_maintainer_email_address( 'tony@aardvark.net' );
74  
75 $globals->current_user_path_level( 1 );
76 $globals->set_prefs( 'myconfig.pl' );
77 $globals->call_component( 'CGI::Portable::AppMultiScreen' );
78  
79 $io->send_user_output( $globals );
80  
81 1;
82  
83 =head2 Content of file 'myconfig.pl' telling program what to do:
84  
85 I<This is not a whole example, but you can get an idea what to substitute.
86 This example shows 5 screens handled by 5 module instances, most of
87 which has their own preferences and optional subdirectory for files.>
88  
89 my $rh_preferences = {
90 delegate_list => {
91 pg_one => {
92 file_subdir => 'statics',
93 preferences => { filename => 'welcome.html' },
94 module_name => 'other::module::textfile',
95 },
96 pg_two => {
97 file_subdir => 'content',
98 preferences => 'content_config.pl',
99 module_name => 'other::module::content',
100 },
101 pg_three => {
102 file_subdir => 'indexes',
103 preferences => { key1 => 'value1', key2 => 'value2' },
104 module_name => 'other::module::search',
105 },
106 pg_four => {
107 file_subdir => 'statics',
108 preferences => { filename => 'help.html' },
109 module_name => 'other::module::textfile',
110 },
111 },
112 default_delegate => 'pg_one',
113 unknown_delegate => {
114 file_subdir => undef,
115 preferences => {},
116 module_name => 'other::module::error',
117 },
118 };
119  
120 =head1 DESCRIPTION
121  
122 This Perl 5 object class is a simple encapsulated application, or "component",
123 that runs in the CGI::Portable environment. It allows you to easily define a
124 group of screens that are related, delegate the construction of each screen to
125 separate "components", and simplify the creation of links between the screens.
126 When screens are related hierarchically according to the possible values of the
127 "user path", this class will evaluate the "current user path element" and compare
128 it to the valid options you specify, in order to resolve exactly which screen the
129 user requested. You define what options are available at the current hierarchy
130 node or branch within the standard "preferences", or specifically in the
131 "delegate_list" hash preference; each key/value pair has a user path segment to
132 match and instructions for how to build that chosen node or screen. Each hash
133 value says what delegate module to call and what its preferences are. If the
134 option leads to another branch then the called module could be another
135 AppMultiScreen instance; these can be chained as far as you want. This class is
136 subclassed from CGI::Portable::AppStatic, which is convenient if you want to do
137 simple post-processing of your new screen using its preferences. This module is
138 designed to be easily subclassed by your own application components, should you
139 wish to extend or customize its functionality.
140  
141 =cut
142  
143 ######################################################################
144  
145 # Constant values used by this class:
146  
147 # Keys for items in site page preferences:
148 1 my $PKEY_DELEGATE_LIST = 'delegate_list';
149 1 my $PKEY_DEFAULT_DELEGATE = 'default_delegate';
150 1 my $PKEY_UNKNOWN_DELEGATE = 'unknown_delegate';
151  
152 # Keys for elements in $PKEY_DELEGATE_LIST hash:
153 1 my $DKEY_FILE_SUBDIR = 'file_subdir';
154 1 my $DKEY_PREFERENCES = 'preferences';
155 1 my $DKEY_MODULE_NAME = 'module_name';
156  
157 ######################################################################
158  
159 =head1 SYNTAX
160  
161 This class does not export any functions or methods, so you need to call them
162 using object notation. This means using B<Class-E<gt>function()> for functions
163 and B<$object-E<gt>method()> for methods. If you are inheriting this class for
164 your own modules, then that often means something like B<$self-E<gt>method()>.
165  
166 This class does not have any of its own stored properties, so there is no risk
167 of property namespace collisions with subclasses. Instead, you must provide the
168 CGI::Portable object to each method explicitely as its first argument.
169  
170 =head1 THE ONLY PUBLIC METHOD
171  
172 =head2 main( GLOBALS )
173  
174 You invoke this method to run the application component that is encapsulated by
175 this class. The required argument GLOBALS is an CGI::Portable object that
176 you have previously configured to hold the instance settings and user input for
177 this class. When this method returns then the encapsulated application will
178 have finished and you can get its user output from the CGI::Portable object.
179  
180 This method is simple and intended to be overriden by subclasses. All it does
181 by itself is invoke the private methods listed under PRIVATE METHODS FOR USE BY
182 SUBCLASSES, and equivalents in CGI::Portable::Static, which do the actual work.
183  
184 =cut
185  
186 ######################################################################
187  
188 0 ▶sub main {
Subroutine main: not called CC=3 SCAR=24.8
Pod documented
189 0 my ($class, $globals) = @_;
190 0 ▶ my $self = bless( {}, ref($class) || $class );
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
191  
192 0 ▶ UNIVERSAL::isa( $globals, 'CGI::Portable' ) or
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($globals, "CGI::Portable") or die "initializer is not a valid CGI::Portable object" 0 0 0
193 die "initializer is not a valid CGI::Portable object";
194  
195 0 $self->set_static_low_replace( $globals );
196  
197 0 $self->set_multi_screen_navigate_delegate_list( $globals );
198  
199 0 $self->set_static_high_replace( $globals );
200 0 $self->set_static_attach_unordered( $globals );
201 0 $self->set_static_attach_ordered( $globals );
202 0 $self->set_static_search_and_replace( $globals );
203 }
204  
205 ######################################################################
206  
207 =head1 PREFERENCES HANDLED BY THIS MODULE
208  
209 This module is a subclass of CGI::Portable::AppStatic and will handle all of
210 that module's preferences in addition to the new ones discussed below.
211  
212 The preferences specifically for CGI::Portable::AppMultiScreen allow you to
213 define a list of other modules which are to be called under certain circumstances
214 to construct a result screen. AppMultiScreen doesn't normally do any screen
215 construction by itself, but instead handles the conditional logic required to
216 pick one module from the list and call it. The key preference that you need to
217 set is "delegate_list", but you can optionally set "default_delegate" and
218 "unknown_delegate" as well. See the SYNOPSIS for the format of each preference.
219  
220 =head2 delegate_list
221  
222 This hash-ref preference is a lookup table which matches the "current user path
223 element" with an "application component" module instance that will handle the
224 screen or hierarchy of screens associated with the currently resolved user path.
225 The keys in the hash are scalars that match a user path element, and associated
226 values are hash-refs that say what other module to call and what its config
227 preferences are. A key can only match if the associated value is a hash-ref.
228  
229 Each of these latter hash-refs must have values for at least these two keys set:
230 "module_name" and "preferences". Each can optionally have this one key set:
231 "file_subdir". The value for "module_name" is a scalar having the name of the
232 Perl 5 module to invoke as an "application component" that will make the new
233 result screen. The value for "preferences" can be either a hash-ref having
234 literal preferences for the invoked module, or a scalar having a filename of a
235 Perl file that can be executed to return the same preferences hash ref; it must
236 be an empty hash ref if the module takes no preferences. The value for
237 "file_subdir" is a relative file system path within which the invoked module
238 should look for its files (or config file) if any; if this value is not set then
239 the current file directory is used instead.
240  
241 Delegated modules operate within a new "context" created by the current
242 CGI::Portable object's make_new_context() method. Within that context,
243 AppMultiScreen does the following: 1. call inc_user_path_level() so that the
244 delegated module sees a different "current user path element" than this one does;
245 2. call navigate_url_path() with the "current user path element" as its argument
246 so that any self-referencing urls generated by the delegated module are correct
247 for the current screen; 3. call navigate_file_path() with the "file_subdir" value
248 as its argument; 4. call set_prefs() with the "preferences" value as its
249 argument; 5. call call_component() with the "module_name" value as its argument.
250 Finally, the output held by the inner context is saved using the current
251 CGI::Portable object's take_context_output() method.
252  
253 =head2 default_delegate
254  
255 This optional scalar preference is a default value for the "current user path
256 element" that will be used when processing "delegate_list" in situations where
257 the existing "current user path element" is not defined or false. The
258 functionality of this is similar to how web browsers look for specific filenames
259 like "index.html" when the user didn't specify a file name inside a directory.
260 If this preference is not set then it is unlikely that any screen would match the
261 user's request.
262  
263 =head2 unknown_delegate
264  
265 This optional hash-ref preference lets you customize the response screen that
266 the user will get if they provide a "current user path element" that does not
267 match any keys in the "delegate_list" lookup table. The functionality of this
268 is similar to how web browsers produce a "404 Page Not Found" message when the
269 user specifies a file or directory name that doesn't exist. This preference is
270 formatted the name way as a value in "delegate_list", so it requires the same
271 "module_name" and "preferences" settings to work properly. If this preference
272 is not set then AppMultiScreen will generate a default error screen instead.
273  
274 =head1 PRIVATE METHODS FOR USE BY SUBCLASSES
275  
276 =head2 set_multi_screen_navigate_delegate_list( GLOBALS )
277  
278 This method will resolve the "current user path element" towards a screen some
279 how, using the [delegate_list, default_delegate, unknown_delegate] preferences to
280 decide how that is done. If a preferences-defined screen is resolved to, whether
281 a normal or an error screen, then this method calls the
282 set_multi_screen_dispatch_one_delegate() method to handle the details of invoking
283 the delegate module, including any context-related matters. Otherwise, this
284 method calls the set_multi_screen_no_delegate_message() method which produces a
285 default error screen instead.
286  
287 =cut
288  
289 ######################################################################
290  
291 0 ▶sub set_multi_screen_navigate_delegate_list {
Subroutine set_multi_screen_navigate_delegate_list: not called CC=4 SCAR=30.0
Pod documented
292 0 my ($self, $globals) = @_;
293 0 my $rh_prefs = $globals->get_prefs_ref();
294  
295 0 my $user_path_element = $globals->current_user_path_element();
296 0 ▶ $user_path_element ||= $rh_prefs->{$PKEY_DEFAULT_DELEGATE};
Condition0%0/3
$user_path_element ||= $rh_prefs->{$PKEY_DEFAULT_DELEGATE}
l!l&&r!l&&!r
000
MC/DC0%0/2
$user_path_element ||= $rh_prefs->{$PKEY_DEFAULT_DELEGATE}
$user_path_element $rh_prefs->{$PKEY_DEFAULT_DELEGATE}
Truth table0%0/3
A ||= B
A= $user_path_elementB= $rh_prefs->{$PKEY_DEFAULT_DELEGATE}
ABresult
1X1
011
000
297  
298 0 my $delegate = $rh_prefs->{$PKEY_DELEGATE_LIST}->{$user_path_element};
299  
300 0 ▶ unless( ref( $delegate ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $delegate eq "HASH") 0 0 0
301 0 $delegate = $rh_prefs->{$PKEY_UNKNOWN_DELEGATE};
302 }
303  
304 0 ▶ if( ref( $delegate ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $delegate eq 'HASH') { } 0 0 0
305 0 $self->set_multi_screen_dispatch_one_delegate(
306 $globals, $delegate, $user_path_element );
307  
308 } else {
309 0 $self->set_multi_screen_no_delegate_message( $globals );
310 }
311 }
312  
313 ######################################################################
314  
315 =head2 set_multi_screen_dispatch_one_delegate( GLOBALS, DELEGATE, USER_PATH )
316  
317 This method will use its DELEGATE argument to construct a new screen and store it
318 in the GLOBALS context argument. DELEGATE is a hash ref whose values describe
319 how the new screen is to be made. It's properties are described by the
320 delegate_list preference documentation, above, as DELEGATE can be set as an
321 element in the delegate_list lookup table; it may also have been set from the
322 unknown_delegate preference. This method uses its USER_PATH argument as an
323 argument for navigate_url_path() rather than taking the "current user path
324 element" from GLOBALS because the latter may be empty, meaning that USER_PATH
325 would have the default value. This method returns without doing anything if
326 DELEGATE is not a valid hash ref.
327  
328 =cut
329  
330 ######################################################################
331  
332 0 ▶sub set_multi_screen_dispatch_one_delegate {
Subroutine set_multi_screen_dispatch_one_delegate: not called CC=2 SCAR=17.9
Pod documented
333 0 my ($self, $globals, $delegate, $user_path_element) = @_;
334 0 ▶ ref( $delegate ) eq 'HASH' or return( 0 );
Branch: 1 branch
BranchTrueFalseTotal
ref $delegate eq "HASH" or return 0 0 0 0
335 0 my $inner_context = $globals->make_new_context();
336 0 $inner_context->inc_user_path_level();
337 0 $inner_context->navigate_url_path( $user_path_element );
338 0 $inner_context->navigate_file_path( $delegate->{$DKEY_FILE_SUBDIR} );
339 0 $inner_context->set_prefs( $delegate->{$DKEY_PREFERENCES} );
340 0 $inner_context->call_component( $delegate->{$DKEY_MODULE_NAME} );
341 0 $globals->take_context_output( $inner_context );
342 }
343  
344 ######################################################################
345  
346 =head2 set_multi_screen_no_delegate_message( GLOBALS )
347  
348 This method produces a default error screen to use if the user requested a
349 screen not defined in the "delegate_list" preference, and the "unknown_delegate"
350 preference is not set. This default screen has some resemblence to the error
351 screen that CGI::Portable's call_component() method produces when it fails,
352 except that the other message is for a program error while this method's screen
353 is for a user error. It says '404 Page Not Found'. Since this method assumes
354 a user error, CGI::Portable's error list is not supplemented.
355  
356 =cut
357  
358 ######################################################################
359  
360 0 ▶sub set_multi_screen_no_delegate_message {
Subroutine set_multi_screen_no_delegate_message: not called CC=1 SCAR=6.9
Pod documented
361 0 my ($self, $globals) = @_;
362  
363 0 $globals->http_status_code( '404 Not Found' );
364  
365 0 $globals->page_title( '404 Page Not Found' );
366  
367 0 $globals->set_page_body( <<__endquote );
368 0 <h1>@{[$globals->page_title()]}</h1>
369  
370 0 <p>I'm sorry, but the screen you requested, "@{[$globals->user_path_string()]}",
371 doesn't seem to exist. If you manually typed that address into your browser,
372 then it is either out-dated or you mis-spelled it. If you got this error from
373 clicking a navigation link on another screen, then the problem is likely at this
374 end. In the latter case...</p>
375  
376 <p>This should be temporary, the result of a transient server problem or an
377 0 update being performed at the moment. Click @{[$globals->recall_html('here')]}
378 to automatically try again. If the problem persists, please try again later,
379 0 or send an @{[$globals->maintainer_email_html('e-mail')]} message about the
380 problem, so it can be fixed.</p>
381 __endquote
382 }
383  
384 ######################################################################
385  
386 1 1;
387 __END__
« DemoMailForm.pm ↑ Summary CGI/Portable/AppSplitScreen.pm »