mirror of
https://github.com/silverstripe/silverstripe-multiform
synced 2024-10-22 11:05:49 +02:00
Encapsulated getting session records, added notes
This commit is contained in:
parent
d5e613a9fb
commit
18fe36cdd9
@ -151,22 +151,50 @@ abstract class MultiForm extends Form {
|
|||||||
$urlType = $this->stat('url_type');
|
$urlType = $this->stat('url_type');
|
||||||
|
|
||||||
// If there's a MultiFormSessionID variable set, find that, otherwise create a new session
|
// If there's a MultiFormSessionID variable set, find that, otherwise create a new session
|
||||||
if(isset($_GET['MultiFormSessionID'])) {
|
if(isset($_GET['MultiFormSessionID'])) {
|
||||||
if($urlType == 'Hash') {
|
switch($urlType) {
|
||||||
$hash = Convert::raw2sql($_GET['MultiFormSessionID']);
|
case 'Hash':
|
||||||
$this->session = DataObject::get_one('MultiFormSession', "Hash = '$hash'");
|
$this->session = $this->getSessionRecordByHash($_GET['MultiFormSessionID']);
|
||||||
} elseif($urlType == 'ID') {
|
break;
|
||||||
$this->session = DataObject::get_by_id('MultiFormSession', (int)$_GET['MultiFormSessionID']);
|
case 'ID':
|
||||||
} else {
|
$this->session = $this->getSessionRecordByID($_GET['MultiFormSessionID']);
|
||||||
user_error('MultiForm::init(): Please define a correct value for $url_type on ' . $this->class, E_USER_ERROR);
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
user_error('MultiForm::init(): Please define a correct value for $url_type on ' . $this->class, E_USER_ERROR);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// @TODO fix the fact that you can continually refresh on the first step creating new records
|
// @TODO fix the fact that you can continually refresh on the first step creating new records
|
||||||
$this->session = new MultiFormSession();
|
$this->session = new MultiFormSession();
|
||||||
$this->session->write();
|
$this->session->write();
|
||||||
|
|
||||||
|
// We have to have an ID, before we can hash the ID of the session. @TODO a better way here?
|
||||||
if($urlType == 'Hash') $this->session->Hash = sha1($this->session->ID . '-' . microtime());
|
if($urlType == 'Hash') $this->session->Hash = sha1($this->session->ID . '-' . microtime());
|
||||||
$this->session->write(); // I guess we could hash something else than the ID, this is a bit ugly...
|
$this->session->write(); // I guess we could hash something else than the ID, this is a bit ugly...
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an instance of MultiFormSession from the database by a single
|
||||||
|
* record with the hash passed into this method.
|
||||||
|
*
|
||||||
|
* @param string $hash The Hash field of the record to retrieve
|
||||||
|
* @return MultiFormSession
|
||||||
|
*/
|
||||||
|
function getSessionRecordByHash($hash) {
|
||||||
|
$SQL_hash = Convert::raw2sql($hash);
|
||||||
|
return DataObject::get_one('MultiFormSession', "Hash = '$SQL_hash'");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an instance of MultiFormSession from the database by it's ID.
|
||||||
|
*
|
||||||
|
* @param int|string $id The ID of the record to retrieve
|
||||||
|
* @return MultiFormSession
|
||||||
|
*/
|
||||||
|
function getSessionRecordByID($id) {
|
||||||
|
return DataObject::get_by_id('MultiFormSession', $id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -347,11 +375,11 @@ abstract class MultiForm extends Form {
|
|||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function FormAction() {
|
function FormAction() {
|
||||||
$id = ($this->stat('url_type') == 'ID') ? $this->session->ID : $this->session->Hash;
|
$urlMethod = $this->stat('url_type');
|
||||||
$action = parent::FormAction();
|
$action = parent::FormAction();
|
||||||
$action .= (strpos($action, '?')) ? '&' : '?';
|
$action .= (strpos($action, '?')) ? '&' : '?';
|
||||||
$action .= "MultiFormSessionID={$id}";
|
$action .= "MultiFormSessionID={$this->session->$urlMethod}";
|
||||||
|
|
||||||
return $action;
|
return $action;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user