2008-04-18 00:03:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2008-04-22 13:03:03 +02:00
|
|
|
* MultiForm manages the loading of single form steps, and acts as a state
|
|
|
|
* machine that connects to a {@link MultiFormSession} object as a persistence
|
|
|
|
* layer.
|
2008-04-18 00:03:51 +02:00
|
|
|
*
|
|
|
|
* CAUTION: If you're using controller permission control,
|
|
|
|
* you have to allow the following methods:
|
2013-05-25 09:04:59 +02:00
|
|
|
*
|
2008-04-18 00:03:51 +02:00
|
|
|
* <code>
|
2013-05-25 09:04:59 +02:00
|
|
|
* private static $allowed_actions = array('next','prev');
|
2008-04-18 00:03:51 +02:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @package multiform
|
|
|
|
*/
|
|
|
|
abstract class MultiForm extends Form {
|
|
|
|
|
|
|
|
/**
|
2008-07-21 23:06:59 +02:00
|
|
|
* A session object stored in the database, to identify and store
|
|
|
|
* data for this MultiForm instance.
|
2008-04-18 00:03:51 +02:00
|
|
|
*
|
|
|
|
* @var MultiFormSession
|
|
|
|
*/
|
|
|
|
protected $session;
|
|
|
|
|
2009-08-05 12:12:13 +02:00
|
|
|
/**
|
|
|
|
* The current encrypted MultiFormSession identification.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $currentSessionHash;
|
|
|
|
|
2008-04-18 00:03:51 +02:00
|
|
|
/**
|
2008-07-21 23:06:59 +02:00
|
|
|
* Defines which subclass of {@link MultiFormStep} should be the first
|
|
|
|
* step in the multi-step process.
|
2008-04-18 00:03:51 +02:00
|
|
|
*
|
|
|
|
* @var string Classname of a {@link MultiFormStep} subclass
|
|
|
|
*/
|
2010-05-11 05:30:55 +02:00
|
|
|
public static $start_step;
|
2008-04-19 03:14:16 +02:00
|
|
|
|
2008-04-22 13:03:03 +02:00
|
|
|
/**
|
|
|
|
* Set the casting for these fields.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2008-07-21 23:06:59 +02:00
|
|
|
public static $casting = array(
|
2008-04-18 00:03:51 +02:00
|
|
|
'CompletedStepCount' => 'Int',
|
|
|
|
'TotalStepCount' => 'Int',
|
|
|
|
'CompletedPercent' => 'Float'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* These fields are ignored when saving the raw form data into session.
|
|
|
|
* This ensures only field data is saved, and nothing else that's useless
|
|
|
|
* or potentially dangerous.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2008-07-21 23:06:59 +02:00
|
|
|
public static $ignored_fields = array(
|
2008-04-18 00:03:51 +02:00
|
|
|
'url',
|
|
|
|
'executeForm',
|
|
|
|
'MultiFormSessionID',
|
|
|
|
'SecurityID'
|
2008-10-01 20:36:52 +02:00
|
|
|
);
|
2008-04-18 00:03:51 +02:00
|
|
|
|
2008-12-17 12:08:46 +01:00
|
|
|
/**
|
|
|
|
* Any of the actions defined in this variable are exempt from
|
|
|
|
* being validated.
|
|
|
|
*
|
|
|
|
* This is most useful for the "Back" (action_prev) action, as
|
|
|
|
* you typically don't validate the form when the user is going
|
|
|
|
* back a step.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $actions_exempt_from_validation = array(
|
|
|
|
'action_prev'
|
|
|
|
);
|
2011-12-19 11:38:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $displayLink;
|
|
|
|
|
2008-04-18 00:03:51 +02:00
|
|
|
/**
|
2008-09-20 11:36:54 +02:00
|
|
|
* Start the MultiForm instance.
|
2008-06-19 12:29:47 +02:00
|
|
|
*
|
2008-09-20 11:36:54 +02:00
|
|
|
* @param Controller instance $controller Controller this form is created on
|
2008-06-19 12:29:47 +02:00
|
|
|
* @param string $name The form name, typically the same as the method name
|
2008-04-18 00:03:51 +02:00
|
|
|
*/
|
2008-06-19 12:29:47 +02:00
|
|
|
public function __construct($controller, $name) {
|
2009-08-05 12:12:13 +02:00
|
|
|
if(isset($_GET['MultiFormSessionID'])) {
|
|
|
|
$this->setCurrentSessionHash($_GET['MultiFormSessionID']);
|
|
|
|
}
|
2011-02-05 11:50:17 +01:00
|
|
|
|
|
|
|
// First set the controller and name manually so they are available for
|
|
|
|
// field construction.
|
|
|
|
$this->controller = $controller;
|
|
|
|
$this->name = $name;
|
|
|
|
|
2008-05-27 06:49:16 +02:00
|
|
|
// Set up the session for this MultiForm instance
|
2008-04-20 02:01:27 +02:00
|
|
|
$this->setSession();
|
2008-07-21 05:21:51 +02:00
|
|
|
|
2008-05-27 06:49:16 +02:00
|
|
|
// Get the current step available (Note: either returns an existing
|
|
|
|
// step or creates a new one if none available)
|
2008-04-20 02:01:27 +02:00
|
|
|
$currentStep = $this->getCurrentStep();
|
2008-07-21 05:21:51 +02:00
|
|
|
|
2008-07-21 07:22:06 +02:00
|
|
|
// Set the step returned above as the current step
|
|
|
|
$this->setCurrentStep($currentStep);
|
|
|
|
|
|
|
|
// Set the form of the step to this form instance
|
2009-07-20 11:22:49 +02:00
|
|
|
$currentStep->setForm($this);
|
2008-07-21 07:22:06 +02:00
|
|
|
|
2008-07-21 05:21:51 +02:00
|
|
|
// Set up the fields for the current step
|
|
|
|
$fields = $currentStep->getFields();
|
|
|
|
|
|
|
|
// Set up the actions for the current step
|
|
|
|
$actions = $this->actionsFor($currentStep);
|
|
|
|
|
2008-10-01 20:36:52 +02:00
|
|
|
// Set up validation (if necessary)
|
2008-07-21 23:06:59 +02:00
|
|
|
$validator = null;
|
2008-12-17 12:08:46 +01:00
|
|
|
$applyValidation = true;
|
|
|
|
|
2013-05-25 09:04:59 +02:00
|
|
|
$actionNames = static::$actions_exempt_from_validation;
|
2012-05-14 06:36:41 +02:00
|
|
|
|
2009-07-28 01:14:04 +02:00
|
|
|
if( $actionNames ) {
|
|
|
|
foreach( $actionNames as $exemptAction) {
|
2008-12-17 12:08:46 +01:00
|
|
|
if(!empty($_REQUEST[$exemptAction])) {
|
|
|
|
$applyValidation = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply validation if the current step requires validation (is not exempt)
|
|
|
|
if($applyValidation) {
|
2008-12-17 12:11:53 +01:00
|
|
|
if($currentStep->getValidator()) {
|
|
|
|
$validator = $currentStep->getValidator();
|
2008-07-21 05:21:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give the fields, actions, and validation for the current step back to the parent Form class
|
2008-07-21 23:06:59 +02:00
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator);
|
2008-06-18 10:07:11 +02:00
|
|
|
|
2008-07-21 23:06:59 +02:00
|
|
|
// Set a hidden field in our form with an encrypted hash to identify this session.
|
|
|
|
$this->fields->push(new HiddenField('MultiFormSessionID', false, $this->session->Hash));
|
2008-04-20 02:01:27 +02:00
|
|
|
|
2008-07-21 23:06:59 +02:00
|
|
|
// If there is saved data for the current step, we load it into the form it here
|
|
|
|
//(CAUTION: loadData() MUST unserialize first!)
|
2012-09-26 23:50:13 +02:00
|
|
|
if($data = $currentStep->loadData()) {
|
|
|
|
$this->loadDataFrom($data);
|
2008-04-30 05:47:33 +02:00
|
|
|
}
|
2008-07-04 03:07:33 +02:00
|
|
|
|
2008-07-21 23:06:59 +02:00
|
|
|
// Disable security token - we tie a form to a session ID instead
|
2008-07-04 03:07:33 +02:00
|
|
|
$this->disableSecurityToken();
|
2008-04-20 02:01:27 +02:00
|
|
|
}
|
|
|
|
|
2008-04-21 10:32:05 +02:00
|
|
|
/**
|
2008-07-21 23:06:59 +02:00
|
|
|
* Accessor method to $this->controller.
|
|
|
|
*
|
|
|
|
* @return Controller this MultiForm was instanciated on.
|
2008-04-21 10:32:05 +02:00
|
|
|
*/
|
|
|
|
public function getController() {
|
|
|
|
return $this->controller;
|
|
|
|
}
|
|
|
|
|
2008-04-20 02:01:27 +02:00
|
|
|
/**
|
|
|
|
* Get the current step.
|
2008-04-22 13:03:03 +02:00
|
|
|
*
|
|
|
|
* If StepID has been set in the URL, we attempt to get that record
|
|
|
|
* by the ID. Otherwise, we check if there's a current step ID in
|
|
|
|
* our session record. Failing those cases, we assume that the form has
|
|
|
|
* just been started, and so we create the first step and return it.
|
|
|
|
*
|
2008-04-20 02:01:27 +02:00
|
|
|
* @return MultiFormStep subclass
|
|
|
|
*/
|
|
|
|
public function getCurrentStep() {
|
2013-05-25 09:04:59 +02:00
|
|
|
$startStepClass = static::$start_step;
|
2008-04-19 03:14:16 +02:00
|
|
|
|
2008-04-19 03:23:28 +02:00
|
|
|
// Check if there was a start step defined on the subclass of MultiForm
|
2008-04-19 03:14:16 +02:00
|
|
|
if(!isset($startStepClass)) user_error('MultiForm::init(): Please define a $startStep on ' . $this->class, E_USER_ERROR);
|
2008-04-18 00:03:51 +02:00
|
|
|
|
|
|
|
// Determine whether we use the current step, or create one if it doesn't exist
|
2011-07-19 09:51:04 +02:00
|
|
|
$currentStep = null;
|
2008-04-18 00:03:51 +02:00
|
|
|
if(isset($_GET['StepID'])) {
|
|
|
|
$stepID = (int)$_GET['StepID'];
|
2010-03-14 22:09:04 +01:00
|
|
|
$currentStep = DataObject::get_one('MultiFormStep', "\"SessionID\" = {$this->session->ID} AND \"ID\" = {$stepID}");
|
2008-04-18 00:03:51 +02:00
|
|
|
} elseif($this->session->CurrentStepID) {
|
|
|
|
$currentStep = $this->session->CurrentStep();
|
2011-07-19 09:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Always fall back to creating a new step (in case the session or request data is invalid)
|
2012-10-10 12:49:56 +02:00
|
|
|
if(!$currentStep || !$currentStep->ID) {
|
2008-04-18 00:03:51 +02:00
|
|
|
$currentStep = new $startStepClass();
|
|
|
|
$currentStep->SessionID = $this->session->ID;
|
|
|
|
$currentStep->write();
|
2012-10-10 12:49:56 +02:00
|
|
|
$this->session->CurrentStepID = $currentStep->ID;
|
|
|
|
$this->session->write();
|
|
|
|
$this->session->flushCache();
|
2008-04-18 00:03:51 +02:00
|
|
|
}
|
2008-05-14 12:57:41 +02:00
|
|
|
|
2011-07-19 09:51:04 +02:00
|
|
|
if($currentStep) $currentStep->setForm($this);
|
2009-07-20 11:22:49 +02:00
|
|
|
|
2008-04-20 02:01:27 +02:00
|
|
|
return $currentStep;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the step passed in as the current step.
|
2008-06-18 10:07:11 +02:00
|
|
|
*
|
2008-04-20 02:01:27 +02:00
|
|
|
* @param MultiFormStep $step A subclass of MultiFormStep
|
2008-07-21 23:06:59 +02:00
|
|
|
* @return boolean The return value of write()
|
2008-04-20 02:01:27 +02:00
|
|
|
*/
|
|
|
|
protected function setCurrentStep($step) {
|
|
|
|
$this->session->CurrentStepID = $step->ID;
|
2011-12-19 04:21:47 +01:00
|
|
|
$step->setForm($this);
|
|
|
|
|
2008-07-21 23:06:59 +02:00
|
|
|
return $this->session->write();
|
2008-04-20 02:01:27 +02:00
|
|
|
}
|
|
|
|
|
2008-05-14 12:22:58 +02:00
|
|
|
/**
|
|
|
|
* Accessor method to $this->session.
|
2008-06-18 10:07:11 +02:00
|
|
|
*
|
2008-05-14 12:22:58 +02:00
|
|
|
* @return MultiFormSession
|
|
|
|
*/
|
|
|
|
function getSession() {
|
|
|
|
return $this->session;
|
|
|
|
}
|
|
|
|
|
2008-04-20 02:01:27 +02:00
|
|
|
/**
|
2008-04-22 13:03:03 +02:00
|
|
|
* Set up the session.
|
|
|
|
*
|
|
|
|
* If MultiFormSessionID isn't set, we assume that this is a new
|
|
|
|
* multiform that requires a new session record to be created.
|
|
|
|
*
|
|
|
|
* @TODO Fix the fact you can continually refresh and create new records
|
|
|
|
* if MultiFormSessionID isn't set.
|
2008-05-14 12:45:19 +02:00
|
|
|
*
|
|
|
|
* @TODO Not sure if we should bake the session stuff directly into MultiForm.
|
|
|
|
* Perhaps it would be best dealt with on a separate class?
|
2008-04-20 02:01:27 +02:00
|
|
|
*/
|
|
|
|
protected function setSession() {
|
2009-08-05 12:12:13 +02:00
|
|
|
$this->session = $this->getCurrentSession();
|
|
|
|
|
2008-05-14 13:30:35 +02:00
|
|
|
// If there was no session found, create a new one instead
|
|
|
|
if(!$this->session) {
|
2008-04-20 02:01:27 +02:00
|
|
|
$this->session = new MultiFormSession();
|
2008-05-14 13:30:35 +02:00
|
|
|
$this->session->write();
|
|
|
|
}
|
2009-08-05 12:12:13 +02:00
|
|
|
|
2008-07-21 23:06:59 +02:00
|
|
|
// Create encrypted identification to the session instance if it doesn't exist
|
|
|
|
if(!$this->session->Hash) {
|
|
|
|
$this->session->Hash = sha1($this->session->ID . '-' . microtime());
|
|
|
|
$this->session->write();
|
2008-04-18 00:03:51 +02:00
|
|
|
}
|
2008-10-01 20:36:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-08-05 12:12:13 +02:00
|
|
|
* Set the currently used encrypted hash to identify
|
|
|
|
* the MultiFormSession.
|
|
|
|
*
|
|
|
|
* @param string $hash Encrypted identification to session
|
|
|
|
*/
|
|
|
|
function setCurrentSessionHash($hash) {
|
|
|
|
$this->currentSessionHash = $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the currently used {@link MultiFormSession}
|
|
|
|
* @return MultiFormSession|boolean FALSE
|
2008-10-01 20:36:52 +02:00
|
|
|
*/
|
2009-08-05 12:12:13 +02:00
|
|
|
function getCurrentSession() {
|
|
|
|
if(!$this->currentSessionHash) return false;
|
|
|
|
$SQL_hash = Convert::raw2sql($this->currentSessionHash);
|
2010-03-14 22:09:04 +01:00
|
|
|
return DataObject::get_one('MultiFormSession', "\"Hash\" = '$SQL_hash' AND \"IsComplete\" = 0");
|
2008-10-01 20:36:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all steps saved in the database for the currently active session,
|
|
|
|
* in the order they were saved, oldest to newest (automatically ordered by ID).
|
|
|
|
* If you want a full chain of steps regardless if they've already been saved
|
|
|
|
* to the database, use {@link getAllStepsLinear()}.
|
|
|
|
*
|
2011-07-19 12:52:21 +02:00
|
|
|
* @param String $filter SQL WHERE statement
|
2008-10-01 20:36:52 +02:00
|
|
|
* @return DataObjectSet|boolean A set of MultiFormStep subclasses
|
|
|
|
*/
|
2011-07-19 12:52:21 +02:00
|
|
|
function getSavedSteps($filter = null) {
|
|
|
|
$filter .= ($filter) ? ' AND ' : '';
|
|
|
|
$filter .= sprintf("\"SessionID\" = '%s'", $this->session->ID);
|
|
|
|
return DataObject::get('MultiFormStep', $filter);
|
2008-10-01 20:36:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a step which was previously saved to the database in the current session.
|
|
|
|
* Caution: This might cause unexpected behaviour if you have multiple steps
|
|
|
|
* in your chain with the same classname.
|
|
|
|
*
|
|
|
|
* @param string $className Classname of a {@link MultiFormStep} subclass
|
|
|
|
* @return MultiFormStep
|
|
|
|
*/
|
|
|
|
function getSavedStepByClass($className) {
|
|
|
|
return DataObject::get_one(
|
|
|
|
'MultiFormStep',
|
2010-03-14 22:09:04 +01:00
|
|
|
sprintf("\"SessionID\" = '%s' AND \"ClassName\" = '%s'",
|
2008-10-01 20:36:52 +02:00
|
|
|
$this->session->ID,
|
|
|
|
Convert::raw2sql($className)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2008-07-21 23:06:59 +02:00
|
|
|
|
2008-05-25 01:49:49 +02:00
|
|
|
/**
|
2012-05-14 05:23:01 +02:00
|
|
|
* Build a FieldList of the FormAction fields for the given step.
|
|
|
|
*
|
2008-04-22 13:03:03 +02:00
|
|
|
* If the current step is the final step, we push in a submit button, which
|
|
|
|
* calls the action {@link finish()} to finalise the submission. Otherwise,
|
|
|
|
* we push in a next button which calls the action {@link next()} to determine
|
|
|
|
* where to go next in our step process, and save any form data collected.
|
2012-05-14 05:23:01 +02:00
|
|
|
*
|
2008-04-22 13:03:03 +02:00
|
|
|
* If there's a previous step (a step that has the current step as it's next
|
|
|
|
* step class), then we allow a previous button, which calls the previous action
|
|
|
|
* to determine which step to go back to.
|
2012-05-14 05:23:01 +02:00
|
|
|
*
|
2008-04-22 13:03:03 +02:00
|
|
|
* If there are any extra actions defined in MultiFormStep->getExtraActions()
|
|
|
|
* then that set of actions is appended to the end of the actions FieldSet we
|
|
|
|
* have created in this method.
|
2012-05-14 05:23:01 +02:00
|
|
|
*
|
2008-07-21 05:21:51 +02:00
|
|
|
* @param $currentStep Subclass of MultiFormStep
|
2012-05-14 05:23:01 +02:00
|
|
|
* @return FieldList of FormAction objects
|
2008-04-18 00:03:51 +02:00
|
|
|
*/
|
2008-07-21 05:21:51 +02:00
|
|
|
function actionsFor($step) {
|
2008-04-18 00:03:51 +02:00
|
|
|
// Create default multi step actions (next, prev), and merge with extra actions, if any
|
2012-05-14 05:23:01 +02:00
|
|
|
$actions = (class_exists('FieldList')) ? new FieldList() : new FieldSet();
|
2008-04-18 00:03:51 +02:00
|
|
|
|
|
|
|
// If the form is at final step, create a submit button to perform final actions
|
|
|
|
// The last step doesn't have a next button, so add that action to any step that isn't the final one
|
2008-07-21 05:21:51 +02:00
|
|
|
if($step->isFinalStep()) {
|
2010-05-11 05:27:42 +02:00
|
|
|
$actions->push(new FormAction('finish', $step->getSubmitText()));
|
2008-04-18 00:03:51 +02:00
|
|
|
} else {
|
2010-05-11 05:27:42 +02:00
|
|
|
$actions->push(new FormAction('next', $step->getNextText()));
|
2008-04-18 00:03:51 +02:00
|
|
|
}
|
2008-10-01 20:36:52 +02:00
|
|
|
|
2008-04-18 00:03:51 +02:00
|
|
|
// If there is a previous step defined, add the back button
|
2008-07-21 05:21:51 +02:00
|
|
|
if($step->getPreviousStep() && $step->canGoBack()) {
|
2008-05-27 06:49:16 +02:00
|
|
|
// If there is a next step, insert the action before the next action
|
2008-07-21 05:21:51 +02:00
|
|
|
if($step->getNextStep()) {
|
2010-05-11 05:27:42 +02:00
|
|
|
$actions->insertBefore(new FormAction('prev', $step->getPrevText()), 'action_next');
|
2008-05-27 06:49:16 +02:00
|
|
|
// Assume that this is the last step, insert the action before the finish action
|
2008-04-18 00:03:51 +02:00
|
|
|
} else {
|
2010-05-11 05:27:42 +02:00
|
|
|
$actions->insertBefore(new FormAction('prev', $step->getPrevText()), 'action_finish');
|
2008-04-18 00:03:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge any extra action fields defined on the step
|
2008-07-21 05:21:51 +02:00
|
|
|
$actions->merge($step->getExtraActions());
|
|
|
|
|
|
|
|
return $actions;
|
2008-04-18 00:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a rendered version of this form, with a specific template.
|
|
|
|
* Looks through the step ancestory templates (MultiFormStep, current step
|
|
|
|
* subclass template) to see if one is available to render the form with. If
|
|
|
|
* any of those don't exist, look for a default Form template to render
|
|
|
|
* with instead.
|
2008-07-21 23:06:59 +02:00
|
|
|
*
|
|
|
|
* @return SSViewer object to render the template with
|
2008-04-18 00:03:51 +02:00
|
|
|
*/
|
|
|
|
function forTemplate() {
|
|
|
|
return $this->renderWith(array(
|
2008-04-20 02:28:18 +02:00
|
|
|
$this->getCurrentStep()->class,
|
2008-04-18 00:03:51 +02:00
|
|
|
'MultiFormStep',
|
|
|
|
$this->class,
|
|
|
|
'MultiForm',
|
|
|
|
'Form'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method saves the data on the final step, after submitting.
|
|
|
|
* It should always be overloaded with parent::finish($data, $form)
|
|
|
|
* so you can create your own functionality which handles saving
|
|
|
|
* of all the data collected through each step of the form.
|
|
|
|
*
|
2008-10-01 20:36:52 +02:00
|
|
|
* @param array $data The request data returned from the form
|
2008-04-18 00:03:51 +02:00
|
|
|
* @param object $form The form that the action was called on
|
|
|
|
*/
|
2008-10-01 20:36:52 +02:00
|
|
|
public function finish($data, $form) {
|
|
|
|
if(!$this->getCurrentStep()->isFinalStep()) {
|
2012-06-28 03:51:08 +02:00
|
|
|
$this->controller->redirectBack();
|
2008-10-01 20:36:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-12-19 04:21:56 +01:00
|
|
|
|
|
|
|
if(!$this->getCurrentStep()->validateStep($data, $form)) {
|
|
|
|
Session::set("FormInfo.{$form->FormName()}.data", $form->getData());
|
2012-06-28 03:51:08 +02:00
|
|
|
$this->controller->redirectBack();
|
2011-12-19 04:21:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-01 20:36:52 +02:00
|
|
|
// Save the form data for the current step
|
2008-04-18 00:03:51 +02:00
|
|
|
$this->save($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine what to do when the next action is called.
|
|
|
|
*
|
|
|
|
* Saves the current step session data to the database, creates the
|
2008-04-22 13:03:03 +02:00
|
|
|
* new step based on getNextStep() of the current step (or fetches
|
|
|
|
* an existing one), resets the current step to the next step,
|
2008-10-01 20:36:52 +02:00
|
|
|
* then redirects to the newly set step.
|
|
|
|
*
|
|
|
|
* @param array $data The request data returned from the form
|
2008-04-18 00:03:51 +02:00
|
|
|
* @param object $form The form that the action was called on
|
|
|
|
*/
|
|
|
|
public function next($data, $form) {
|
2010-01-10 01:57:33 +01:00
|
|
|
// Save the form data for the current step
|
|
|
|
$this->save($form->getData());
|
2009-07-28 01:14:04 +02:00
|
|
|
|
2008-10-01 20:36:52 +02:00
|
|
|
// Get the next step class
|
|
|
|
$nextStepClass = $this->getCurrentStep()->getNextStep();
|
|
|
|
|
|
|
|
if(!$nextStepClass) {
|
2012-06-28 03:51:08 +02:00
|
|
|
$this->controller->redirectBack();
|
2008-10-01 20:36:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-05 21:59:59 +01:00
|
|
|
// Perform custom step validation (use MultiFormStep->getValidator() for
|
|
|
|
// built-in functionality). The data needs to be manually saved on error
|
|
|
|
// so the form is re-populated.
|
2008-10-01 20:36:52 +02:00
|
|
|
if(!$this->getCurrentStep()->validateStep($data, $form)) {
|
2011-02-05 21:59:59 +01:00
|
|
|
Session::set("FormInfo.{$form->FormName()}.data", $form->getData());
|
2012-06-28 03:51:08 +02:00
|
|
|
$this->controller->redirectBack();
|
2008-04-18 00:03:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-24 17:24:27 +02:00
|
|
|
// validation succeeded so we reset it to remove errors and messages
|
|
|
|
$this->resetValidation();
|
|
|
|
|
2008-10-01 20:36:52 +02:00
|
|
|
// Determine whether we can use a step already in the DB, or have to create a new one
|
2010-03-14 22:09:04 +01:00
|
|
|
if(!$nextStep = DataObject::get_one($nextStepClass, "\"SessionID\" = {$this->session->ID}")) {
|
2008-04-18 00:03:51 +02:00
|
|
|
$nextStep = new $nextStepClass();
|
|
|
|
$nextStep->SessionID = $this->session->ID;
|
2008-04-20 02:28:18 +02:00
|
|
|
$nextStep->write();
|
2008-04-18 00:03:51 +02:00
|
|
|
}
|
|
|
|
|
2008-04-22 13:03:03 +02:00
|
|
|
// Set the next step found as the current step
|
2008-04-20 02:28:18 +02:00
|
|
|
$this->setCurrentStep($nextStep);
|
2008-04-18 00:03:51 +02:00
|
|
|
|
|
|
|
// Redirect to the next step
|
2012-06-28 03:51:08 +02:00
|
|
|
$this->controller->redirect($nextStep->Link());
|
2008-04-18 00:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine what to do when the previous action is called.
|
|
|
|
*
|
2008-04-22 13:03:03 +02:00
|
|
|
* Retrieves the previous step class, finds the record for that
|
|
|
|
* class in the DB, and sets the current step to that step found.
|
2008-10-01 20:36:52 +02:00
|
|
|
* Finally, it redirects to that step.
|
|
|
|
*
|
|
|
|
* @param array $data The request data returned from the form
|
2008-04-18 00:03:51 +02:00
|
|
|
* @param object $form The form that the action was called on
|
|
|
|
*/
|
|
|
|
public function prev($data, $form) {
|
2010-01-10 01:57:33 +01:00
|
|
|
// Save the form data for the current step
|
|
|
|
$this->save($form->getData());
|
|
|
|
|
2008-10-02 00:18:44 +02:00
|
|
|
// Get the previous step class
|
|
|
|
$prevStepClass = $this->getCurrentStep()->getPreviousStep();
|
|
|
|
|
|
|
|
if(!$prevStepClass && !$this->getCurrentStep()->canGoBack()) {
|
2012-06-28 03:51:08 +02:00
|
|
|
$this->controller->redirectBack();
|
2008-04-18 00:03:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the previous step of the class instance returned from $currentStep->getPreviousStep()
|
2010-03-22 11:49:04 +01:00
|
|
|
$prevStep = DataObject::get_one($prevStepClass, "\"SessionID\" = {$this->session->ID}");
|
2008-04-18 00:03:51 +02:00
|
|
|
|
|
|
|
// Set the current step as the previous step
|
2008-04-20 02:28:18 +02:00
|
|
|
$this->setCurrentStep($prevStep);
|
2008-04-18 00:03:51 +02:00
|
|
|
|
|
|
|
// Redirect to the previous step
|
2012-06-28 03:51:08 +02:00
|
|
|
$this->controller->redirect($prevStep->Link());
|
2008-04-18 00:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the raw data given back from the form into session.
|
|
|
|
*
|
2008-04-22 13:03:03 +02:00
|
|
|
* Take the submitted form data for the current step, removing
|
|
|
|
* any key => value pairs that shouldn't be saved, then saves
|
|
|
|
* the data into the session.
|
2008-04-18 00:03:51 +02:00
|
|
|
*
|
|
|
|
* @param array $data An array of data to save
|
|
|
|
*/
|
|
|
|
protected function save($data) {
|
2008-04-20 02:28:18 +02:00
|
|
|
$currentStep = $this->getCurrentStep();
|
2008-04-18 00:03:51 +02:00
|
|
|
if(is_array($data)) {
|
|
|
|
foreach($data as $field => $value) {
|
2013-05-25 09:04:59 +02:00
|
|
|
if(in_array($field, static::$ignored_fields)) {
|
2008-04-18 00:03:51 +02:00
|
|
|
unset($data[$field]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$currentStep->saveData($data);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ############ Misc ############
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the MultiFormSessionID variable to the URL on form submission.
|
2008-07-21 23:06:59 +02:00
|
|
|
* This is a means to persist the session, by adding it's identification
|
2008-10-01 20:36:52 +02:00
|
|
|
* to the URL, which ties it back to this MultiForm instance.
|
|
|
|
*
|
2008-04-18 00:03:51 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2008-07-21 23:06:59 +02:00
|
|
|
function FormAction() {
|
2008-04-18 00:03:51 +02:00
|
|
|
$action = parent::FormAction();
|
|
|
|
$action .= (strpos($action, '?')) ? '&' : '?';
|
2008-07-21 23:06:59 +02:00
|
|
|
$action .= "MultiFormSessionID={$this->session->Hash}";
|
2008-04-18 00:03:51 +02:00
|
|
|
|
|
|
|
return $action;
|
|
|
|
}
|
|
|
|
|
2011-12-19 11:38:25 +01:00
|
|
|
/**
|
|
|
|
* Returns the link to the page where the form is displayed. The user is
|
|
|
|
* redirected to this link with a session param after each step is
|
|
|
|
* submitted.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDisplayLink() {
|
|
|
|
return $this->displayLink ? $this->displayLink : Controller::curr()->Link();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the link to the page on which the form is displayed.
|
|
|
|
*
|
|
|
|
* The link defaults to the controllers current link. However if the form
|
|
|
|
* is displayed inside an action the display link must be explicitly set.
|
|
|
|
*
|
|
|
|
* @param string $link
|
|
|
|
*/
|
|
|
|
public function setDisplayLink($link) {
|
|
|
|
$this->displayLink = $link;
|
|
|
|
}
|
|
|
|
|
2008-10-01 20:36:52 +02:00
|
|
|
/**
|
2008-04-18 00:03:51 +02:00
|
|
|
* Determine the steps to show in a linear fashion, starting from the
|
2008-10-01 20:36:52 +02:00
|
|
|
* first step. We run {@link getAllStepsRecursive} passing the steps found
|
|
|
|
* by reference to get a listing of the steps.
|
2008-04-18 00:03:51 +02:00
|
|
|
*
|
2011-07-12 11:40:03 +02:00
|
|
|
* @return DataObjectSet of MultiFormStep instances
|
2008-10-01 20:36:52 +02:00
|
|
|
*/
|
2008-04-18 00:03:51 +02:00
|
|
|
public function getAllStepsLinear() {
|
2012-05-14 05:23:01 +02:00
|
|
|
$stepsFound = (class_exists('ArrayList')) ? new ArrayList() : new DataObjectSet();
|
2008-04-18 00:03:51 +02:00
|
|
|
|
2013-05-25 09:04:59 +02:00
|
|
|
$firstStep = DataObject::get_one(static::$start_step, "\"SessionID\" = {$this->session->ID}");
|
2011-07-12 11:40:03 +02:00
|
|
|
$firstStep->LinkingMode = ($firstStep->ID == $this->getCurrentStep()->ID) ? 'current' : 'link';
|
|
|
|
$firstStep->setForm($this);
|
|
|
|
$stepsFound->push($firstStep);
|
2008-04-18 00:03:51 +02:00
|
|
|
|
|
|
|
$this->getAllStepsRecursive($firstStep, $stepsFound);
|
|
|
|
|
|
|
|
return $stepsFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively run through steps using the getNextStep() method on each step
|
|
|
|
* to determine what the next step is, gathering each step along the way.
|
|
|
|
* We stop on the last step, and return the results.
|
2008-10-01 20:36:52 +02:00
|
|
|
* If a step in the chain was already saved to the database in the current
|
|
|
|
* session, its used - otherwise a singleton of this step is used.
|
|
|
|
* Caution: Doesn't consider branching for steps which aren't in the database yet.
|
2008-04-18 00:03:51 +02:00
|
|
|
*
|
|
|
|
* @param $step Subclass of MultiFormStep to find the next step of
|
|
|
|
* @param $stepsFound $stepsFound DataObjectSet reference, the steps found to call back on
|
2011-07-12 11:40:03 +02:00
|
|
|
* @return DataObjectSet of MultiFormStep instances
|
2008-04-18 00:03:51 +02:00
|
|
|
*/
|
|
|
|
protected function getAllStepsRecursive($step, &$stepsFound) {
|
|
|
|
// Find the next step to the current step, the final step has no next step
|
|
|
|
if(!$step->isFinalStep()) {
|
|
|
|
if($step->getNextStep()) {
|
|
|
|
// Is this step in the DB? If it is, we use that
|
2011-07-12 11:40:03 +02:00
|
|
|
$nextStep = $step->getNextStepFromDatabase();
|
|
|
|
if(!$nextStep) {
|
2008-04-18 00:03:51 +02:00
|
|
|
// If it's not in the DB, we use a singleton instance of it instead - this step hasn't been accessed yet
|
|
|
|
$nextStep = singleton($step->getNextStep());
|
2011-07-12 11:40:03 +02:00
|
|
|
}
|
|
|
|
$nextStep->LinkingMode = ($nextStep->ID == $this->getCurrentStep()->ID) ? 'current' : 'link';
|
|
|
|
$nextStep->setForm($this);
|
2008-04-20 02:43:19 +02:00
|
|
|
// Add the array data, and do a callback
|
2011-07-12 11:40:03 +02:00
|
|
|
$stepsFound->push($nextStep);
|
2008-04-18 00:03:51 +02:00
|
|
|
$this->getAllStepsRecursive($nextStep, $stepsFound);
|
|
|
|
}
|
|
|
|
// Once we've reached the final step, we just return what we've collected
|
|
|
|
} else {
|
|
|
|
return $stepsFound;
|
|
|
|
}
|
2008-10-01 20:36:52 +02:00
|
|
|
}
|
2008-04-18 00:03:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of steps already completed (excluding currently started step).
|
|
|
|
* The way we determine a step is complete is to check if it has the Data
|
|
|
|
* field filled out with a serialized value, then we know that the user has
|
|
|
|
* clicked next on the given step, to proceed.
|
|
|
|
*
|
2008-04-22 13:03:03 +02:00
|
|
|
* @TODO Not sure if it's entirely appropriate to check if Data is set as a
|
|
|
|
* way to determine a step is "completed".
|
|
|
|
*
|
2008-04-18 00:03:51 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getCompletedStepCount() {
|
2010-03-22 11:49:04 +01:00
|
|
|
$steps = DataObject::get('MultiFormStep', "\"SessionID\" = {$this->session->ID} && \"Data\" IS NOT NULL");
|
2008-04-18 00:03:51 +02:00
|
|
|
return $steps ? $steps->Count() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Total number of steps in the shortest path (only counting straight path without any branching)
|
|
|
|
* The way we determine this is to check if each step has a next_step string variable set. If it's
|
|
|
|
* anything else (like an array, for defining multiple branches) then it gets counted as a single step.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getTotalStepCount() {
|
|
|
|
return $this->getAllStepsLinear() ? $this->getAllStepsLinear()->Count() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Percentage of steps completed (excluding currently started step)
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getCompletedPercent() {
|
|
|
|
return (float)$this->CompletedStepCount * 100 / $this->TotalStepCount;
|
2008-10-01 20:36:52 +02:00
|
|
|
}
|
2008-04-18 00:03:51 +02:00
|
|
|
|
2008-10-01 20:36:52 +02:00
|
|
|
}
|