patrickt

UserPage-based Registration Form

Nov 19th, 2013
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.67 KB | None | 0 0
  1. <?php
  2.  
  3. use code\Model as Model;
  4. use code\Action as Action;
  5.  
  6. class UserPage extends Page
  7. {
  8.     static public $description = 'Sign In to Your Account';
  9. }
  10.  
  11. class UserPage_Controller extends Page_Controller
  12. {
  13.     private $showUserSigninForm = false;
  14.     private $showInactivityMessage = false;
  15.     private $showLoginMessage = false;
  16.     private $showLockoutMessage = false;
  17.    
  18.     static $allowed_actions = array(
  19.         'index',
  20.         'edit',
  21.         'login',
  22.         'logout',
  23.         'test',
  24.         'register',
  25.         'RegistrationForm',
  26.         'doRegister',
  27.         'profile'
  28.     );
  29.  
  30.     public function __construct($dataRecord = null)
  31.     {
  32.         parent::__construct($dataRecord);
  33.        
  34.         if (!empty($_GET['inactivity'])) {
  35.             $this->showInactivityMessage = true;
  36.         }
  37.        
  38.         if (!empty($_GET['login'])) {
  39.             $this->showLoginMessage = true;
  40.         }
  41.     }
  42.  
  43.     public function login()
  44.     {
  45.         $this->redirect('Security/login');
  46.     }
  47.  
  48.     public function logout()
  49.     {
  50.         $this->redirect('Security/logout');
  51.     }
  52.  
  53.     public function ShowUserSigninForm()
  54.     {
  55.         return $this->showUserSigninForm;
  56.     }
  57.    
  58.     public function ShowInactivityMessage()
  59.     {
  60.         return $this->showInactivityMessage;
  61.     }
  62.    
  63.     public function ShowLoginMessage()
  64.     {
  65.         return $this->showLoginMessage;
  66.     }
  67.    
  68.     public function ShowLockoutMessage()
  69.     {
  70.         return $this->showLockoutMessage;
  71.     }
  72.    
  73.     public function index(SS_HTTPRequest $request)
  74.     {
  75.         return $this->renderWith(array('UserPage', 'Page'));
  76.     }
  77.    
  78.     /**
  79.      * Determines if we show the sitemap and security badges in the footer
  80.      *
  81.      * @return bool
  82.      */
  83.     public function ShowSiteMap()
  84.     {
  85.         return TRUE;
  86.     }
  87.  
  88.     /**
  89.      * Determines if we show the legal disclaimers in the footer
  90.      *
  91.      * @return bool
  92.      */
  93.     public function ShowLegalDisclaimers()
  94.     {
  95.         return TRUE;
  96.     }
  97.  
  98.     /**
  99.      * Determines if we show the footer at all
  100.      *
  101.      * @return bool
  102.      */
  103.     public function ShowFooter()
  104.     {
  105.         return (bool) ($this->ShowSiteMap() || $this->ShowLegalDisclaimers());
  106.     }
  107.  
  108.     public function register(SS_HTTPRequest $request)
  109.     {
  110.         return $this->RegistrationForm();
  111.     }
  112.  
  113.     //Generate the registration form
  114.     function RegistrationForm()
  115.     {
  116.         $contactMethods = MemberHelper::$bestContactMethods;
  117.         $alertOptions = MemberHelper::$alertOptions;
  118.  
  119.         try {
  120.             $fields = new FieldList(
  121.                 new TextField('FirstName', 'First Name'),
  122.                 new TextField('Surname', 'Last Name'),
  123.                 new TextField('Email', 'Email'),
  124.                 new TextField('Address', 'Address'),
  125.                 new TextField('City', 'City'),
  126.                 new TextField('State', 'State'),
  127.                 new TextField('Zip', 'Zip'),
  128.                 new PhoneNumberField('PhoneHome', 'Home Phone'),
  129.                 new PhoneNumberField('PhoneCell', 'Cell Phone'),
  130.                 new PhoneNumberField('PhoneWork', 'Work Phone'),
  131.                 new PhoneNumberField('PhoneEmployer', 'Employer Phone'),
  132.                 new TextField('Employer', 'Employer'),
  133.                 new DropdownField('BestContactMethod', 'Best Contact Method', $contactMethods),
  134.                 new OptionsetField('AlertPreferences', 'Alert Preferences', $alertOptions)
  135.             );
  136.         }
  137.         catch (Exception $e) {
  138.             throw new Exception("Unable to generate the field list.");
  139.         }
  140.  
  141.         // Create action
  142.         $actions = new FieldList(
  143.             new FormAction('doRegister', 'Register')
  144.         );
  145.  
  146.         // Create action
  147.         $validator = new RequiredFields('FirstName', 'Surname', 'Email', 'Address', 'City', 'State', 'Zip', 'PhoneHome', 'BestContactMethod');
  148.  
  149.         //Now build said form
  150.         try {
  151.             $form = new Form($this, 'RegistrationForm', $fields, $actions, $validator);
  152.         }
  153.         catch (Exception $e) {
  154.             throw new Exception("Unable to generate the form captain!");
  155.         }
  156.  
  157.         // Load the form with previously sent data
  158.         $form->loadDataFrom($this->request->postVars());
  159.  
  160.         //Return what you built
  161.         return $form;
  162.     }
  163.  
  164.     //Submit the registration form
  165.     function doRegister($data, Form $form)
  166.     {
  167.         //Check for existing member email address
  168.         if ($member = DataObject::get_one("Member", "`Email` = '". Convert::raw2sql($data['Email']) . "'"))
  169.         {
  170.             //Set error message
  171.             $form->AddErrorMessage('Email', "Sorry, that email address already exists. Please choose another.", 'bad');
  172.  
  173.             //Set form data from submitted values
  174.             Session::set("FormInfo.Form_RegistrationForm.data", $data);
  175.  
  176.             //Return back to form
  177.             return $this->redirectBack();
  178.         }
  179.  
  180.         //Otherwise create new member and log them in
  181.         $Member = new Member();
  182.         $form->saveInto($Member);
  183.         $Member->write();
  184.         $Member->login();
  185.         $g = Group::$allowed_actions;
  186.  
  187.         $userGroup = DataObject::get_one('Group', "Code = 'customers'");
  188.  
  189.         //Find or create the 'user' group
  190.         if (!$userGroup)
  191.         {
  192.             $userGroup = new Group();
  193.             $userGroup->Code = "customers";
  194.             $userGroup->Title = "Customers";
  195.             $userGroup->Write();
  196.             $userGroup->Members()->add($Member);
  197.         }
  198.  
  199.         //Add member to user group
  200.         $userGroup->Members()->add($Member);
  201.  
  202.         return $this->redirect('/user/profile');
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment