01/02/2017 10:24pm

PHP | Problem -


<?php


class RegistrationPage extends Page {

public static $db = array(
'RegistratinSuccessContent' => "HTMLText",
'SendMailAfterRegistration' => 'Boolean',
'RegistrationEmailSubject' => 'Varchar',
'RegistrationEmailTemplate' => "HTMLText"
);

public function canCreate($member = null){
return !RegistrationPage::get()->first();
}

public function canDelete($member = null){
return null;
}

public function getCMSFields(){
$fields = parent::getCMSFields();

$fields->addFieldsToTab("Root.Main", array(
HTMLEditorField::Create("RegistratinSuccessContent")->SetTitle("Registratin Success Page Content")->setRightTitle("Available Placeholders '#Title' '#FirstName' '#LastName' ")
));

$fields->addFieldsToTab("Root.EmailSettings", array(
new CheckboxField("SendMailAfterRegistration", "Send Mail After Registration?"),
new TextField("RegistrationEmailSubject", "Email Subject"),
HTMLEditorField::Create("RegistrationEmailTemplate")->SetTitle("Email Content")->setRightTitle("Available Placeholders '#Title' '#FirstName' '#LastName' ")
));

return $fields;
}
}

class RegistrationPage_Controller extends Page_Controller {

private static $allowed_actions = array (
'RegistrationForm',
'complete'
);

public function init() {
parent::init();
}

public function RegistrationForm() {

$country = DropdownField::create('Country');//->setValue($c);
$country->setAttribute('id', 'country');
$state = DropdownField::Create('State');//->setValue($s);;
$state->setAttribute('id', 'state');

$age = array();
for ($i=10; $i < 90; $i++) {
$age[$i] = $i;
$i++;
}

$fields = new FieldList(


TextField::Create('FirstName')->SetTitle('Name <span>*</span>'),

DropdownField::Create('Age', 'Age', $age)->SetTitle('Age <span>*</span>'),

OptionsetField::create('Gender', 'Gender', array(
'Male' => 'Male',
'Female' => 'Female'
),$value = "Male")->SetTitle('Gender <span>*</span>'),

TextField::Create('UserID')->SetTitle('UserID <span>*</span>'),

TextField::Create('UserName')->SetTitle('
UserName <span>*</span>'),

EmailField::Create('Email')->SetTitle('Email Address <span>*</span>'),


ConfirmedPasswordField::Create('Password')->SetTitle(''),
$country,
$state
);


$actions = new FieldList(
new FormAction('doRegister', 'Register')
);

$validator = new RequiredFields('FirstName', 'LastName', 'Email', 'Password', 'Country', 'State');

if(Member::currentUser()) {
$this->redirect('home');
}

return new Form($this, 'RegistrationForm', $fields, $actions, $validator);
}

public function doRegister($data,$form) {
die('test');
if($member = Member::get("Member")->filter("Email" , Convert::raw2sql($data['Email']))->first()) {
$form->AddErrorMessage('Email', "Sorry, that email address already exists. Please choose another.", 'bad');
Session::set("FormInfo.Form_RegistrationForm.data", $data);
return $this->redirectBack();;
}

if($member = Member::get("Member")->filter("UserID" , Convert::raw2sql($data['UserID']))->first()) {
$form->AddErrorMessage('UserID', "Sorry, that userID already exists. Please choose another.", 'bad');
Session::set("FormInfo.Form_RegistrationForm.data", $data);
return $this->redirectBack();;
}

$Member = new Member();
$ip = new IPBlocker();
$form->saveInto($Member);
$Member->write();
$ipaddr = $_SERVER['REMOTE_ADDR'];
$ip->UserName = $data['FirstName'];
$ip->IPAdress = $ipaddr;
$ip->write();

$Member->login();

if(!$userGroup = Group::get()->filter("Code" , "dyamembers")->first()) {
$userGroup = new Group();
$userGroup->Code = "dyamembers";
$userGroup->Title = "DYAMembers";
$userGroup->Write();
$userGroup->Members()->add($Member);
}

$userGroup->Members()->add($Member);

if($this->SendMailAfterRegistration) {
$this->SendMail($Member);
}

Session::set('RegisteredMemberID', $Member->ID);

$this->redirect($this->Link('complete'));

}

public function SendMail($Member) {
$strContent = $this->RegistratinSuccessContent;

$strContent = str_replace('#Title', $Member->NameTitle ,$strContent);
$strContent = str_replace('#FirstName', $Member->FirstName ,$strContent);
$strContent = str_replace('#LastName', $Member->Surname ,$strContent);

$e = new Email();
$e->To = $Member->Email;
$e->Subject = $this->RegistrationEmailSubject;
$e->Body = $strContent;
$e->send();
}

public function complete() {
$strContent = $this->RegistratinSuccessContent;
$iRegisteredMemberID = Session::get('RegisteredMemberID');

if($iRegisteredMemberID) {
$doMember = Member::get()->byID($iRegisteredMemberID);

if(is_object($doMember)) {
$strContent = str_replace('#Title', $doMember->NameTitle ,$strContent);
$strContent = str_replace('#FirstName',$doMember->FirstName ,$strContent);
$strContent = str_replace('#LastName', $doMember->Surname ,$strContent);

Session::clear('RegisteredMemberID');

return array("Title" => 'Registration Complete', "Content" => $strContent);
}

} else {
$this->redirect($this->Link());
}
}
}