Advertisement
patrickt

RegistrationPage registration form

Nov 19th, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.98 KB | None | 0 0
  1. <?php
  2.  
  3. use code\Model as Model;
  4. use code\Action as Action;
  5.  
  6. /**
  7.  * Created by PhpStorm.
  8.  * User: pthurmond
  9.  * Date: 11/5/13
  10.  * Time: 1:35 PM
  11.  */
  12.  
  13. class RegistrationPage extends Page {
  14.     public static $description = 'Register your account.';
  15. }
  16.  
  17. class RegistrationPage_Controller extends Page_Controller {
  18.     //Allow our form as an action
  19.     public static $allowed_actions = array(
  20.         'RegistrationForm',
  21.         'register',
  22.         'doRegister'
  23.     );
  24.  
  25.     /*public static $url_handlers = array(
  26.         'register/$Action/$ID/$Name' => 'RegistrationForm'
  27.     );*/
  28.     public function register(SS_HTTPRequest $request)
  29.     {
  30.         return $this->RegistrationForm();
  31.     }
  32.  
  33.     //Generate the registration form
  34.     function RegistrationForm()
  35.     {
  36.         $contactMethods = MemberHelper::$bestContactMethods;
  37.         $alertOptions = MemberHelper::$alertOptions;
  38.  
  39.         try {
  40.             $fields = new FieldList(
  41.                 new TextField('FirstName', 'First Name'),
  42.                 new TextField('Surname', 'Last Name'),
  43.                 new TextField('Email', 'Email'),
  44.                 new TextField('Address', 'Address'),
  45.                 new TextField('City', 'City'),
  46.                 new TextField('State', 'State'),
  47.                 new TextField('Zip', 'Zip'),
  48.                 new PhoneNumberField('PhoneHome', 'Home Phone'),
  49.                 new PhoneNumberField('PhoneCell', 'Cell Phone'),
  50.                 new PhoneNumberField('PhoneWork', 'Work Phone'),
  51.                 new PhoneNumberField('PhoneEmployer', 'Employer Phone'),
  52.                 new TextField('Employer', 'Employer'),
  53.                 new DropdownField('BestContactMethod', 'Best Contact Method', $contactMethods),
  54.                 new OptionsetField('AlertPreferences', 'Alert Preferences', $alertOptions)
  55.             );
  56.         }
  57.         catch (Exception $e) {
  58.             throw new Exception("Unable to generate the field list.");
  59.         }
  60.  
  61.         // Create action
  62.         $actions = new FieldList(
  63.             new FormAction('doRegister', 'Register')
  64.         );
  65.  
  66.         // Create action
  67.         $validator = new RequiredFields('FirstName', 'Surname', 'Email', 'Address', 'City', 'State', 'Zip', 'PhoneHome', 'BestContactMethod');
  68.  
  69.         //Now build said form
  70.         try {
  71.             $form = new Form($this, 'RegistrationForm', $fields, $actions, $validator);
  72.         }
  73.         catch (Exception $e) {
  74.             throw new Exception("Unable to generate the form captain!");
  75.         }
  76.  
  77.         // Load the form with previously sent data
  78.         $form->loadDataFrom($this->request->postVars());
  79.  
  80.         //Return what you built
  81.         return $form;
  82.     }
  83.  
  84.     //Submit the registration form
  85.     function doRegister($data, Form $form)
  86.     {
  87.         //Check for existing member email address
  88.         if ($member = DataObject::get_one("Member", "`Email` = '". Convert::raw2sql($data['Email']) . "'"))
  89.         {
  90.             //Set error message
  91.             $form->AddErrorMessage('Email', "Sorry, that email address already exists. Please choose another.", 'bad');
  92.  
  93.             //Set form data from submitted values
  94.             Session::set("FormInfo.Form_RegistrationForm.data", $data);
  95.  
  96.             //Return back to form
  97.             return $this->redirectBack();
  98.         }
  99.  
  100.         //Otherwise create new member and log them in
  101.         $Member = new Member();
  102.         echo "Created member object<br>\n";
  103.  
  104.         $form->saveInto($Member);
  105.  
  106.         echo "Form has been saved!<br>\n";
  107.         $Member->write();
  108.         echo "Wrote to member object<br>\n";
  109.  
  110.         $Member->login();
  111.         echo "Logged in member<br>\n";
  112.         $g = Group::$allowed_actions;
  113.  
  114.         $userGroup = DataObject::get_one('Group', "Code = 'customers'");
  115.         echo "userGroup object built<br>\n<pre>" . print_r($userGroup, true) . "</pre><br>\n";
  116.  
  117.  
  118.         //Find or create the 'user' group
  119.         if (!$userGroup)
  120.         {
  121.             echo "Apparently the user group is not.\n<br>\n";
  122.             $userGroup = new Group();
  123.             $userGroup->Code = "customers";
  124.             $userGroup->Title = "Customers";
  125.             $userGroup->Write();
  126.             echo "User group write.<br>\n";
  127.             $userGroup->Members()->add($Member);
  128.             echo "Member add<br>\n";
  129.         }
  130.         else {
  131.             echo "The user group is fine.<br>\n";
  132.         }
  133.  
  134.         //Add member to user group
  135.         $userGroup->Members()->add($Member);
  136.         echo "Added the user to the group.<br>\n";
  137.  
  138.         $ProfilePage = DataObject::get_one('ProfilePage');
  139.         echo "ProfilePage object built<br>\n<pre>" . print_r($ProfilePage, true) . "</pre><br>\n";
  140.  
  141.  
  142.         //Get profile page
  143.         if ($ProfilePage)
  144.         {
  145.             echo "About to redirect<br>\n";
  146.             die();
  147.             return $this->redirect($ProfilePage->Link('?success=1'));
  148.         }
  149.         else {
  150.             echo "No redirect, we are doomed.<br>\n";
  151.         }
  152.  
  153.         $this->redirect('/');
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement