| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Page containing an edit details form |
|---|
| 5 | * Uses Member::getMemberFormFields() to know what to make available for editing |
|---|
| 6 | */ |
|---|
| 7 | class RegisterAndEditDetailsPage extends Page { |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | static $icon = "userpage/images/treeicons/RegisterAndEditDetailsPage"; |
|---|
| 11 | |
|---|
| 12 | static $can_be_root = false; |
|---|
| 13 | |
|---|
| 14 | static $db = array( |
|---|
| 15 | "ThankYouTitle" => "Varchar(255)", |
|---|
| 16 | "ThankYouContent" => "HTMLText", |
|---|
| 17 | "WelcomeTitle" => "Varchar(255)", |
|---|
| 18 | "WelcomeContent" => "HTMLText", |
|---|
| 19 | "TitleLoggedIn" => "Varchar(255)", |
|---|
| 20 | "MenuTitleLoggedIn" => "Varchar(255)", |
|---|
| 21 | "MetaTitleLoggedIn" => "Varchar(255)", |
|---|
| 22 | "ContentLoggedIn" => "HTMLText" |
|---|
| 23 | ); |
|---|
| 24 | |
|---|
| 25 | static $register_group_title = "Registered users"; |
|---|
| 26 | |
|---|
| 27 | static $register_group_code = "registrations"; |
|---|
| 28 | |
|---|
| 29 | static $register_group_access_key = "REGISTRATIONS"; |
|---|
| 30 | |
|---|
| 31 | public function getTitle() {if($this->showLoggedInFields()) {$field = "TitleLoggedIn";}else {$field = "Title";} return $this->getField($field);} |
|---|
| 32 | public function getMenuTitle() {if($this->isCMS()) {return "Register"; } elseif($this->showLoggedInFields()) {$field = "MenuTitleLoggedIn";}else {$field = "MenuTitle";} return $this->getField($field);} |
|---|
| 33 | public function getMetaTitle() {if($this->isCMS()) {return "Register"; } elseif($this->showLoggedInFields()) {$field = "MetaTitleLoggedIn";}else {$field = "MetaTitle";} return $this->getField($field);} |
|---|
| 34 | public function getContent() {if($this->showLoggedInFields()) {$field = "ContentLoggedIn";}else {$field = "Content";}return $this->getField($field);} |
|---|
| 35 | |
|---|
| 36 | private function showLoggedInFields() { |
|---|
| 37 | if(!$this->isCMS() && Member::currentUser() ) { |
|---|
| 38 | return true; |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | private function isCMS () { |
|---|
| 43 | $actions = Director::urlParams(); |
|---|
| 44 | if(Director::is_ajax() || "CMSMain" == $actions["Controller"]) { |
|---|
| 45 | return true; |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | public function getCMSFields($cms) { |
|---|
| 52 | $fields = parent::getCMSFields($cms); |
|---|
| 53 | $fields->addFieldToTab('Root.Content.LoggedIn', new TextField('TitleLoggedIn', 'Title when user is Logged In')); |
|---|
| 54 | $fields->addFieldToTab('Root.Content.LoggedIn', new TextField('MenuTitleLoggedIn', 'Navigation Label when user is Logged In')); |
|---|
| 55 | $fields->addFieldToTab('Root.Content.LoggedIn', new HTMLEditorField('ContentLoggedIn', 'Content when user is Logged In')); |
|---|
| 56 | $fields->addFieldToTab('Root.Content.Welcome', new TextField('WelcomeTitle', 'Welcome Title (afer user creates an account)')); |
|---|
| 57 | $fields->addFieldToTab('Root.Content.Welcome', new HTMLEditorField('WelcomeContent', 'Welcome message (afer user creates an account)')); |
|---|
| 58 | |
|---|
| 59 | $fields->addFieldToTab('Root.Content.ThankYou', new TextField('ThankYouTitle', 'Thank you Title (afer user updates their details)')); |
|---|
| 60 | $fields->addFieldToTab('Root.Content.ThankYou', new HTMLEditorField('ThankYouContent', 'Thank you message (afer user updates their details)')); |
|---|
| 61 | return $fields; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | public function canCreate() { |
|---|
| 65 | return !DataObject::get_one("RegisterAndEditDetailsPage"); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public function canDelete() { |
|---|
| 69 | false; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public function requireDefaultRecords() { |
|---|
| 73 | parent::requireDefaultRecords(); |
|---|
| 74 | if(!$group = DataObject::get_one("Group", 'Code = "'.self::$register_group_code.'"')) { |
|---|
| 75 | $group = new Group(); |
|---|
| 76 | $group->Code = self::$register_group_code; |
|---|
| 77 | $group->Title = self::$register_group_title; |
|---|
| 78 | $group->write(); |
|---|
| 79 | Permission::grant( $group->ID, self::$register_group_access_key); |
|---|
| 80 | Database::alteration_message("GROUP: ".self::$register_group_code.' ('.self::$register_group_title.')' ,"created"); |
|---|
| 81 | } |
|---|
| 82 | elseif(DB::query('SELECT * FROM Permission WHERE `GroupID` = '.$group->ID.' AND `Code` = "'.self::$register_group_access_key.'"')->numRecords() == 0) { |
|---|
| 83 | Permission::grant($group->ID, self::$register_group_access_key); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | class RegisterAndEditDetailsPage_Controller extends Page_Controller { |
|---|
| 89 | |
|---|
| 90 | function index() { |
|---|
| 91 | if(isset($_GET["ajax"])) { |
|---|
| 92 | return $this->renderWith(array("Thickbox", "RegisterPage")); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | return array(); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | function Form() { |
|---|
| 99 | if(isset($_REQUEST["BackURL"])){ |
|---|
| 100 | Session::set('BackURL', $_REQUEST["BackURL"]); |
|---|
| 101 | } |
|---|
| 102 | $member = Member::currentUser(); |
|---|
| 103 | $fields = new FieldSet(); |
|---|
| 104 | $passwordField = new ConfirmedPasswordField("Password", "Password"); |
|---|
| 105 | if($member) { |
|---|
| 106 | $logoutField = new LiteralField('LogoutNote', '<p class="message good">You are currently logged in as '. $member->FirstName . ' ' . $member->Surname . '. Click <a href="Security/logout" title="Click here to log out">here</a> to log out.</p>'); |
|---|
| 107 | if($member && $member->Password != '') { |
|---|
| 108 | $passwordField->setCanBeEmpty(true); |
|---|
| 109 | } |
|---|
| 110 | $actions = new FieldSet(new FormAction("submit", "Update your details")); |
|---|
| 111 | } |
|---|
| 112 | else { |
|---|
| 113 | $logoutField = new LiteralField('LogoutNote', '<p class="message good">You are currently not logged in. Click <a href="Security/login" title="Click here to log out" class="thickbox">here</a> to log-in.</p>'); |
|---|
| 114 | $actions = new FieldSet(new FormAction("submit", "Register")); |
|---|
| 115 | } |
|---|
| 116 | $fields->push($logoutField); |
|---|
| 117 | |
|---|
| 118 | if($memberFormFields = Member::getMemberFormFields()) { |
|---|
| 119 | $fields->merge($memberFormFields); |
|---|
| 120 | } |
|---|
| 121 | $fields->push(new HeaderField("Login Details", 3)); |
|---|
| 122 | $fields->push($passwordField); |
|---|
| 123 | |
|---|
| 124 | $requiredFieldList = array( |
|---|
| 125 | "FirstName", |
|---|
| 126 | "Email", |
|---|
| 127 | ); |
|---|
| 128 | foreach($requiredFieldList as $fieldName) { |
|---|
| 129 | $fields->fieldByName($fieldName)->addExtraClass("RequiredField"); |
|---|
| 130 | } |
|---|
| 131 | $requiredFields = new CustomRequiredFields($requiredFieldList); |
|---|
| 132 | $form = new Form($this, "Form", $fields, $actions, $requiredFields); |
|---|
| 133 | // Load any data avaliable into the form. |
|---|
| 134 | if($member) { |
|---|
| 135 | $form->loadNonBlankDataFrom($member); |
|---|
| 136 | } |
|---|
| 137 | return $form; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | * Save the changes to the form |
|---|
| 142 | */ |
|---|
| 143 | function submit($data, $form) { |
|---|
| 144 | $member = Member::currentUser(); |
|---|
| 145 | $newPerson = false; |
|---|
| 146 | if(!$member) { |
|---|
| 147 | $newPerson = true; |
|---|
| 148 | $member = new Member(); |
|---|
| 149 | $form->sessionMessage('Thank you. You have been registered.', 'good'); |
|---|
| 150 | } |
|---|
| 151 | else { |
|---|
| 152 | $form->sessionMessage('Your details have been saved', 'good'); |
|---|
| 153 | |
|---|
| 154 | } |
|---|
| 155 | $form->saveInto($member); |
|---|
| 156 | $member->write(); |
|---|
| 157 | |
|---|
| 158 | $group = DataObject::get_one("Group", '`Code` = "'.RegisterAndEditDetailsPage::$register_group_code.'"'); |
|---|
| 159 | if($group) { |
|---|
| 160 | $member->Groups()->add($group); |
|---|
| 161 | } |
|---|
| 162 | if($newPerson) { |
|---|
| 163 | $member->logIn(); |
|---|
| 164 | $link = ContentController::join_links($this->Link() , 'welcome'); |
|---|
| 165 | } |
|---|
| 166 | else { |
|---|
| 167 | $link = ContentController::join_links($this->Link() , 'thanks'); |
|---|
| 168 | } |
|---|
| 169 | if(!isset($_REQUEST["BackURL"]) && Session::get('BackURL')) { |
|---|
| 170 | $_REQUEST["BackURL"] = Session::get('BackURL'); |
|---|
| 171 | } |
|---|
| 172 | if(isset($_REQUEST["BackURL"])){ |
|---|
| 173 | $link = urldecode($_REQUEST["BackURL"]); |
|---|
| 174 | Session::set('BackURL', ''); |
|---|
| 175 | } |
|---|
| 176 | if($link) { |
|---|
| 177 | Director::redirect($link); |
|---|
| 178 | } |
|---|
| 179 | return array(); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | function thanks() { |
|---|
| 183 | if(!Member::currentUser()) { |
|---|
| 184 | Director::redirect($this->Link()); |
|---|
| 185 | } |
|---|
| 186 | $this->Title = $this->ThankYouTitle; |
|---|
| 187 | $this->Content = $this->ThankYouContent; |
|---|
| 188 | return array(); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | function welcome() { |
|---|
| 192 | if(!Member::currentUser()) { |
|---|
| 193 | Director::redirect($this->Link()); |
|---|
| 194 | } |
|---|
| 195 | $this->Title = $this->WelcomeTitle; |
|---|
| 196 | $this->Content = $this->WelcomeContent; |
|---|
| 197 | return array(); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | } |
|---|