| 1 | # -*- Mode: perl -*- | ||
| 2 | # | ||
| 3 | # $Id: Screen.pm,v 0.1.1.1 2001/05/30 21:14:06 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: Screen.pm,v $ | ||
| 13 | # Revision 0.1.1.1 2001/05/30 21:14:06 ram | ||
| 14 | # patch1: added sub-section on creation routine | ||
| 15 | # | ||
| 16 | # Revision 0.1 2001/04/22 17:57:03 ram | ||
| 17 | # Baseline for first Alpha release. | ||
| 18 | # | ||
| 19 | # $EndLog$ | ||
| 20 | # | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | |||
| 24 | package CGI::MxScreen::Screen; | ||
| 25 | |||
| 26 | use Carp::Datum; | ||
| 27 | use Log::Agent; | ||
| 28 | use Getargs::Long; | ||
| 29 | |||
| 30 | use CGI::MxScreen::Constant; | ||
| 31 | use CGI::MxScreen::Error; | ||
| 32 | require CGI; | ||
| 33 | |||
| 34 | require CGI::MxScreen::Form::Field; | ||
| 35 | require CGI::MxScreen::Form::Button; | ||
| 36 | |||
| 37 | # | ||
| 38 | # ->make | ||
| 39 | # | ||
| 40 | # Screen is the base class of all user defined screens. | ||
| 41 | # | ||
| 42 | # Once done, the value of the defined parameters are initialized | ||
| 43 | # thanks to the data collected by the CGI package within the CGI post. | ||
| 44 | # For each of the parameters which has a given value, a field 'value' | ||
| 45 | # is inserted into the parameter record. | ||
| 46 | # | ||
| 47 | # Returns: a reference to the newly created object | ||
| 48 | # | ||
| 49 | sub make { | ||
| 50 | DFEATURE my $f_; | ||
| 51 | my $self = bless {}, shift; | ||
| 52 | |||
| 53 | my ($manager, $name, $title, $bgcolor) = cxgetargs(@_, | ||
| 54 | 'manager' => 'CGI::MxScreen', | ||
| 55 | 'name' => undef, | ||
| 56 | 'title' => undef, | ||
| 57 | 'bgcolor' => [] | ||
| 58 | ); | ||
| 59 | |||
| 60 | $self->{__name} = $name; | ||
| 61 | $self->{__title} = $title; | ||
| 62 | $self->{__bgcolor} = $bgcolor; | ||
| 63 | |||
| 64 | $self->relink_to_manager($manager); | ||
| 65 | $self->_init; # Internal initialization | ||
| 66 | $self->init; # May be redefined by heirs | ||
| 67 | |||
| 68 | return DVAL $self; | ||
| 69 | } | ||
| 70 | |||
| 71 | # | ||
| 72 | # ->init | ||
| 73 | # | ||
| 74 | # Initialization routine | ||
| 75 | # It is called the first time the screen is ever made. | ||
| 76 | # | ||
| 77 | # This routine is meant to be redefined by heirs, when needed. | ||
| 78 | # | ||
| 79 | sub init {} | ||
| 80 | |||
| 81 | # | ||
| 82 | # ->remake | ||
| 83 | # | ||
| 84 | # Alternate "creation" routine where object was already created but | ||
| 85 | # incompletely initialized. | ||
| 86 | # | ||
| 87 | sub remake { | ||
| 88 | DFEATURE my $f_; | ||
| 89 | my $self = shift; | ||
| 90 | my ($manager) = @_; | ||
| 91 | |||
| 92 | DREQUIRE defined $manager && $manager->isa("CGI::MxScreen"); | ||
| 93 | DREQUIRE $self->manager == $manager; | ||
| 94 | |||
| 95 | $self->_init; | ||
| 96 | return DVAL $self; | ||
| 97 | } | ||
| 98 | |||
| 99 | # | ||
| 100 | # ->relink_to_manager | ||
| 101 | # | ||
| 102 | # Partial initialization routine which links _manager and _context | ||
| 103 | # to the new manager. This allows callbacks referring to a screen to | ||
| 104 | # still use $screen->vars for instance and get something useful. | ||
| 105 | # | ||
| 106 | # This routine is systematically called on all the screens present in | ||
| 107 | # the context. | ||
| 108 | # | ||
| 109 | sub relink_to_manager { | ||
| 110 | DFEATURE my $f_; | ||
| 111 | my $self = shift; | ||
| 112 | my ($manager) = @_; | ||
| 113 | |||
| 114 | $self->{__manager} = $manager; | ||
| 115 | $self->{__context} = $manager->context(PERSISTENT); | ||
| 116 | |||
| 117 | return DVOID; | ||
| 118 | } | ||
| 119 | |||
| 120 | # | ||
| 121 | # ->_init | ||
| 122 | # | ||
| 123 | # Internal initalization routine. | ||
| 124 | # | ||
| 125 | sub _init { | ||
| 126 | DFEATURE my $f_; | ||
| 127 | my $self = shift; | ||
| 128 | my $manager = $self->manager; | ||
| 129 | |||
| 130 | # | ||
| 131 | # Restore previous displayed fields of this screen (if any) | ||
| 132 | # | ||
| 133 | |||
| 134 | $self->{__field} = {}; | ||
| 135 | for my $field (@{$manager->context(SCREEN_FIELD)}) { | ||
| 136 | DASSERT $field->isa('CGI::MxScreen::Form::Field'); | ||
| 137 | $self->{__field}->{$field->name} = $field; | ||
| 138 | } | ||
| 139 | |||
| 140 | # | ||
| 141 | # Restore previous displayed button of this screen (if any) | ||
| 142 | # | ||
| 143 | |||
| 144 | $self->{__transition} = {}; | ||
| 145 | for my $button (@{$manager->context(SCREEN_BUTTON)}) { | ||
| 146 | DASSERT $button->isa('CGI::MxScreen::Form::Button'); | ||
| 147 | $self->{__transition}->{$button->name} = $button; | ||
| 148 | } | ||
| 149 | |||
| 150 | return DVOID; | ||
| 151 | } | ||
| 152 | |||
| 153 | # | ||
| 154 | # Public attribute access | ||
| 155 | # | ||
| 156 | |||
| 157 | sub manager { $_[0]->{__manager} } | ||
| 158 | sub name { $_[0]->{__name} } | ||
| 159 | |||
| 160 | sub error { $_[0]->{__error} } | ||
| 161 | sub set_error { $_[0]->{__error} = $_[1] } | ||
| 162 | sub error_env { $_[0]->{__error_env} } | ||
| 163 | |||
| 164 | sub vars { $_[0]->{__context} } | ||
| 165 | sub screen_title { $_[0]->{__title} } # title() conflicts with CGI | ||
| 166 | sub default_button { $_[0]->{__default_button} } | ||
| 167 | |||
| 168 | # | ||
| 169 | # Private attribute access/setting | ||
| 170 | # | ||
| 171 | |||
| 172 | sub set_error_env { $_[0]->{__error_env} = $_[1] } | ||
| 173 | |||
| 174 | # | ||
| 175 | # ->bgcolor | ||
| 176 | # | ||
| 177 | # The background color used to display this screen | ||
| 178 | # | ||
| 179 | sub bgcolor { | ||
| 180 | DFEATURE my $f_; | ||
| 181 | my $self = shift; | ||
| 182 | my $color = $self->{__bgcolor}; | ||
| 183 | $color = $self->manager->bgcolor unless defined $color; | ||
| 184 | return DVAL $color; | ||
| 185 | } | ||
| 186 | |||
| 187 | # | ||
| 188 | # ->_clear_internal_context | ||
| 189 | # | ||
| 190 | # clear the fields and buttons context | ||
| 191 | # | ||
| 192 | # That method is not suppose to be used by someone else than the | ||
| 193 | # manager. | ||
| 194 | # | ||
| 195 | sub _clear_internal_context { | ||
| 196 | DFEATURE my $f_; | ||
| 197 | my $self = shift; | ||
| 198 | |||
| 199 | $self->{__field} = {}; | ||
| 200 | $self->{__transition} = {}; | ||
| 201 | $self->{__default_button} = undef; | ||
| 202 | |||
| 203 | return DVOID; | ||
| 204 | } | ||
| 205 | |||
| 206 | # | ||
| 207 | # Public methods | ||
| 208 | # | ||
| 209 | |||
| 210 | # | ||
| 211 | # ->set_default_button | ||
| 212 | # | ||
| 213 | # Screens may record a default button (object returned by record_button()) | ||
| 214 | # which will be used when the browser submits a form without indicating a | ||
| 215 | # pressed button, because they hit "return" in the sole textfield of a form. | ||
| 216 | # | ||
| 217 | sub set_default_button { | ||
| 218 | DFEATURE my $f_; | ||
| 219 | my $self = shift; | ||
| 220 | my ($button) = @_; | ||
| 221 | |||
| 222 | VERIFY ref $button && $button->isa("CGI::MxScreen::Form::Button"), | ||
| 223 | "argument is a button object: $button"; | ||
| 224 | |||
| 225 | my $prev_default = $self->{__default_button}; | ||
| 226 | logwarn "new default button (%s) in screen \"%s\" supersedes old (%s)", | ||
| 227 | $button->value, $self->name, $prev_default->value | ||
| 228 | if defined $prev_default; | ||
| 229 | |||
| 230 | return DVAL $self->{__default_button} = $button; | ||
| 231 | } | ||
| 232 | |||
| 233 | # | ||
| 234 | # ->current_screen | ||
| 235 | # | ||
| 236 | # Return screen id along with its parameters for displaying. | ||
| 237 | # | ||
| 238 | sub current_screen { | ||
| 239 | DFEATURE my $f_; | ||
| 240 | return DVAL $_[0]->manager->current_screen; | ||
| 241 | } | ||
| 242 | |||
| 243 | # | ||
| 244 | # ->previous_screen | ||
| 245 | # | ||
| 246 | # Return previous screen id along with its parameters for displaying. | ||
| 247 | # | ||
| 248 | sub previous_screen { | ||
| 249 | DFEATURE my $f_; | ||
| 250 | return DVAL $_[0]->manager->previous_screen; | ||
| 251 | } | ||
| 252 | |||
| 253 | # | ||
| 254 | # ->spring_screen | ||
| 255 | # | ||
| 256 | # Return screen we sprang from, along with its parameters for displaying. | ||
| 257 | # | ||
| 258 | sub spring_screen { | ||
| 259 | DFEATURE my $f_; | ||
| 260 | return DVAL $_[0]->manager->spring_screen; | ||
| 261 | } | ||
| 262 | |||
| 263 | # | ||
| 264 | # ->record_field | ||
| 265 | # | ||
| 266 | # Arguments: | ||
| 267 | # | ||
| 268 | sub record_field { | ||
| 269 | DFEATURE my $f_; | ||
| 270 | my $self = shift; | ||
| 271 | my (%args) = @_; | ||
| 272 | |||
| 273 | VERIFY defined $args{-name}; | ||
| 274 | |||
| 275 | my $mgr = $self->manager; | ||
| 276 | my $id = $args{-name}; | ||
| 277 | if (defined $self->{__field}->{$id}) { | ||
| 278 | DTRACE "field $id already exists"; | ||
| 279 | $self->{__field}->{$id}->update(%args); | ||
| 280 | |||
| 281 | # save it into the context that must have been previously reset | ||
| 282 | push @{$mgr->context(SCREEN_FIELD)}, $self->{__field}->{$id}; | ||
| 283 | return DVAL $self->{__field}->{$id}; | ||
| 284 | } | ||
| 285 | |||
| 286 | my $field = CGI::MxScreen::Form::Field->make(%args); | ||
| 287 | $self->{__field}->{$id} = $field; | ||
| 288 | push @{$mgr->context(SCREEN_FIELD)}, $field; | ||
| 289 | |||
| 290 | return DVAL $field; | ||
| 291 | } | ||
| 292 | |||
| 293 | # | ||
| 294 | # ->record_button | ||
| 295 | # | ||
| 296 | # Arguments: | ||
| 297 | # | ||
| 298 | sub record_button { | ||
| 299 | DFEATURE my $f_; | ||
| 300 | my $self = shift; | ||
| 301 | my (%args) = @_; | ||
| 302 | |||
| 303 | VERIFY defined $args{-name}; | ||
| 304 | |||
| 305 | my $mgr = $self->manager; | ||
| 306 | my $id = $args{-name}; | ||
| 307 | if (defined $self->{__transition}->{$id}) { | ||
| 308 | DTRACE("button $id already exists"); | ||
| 309 | $self->{__transition}->{$id}->update(%args); | ||
| 310 | |||
| 311 | # save it into the context that must have been previously reset | ||
| 312 | push @{$mgr->context(SCREEN_BUTTON)}, $self->{__transition}->{$id}; | ||
| 313 | return DVAL $self->{__transition}->{$id}; | ||
| 314 | } | ||
| 315 | |||
| 316 | my $button = CGI::MxScreen::Form::Button->make(%args); | ||
| 317 | $self->{__transition}->{$id} = $button; | ||
| 318 | push @{$mgr->context(SCREEN_BUTTON)}, $button; | ||
| 319 | |||
| 320 | return DVAL $button; | ||
| 321 | } | ||
| 322 | |||
| 323 | # | ||
| 324 | # ->display -- deferred | ||
| 325 | # | ||
| 326 | sub display { logconfess "deferred" } | ||
| 327 | |||
| 328 | # | ||
| 329 | # (validate) -- CGI::MxScreen action callback | ||
| 330 | # | ||
| 331 | # Validate all the fields, by running all the validation routines. | ||
| 332 | # | ||
| 333 | # If any error is found, return CGI_MX_ABORT to abort processing, unless | ||
| 334 | # $cont was true in which case CGI_MX_ERROR is returned. | ||
| 335 | # | ||
| 336 | sub validate { | ||
| 337 | DFEATURE my $f_; | ||
| 338 | my $self = shift; | ||
| 339 | my $env = pop @_; # Action_Env field | ||
| 340 | my ($cont) = @_; | ||
| 341 | my $error = 0; | ||
| 342 | |||
| 343 | # run the validation routine for each of fields | ||
| 344 | while (my ($k, $field) = each %{$self->{__field}}) { | ||
| 345 | DASSERT $field->isa('CGI::MxScreen::Form::Field'); | ||
| 346 | |||
| 347 | unless ($field->validate()) { | ||
| 348 | $error++; | ||
| 349 | next; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | return DVAL | ||
| 354 | $error == 0 ? CGI_MX_OK : ($cont ? CGI_MX_ERROR : CGI_MX_ABORT); | ||
| 355 | } | ||
| 356 | |||
| 357 | # | ||
| 358 | # (abort_on_error) -- CGI::MxScreen action callback | ||
| 359 | # | ||
| 360 | # Action callback which returns CGI_MX_ABORT if an error has been already | ||
| 361 | # detected in the callback chain. | ||
| 362 | # | ||
| 363 | sub abort_on_error { | ||
| 364 | DFEATURE my $f_; | ||
| 365 | my $self = shift; | ||
| 366 | my ($env) = @_; | ||
| 367 | |||
| 368 | DREQUIRE defined $env && $env->isa("CGI::MxScreen::Action_Env"); | ||
| 369 | |||
| 370 | return $env->error_count == 0 ? CGI_MX_OK : CGI_MX_ABORT; | ||
| 371 | } | ||
| 372 | |||
| 373 | # | ||
| 374 | # ->bounce | ||
| 375 | # | ||
| 376 | # Raise exception to bounce to another state. | ||
| 377 | # | ||
| 378 | # This routine must be called only during ->display(), and no output should | ||
| 379 | # have been emitted by the displaying routine routine. | ||
| 380 | # | ||
| 381 | sub bounce { | ||
| 382 | DFEATURE my $f_; | ||
| 383 | my $self = shift; | ||
| 384 | |||
| 385 | require CGI::MxScreen::Exception::Bounce; | ||
| 386 | my $exception = CGI::MxScreen::Exception::Bounce->make([@_]); | ||
| 387 | |||
| 388 | DTRACE "raising $exception..."; | ||
| 389 | |||
| 390 | die $exception; # The manager traps errors from display() | ||
| 391 | } | ||
| 392 | |||
| 393 | # | ||
| 394 | # ->enter -- to be redefined by heirs | ||
| 395 | # | ||
| 396 | # Called when we enter a screen from another state. | ||
| 397 | # | ||
| 398 | # The screen we come from is given as sole parameter, but can be | ||
| 399 | # undef for the very first screen ever displayed in the session. | ||
| 400 | # | ||
| 401 | sub enter { | ||
| 402 | } | ||
| 403 | |||
| 404 | # | ||
| 405 | # ->leave -- to be redefined by heirs | ||
| 406 | # | ||
| 407 | # Called when we leave a screen to go another state. | ||
| 408 | # | ||
| 409 | # The screen we go to is given as sole parameter. | ||
| 410 | # | ||
| 411 | sub leave { | ||
| 412 | } | ||
| 413 | |||
| 414 | # | ||
| 415 | # (clear_context) -- CGI::MxScreen action callback | ||
| 416 | # | ||
| 417 | # Clear a specific section of the overal context where CGI params that | ||
| 418 | # are considered orphans (no record_field for them) are automatically | ||
| 419 | # saved. That section is indexed by the screen name to allow this | ||
| 420 | # partial clearing. | ||
| 421 | # | ||
| 422 | # That method is a good candidate to be put in a list of actions | ||
| 423 | # associated to a button. | ||
| 424 | # | ||
| 425 | # If a state is given, the context of that state is deleted instead of the | ||
| 426 | # one for the current screen. | ||
| 427 | # | ||
| 428 | sub clear_context { | ||
| 429 | DFEATURE my $f_; | ||
| 430 | my $self = shift; | ||
| 431 | pop @_; # any action callbacks receive an extra parameter | ||
| 432 | my ($state) = @_; | ||
| 433 | |||
| 434 | VERIFY implies(defined $state, $self->manager->is_valid_state($state)), | ||
| 435 | "valid state '$state'"; | ||
| 436 | |||
| 437 | $state = $self->name unless defined $state; | ||
| 438 | my $cgi_ctxt = $self->manager->context(CGI_PARAM); | ||
| 439 | |||
| 440 | return DVAL CGI_MX_OK unless defined (my $ctxt = $cgi_ctxt->{$state}); | ||
| 441 | |||
| 442 | DTRACE "About to delete context of state '$state'"; | ||
| 443 | |||
| 444 | # first, delete the param in the CGI table | ||
| 445 | while (my ($k, $v) = each %$ctxt) { | ||
| 446 | DTRACE "Deleting CGI param '$k'"; | ||
| 447 | CGI::delete($k); | ||
| 448 | } | ||
| 449 | |||
| 450 | # then, delete the key in the context | ||
| 451 | delete $cgi_ctxt->{$state}; | ||
| 452 | |||
| 453 | return DVAL CGI_MX_OK; # means a success for the action | ||
| 454 | } | ||
| 455 | |||
| 456 | ### | ||
| 457 | ### Storable hooks. | ||
| 458 | ### | ||
| 459 | |||
| 460 | sub STORABLE_freeze { | ||
| 461 | DFEATURE my $f_; | ||
| 462 | my ($self, $cloning) = @_; | ||
| 463 | return if $cloning; | ||
| 464 | |||
| 465 | my %copy = %$self; | ||
| 466 | delete $copy{__context}; | ||
| 467 | delete $copy{__error_env}; | ||
| 468 | delete $copy{__error}; | ||
| 469 | delete $copy{__field}; | ||
| 470 | delete $copy{__manager}; | ||
| 471 | delete $copy{__transition}; | ||
| 472 | |||
| 473 | return DARY ("", \%copy); | ||
| 474 | } | ||
| 475 | |||
| 476 | sub STORABLE_thaw { | ||
| 477 | DFEATURE my $f_; | ||
| 478 | my ($self, $cloning, $x, $copy) = @_; | ||
| 479 | |||
| 480 | DREQUIRE !$cloning; | ||
| 481 | DREQUIRE length $x == 0; | ||
| 482 | DREQUIRE ref $copy eq 'HASH'; | ||
| 483 | |||
| 484 | %$self = %$copy; | ||
| 485 | |||
| 486 | return DVOID; | ||
| 487 | } | ||
| 488 | |||
| 489 | 1; | ||
| 490 | |||
| 491 | =head1 NAME | ||
| 492 | |||
| 493 | CGI::MxScreen::Screen - Ancestor for all user screens | ||
| 494 | |||
| 495 | =head1 SYNOPSIS | ||
| 496 | |||
| 497 | use base qw(CGI::MxScreen::Screen); | ||
| 498 | |||
| 499 | sub display { # display screen -- defined | ||
| 500 | my $self = shift; | ||
| 501 | my ($arg1, $arg2) = @_; | ||
| 502 | ... | ||
| 503 | } | ||
| 504 | |||
| 505 | =head1 DESCRIPTION | ||
| 506 | |||
| 507 | This B<deferred> class is meant to be the ancestor of all your application | ||
| 508 | screens. You must inherit from C<CGI::MxScreen::Screen> and at least | ||
| 509 | define the C<display()> routine, which will be called by the C<CGI::MxScreen> | ||
| 510 | manager when that screen is selected for display. | ||
| 511 | |||
| 512 | C<CGI::MxScreen::Screen> objects are blessed hashes. In order to leave | ||
| 513 | you as much of the key namespace as possible, all attributes defined by | ||
| 514 | the deferred class start with I<two> leading underscores. Contrary to the | ||
| 515 | routine namespace (see L<"INTERFACE"> below), this simple workaround should | ||
| 516 | prevent any accidental attribute collision. | ||
| 517 | |||
| 518 | Screen objects must be B<serializable>. That means they must never hold or | ||
| 519 | refer to objects or data containing things known to be non-serializable, | ||
| 520 | like GLOB and CODE references. | ||
| 521 | |||
| 522 | Screen objects are never created by the user. They are handled by the | ||
| 523 | C<CGI::MxScreen> manager, who will ensure that at most B<one instance> of | ||
| 524 | each screen name will be created during a session. That means the C<init()> | ||
| 525 | routine will be called at most once. I say I<at most> because screens | ||
| 526 | are created on demand, and if your application does not need to show some | ||
| 527 | state during a session, then the screen object will never be created. | ||
| 528 | |||
| 529 | =head1 INTERFACE | ||
| 530 | |||
| 531 | Because you need to inherit, you must be very familiar with the internals | ||
| 532 | of the class, i.e. not just only the public interface, but also with the | ||
| 533 | routines used throughout the framework but not meant for public consumption. | ||
| 534 | Indeed, Perl will not warn you when you accidentally define a routine bearing | ||
| 535 | the same name as one present in the ancestors (i.e. redefinition is automatic, | ||
| 536 | which is dangerous here). And redefining routines as essential as | ||
| 537 | C<validate()> or C<name()> would lead to havoc. | ||
| 538 | |||
| 539 | =head2 Listing | ||
| 540 | |||
| 541 | Before detailing the interface, here is a compact list of all the public | ||
| 542 | features, to make it easier to see what is provided (and know which routine | ||
| 543 | names are forbidden to you... A trailing + indicates a routine that you | ||
| 544 | may choose to redefine, a trailing * indicates a deferred routine, which you | ||
| 545 | must define. Functions listed between (parenthesis) are action callbacks, | ||
| 546 | not meant to be called directly. | ||
| 547 | |||
| 548 | The following interface is public, i.e. you may safely use those features | ||
| 549 | on screen objects: | ||
| 550 | |||
| 551 | (abort_on_error) | ||
| 552 | bgcolor | ||
| 553 | bounce | ||
| 554 | (clear_context) | ||
| 555 | current_screen | ||
| 556 | default_button | ||
| 557 | display* | ||
| 558 | enter+ | ||
| 559 | error | ||
| 560 | error_env | ||
| 561 | init+ | ||
| 562 | leave+ | ||
| 563 | manager | ||
| 564 | name | ||
| 565 | previous_screen | ||
| 566 | record_button | ||
| 567 | record_field | ||
| 568 | screen_title | ||
| 569 | set_default_button | ||
| 570 | set_error | ||
| 571 | spring_screen | ||
| 572 | (validate) | ||
| 573 | vars | ||
| 574 | |||
| 575 | The following interface is private, i.e. you should never make direct use | ||
| 576 | of those features on screen objects. It is used internally by C<CGI::MxScreen> | ||
| 577 | and is documented (well, listed) so that you never define a feature bearing | ||
| 578 | that name in your own classes. | ||
| 579 | |||
| 580 | Those names you should absolutely B<never override>: | ||
| 581 | |||
| 582 | make | ||
| 583 | remake | ||
| 584 | relink_to_manager | ||
| 585 | _init | ||
| 586 | set_error_env | ||
| 587 | _clear_internal_context | ||
| 588 | |||
| 589 | You must also know that, in order to be serializable with C<Storable>, the | ||
| 590 | screen defines the following hooks: | ||
| 591 | |||
| 592 | STORABLE_freeze | ||
| 593 | STORABLE_thaw | ||
| 594 | |||
| 595 | If for some reason you need to redefine those hooks, you B<can't simply> | ||
| 596 | call C<SUPER::> on them, whilst doing your local processing in the | ||
| 597 | redefinition. Look at the source code to understand what needs to be done. | ||
| 598 | |||
| 599 | Because the above hooks were necessary, it means that adding other | ||
| 600 | serializer support (see L<CGI::MxScreen::Serializer>) will probably require | ||
| 601 | similar hooks. Unfortunately, although we could design things so as to make | ||
| 602 | this choice possible, the only serializer we knew about was C<Storable>. | ||
| 603 | |||
| 604 | =head2 Creation Routine | ||
| 605 | |||
| 606 | Screens are created automatically by the C<CGI::MxScreen> manager, based | ||
| 607 | on the C<-screens> settings, as explained in | ||
| 608 | L<CGI::MxScreen/"Creation Routine">. The only special argument is C<-class>, | ||
| 609 | which is handled internally by C<CGI::MxScreen>, but the others are passed | ||
| 610 | verbatim to the screen creation routine. | ||
| 611 | |||
| 612 | The supported arguments are: | ||
| 613 | |||
| 614 | =over 4 | ||
| 615 | |||
| 616 | =item C<-bgcolor> => I<color> | ||
| 617 | |||
| 618 | Optinal. Overrides the default background for this screen. | ||
| 619 | |||
| 620 | =item C<-title> => I<screen_title> | ||
| 621 | |||
| 622 | Mandatory. Sets the screen title. | ||
| 623 | |||
| 624 | =back | ||
| 625 | |||
| 626 | =head2 Attributes | ||
| 627 | |||
| 628 | The following attributes are defined: | ||
| 629 | |||
| 630 | =over 4 | ||
| 631 | |||
| 632 | =item C<bgcolor> | ||
| 633 | |||
| 634 | The background color (string form, i.e. either a color name like C<"gray"> or | ||
| 635 | an hexadecimal representation of the RGB triplet like C<"#1e32ef">) used | ||
| 636 | to display this screen. | ||
| 637 | |||
| 638 | =item C<current_screen> | ||
| 639 | |||
| 640 | The current screen being displayed, along with the C<display()> arguments, | ||
| 641 | as an array reference, for instance: | ||
| 642 | |||
| 643 | ["Welcome", 010125] | ||
| 644 | |||
| 645 | where the first item is the screen name, the remaining are the displaying | ||
| 646 | arguments. This makes it possible to use that as a C<-target> argument | ||
| 647 | for buttons (see L<CGI::MxScreen::Form::Button>). | ||
| 648 | |||
| 649 | B<Note>: The notion of C<current_screen> is maintained by the manager. | ||
| 650 | Whatever screen object you query, you will always obtain the I<same> answer. | ||
| 651 | This note also applies to C<previous_screen> and C<spring_screen>. | ||
| 652 | |||
| 653 | =item C<default_button> | ||
| 654 | |||
| 655 | The default button recorded via C<set_default_button>. | ||
| 656 | |||
| 657 | The default button is used when the user presses <Enter> when submitting | ||
| 658 | a form, without pressing one of the submit buttons. Usually, browsers allow | ||
| 659 | this when there is only one CGI input field in the form. | ||
| 660 | |||
| 661 | If there is no default button specified, C<CGI::MxScreen> will remain in | ||
| 662 | the current state and redisplay the screen. | ||
| 663 | |||
| 664 | =item C<error> | ||
| 665 | |||
| 666 | The error information, recorded vi C<set_error>. This is a user-defined | ||
| 667 | field, i.e. it is not used by C<CGI::MxScreen>. It is meant to be filled | ||
| 668 | by action callbacks, when an error is detected. Since it will be used by | ||
| 669 | your own screens, you may put whatever you wish. | ||
| 670 | |||
| 671 | =item C<error_env> | ||
| 672 | |||
| 673 | When an action callback failed, this attribute holds the | ||
| 674 | C<CGI::MxScreen::Action_Env> object used during the execution of the callback | ||
| 675 | chain. See L<CGI::MxScreen::Action_Env> for its access interface. | ||
| 676 | |||
| 677 | The attribute is otherwise C<undef>, so you may test it boolean-wise to | ||
| 678 | determine whether an error did occur or not. | ||
| 679 | |||
| 680 | =item C<manager> | ||
| 681 | |||
| 682 | The C<CGI::MxScreen> object who manages this screen. | ||
| 683 | |||
| 684 | =item C<name> | ||
| 685 | |||
| 686 | The screen name, as defined by the C<-screens> argument when C<CGI::MxScreen> | ||
| 687 | was built. See L<CGI::MxScreen/"Creation Routine">. | ||
| 688 | |||
| 689 | =item C<previous_screen> | ||
| 690 | |||
| 691 | The previously displayed screen, in the same form as C<current_screen>. | ||
| 692 | It will be C<undef> the first time. | ||
| 693 | |||
| 694 | If you had a transition from one screen to the same one, then | ||
| 695 | C<previous_screen> and C<current_screen> will return the same information. | ||
| 696 | However, see C<spring_screen>. | ||
| 697 | |||
| 698 | =item C<screen_title> | ||
| 699 | |||
| 700 | This is the title of the screen, as configured during the creation of | ||
| 701 | the C<CGI::MxScreen> manager, via the C<-screens> argument. See | ||
| 702 | L<CGI::MxScreen/"Creation Routine">. | ||
| 703 | |||
| 704 | It is not called simply C<title> because screens may use the C<CGI> module, | ||
| 705 | and C<CGI> exports a C<title()> routine as part of its HTML generation | ||
| 706 | routines. | ||
| 707 | |||
| 708 | =item C<spring_screen> | ||
| 709 | |||
| 710 | The screen we sprang from. This is the last I<stable> state we were in | ||
| 711 | before jumping into the current screen, transitions to the same screen | ||
| 712 | not withstanding. The format is the same as C<current_screen>. | ||
| 713 | |||
| 714 | This may be used as C<-target> for buttons to come back to the screen that | ||
| 715 | directed a transition to the current screen, whatever it was. See | ||
| 716 | L<CGI::MxScreen::Form::Button>. | ||
| 717 | |||
| 718 | =item C<vars> | ||
| 719 | |||
| 720 | This returns a reference to a global persistent hash table, available in all | ||
| 721 | states. It is free for use by user screens, but all the values you will | ||
| 722 | store there must be serializable (by C<Storable>, or any other serializer | ||
| 723 | you configured). | ||
| 724 | |||
| 725 | By default, access to keys within this hash table are protected, to guard | ||
| 726 | against typos (at runtime, alas). If you don't like that behaviour, you | ||
| 727 | can set C<$mx_check_vars> to false in the global configuration. See | ||
| 728 | L<CGI::MxScreen::Config>. | ||
| 729 | |||
| 730 | =back | ||
| 731 | |||
| 732 | =head2 Attribute Setting | ||
| 733 | |||
| 734 | The following routines allow changing of some attributes: | ||
| 735 | |||
| 736 | =over 4 | ||
| 737 | |||
| 738 | =item C<set_default_button> I<button_object> | ||
| 739 | |||
| 740 | Records a default button for the screen, to be used if they press <Enter> to | ||
| 741 | submit the form data. The I<button_object> is a C<CGI::MxScreen::Form::Button> | ||
| 742 | object, obtained via C<record_button()>. | ||
| 743 | |||
| 744 | =item C<set_error> I<value> | ||
| 745 | |||
| 746 | Set the C<error> attribute, which is free for use by action callback, for | ||
| 747 | instance to propagate some error indication to the screen. | ||
| 748 | |||
| 749 | =back | ||
| 750 | |||
| 751 | =head2 Feature To Be Defined | ||
| 752 | |||
| 753 | To make your screen a concrete class, you need to define the B<deferred> | ||
| 754 | feature, C<display>. | ||
| 755 | |||
| 756 | =over 4 | ||
| 757 | |||
| 758 | =item C<display> I<args> | ||
| 759 | |||
| 760 | The routine that displays the CGI form. | ||
| 761 | |||
| 762 | It is called by the C<CGI::MxScreen> manager, with some arguments, as defined | ||
| 763 | by the C<-initial> argument (see L<CGI::MxScreen/"Creation Routine">) or | ||
| 764 | by the C<-target> button argument (see L<CGI::MxScreen::Form::Button>), or | ||
| 765 | generally speaking, anything that defines a state transition (e.g. the | ||
| 766 | C<bounce()> routine, as described below). | ||
| 767 | |||
| 768 | Before calling the routine, C<CGI::MxScreen> initialized the HTML headers | ||
| 769 | and opened the form tag, directing the submission to the script's URL, but | ||
| 770 | I<without> the query string: if one is supplied initially, it is up to you | ||
| 771 | to save the relevant information in the persistent context, or in your | ||
| 772 | screen objects, since they are also persistent. | ||
| 773 | |||
| 774 | The routine must print to STDOUT its generated HTML, and can make use of | ||
| 775 | all the helper routines from the C<CGI> module to generate form controls | ||
| 776 | or emit HTML via routines. There are also some other helper routines defined | ||
| 777 | in L<CGI::MxScreen::HTML>. | ||
| 778 | |||
| 779 | When it returns, the screen is supposed to have been fully displayed, and the | ||
| 780 | form will be closed automatically by C<CGI::MxScreen>. If you haven't read | ||
| 781 | them already, you should study L<CGI::MxScreen/"Example"> and | ||
| 782 | L<CGI::MxScreen::Layout>. | ||
| 783 | |||
| 784 | A screen is given the opportunity to redirect itself to another state, by | ||
| 785 | sending a bounce execption to the manager via C<bounce()>. However, it may | ||
| 786 | do so only if it has not already emitted anything. If you left | ||
| 787 | C<$mx_buffer_stdout> to its default I<true> setting | ||
| 788 | (see L<CGI::MxScreen::Config>), anything you output before bouncing will be | ||
| 789 | discarded for you. | ||
| 790 | |||
| 791 | Usually, your screens will define fields and submit buttons. You should | ||
| 792 | record them to be able to attach validation routines or action callbacks, | ||
| 793 | but you may choose not to and use plain raw C<CGI> routines, with manual | ||
| 794 | hidden context propagation. However, note that it would be a waste, because | ||
| 795 | C<CGI::MxScreen> is supposed to handle that for you, and also the only | ||
| 796 | C<display()> routine called is the one for the visible screen. Any other | ||
| 797 | parameters defined on other screens would not even have the opportunity to | ||
| 798 | hide themselves... As for buttons, not recording them means you won't be | ||
| 799 | able to make use of the state machine features. | ||
| 800 | |||
| 801 | To record fields and buttons, use C<record_field()> and C<record_button()>. | ||
| 802 | |||
| 803 | =back | ||
| 804 | |||
| 805 | =head2 Features To Be Redefined | ||
| 806 | |||
| 807 | The following features have empty default implementations, and are meant to | ||
| 808 | be B<redefined> in your screens. It is not necessary to redefine all of | ||
| 809 | them, or any of them, if you don't need them: | ||
| 810 | |||
| 811 | =over 4 | ||
| 812 | |||
| 813 | =item C<enter> I<from_screen> | ||
| 814 | |||
| 815 | Called when we enter a screen whilst coming from another one. The screen | ||
| 816 | object we sprang from is given as argument, but will be C<undef> for the | ||
| 817 | very first screen displayed (the I<initial> screen). | ||
| 818 | |||
| 819 | B<Note>: we're passed a screen I<object>, not a list representation like | ||
| 820 | the one returned by C<spring_screen()>. | ||
| 821 | |||
| 822 | =item C<init> | ||
| 823 | |||
| 824 | Called when the screen object is created. | ||
| 825 | |||
| 826 | You may do whatever initialization is necessary on your object, but remember | ||
| 827 | that screen objects are created B<once> and remain persistent accross the | ||
| 828 | whole session. Therefore, if you need runtime initialization each time | ||
| 829 | one enters this screen, write it within C<enter()>. | ||
| 830 | |||
| 831 | =item C<leave> I<to_screen> | ||
| 832 | |||
| 833 | Called when we leave a screen to go to I<to_screen>, which is a screen | ||
| 834 | object. Contrary to C<enter()>, this one is always defined, by construction. | ||
| 835 | |||
| 836 | B<Note>: we're passed a screen I<object>, not a list representation like | ||
| 837 | the one returned by C<spring_screen()>. | ||
| 838 | |||
| 839 | =back | ||
| 840 | |||
| 841 | =head2 Control Features | ||
| 842 | |||
| 843 | Those features are necessary to let the screen control what's going to | ||
| 844 | happen when the form is submitted. They are meant to be used during | ||
| 845 | C<display()> processing: | ||
| 846 | |||
| 847 | =over 4 | ||
| 848 | |||
| 849 | =item C<bounce> I<screen_name>, I<args> | ||
| 850 | |||
| 851 | This is an I<exception> (sic!) to the definition given above. | ||
| 852 | By calling C<bounce()>, a screen redirects the state machine to the | ||
| 853 | screen called I<screen_name>, with I<args> being the C<display()> arguments. | ||
| 854 | |||
| 855 | You should not call C<bounce()> after having emitted something. This feature | ||
| 856 | is meant to be an exception, allowing to bypass a state when some condition | ||
| 857 | is met. | ||
| 858 | |||
| 859 | To avoid endless loops, there is an hardwired limit of 20 consecutive bounces | ||
| 860 | allowed. As famous people said on other occasion when talking about | ||
| 861 | computer limits, "this should be sufficient for any application". | ||
| 862 | |||
| 863 | =item C<record_button> I<args> | ||
| 864 | |||
| 865 | Records a submit button, and returns a C<CGI::MxScreen::Form::Button> object. | ||
| 866 | Please see L<CGI::MxScreen::Form::Button> for the interface, and the | ||
| 867 | description of what I<args> can be. | ||
| 868 | |||
| 869 | =item C<record_field> I<args> | ||
| 870 | |||
| 871 | Records a control field, and returns a C<CGI::MxScreen::Form::Field> object. | ||
| 872 | Please see L<CGI::MxScreen::Form::Field> for the interface, and the | ||
| 873 | description of what I<args> can be. | ||
| 874 | |||
| 875 | =back | ||
| 876 | |||
| 877 | =head2 Action Callbacks | ||
| 878 | |||
| 879 | Those features are not meant to be used directly, but are provided so that | ||
| 880 | they can be used as action callbacks attached to buttons, as described in | ||
| 881 | L<CGI::MxScreen::Form::Button>. | ||
| 882 | |||
| 883 | The most important one is C<'validate'> (spelled as a string because this | ||
| 884 | is how it should be used: see L<CGI::MxScreen/"Callbacks">), which will | ||
| 885 | trigger all the field verfication and patching callbacks. | ||
| 886 | |||
| 887 | =over 4 | ||
| 888 | |||
| 889 | =item C<abort_on_error> | ||
| 890 | |||
| 891 | This callback returns C<CGI_MX_ABORT> to immediately abort the callback | ||
| 892 | chain if there is an error already in one of the preceding callbacks. | ||
| 893 | See L<CGI::MxScreen::Error>. | ||
| 894 | |||
| 895 | =item C<clear_context> [I<screen_name>] | ||
| 896 | |||
| 897 | Clears a specific section of the overal context where I<orphan> CGI parameters | ||
| 898 | are saved. A CGI parameter is B<orphan> if there was no C<record_field()> | ||
| 899 | done for it. | ||
| 900 | |||
| 901 | If I<screen_name> is not specified, this applies to the current screen. | ||
| 902 | |||
| 903 | This callback is useful if you wish to discard the state of orphan CGI | ||
| 904 | parameters, so that the next time they are created, they get their default | ||
| 905 | value. | ||
| 906 | |||
| 907 | =item C<validate> [I<continue>] | ||
| 908 | |||
| 909 | Runs the validation and patching callbacks on all the recorded fields for | ||
| 910 | this screen. If I<continue> is I<true>, any error will not be fatal | ||
| 911 | immediately, i.e. C<CGI_MX_ERROR> will be returned, so that other action | ||
| 912 | callbacks may execute. If not specified, it defaults to I<false>, meaning | ||
| 913 | a failed validation immediately triggers the error and the end of the | ||
| 914 | action callback sequence. | ||
| 915 | |||
| 916 | =back | ||
| 917 | |||
| 918 | Here is an example of action callback settings for a submit button: | ||
| 919 | |||
| 920 | my $ok = $self->record_button( | ||
| 921 | -name => "OK", | ||
| 922 | -target => "Next", | ||
| 923 | -action => [ | ||
| 924 | 'validate', # Same as ['validate', 0] | ||
| 925 | ['do_something', $self], | ||
| 926 | 'abort_on_error', | ||
| 927 | ['clear_context', "Next"], | ||
| 928 | ] | ||
| 929 | ); | ||
| 930 | |||
| 931 | See L<CGI::MxScreen::Form::Button> for more information on C<record_button()>. | ||
| 932 | |||
| 933 | =head1 AUTHORS | ||
| 934 | |||
| 935 | The original authors are | ||
| 936 | Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>> | ||
| 937 | and | ||
| 938 | Christophe Dehaudt F<E<lt>Christophe.Dehaudt@teamlog.frE<gt>>. | ||
| 939 | |||
| 940 | Send bug reports, suggestions, problems or questions to | ||
| 941 | Jason Purdy F<E<lt>Jason@Purdy.INFOE<gt>> | ||
| 942 | |||
| 943 | =head1 SEE ALSO | ||
| 944 | |||
| 945 | CGI::MxScreen(3), CGI::MxScreen::Form::Button(3), | ||
| 946 | CGI::MxScreen::Form::Field(3). | ||
| 947 | |||
| 948 | =cut | ||
| 949 |