-
-
Save Liongold/ca027bca6673bf95e01e to your computer and use it in GitHub Desktop.
A code snippet showing the Registration and Edit forms implementation in the Contributor Maps module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function RegistrationForm() { | |
if($this->request->getVar('key') && $this->request->getVar('token')) { | |
$key = $this->request->getVar('key'); | |
$token = $this->request->getVar('token'); | |
}else if($this->request->isPOST()) { | |
$key = $this->request->requestVar('key'); | |
$token = $this->request->requestVar('token'); | |
} | |
if($key && $token) { | |
$memory = ContributorMaps_Data::get()->filter(array( | |
'Unique_Key' => $key, | |
'EditToken' => $token, | |
'EditTokenExpires:GreaterThan' => date('Y-m-d') | |
))->First(); | |
if(!$memory) { | |
return $this->redirect($this->Link("?action=edit&status=2")); | |
} | |
} | |
$fields = new FieldList( | |
new TextField('Name', 'Name*'), | |
new TextField('Surname', 'Surname*'), | |
new EmailField('Email', 'Email*'), | |
new TextField('Location', 'Location* (e.g. Berlin, Deutschland)'), | |
new HeaderField('Skills'), | |
new CheckboxField('Skills_Design', 'Design'), | |
new CheckboxField('Skills_Dev', 'Development'), | |
new CheckboxField('Skills_Doc', 'Documentation'), | |
new CheckboxField('Skills_Infra', 'Infrastructure'), | |
new CheckboxField('Skills_l10n', 'Localisation'), | |
new CheckboxField('Skills_Marketing', 'Marketing'), | |
new CheckboxField('Skills_QA', 'Quality Assurance'), | |
new CheckboxField('Skills_Base', 'Support - Base'), | |
new CheckboxField('Skills_Calc', 'Support - Calc'), | |
new CheckboxField('Skills_Draw', 'Support - Draw'), | |
new CheckboxField('Skills_Impress', 'Support - Impress'), | |
new CheckboxField('Skills_Math', 'Support - Math'), | |
new CheckboxField('Skills_Writer', 'Support - Writer') | |
); | |
if($memory) { | |
$fields->push(new HiddenField('Key','',$key)); | |
$fields->push(new HiddenField('Token','',$token)); | |
$actions = new FieldList( | |
new FormAction('processEditForm', 'Update') | |
); | |
}else{ | |
die("test"); | |
RecaptchaField::$js_options = array( | |
'theme' => 'white' | |
); | |
$fields->push(new RecaptchaField('Captcha')); | |
$actions = new FieldList( | |
new FormAction('processForm', 'Register') | |
); | |
} | |
$validator = new RequiredFields('Name', 'Surname', 'Email', 'Location'); | |
$form = new Form($this, 'RegistrationForm', $fields, $actions, $validator); | |
//Load previously submitted data | |
if($memory) { | |
$form->loadDataFrom($memory); | |
}else{ | |
$form->loadDataFrom(Session::get("FormInfo.{$form->FormName()}.data")); | |
} | |
return $form; | |
} | |
public function processForm($data, $form) { | |
$errors = 0; | |
//Check if Email is used | |
if(ContributorMaps_Data::get()->filter('Email', $data['Email'])->exists()) { | |
$form->addErrorMessage('Email', 'Sorry, this email address is already being used. ', 'bad'); | |
$errors++; | |
}; | |
if(!($data['Skills_Base'] || $data['Skills_Calc'] || $data['Skills_Dev'] | |
|| $data['Skills_Doc'] || $data['Skills_Draw'] || $data['Skills_Impress'] || $data['Skills_Infra'] | |
|| $data['Skills_l10n'] || $data['Skills_Marketing'] || $data['Skills_Math'] || $data['Skills_QA'] | |
|| $data['Skills_Writer'])) { | |
$form->addErrorMessage('Skills', 'Sorry, you must choose at least 1 skill. ', 'bad'); | |
$errors++; | |
}; | |
if($errors > 0) { | |
Session::set("FormInfo.{$form->FormName()}.data", $data); | |
return $this->redirect($this->Link("?registered=2")); | |
}; | |
$submission = new ContributorMaps_Data(); | |
$form->saveInto($submission); | |
$key = sha1(microtime(true).mt_rand(10000,90000)); | |
$submission->Unique_Key = $key; | |
$submission->Confirmed = 0; | |
$submission->Expiry = strtotime('+1 year'); | |
$submission->EditToken = 0; | |
$submission->EditTokenRequest = date('Y-m-d'); | |
$submission->write(); | |
//Email User Confirmation Link | |
$subject = "Please confirm your submission to the LibreOffice Contributor Maps"; | |
$body = "Thanks for your submission to the LibreOffice Contributor Maps! " | |
."However, in order to ensure that you made this submission, you" | |
." are required to confirm this email address by going to http://" | |
."www.libreoffice.org/new-contributor-maps/AccountConfirmation?key=$key" | |
.". Your listing will be published to the site for a period of 1 year. After" | |
." this period we will contact you again so that you can review and extend " | |
."your information. Please note that your email address will be shown in public" | |
." on The LibreOffice Contributor Maps site. "; | |
$email = new Email("hostmaster@documentfoundation.org", $data['Email'], $subject, $body); | |
$email->send(); | |
return $this->redirect($this->Link("?registered=1")); | |
} | |
public function processEditForm($data, $form) { | |
$entry = ContributorMaps_Data::get()->filter(array( | |
'Unique_Key' => $data['Key'], | |
'EditToken' => $data['Token'] | |
))->First(); | |
if($entry) { | |
$errors = 0; | |
if($entry->Email !== $data['Email']) { | |
//Email Confirmation Email Again | |
//Check if Email is used | |
if(ContributorMaps_Data::get()->filter('Email', $data['Email'])->exists()) { | |
$form->addErrorMessage('Email', 'Sorry, this email address is already being used. ', 'bad'); | |
$errors++; | |
}else{ | |
$entry->Email = $data['Email']; | |
//Email User Confirmation Link | |
$subject = "Please confirm your submission to the LibreOffice Contributor Maps"; | |
$body = "Thanks for your submission to the LibreOffice Contributor Maps! " | |
."However, in order to ensure that you made this submission, you" | |
." are required to confirm this email address by going to http://" | |
."www.libreoffice.org/new-contributor-maps/AccountConfirmation?key=$key" | |
.". Your listing will be published to the site for a period of 1 year. After" | |
." this period we will contact you again so that you can review and extend " | |
."your information. Please note that your email address will be shown in public" | |
." on The LibreOffice Contributor Maps site. "; | |
$email = new Email("hostmaster@documentfoundation.org", $data['Email'], $subject, $body); | |
$email->send(); | |
$entry->Confirmed = 0; | |
} | |
} | |
if(!($data['Skills_Base'] || $data['Skills_Calc'] || $data['Skills_Dev'] | |
|| $data['Skills_Doc'] || $data['Skills_Draw'] || $data['Skills_Impress'] || $data['Skills_Infra'] | |
|| $data['Skills_l10n'] || $data['Skills_Marketing'] || $data['Skills_Math'] || $data['Skills_QA'] | |
|| $data['Skills_Writer'])) { | |
$form->addErrorMessage('Skills', 'Sorry, you must choose at least 1 skill. ', 'bad'); | |
$errors++; | |
}; | |
$entry->Name = $data['Name']; | |
$entry->Surname = $data['Surname']; | |
$entry->Location = $data['Location']; | |
$entry->Skills_Design = $data['Skills_Design']; | |
$entry->Skills_Dev = $data['Skills_Dev']; | |
$entry->Skills_Doc = $data['Skills_Doc']; | |
$entry->Skills_Infra = $data['Skills_Infra']; | |
$entry->Skills_l10n = $data['Skills_l10n']; | |
$entry->Skills_Marketing = $data['Skills_Marketing']; | |
$entry->Skills_QA = $data['Skills_QA']; | |
$entry->Skills_Base = $data['Skills_Base']; | |
$entry->Skills_Calc = $data['Skills_Calc']; | |
$entry->Skills_Draw = $data['Skills_Draw']; | |
$entry->Skills_Impress = $data['Skills_Impress']; | |
$entry->Skills_Math = $data['Skills_Math']; | |
$entry->Skills_Writer = $data['Skills_Writer']; | |
if($errors > 0) { | |
Session::set("FormInfo.{$form->FormName()}.data", $data); | |
return $this->redirect($this->Link("?registered=3")); | |
}else{ | |
$entry->write(); | |
return $this->redirect($this->Link("?action=edit&status=4")); | |
} | |
}else{ | |
return $this->redirect($this->Link("?action=edit&status=3")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment