Source of file AccountPageController.php
Size: 8,356 Bytes - Last Modified: 2021-12-24T06:35:03+00:00
/var/www/docs.ssmods.com/process/src/src/Page/AccountPageController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 | <?php namespace SilverShop\Page; use PageController; use SilverShop\Forms\ShopAccountForm; use SilverShop\Model\Address; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\RequiredFields; use SilverStripe\Security\Member; use SilverStripe\Security\MemberAuthenticator\ChangePasswordForm; use SilverStripe\Security\MemberAuthenticator\ChangePasswordHandler; use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator; use SilverStripe\Security\Security; use SilverStripe\SiteConfig\SiteConfig; class AccountPageController extends PageController { private static $url_segment = 'account'; private static $allowed_actions = [ 'addressbook', 'CreateAddressForm', 'DefaultAddressForm', 'editprofile', 'EditAccountForm', 'ChangePasswordForm', 'changepassword', // redirects to editprofile 'deleteaddress', 'setdefaultbilling', 'setdefaultshipping', ]; /** * @var Member */ protected $member; public function init() { parent::init(); $this->member = Security::getCurrentUser(); if (!$this->member) { $messages = array( 'default' => _t( 'SilverShop\Page\AccountPage.Login', 'You\'ll need to login before you can access the account page. If you are not registered, you won\'t be able to access it until you make your first order, otherwise please enter your details below.' ), 'logInAgain' => _t( 'SilverShop\Page\AccountPage.LoginAgain', 'You have been logged out. If you would like to log in again, please do so below.' ), ); Security::permissionFailure($this, $messages); } } public function getTitle() { if ($this->dataRecord && $title = $this->dataRecord->Title) { return $title; } return _t('SilverShop\Page\AccountPage.DefaultTitle', 'Account'); } public function getMember() { return $this->member; } public function addressbook() { return array( 'DefaultAddressForm' => $this->DefaultAddressForm(), 'CreateAddressForm' => $this->CreateAddressForm(), ); } public function DefaultAddressForm() { $addresses = $this->member->AddressBook()->sort('Created', 'DESC'); if ($addresses->exists()) { $fields = FieldList::create( DropdownField::create( 'DefaultShippingAddressID', _t('SilverShop\Model\Address.ShippingAddress', 'Shipping Address'), $addresses->map('ID', 'toString')->toArray() ), DropdownField::create( 'DefaultBillingAddressID', _t('SilverShop\Model\Address.BillingAddress', 'Billing Address'), $addresses->map('ID', 'toString')->toArray() ) ); $actions = FieldList::create( FormAction::create('savedefaultaddresses', _t('SilverShop\Model\Address.SaveDefaults', 'Save Defaults')) ); $form = Form::create($this, 'DefaultAddressForm', $fields, $actions); $form->loadDataFrom($this->member); $this->extend('updateDefaultAddressForm', $form); return $form; } return false; } public function savedefaultaddresses($data, $form) { $form->saveInto($this->member); $this->member->write(); $this->extend('updateDefaultAddressFormResponse', $form, $data, $response); return $response ?: $this->redirect($this->Link('addressbook')); } public function CreateAddressForm() { $singletonaddress = singleton(Address::class); $fields = $singletonaddress->getFrontEndFields(); $actions = FieldList::create( FormAction::create('saveaddress', _t('SilverShop\Model\Address.SaveNew', 'Save New Address')) ); $validator = RequiredFields::create($singletonaddress->getRequiredFields()); $form = Form::create($this, 'CreateAddressForm', $fields, $actions, $validator); $this->extend('updateCreateAddressForm', $form); return $form; } public function saveaddress($data, $form) { $member = $this->getMember(); $address = Address::create(); $form->saveInto($address); $address->MemberID = $member->ID; // Add value for Country if missing (due readonly field in form) if ($country = SiteConfig::current_site_config()->getSingleCountry()) { $address->Country = $country; } $address->write(); if (!$member->DefaultShippingAddressID) { $member->DefaultShippingAddressID = $address->ID; $member->write(); } if (!$member->DefaultBillingAddressID) { $member->DefaultBillingAddressID = $address->ID; $member->write(); } $form->sessionMessage(_t('SilverShop\Model\Address.AddressSaved', 'Your address has been saved'), 'good'); $this->extend('updateCreateAddressFormResponse', $form, $data, $response); return $response ?: $this->redirect($this->Link('addressbook')); } public function editprofile() { return array(); } /** * @param HTTPRequest $req * @return HTTPResponse */ function deleteaddress($req) { // NOTE: we don't want to fully delete the address because it's presumably still // attached to an order. Setting MemberID to 0 means it won't show up in the address // book any longer. $address = $this->member->AddressBook()->byID($req->param('ID')); if ($address) { $address->MemberID = 0; $address->write(); } else { return $this->httpError(404, 'Address not found'); } return $this->redirectBack(); } /** * @param HTTPRequest $req * @return HTTPResponse */ function setdefaultbilling($req) { $this->member->DefaultBillingAddressID = $req->param('ID'); $this->member->write(); return $this->redirectBack(); } /** * @param HTTPRequest $req * @return HTTPResponse */ function setdefaultshipping($req) { $this->member->DefaultShippingAddressID = $req->param('ID'); $this->member->write(); return $this->redirectBack(); } /** * Return a form allowing the user to edit their details. * * @return ShopAccountForm */ public function EditAccountForm() { return ShopAccountForm::create($this, 'EditAccountForm'); } public function ChangePasswordForm() { /** * @var ChangePasswordHandler $handler */ $handler = Injector::inst()->get(MemberAuthenticator::class)->getChangePasswordHandler($this->Link()); $handler->setRequest($this->getRequest()); /** * @var ChangePasswordForm $form */ $form = $handler->changePasswordForm(); // The default form tries to redirect to /account/login which doesn't exist $backURL = $form->Fields()->fieldByName('BackURL'); if (!$backURL) { $backURL = new HiddenField('BackURL', 'BackURL'); $form->Fields()->push($backURL); } $backURL->setValue($this->Link('editprofile')); $this->extend('updateChangePasswordForm', $form); return $form; } /** * By default, ChangePasswordForm redirects to /account/changepassword when it's done. * This catches that and sends it back to editprofile, which seems easier and less error-prone * than the alternative of trying to manipulate the BackURL field. */ public function changepassword() { $this->redirect($this->Link('editprofile')); } } |