Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use code\Model as Model;
- use code\Action as Action;
- class UserPage extends Page
- {
- static public $description = 'Sign In to Your Account';
- }
- class UserPage_Controller extends Page_Controller
- {
- private $showUserSigninForm = false;
- private $showInactivityMessage = false;
- private $showLoginMessage = false;
- private $showLockoutMessage = false;
- static $allowed_actions = array(
- 'index',
- 'edit',
- 'login',
- 'logout',
- 'test',
- 'register',
- 'RegistrationForm',
- 'doRegister',
- 'profile'
- );
- public function __construct($dataRecord = null)
- {
- parent::__construct($dataRecord);
- if (!empty($_GET['inactivity'])) {
- $this->showInactivityMessage = true;
- }
- if (!empty($_GET['login'])) {
- $this->showLoginMessage = true;
- }
- }
- public function login()
- {
- $this->redirect('Security/login');
- }
- public function logout()
- {
- $this->redirect('Security/logout');
- }
- public function ShowUserSigninForm()
- {
- return $this->showUserSigninForm;
- }
- public function ShowInactivityMessage()
- {
- return $this->showInactivityMessage;
- }
- public function ShowLoginMessage()
- {
- return $this->showLoginMessage;
- }
- public function ShowLockoutMessage()
- {
- return $this->showLockoutMessage;
- }
- public function index(SS_HTTPRequest $request)
- {
- return $this->renderWith(array('UserPage', 'Page'));
- }
- /**
- * Determines if we show the sitemap and security badges in the footer
- *
- * @return bool
- */
- public function ShowSiteMap()
- {
- return TRUE;
- }
- /**
- * Determines if we show the legal disclaimers in the footer
- *
- * @return bool
- */
- public function ShowLegalDisclaimers()
- {
- return TRUE;
- }
- /**
- * Determines if we show the footer at all
- *
- * @return bool
- */
- public function ShowFooter()
- {
- return (bool) ($this->ShowSiteMap() || $this->ShowLegalDisclaimers());
- }
- 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();
- $form->saveInto($Member);
- $Member->write();
- $Member->login();
- $g = Group::$allowed_actions;
- $userGroup = DataObject::get_one('Group', "Code = 'customers'");
- //Find or create the 'user' group
- if (!$userGroup)
- {
- $userGroup = new Group();
- $userGroup->Code = "customers";
- $userGroup->Title = "Customers";
- $userGroup->Write();
- $userGroup->Members()->add($Member);
- }
- //Add member to user group
- $userGroup->Members()->add($Member);
- return $this->redirect('/user/profile');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment