PHP Graveyard: fw_substage($stage_name, $skip_flag)
Figured out a much more sensible way to implement optional stages in a form wizard.
/* fw_substage
ARGS: stage_name (name of optional stage)
skip_flag (post value from previous stage that dictates whether to skip stage
RETURN: global array SUB_MEMO to which fw_backend reacts
add this to main root wizard page before fw_constructor
******************************/
function fw_substage($stage_name, $skip_flag) {
// global variables
global $SUB_MEMO, $STAGES;
// verify $STAGES array
if ( !is_array($STAGES) ) {
trigger_error("STAGES not array", E_USER_ERROR);
}
// if $stage_name not current stage, go no further
if ( $stage_name != $_SESSION['form_wizard']['current_stage'] ) {
return;
}
// naturalize POST
$skip = $_POST[$skip_flag];
// process skip_flag
if ( strtolower($skip) == 'y' ) { $skip = TRUE; }
if ( strtolower($skip) == 'n' ) { $skip = FALSE; }
// id next stage
reset($STAGES);
while ( current($STAGES) != $stage_name ) {
if ( !next($STAGES) ) {
trigger_error(__FUNCTION__."stage name '$stage_name' not matched
", E_USER_ERROR);
}
}
$next_stage = next($STAGES);
// determine whether to skip this stage
/* going forward */
if ( $skip === TRUE ) { /* skip flag set, SUB_MEMO to fw_backend: skip the next stage */
$_SESSION['skip_stage'][$stage_name] = TRUE;
$SUB_MEMO = array('action' => 'redirect', 'target' => 'self', 'stage' => 1);
return $SUB_MEMO;
} elseif ( $skip === FALSE ) { /* if isset and !TRUE */
$_SESSION['skip_stage'][$stage_name] = FALSE; /* session SESSION skip_stage {$name} to FALSE */
return $SUB_MEMO;
}
/* going backward (from subsequent stage) */
if ( $_SESSION['form_wizard']['prev_stage'] == $next_stage && $_SESSION['skip_stage'][$stage_name] ) {
$_SESSION['form_wizard']['prev_stage'] = 'null';
$SUB_MEMO = array('action' => 'redirect', 'target' => 'self', 'stage' => -1);
return $SUB_MEMO;
}
return $SUB_MEMO;
} /* end Fx */
/******************************/
ARGS: stage_name (name of optional stage)
skip_flag (post value from previous stage that dictates whether to skip stage
RETURN: global array SUB_MEMO to which fw_backend reacts
add this to main root wizard page before fw_constructor
******************************/
function fw_substage($stage_name, $skip_flag) {
// global variables
global $SUB_MEMO, $STAGES;
// verify $STAGES array
if ( !is_array($STAGES) ) {
trigger_error("STAGES not array", E_USER_ERROR);
}
// if $stage_name not current stage, go no further
if ( $stage_name != $_SESSION['form_wizard']['current_stage'] ) {
return;
}
// naturalize POST
$skip = $_POST[$skip_flag];
// process skip_flag
if ( strtolower($skip) == 'y' ) { $skip = TRUE; }
if ( strtolower($skip) == 'n' ) { $skip = FALSE; }
// id next stage
reset($STAGES);
while ( current($STAGES) != $stage_name ) {
if ( !next($STAGES) ) {
trigger_error(__FUNCTION__."stage name '$stage_name' not matched
", E_USER_ERROR);
}
}
$next_stage = next($STAGES);
// determine whether to skip this stage
/* going forward */
if ( $skip === TRUE ) { /* skip flag set, SUB_MEMO to fw_backend: skip the next stage */
$_SESSION['skip_stage'][$stage_name] = TRUE;
$SUB_MEMO = array('action' => 'redirect', 'target' => 'self', 'stage' => 1);
return $SUB_MEMO;
} elseif ( $skip === FALSE ) { /* if isset and !TRUE */
$_SESSION['skip_stage'][$stage_name] = FALSE; /* session SESSION skip_stage {$name} to FALSE */
return $SUB_MEMO;
}
/* going backward (from subsequent stage) */
if ( $_SESSION['form_wizard']['prev_stage'] == $next_stage && $_SESSION['skip_stage'][$stage_name] ) {
$_SESSION['form_wizard']['prev_stage'] = 'null';
$SUB_MEMO = array('action' => 'redirect', 'target' => 'self', 'stage' => -1);
return $SUB_MEMO;
}
return $SUB_MEMO;
} /* end Fx */
/******************************/
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home