MxScreen/Form/Button.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 116 CC 2 SCAR 8.4
CC
2
Cov
0%
make
6.0
BEGIN
2.0
NAME
2.0
CRAP 2.3
1 # -*- Mode: perl -*-
2 #
3 # $Id: Button.pm,v 0.1 2001/04/22 17:57:03 ram Exp $
4 #
5 # Copyright (c) 1998-2001, Raphael Manfredi
6 # Copyright (c) 2000-2001, Christophe Dehaudt
7 #
8 # You may redistribute only under the terms of the Artistic License,
9 # as specified in the README file that comes with the distribution.
10 #
11 # HISTORY
12 # $Log: Button.pm,v $
13 # Revision 0.1 2001/04/22 17:57:03 ram
14 # Baseline for first Alpha release.
15 #
16 # $EndLog$
17 #
18  
19  
20 package CGI::MxScreen::Form::Button;
21  
22 use Carp::Datum;
23 use Getargs::Long qw(ignorecase);
24  
25 # index of the array
26 BEGIN {
27 sub NAME () {0}
28 sub TARGET () {1}
29 sub ACTION () {2}
30 sub VALUE () {3}
31 sub DYN_TARGET () {4}
32 sub ON_ERROR () {5}
33 sub DYN_ON_ERROR () {6}
34  
35 sub CGI_ARGS () {7}
36 }
37  
38  
39 #
40 # ->make
41 #
42 # Arguments: (that are considered, others values are kept aside)
43 # -name => string
44 # -action =>
45 # -target => 'symbol' or ['symbol' , @arg ]
46 # -value => string to display (optional, defaults to name)
47 # -dyn_target => 'symbol' or ['symbol' , @arg ]
48 # -on_error => 'symbol' or ['symbol' , @arg ]
49 # -dyn_on_error => 'symbol' or ['symbol' , @arg ]
50 #
51 sub make {
52 DFEATURE my $f_;
53 my $self = bless [], shift;
54 my ($name, $value, $target, $dyn_target, $action,
55 $on_error, $dyn_on_error) =
56 cxgetargs(@_, { -strict => 0, -extra => 0 },
57 -name => 's',
58 -value => ['s'],
59 -target => [],
60 -dyn_target => [],
61 -action => [],
62 -on_error => [],
63 -dyn_on_error => [],
64 );
65  
66 VERIFY defined $target || defined $dyn_target,
67 "must specify one of -target or -dyn_target";
68  
69 VERIFY !defined $on_error || !defined $dyn_on_error,
70 "cannot specify both -on_error and -dyn_on_error";
71  
72 VERIFY implies(defined $on_error || defined $dyn_on_error, defined $action),
73 "must specify -action when using -on_error or -dyn_on_error";
74  
75 $self->[VALUE] = $value eq '' ? $name : $value;
76 $self->update(@_);
77 return DVAL $self;
78 }
79  
80 #
81 # ->update
82 #
83 sub update {
84 DFEATURE my $f;
85 my $self = shift;
86
87 ($self->[NAME],
88 $self->[ACTION],
89 $self->[TARGET],
90 $self->[DYN_TARGET],
91 $self->[ON_ERROR],
92 $self->[DYN_ON_ERROR],
93 @{$self->[CGI_ARGS]}) =
94 cxgetargs(@_, {-strict => 0},
95 -name => 's',
96 -action => ['ARRAY'],
97 -target => [undef],
98 -dyn_target => [undef],
99 -on_error => [undef],
100 -dyn_on_error => [undef],
101 );
102  
103 DREQUIRE equiv(defined $self->[DYN_TARGET], !defined $self->[TARGET]);
104  
105 unshift @{$self->[CGI_ARGS]}, (-name => $self->name);
106 return DVOID;
107 }
108  
109  
110 # Temporary method
111 #
112 # To Be Removed
113 # when storable would be able to select what is stored with
114 # Storable::Hook
115 sub cleanup {
116 DFEATURE my $f_;
117 my $self = shift;
118
119 $#{$self} = DYN_ON_ERROR;
120  
121 return DVOID;
122 }
123  
124  
125 #########################################################################
126 # Class Feature: usable from the external world #
127 #########################################################################
128 sub name { $_[0]->[NAME] }
129 sub action { $_[0]->[ACTION] }
130 sub target { $_[0]->[TARGET] }
131 sub dyn_target { $_[0]->[DYN_TARGET] }
132 sub on_error { $_[0]->[ON_ERROR] }
133 sub dyn_on_error { $_[0]->[DYN_ON_ERROR] }
134 sub value { $_[0]->[VALUE] }
135  
136 sub is_computed_target { defined $_[0]->[DYN_TARGET] }
137 sub has_error_trap
138 { defined $_[0]->[ON_ERROR] || defined $_[0]->[DYN_ON_ERROR] }
139  
140 #
141 # ->properties
142 #
143 # return the full list of arg that were given at creation time. The
144 # result can be used to supply the CGI functions.
145 #
146 # Return:
147 # a list of arguments.
148 #
149 sub properties {
150 DFEATURE(my $f_);
151 my $self = shift;
152
153 return DARY (@{$self->[CGI_ARGS]});
154 }
155  
156 1;
157  
158 =head1 NAME
159  
160 CGI::MxScreen::Form::Button - A recorded button
161  
162 =head1 SYNOPSIS
163  
164 # $self is a CGI::MxScreen::Screen
165  
166 use CGI qw/:standard/;
167  
168 my $ok = $self->record_button(
169 -name => "ok",
170 -value => "OK to Continue",
171 -action => ['validate', [$obj, 'do_something', @args]],
172 -target => "next_screen",
173 -on_error => "error_screen",
174 );
175  
176 print submit($ok->properties);
177  
178 =head1 DESCRIPTION
179  
180 This class models a recorded button. One does not manually create objects
181 from this class, they are created by C<CGI::MxScreen> when the
182 C<record_button()> routine is called on a screen object to declare a new
183 button.
184  
185 In order to use the state machine features from C<CGI::MxScreen>, it is
186 necessary to declare all the submit buttons you are going to generate.
187 The declaration routine takes a set of meaningful arguments, and lets
188 the others pass through verbatim (they are recorded to be given back when
189 C<properties()> is called).
190  
191 The minimal set of arguments to supply are C<-name> and C<-target> (or
192 C<-dyn_target>). You will probably supply C<-action> as well if you
193 wish to perform validation of control fields, or any other processing
194 attached to the pressing of that button.
195  
196 =head1 INTERFACE
197  
198 =head2 Creation Arguments
199  
200 Some of the arguments below expect I<callback> arguments.
201 The callback representation rules are documented in L<CGI::MxScreen/Callbacks>.
202  
203 Some of the callbacks or arguments below are expected to yield I<states>.
204 See L<CGI::MxScreen/States> for state representation rules.
205  
206 The following named arguments may be given to C<record_button()>.
207 Any other argument is simply recorded and will be propagated via
208 C<properties()>. Arguments are all optional but for C<-name>, and one
209 of C<-target> or C<-dyn_target> must be supplied:
210  
211 =over 4
212  
213 =item C<-action> => [I<callback1>, I<callback2>, ...]
214  
215 The list of action callbacks that should be run when the button is pressed.
216 Those will be run before any state change, since failure of one of those
217 callbacks could mean we stay in the same state.
218  
219 A trailing C<CGI::MxScreen::Action_Env> object is appended to the list
220 of callback arguments. It can be used to check whether any action callback
221 in the chain as already reported an error. In the last action callback
222 listed, this could be used to execute some processing only if we're about
223 to change state, i.e. when no error occured.
224  
225 B<Note>: when using the second form of callback specification (i.e. an array
226 ref), double brackets must be used, since the list of callback actions must
227 itself be within brackets:
228  
229 -action => [['callback', $arg1]], # RIGHT
230  
231 If you were to write by mistake:
232  
233 -action => ['callback', $arg1], # WRONG
234  
235 then that would result in the following actions (C<$screen> is the current
236 screen object where the button is recorded):
237  
238 $screen->callback($env); # $env is the Action_Env
239 $screen->$arg1($env);
240  
241 which will usually not be what you intended.
242  
243 Each callback must return a success/error status, as documented
244 in L<CGI::MxScreen::Error>.
245  
246 =item C<-dyn_on_error> => I<callback>
247  
248 When any of the C<-action> callbacks returns a non-OK status, an error flag
249 is raised. By default, the same screen will be re-displayed. When a
250 C<-dyn_on_error> callback is specified, the screen to display is computed
251 dynamically. You may not use this option in conjunction with C<-on_error>.
252  
253 A trailing C<CGI::MxScreen::Action_Env> object is appended to the callback
254 argument list. This object records the failed action callbacks, in case
255 that could help determine the state to move to.
256 See L<CGI::MxScreen::Action_Env>.
257  
258 The I<callback> is expected to return a new state specification: it can
259 be a single scalar (the state name), or an array ref (state name as
260 first item, remaining values being C<display()> parameters).
261 See L<CGI::MxScreen/States> for details.
262  
263 =item C<-dyn_target> => I<callback>
264  
265 When a C<-dyn_target> callback is specified, the next target state is
266 computed dynamically. You may not use this option in conjunction with
267 C<-target>.
268  
269 The I<callback> is expected to return a new state specification: it can
270 be a single scalar (the state name), or an array ref (state name as
271 first item, remaining values being C<display()> parameters).
272 See L<CGI::MxScreen/States> for details.
273  
274 =item C<-name> => I<name>
275  
276 Madantory parameter, giving the name of the button. This is the CGI parameter
277 name. The displayed button will be labeled with I<name>, unless there is
278 also a C<-value> given.
279  
280 =item C<-on_error> => I<target_state>
281  
282 When any of the C<-action> callbacks returns a non-OK status, an error flag
283 is raised. By default, the same screen will be re-displayed. When an
284 C<-on_error> trap is specified, the screen to display is given by
285 I<target_state>. You cannot use C<-dyn_on_error> in conjunction with
286 this argument.
287  
288 The I<target_state> can be a single scalar (the state name), or an
289 array ref (state name as first item, remaining values being C<display()>
290 parameters). See L<CGI::MxScreen/States>.
291  
292 =item C<-target> => I<target_state>
293  
294 This argument defines the target state to move to when all action callabacks
295 (if any) returned an OK status. Either this argument or C<-dyn_target>
296 B<must> be specified when recording a button.
297  
298 =item C<-value> => I<value>
299  
300 This specifies the button's value, which will be displayed by browser instead
301 of the parameter name.
302  
303 =back
304  
305 Any other argument will be recorded as-is and passed through when
306 C<properties()> is called on the button object.
307  
308 =head2 Features
309  
310 Once created via C<record_button>, the following features may be called
311 on the object:
312  
313 =over 4
314  
315 =item C<has_error_trap>
316  
317 Returns I<true> when there is an C<-on_error> or C<-dyn_on_error> argument.
318  
319 =item C<name>
320  
321 Returns the button name.
322  
323 =item C<properties>
324  
325 Generates the list of CGI arguments suitable to use with the routines
326 in the C<CGI> modules. An easy way to generate a submit button is to
327 do:
328  
329 print submit($b->properties);
330  
331 assuming C<$b> was obtained through a C<record_button()> call.
332  
333 =item C<value>
334  
335 Returns the recorded button value, or the name if no value was given.
336 When referring to a button, this is the feature to use, as in:
337  
338 print p("Once done, press the", b($done->value), "button.");
339  
340 which lets you factorize the button's value (aka label) in one place, making
341 things easier if you decide to change it later on.
342  
343 =back
344  
345 =head1 AUTHORS
346  
347 The original authors are
348 Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>
349 and
350 Christophe Dehaudt F<E<lt>Christophe.Dehaudt@teamlog.frE<gt>>.
351  
352 Send bug reports, suggestions, problems or questions to
353 Jason Purdy F<E<lt>Jason@Purdy.INFOE<gt>>
354  
355 =head1 SEE ALSO
356  
357 CGI::MxScreen(3), CGI::MxScreen::Form::Field(3).
358  
359 =cut
360