2013-05-25 09:04:59 +02:00
|
|
|
<?php
|
|
|
|
|
2017-09-07 01:32:55 +02:00
|
|
|
namespace SilverStripe\MultiForm\Models;
|
|
|
|
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\Security\Security;
|
|
|
|
|
2013-05-25 09:04:59 +02:00
|
|
|
/**
|
2015-11-02 01:38:50 +01:00
|
|
|
* Serializes one or more {@link MultiFormStep}s into
|
2013-05-25 09:04:59 +02:00
|
|
|
* a database object.
|
2015-11-02 01:38:50 +01:00
|
|
|
*
|
2013-05-25 09:04:59 +02:00
|
|
|
* MultiFormSession also stores the current step, so that
|
|
|
|
* the {@link MultiForm} and {@link MultiFormStep} classes
|
|
|
|
* know what the current step is.
|
2015-11-02 01:38:50 +01:00
|
|
|
*
|
2013-05-25 09:04:59 +02:00
|
|
|
* @package multiform
|
|
|
|
*/
|
2017-09-06 22:47:35 +02:00
|
|
|
class MultiFormSession extends DataObject
|
|
|
|
{
|
2017-09-07 01:32:55 +02:00
|
|
|
private static $db = [
|
2017-09-06 22:47:35 +02:00
|
|
|
'Hash' => 'Varchar(40)', // cryptographic hash identification to this session
|
|
|
|
'IsComplete' => 'Boolean' // flag to determine if this session is marked completed
|
2017-09-07 01:32:55 +02:00
|
|
|
];
|
2015-11-02 01:38:50 +01:00
|
|
|
|
2017-09-07 01:32:55 +02:00
|
|
|
private static $has_one = [
|
2017-09-06 22:47:35 +02:00
|
|
|
'Submitter' => 'Member',
|
|
|
|
'CurrentStep' => 'MultiFormStep'
|
2017-09-07 01:32:55 +02:00
|
|
|
];
|
2013-05-25 09:04:59 +02:00
|
|
|
|
2017-09-07 01:32:55 +02:00
|
|
|
private static $has_many = [
|
2017-09-06 22:47:35 +02:00
|
|
|
'FormSteps' => 'MultiFormStep'
|
2017-09-07 01:32:55 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $table_name = 'MultiFormSession';
|
2015-11-02 01:38:50 +01:00
|
|
|
|
2017-09-06 22:47:35 +02:00
|
|
|
/**
|
|
|
|
* Mark this session as completed.
|
|
|
|
*
|
|
|
|
* This sets the flag "IsComplete" to true,
|
|
|
|
* and writes the session back.
|
|
|
|
*/
|
|
|
|
public function markCompleted()
|
|
|
|
{
|
|
|
|
$this->IsComplete = 1;
|
|
|
|
$this->write();
|
|
|
|
}
|
2015-11-02 01:38:50 +01:00
|
|
|
|
2017-09-06 22:47:35 +02:00
|
|
|
/**
|
|
|
|
* These actions are performed when write() is called on this object.
|
|
|
|
*/
|
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
|
|
|
// save submitter if a Member is logged in
|
2017-09-07 01:32:55 +02:00
|
|
|
$currentMember = Security::getCurrentUser();
|
|
|
|
if (!$this->SubmitterID && $currentMember->ID) {
|
2017-09-06 22:47:35 +02:00
|
|
|
$this->SubmitterID = $currentMember->ID;
|
|
|
|
}
|
2015-11-02 01:38:50 +01:00
|
|
|
|
2017-09-06 22:47:35 +02:00
|
|
|
parent::onBeforeWrite();
|
|
|
|
}
|
2013-05-25 09:04:59 +02:00
|
|
|
|
2017-09-06 22:47:35 +02:00
|
|
|
/**
|
|
|
|
* These actions are performed when delete() is called on this object.
|
|
|
|
*/
|
|
|
|
public function onBeforeDelete()
|
|
|
|
{
|
|
|
|
// delete dependent form steps and relation
|
|
|
|
$steps = $this->FormSteps();
|
|
|
|
if ($steps) {
|
|
|
|
foreach ($steps as $step) {
|
|
|
|
if ($step && $step->exists()) {
|
|
|
|
$steps->remove($step);
|
|
|
|
$step->delete();
|
|
|
|
$step->destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-25 09:04:59 +02:00
|
|
|
|
2017-09-06 22:47:35 +02:00
|
|
|
parent::onBeforeDelete();
|
|
|
|
}
|
2013-05-25 09:04:59 +02:00
|
|
|
}
|