Encapsulated getting session records, added notes

This commit is contained in:
Sean Harvey 2008-04-20 09:28:02 +00:00
parent d5e613a9fb
commit 18fe36cdd9

View File

@ -152,23 +152,51 @@ abstract class MultiForm extends Form {
// 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']);
break;
default:
user_error('MultiForm::init(): Please define a correct value for $url_type on ' . $this->class, E_USER_ERROR); 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);
}
/** /**
* Set the fields for this form. * Set the fields for this form.
* *
@ -348,10 +376,10 @@ 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;
} }