request->getVars();
if (isset($getVars['BackURL'])) {
$URL = Convert::raw2sql($getVars['BackURL']);
Session::set('BackURL', $URL);
}
}
//Generate the registration form
function Form()
{
// Name Set
$FirstNameField = new TextField('FirstName', "First Name");
$LastNameField = new TextField('Surname', "Last Name");
// Email Addresses
$PrimaryEmailField = new TextField('Email', "Primary Email Address");
$fields = new FieldList(
$FirstNameField,
$LastNameField,
new LiteralField('break', '
'),
$PrimaryEmailField,
new LiteralField('instructions', 'This will also be your login name.
'),
new LiteralField('break', '
'),
new ConfirmedPasswordField('Password', 'Password')
);
$actions = new FieldList(
new FormAction('doRegister', 'Start Voting')
);
$validator = new Member_Validator(
'FirstName',
'Surname',
'Email',
'Password'
);
return new SafeXSSForm($this, 'Form', $fields, $actions, $validator);
}
//Submit the registration form
function doRegister($data, $form)
{
//Check for existing member email address
if ($member = Member::get()->filter('Email', Convert::raw2sql($data['Email']))->first()) {
//Set error message
$form->AddErrorMessage('Email', "Sorry, that email address already exists. Please choose another.", 'bad');
//Set form data from submitted values
Session::set("FormInfo.SafeXSSForm_Form.data", $data);
//Return back to form
return $this->redirectBack();;
}
//Otherwise create new member and log them in
$Member = new Member();
$form->saveInto($Member);
$Member->write();
//Find or create the 'user' group
if (!$userGroup = Group::get()->filter('Code', 'presentation-voters')->first()) {
$userGroup = new Group();
$userGroup->Code = "presentation-voters";
$userGroup->Title = "Presentation Voters";
$userGroup->Write();
$Member->Groups()->add($userGroup);
}
//Add member to user group
$Member->Groups()->add($userGroup);
$Member->login();
//Get current voting page and redirect there
if ($VotingPage = PresentationVotingPage::get()->first()) {
$BackURL = Session::get('BackURL');
if ($BackURL) {
return $this->redirect($VotingPage->Link() . 'Presentation/' . $BackURL);
} else {
return $this->redirect($VotingPage);
}
}
}
}