fixes and added www_cleanup forced last step

This commit is contained in:
Doug Bell 2010-04-30 12:27:16 -05:00
parent 8cc6ecb30d
commit fe97e9c510
4 changed files with 125 additions and 59 deletions

View file

@ -13,15 +13,17 @@ WebGUI::Wizard -- Generate wizards
use base 'WebGUI::Wizard';
sub _get_steps { [qw( step1 finish )] }
sub _get_steps { [qw( step1 )] }
# Show a form for the first step
sub www_step1 {
my ( $self ) = @_;
return $self->getFormStart
. '<input type="text" name="user" />'
. $self->getFormEnd;
my $f = $self->getForm; # Get a WebGUI::HTMLForm
$f->text( name => "user" );
return $f->print;
}
# Process the form for the first step
sub www_step1Save {
my ( $self ) = @_;
if ( my $user = $self->session->form->get('user') ) {
@ -33,8 +35,10 @@ WebGUI::Wizard -- Generate wizards
}
}
sub www_finish {
# Override cleanup to provide a friendly message
sub www_cleanup {
my ( $self ) = @_;
$self->cleanup;
return "Thank you! " . $self->get('user');
}
@ -48,11 +52,14 @@ WebGUI shows the first step's form (in the synopsis above, step1 / www_step1 ).
Once the user completes the form, the www_step1Save subroutine is run. If an
error is returned, the user is shown the error and the same form again.
Otherwise, the wizard continues to the next step (finish).
Otherwise, the wizard continues to the next step.
After all the steps in the wizard are complete, the wizard runs www_cleanup
to clean up after the wizard and return the user to their previous page.
All parameters gathered by the wizard are saved between page loads into
the user's session scratch. Only by restarting the wizard will their progress
be lost.
the user's session scratch. Only by finishing or restarting the wizard will
their progress be lost.
=head1 METHODS
@ -89,6 +96,33 @@ sub _get_steps {
#----------------------------------------------------------------------------
=head2 canView ( )
Returns true if the current user is allowed to view this wizard. You should
override this.
=cut
sub canView {
return 1;
}
#----------------------------------------------------------------------------
=head2 cleanup ( )
Remove any leftovers after the wizard is finished
=cut
sub cleanup {
my ( $self ) = @_;
$self->session->scratch->delete( $self->getCacheKey );
return;
}
#----------------------------------------------------------------------------
=head2 dispatch ( )
Dispatch the request to the correct step(s). Thaw the user's params and freeze
@ -117,18 +151,24 @@ sub dispatch {
}
else {
my $step = $self->getNextStep;
$self->session->log->info( "Showing next step: " . $step );
$self->setCurrentStep( $step );
my $formSub = $self->can( 'www_' . $step );
my $output = $formSub->($self);
$self->freeze;
return $self->wrapStyle( $output );
if ( $step ) {
# Not done, run the next step
$self->session->log->info( "Showing next step: " . $step );
$self->setCurrentStep( $step );
my $formSub = $self->can( 'www_' . $step );
my $output = $formSub->($self);
$self->freeze;
return $self->wrapStyle( $output );
}
else {
# We're done, run the cleanup
return $self->wrapStyle( $self->www_cleanup );
}
}
}
else {
# Starting over
$self->{_params} = {};
$self->freeze;
$self->cleanup;
my $step = $self->_get_steps->[0];
$self->setCurrentStep( $step );
$self->session->log->info( "Starting wizard: " . $step );
@ -149,7 +189,7 @@ Save the current params to long-term storage.
sub freeze {
my ( $self ) = @_;
$self->session->scratch->set( $self->getCacheKey, JSON->new->encode( $self->{_params} ) );
$self->session->scratch->set( $self->getCacheKey, JSON->new->encode( $self->{_params} || {} ) );
}
#----------------------------------------------------------------------------
@ -212,7 +252,7 @@ sub getForm {
my ( $self, $step ) = @_;
$step ||= $self->getCurrentStep;
my $form = WebGUI::HTMLForm->new( $self->session,
action => '?op=wizard;wizard_class=' . blessed( $self ) . ';wizard_step=' . $step,
action => $self->getStepUrl( $step ),
);
return $form;
}
@ -237,6 +277,23 @@ sub getNextStep {
#----------------------------------------------------------------------------
=head2 getStepUrl ( step )
Get the URL to go to a specific step. Only used when there is no processing
to do.
=cut
sub getStepUrl {
my ( $self, $step ) = @_;
return unless $step;
return $self->session->url->page(
'op=wizard;wizard_class=' . (blessed $self) . ';wizard_step=' . $step
);
}
#----------------------------------------------------------------------------
=head2 session ( )
Get the WebGUI::Session object
@ -305,4 +362,23 @@ sub wrapStyle {
return $output;
}
#----------------------------------------------------------------------------
=head2 www_cleanup ( )
Clean up after the user, removing the variables, and return them to their
previous place.
NOTE: If you override this, you must remember to call C<cleanup> manually!
=cut
sub www_cleanup {
my ( $self ) = @_;
$self->cleanup;
$self->session->http->setRedirect( $self->session->url->page );
return "redirect";
}
1;

View file

@ -20,7 +20,6 @@ sub _get_steps {
return [ qw(
pickStyle
chooseContent
finalize
)
];
}
@ -424,20 +423,5 @@ sub www_chooseContentSave {
return;
} ## end sub www_chooseContentSave
#----------------------------------------------------------------------------
=head2 www_finalize ( $output )
Redirect the user to to the home page
=cut
sub www_finalize {
my ($self) = @_;
my $session = $self->session;
$session->http->setCacheControl("none");
$session->http->setRedirect( $session->url->gateway );
return "redirect";
}
1;

View file

@ -22,7 +22,6 @@ sub _get_steps {
companyInformation
siteStats
defaultStyle
finalize
)];
}
@ -375,19 +374,20 @@ sub www_defaultStyleSave {
#----------------------------------------------------------------------------
=head2 www_finalize ( )
=head2 www_cleanup ( )
Give the user a choice to do the Home Page wizard
=cut
sub www_finalize {
sub www_cleanup {
my ( $self ) = @_;
my $session = $self->session;
my $form = $session->form;
$session->http->setCacheControl("none");
my $i18n = WebGUI::International->new( $session, "WebGUI" );
$self->cleanup;
# Delete specialState
$session->setting->remove( "specialState" );
@ -399,34 +399,18 @@ sub www_finalize {
$starterForm->hidden( name => "styleTemplateId", value => $self->get('styleTemplateId') );
$starterForm->submit( value => $i18n->get( 'yes please' ) );
my $finishForm = $self->getForm;
$finishForm->hidden( name => "runStarter", value => 0 );
$finishForm->submit( value => $i18n->get( 'no thanks' ) );
my $output = '<h1>' . $i18n->get('site starter title') . '</h1>';
$output .= ' <p>' . $i18n->get('site starter body') . '</p>'
. '<div style="float: left">' . $starterForm->print . '</div>'
. '<div style="float: left">' . $finishForm->print . '</div>'
. sprintf(
'<div style="float: left"><a href="%s">%s</a></div>',
$session->url->gateway,
$i18n->get('no thanks'),
)
. '<div style="clear: both">&nbsp;</div>'
;
return $output;
}
#----------------------------------------------------------------------------
=head2 www_finalizeSave ( )
User has declined to do the Home Page wizard
=cut
sub www_finalizeSave {
my ( $self ) = @_;
my $session = $self->session;
my ( $form ) = $session->quick(qw( form ));
$session->http->setRedirect( $session->url->gateway );
}
1;

View file

@ -29,7 +29,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
plan tests => 29; # Increment this number for each test you create
plan tests => 31; # Increment this number for each test you create
#----------------------------------------------------------------------------
# Basic API
@ -127,6 +127,12 @@ cmp_deeply(
"thaw overwrites params"
);
$wizard->cleanup;
cmp_deeply(
$wizard->thaw,
{ },
"cleanup clears scratch"
);
#----------------------------------------------------------------------------
# dispatch
@ -183,6 +189,15 @@ cmp_deeply(
'dispatch did not freeze error data'
);
# Cleanup
$wizard = WebGUI::Wizard::Test->new( $session ); # new object to clear currentStep
$session->request->setup_body({
wizard_class => 'WebGUI::Wizard::Test',
wizard_step => 'five',
});
is( $wizard->dispatch, "cleanup", "cleanup sub run after last step saved" );
package WebGUI::Wizard::Test;
use base 'WebGUI::Wizard';
sub _get_steps { return [qw( one two three four five )] }
@ -209,5 +224,12 @@ sub www_twoSave {
return "error";
}
sub www_fiveSave {
return; # Success!
}
sub www_cleanup {
return "cleanup";
}
#vim:ft=perl