Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use code\Model as Model;
- use code\Action as Action;
- /**
- * Created by PhpStorm.
- * User: pthurmond
- * Date: 11/5/13
- * Time: 1:35 PM
- */
- class RegistrationPage extends Page {
- public static $description = 'Register your account.';
- }
- class RegistrationPage_Controller extends Page_Controller {
- //Allow our form as an action
- public static $allowed_actions = array(
- 'RegistrationForm',
- 'register',
- 'doRegister'
- );
- /*public static $url_handlers = array(
- 'register/$Action/$ID/$Name' => 'RegistrationForm'
- );*/
- public function register(SS_HTTPRequest $request)
- {
- return $this->RegistrationForm();
- }
- //Generate the registration form
- function RegistrationForm()
- {
- $contactMethods = MemberHelper::$bestContactMethods;
- $alertOptions = MemberHelper::$alertOptions;
- try {
- $fields = new FieldList(
- new TextField('FirstName', 'First Name'),
- new TextField('Surname', 'Last Name'),
- new TextField('Email', 'Email'),
- new TextField('Address', 'Address'),
- new TextField('City', 'City'),
- new TextField('State', 'State'),
- new TextField('Zip', 'Zip'),
- new PhoneNumberField('PhoneHome', 'Home Phone'),
- new PhoneNumberField('PhoneCell', 'Cell Phone'),
- new PhoneNumberField('PhoneWork', 'Work Phone'),
- new PhoneNumberField('PhoneEmployer', 'Employer Phone'),
- new TextField('Employer', 'Employer'),
- new DropdownField('BestContactMethod', 'Best Contact Method', $contactMethods),
- new OptionsetField('AlertPreferences', 'Alert Preferences', $alertOptions)
- );
- }
- catch (Exception $e) {
- throw new Exception("Unable to generate the field list.");
- }
- // Create action
- $actions = new FieldList(
- new FormAction('doRegister', 'Register')
- );
- // Create action
- $validator = new RequiredFields('FirstName', 'Surname', 'Email', 'Address', 'City', 'State', 'Zip', 'PhoneHome', 'BestContactMethod');
- //Now build said form
- try {
- $form = new Form($this, 'RegistrationForm', $fields, $actions, $validator);
- }
- catch (Exception $e) {
- throw new Exception("Unable to generate the form captain!");
- }
- // Load the form with previously sent data
- $form->loadDataFrom($this->request->postVars());
- //Return what you built
- return $form;
- }
- //Submit the registration form
- function doRegister($data, Form $form)
- {
- //Check for existing member email address
- if ($member = DataObject::get_one("Member", "`Email` = '". Convert::raw2sql($data['Email']) . "'"))
- {
- //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.Form_RegistrationForm.data", $data);
- //Return back to form
- return $this->redirectBack();
- }
- //Otherwise create new member and log them in
- $Member = new Member();
- echo "Created member object<br>\n";
- $form->saveInto($Member);
- echo "Form has been saved!<br>\n";
- $Member->write();
- echo "Wrote to member object<br>\n";
- $Member->login();
- echo "Logged in member<br>\n";
- $g = Group::$allowed_actions;
- $userGroup = DataObject::get_one('Group', "Code = 'customers'");
- echo "userGroup object built<br>\n<pre>" . print_r($userGroup, true) . "</pre><br>\n";
- //Find or create the 'user' group
- if (!$userGroup)
- {
- echo "Apparently the user group is not.\n<br>\n";
- $userGroup = new Group();
- $userGroup->Code = "customers";
- $userGroup->Title = "Customers";
- $userGroup->Write();
- echo "User group write.<br>\n";
- $userGroup->Members()->add($Member);
- echo "Member add<br>\n";
- }
- else {
- echo "The user group is fine.<br>\n";
- }
- //Add member to user group
- $userGroup->Members()->add($Member);
- echo "Added the user to the group.<br>\n";
- $ProfilePage = DataObject::get_one('ProfilePage');
- echo "ProfilePage object built<br>\n<pre>" . print_r($ProfilePage, true) . "</pre><br>\n";
- //Get profile page
- if ($ProfilePage)
- {
- echo "About to redirect<br>\n";
- die();
- return $this->redirect($ProfilePage->Link('?success=1'));
- }
- else {
- echo "No redirect, we are doomed.<br>\n";
- }
- $this->redirect('/');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement