Tuesday, March 29, 2005

PHP: fw_substage()

A function that makes a stage in my form wizard optional. A bit convoluted. But after hours of mental anguish that pushed my mind to the very brink, I got it to operate:

/* fw_substage
ARGS: stage_name (name of optional stage)
skip_flag (post value from previous stage that dictates
whether to skip stage)
Call before fw_constructor
RETURN: global array SUB_MEMO to which fw_backend reacts
******************************/
// ARGS: $stage_name, $skip_flag = Y/N, T/F
function fw_substage($stage_name, $skip_flag) {

// global variables
global $SUB_MEMO, $STAGES;

// check $STAGES array
if ( !is_array($STAGES) ) {
trigger_error("STAGES not array", E_USER_ERROR);
}

// naturalize POST
$skip = $_POST[$skip_flag];

// conform skip_flag
if ( strtolower($skip) == 'y' ) { $skip = TRUE; };
if ( strtolower($skip) == 'n' ) { $skip = FALSE; };

// id previous stage and 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
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