Skip to content

Instantly share code, notes, and snippets.

@preaction
Created April 23, 2010 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preaction/377111 to your computer and use it in GitHub Desktop.
Save preaction/377111 to your computer and use it in GitHub Desktop.
package WebGUI::Content::Wizard;
sub process {
my ( $session ) = @_;
if ( $session->form->get('op') eq 'wizard' && my $class = $session->form->get('wizard_class') ) {
WebGUI::Pluggable->load($class);
if ( $class->isa( 'WebGUI::Wizard' ) {
my $wizard = $class->new( $session );
return $class->dispatch;
}
else {
return "Sminternal Smerver Smerror";
}
}
}
package WebGUI::Wizard;
use WebGUI::Form;
sub dispatch {
my ($self) = @_;
# See if we process a form
if ( my $step = $self->getCurrentStep ) { # TODO: Only POST can be processed
my $processSub = $self->can( 'www_' . $step . 'Save' );
my $errorMessage = $processSub->($self);
if ($errorMessage) {
my $formSub = $self->can( 'www_' . $step ) );
return $self->wrapStyle( $errorMessage . $formSub->($self) );
}
else {
my $formSub = $self->getNextStep( $step ) );
return $self->wrapStyle( $formSub->($self) );
}
}
else {
my $formSub = $self->can( 'www_' . $self->_get_steps->[0] );
return $self->wrapStyle( $formSub->($self) );
}
} ## end sub dispatch
sub getCurrentStep {
my ( $self ) = @_;
return $self->session->form->get( 'wizard_step' );
}
sub getFormEnd {
my ( $self ) = @_;
return WebGUI::Form->formFooter( );
}
sub getFormStart {
my ( $self ) = @_;
return WebGUI::Form->formHeader( $self->session, {
action => '?op=wizard;wizard_class=' . blessed( $self ) . ';wizard_step=' . $self->getCurrentStep,
} );
}
sub getNextStep {
my ( $self, $step ) = @_;
for my $i ( 0 .. @{ $self->_get_steps } - 1 ) {
if ( $self->_get_steps->[$i] eq $step ) {
return $self->_get_steps->[ $i + 1 ];
}
}
}
sub wrapStyle {
my ( $self, $output ) = @_;
return $output;
}
package WhatsMyName;
use Moose;
extends 'WebGUI::Wizard';
# Define our steps
sub _get_steps {
return [ qw(
askForName respond
)
];
}
# Show a form in our wizard
sub www_askForName {
my ($self) = @_;
return $self->getFormStart . '<input name="name" /><input type="submit"/>' . $self->getFormEnd;
}
# Validate the form and return any errors
sub www_askForNameSave {
my ($self) = @_;
if ( $self->session->form->get('name') eq "doug" ) {
return "WRONG!";
}
}
# All steps prior to this have validated successfully
sub www_respond {
return "That's right! And don't you forget it!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment