Wizard -- making multipart (e.g. wizard) actions: registering an user via several steps, submit something large (like application forms).
# if this called without previous wizard (i.e. new wizard) then # creating wizard with '/users/list' as first step and '/users/last' as second # otherwise append steps '/users/list' and '/users/last' if they wasn't added alread $c->wizard('/users/last', '/users/list'); # going next step ('/users/list'); $c->wizard->goto_next; # creating wizard with 'correct' (left to right) steps order $c->wizard(-first => '/users/list', '/users/last'); $c->wizard->goto_next; # appending step '/users/list' and '+/users/list' -- this will add '/users/list' into wizard twice # you can prepend first '/' with any symbols you like -- just to make your step unique, so it will be added # into wizard $c->wizard(-first => '/users/list', '+/users/list', '/users/last'); $c->wizard->goto_next; # to /users/list $c->wizard->goto_next; # again to /users/list
Something more real:
package App; use strict; use warnings; use Catalyst qw/ Session Session::Store::Dummy Session::State::Cookie Wizard /; # we assume stash automagically saves into wizard and restores from it __PACKAGE__->config({ wizard => { autostash => 1, autoactivate => 1 }}); __PACKAGE__->setup; 1; package App::C::First; use strict; use warnings; sub edit : Local { my ($self, $c) = @_; # goto 'login' wizard unless you are loggedin return $c->wizard(-first => '/first/login', '/first/edit')->goto_next unless $c->session->{loggedin}; $c->res->body('OK!'); } # draw login form sub login : Local { my ($self, $c) = @_; # adding destination of form as step $c->wizard('/first/login_submit'); # dont forget to append wizard id into that form, and also use wizard->next call to # get action for form $c->res->body(<<EOF); <html> <head> <title>Test login</title> </head> <body> @{[delete $c->stash->{error} || '']} <form name="login" action="@{[ $c->wizard->next ]}"> @{[ $c->wizard->id_to_form ]} <input name="username"> <input name="password" type="password"> </form> </body> </html> EOF $c->res->content_type('text/html'); } # check login submited sub login_submit : Local { my ($self, $c) = @_; my $p = $c->req->params; # check if username and password are correct if ($p->{username} eq 'user' && $p->{password} eq 'userpassword') { $c->session->{loggedin} = 1; # ok, goto next ('/first/edit') in this example $c->wizard->goto_next; } else { $c->stash->{error} = 'Incorrect login'; # incorrect, back to login page. $c->wizard->detach_prev(2); } } 1;
This plugin provides functionality for making multipart actions (wizards) more easily.
For example, you may need this plugin in following cases:
When you need to move some items into another folder, you may: 1) keep current folders select in session, 2) use it as wizard and keep that info in wizard (or stash)
When you need to deatch users into login page (see SYNOPSIS)
Returns new wizard (Catalyst::Plugin::Wizard::Instance), existing one with steps you gave inserted into middle of flow, or undef, if no wizards are defined/seted.
When called without arguments it also 'activates' wizard (copying stash from wizard to catalyst if configured so, etc). You can configure plugin so your current active wizard will be autoactivated.
It takes step as an argument. Each step can be prefixed with keyword, which alerts plugin how to work out this step. E.g. you use '/first/step' with some -prefixes => '/first/step'.
Appends steps as an 'redirect' steps. Calling goto_next will cause 'redirect' jump into it.
NOTE: Last step in $c->wizard will be called first (like in stack), except for following case.
MUST BE first in the chain. Signals that this $c->wizard call have steps arranged in left-to-right order.
Addes -default target in step (called when all other steps are done).
Detaches or forwards to this step. Step can be either an simply string with action private path or arrayref within which $c->detach (or $c->forward) will be called.
Forces adding step into wizard. Step will be added even if it already have been added into that wizard. Use carefully! It can cause bugs!
Starts wizard with steps from arguments.
Stop current wizard.
Redirect to next action or to $default_url if no wizard exists or all wizard steps passed.
Detaches to next action or to $default_url if no wizard exists or all wizard steps passed.
Forwards to next action or to $default_url if no wizard exists or all wizard steps passed.
Used to add steps (within $c->wizard(...) args). You can use it if you won't need to create new wizard. Args: same as for $c->wizard(...) call.
Detaches forwards or redirects to next step. You explicity select jump type.
Goto to next step in wizard (jump type -- forward, deatch or redirect -- decided automagically).
Returns next destination -- arrayref for forward/deatch or string with action path for redirects (returns URI via subwizard id attached in case of subwizard)
Returns $c->wizard->next or default step.
Returns last visited step (gets it from subwizard, if last step was from it).
Detaches $back steps back.
Redirects $back steps back.
Data in wizard. Can be called within hashref as first argument to override current data. Returns hash with data.
Sets key/value pairs in wizard data hash.
Gets entry from data hash with key.
Stash in wizard. Can be copyed from $c via $c->wizard->copy_stash call (or automagically if configured so). Note that wizard stash will be copyed into $c only on next step.
Adds key/value pairs into wizard's stash.
Gets stash value named key from wizard's stash.
Params in wizard. Same as stash in wizard. Note that this will be copied into $c only on next step.
Catalyst::Plugin::Continuation, Catalyst::Plugin::Session.
Pavel Boldin <boldin.pavel@gmail.com>
To install Catalyst::Plugin::Wizard, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::Plugin::Wizard
CPAN shell
perl -MCPAN -e shell install Catalyst::Plugin::Wizard
For more information on module installation, please visit the detailed CPAN module installation guide.