1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverShop\Page; |
4
|
|
|
|
5
|
|
|
use PageController; |
|
|
|
|
6
|
|
|
use SilverShop\Forms\ShopAccountForm; |
7
|
|
|
use SilverShop\Model\Address; |
8
|
|
|
use SilverStripe\Control\HTTPRequest; |
9
|
|
|
use SilverStripe\Control\HTTPResponse; |
10
|
|
|
use SilverStripe\Core\Injector\Injector; |
11
|
|
|
use SilverStripe\Forms\DropdownField; |
12
|
|
|
use SilverStripe\Forms\FieldList; |
13
|
|
|
use SilverStripe\Forms\Form; |
14
|
|
|
use SilverStripe\Forms\FormAction; |
15
|
|
|
use SilverStripe\Forms\HiddenField; |
16
|
|
|
use SilverStripe\Forms\RequiredFields; |
17
|
|
|
use SilverStripe\Security\Member; |
18
|
|
|
use SilverStripe\Security\MemberAuthenticator\ChangePasswordForm; |
19
|
|
|
use SilverStripe\Security\MemberAuthenticator\ChangePasswordHandler; |
20
|
|
|
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator; |
21
|
|
|
use SilverStripe\Security\Security; |
22
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
23
|
|
|
|
24
|
|
|
class AccountPageController extends PageController |
25
|
|
|
{ |
26
|
|
|
private static $url_segment = 'account'; |
|
|
|
|
27
|
|
|
|
28
|
|
|
private static $allowed_actions = [ |
|
|
|
|
29
|
|
|
'addressbook', |
30
|
|
|
'CreateAddressForm', |
31
|
|
|
'DefaultAddressForm', |
32
|
|
|
'editprofile', |
33
|
|
|
'EditAccountForm', |
34
|
|
|
'ChangePasswordForm', |
35
|
|
|
'changepassword', // redirects to editprofile |
36
|
|
|
'deleteaddress', |
37
|
|
|
'setdefaultbilling', |
38
|
|
|
'setdefaultshipping', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Member |
43
|
|
|
*/ |
44
|
|
|
protected $member; |
45
|
|
|
|
46
|
|
|
public function init() |
47
|
|
|
{ |
48
|
|
|
parent::init(); |
49
|
|
|
|
50
|
|
|
$this->member = Security::getCurrentUser(); |
51
|
|
|
|
52
|
|
|
if (!$this->member) { |
53
|
|
|
$messages = [ |
54
|
|
|
'default' => _t( |
55
|
|
|
'SilverShop\Page\AccountPage.Login', |
56
|
|
|
'You\'ll need to login before you can access the account page. |
57
|
|
|
If you are not registered, you won\'t be able to access it until |
58
|
|
|
you make your first order, otherwise please enter your details below.' |
59
|
|
|
), |
60
|
|
|
'logInAgain' => _t( |
61
|
|
|
'SilverShop\Page\AccountPage.LoginAgain', |
62
|
|
|
'You have been logged out. If you would like to log in again, please do so below.' |
63
|
|
|
), |
64
|
|
|
]; |
65
|
|
|
Security::permissionFailure($this, $messages); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getTitle() |
70
|
|
|
{ |
71
|
|
|
if ($this->dataRecord && $title = $this->dataRecord->Title) { |
72
|
|
|
return $title; |
73
|
|
|
} |
74
|
|
|
return _t('SilverShop\Page\AccountPage.DefaultTitle', 'Account'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getMember() |
78
|
|
|
{ |
79
|
|
|
return $this->member; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function addressbook() |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
'DefaultAddressForm' => $this->DefaultAddressForm(), |
86
|
|
|
'CreateAddressForm' => $this->CreateAddressForm(), |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function DefaultAddressForm() |
91
|
|
|
{ |
92
|
|
|
$addresses = $this->member->AddressBook()->sort('Created', 'DESC'); |
93
|
|
|
if ($addresses->exists()) { |
94
|
|
|
$fields = FieldList::create( |
95
|
|
|
DropdownField::create( |
96
|
|
|
'DefaultShippingAddressID', |
97
|
|
|
_t('SilverShop\Model\Address.ShippingAddress', 'Shipping Address'), |
98
|
|
|
$addresses->map('ID', 'toString')->toArray() |
99
|
|
|
), |
100
|
|
|
DropdownField::create( |
101
|
|
|
'DefaultBillingAddressID', |
102
|
|
|
_t('SilverShop\Model\Address.BillingAddress', 'Billing Address'), |
103
|
|
|
$addresses->map('ID', 'toString')->toArray() |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
$actions = FieldList::create( |
107
|
|
|
FormAction::create('savedefaultaddresses', _t('SilverShop\Model\Address.SaveDefaults', 'Save Defaults')) |
108
|
|
|
); |
109
|
|
|
$form = Form::create($this, 'DefaultAddressForm', $fields, $actions); |
110
|
|
|
$form->loadDataFrom($this->member); |
111
|
|
|
|
112
|
|
|
$this->extend('updateDefaultAddressForm', $form); |
113
|
|
|
|
114
|
|
|
return $form; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function savedefaultaddresses($data, $form) |
121
|
|
|
{ |
122
|
|
|
$form->saveInto($this->member); |
123
|
|
|
$this->member->write(); |
124
|
|
|
|
125
|
|
|
$this->extend('updateDefaultAddressFormResponse', $form, $data, $response); |
|
|
|
|
126
|
|
|
|
127
|
|
|
return $response ?: $this->redirect($this->Link('addressbook')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function CreateAddressForm() |
131
|
|
|
{ |
132
|
|
|
$singletonaddress = singleton(Address::class); |
133
|
|
|
$fields = $singletonaddress->getFrontEndFields(); |
134
|
|
|
$actions = FieldList::create( |
135
|
|
|
FormAction::create('saveaddress', _t('SilverShop\Model\Address.SaveNew', 'Save New Address')) |
136
|
|
|
); |
137
|
|
|
$validator = RequiredFields::create($singletonaddress->getRequiredFields()); |
138
|
|
|
$form = Form::create($this, 'CreateAddressForm', $fields, $actions, $validator); |
139
|
|
|
$this->extend('updateCreateAddressForm', $form); |
140
|
|
|
return $form; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function saveaddress($data, $form) |
144
|
|
|
{ |
145
|
|
|
$member = $this->getMember(); |
146
|
|
|
$address = Address::create(); |
147
|
|
|
$form->saveInto($address); |
148
|
|
|
$address->MemberID = $member->ID; |
149
|
|
|
|
150
|
|
|
// Add value for Country if missing (due readonly field in form) |
151
|
|
|
if ($country = SiteConfig::current_site_config()->getSingleCountry()) { |
152
|
|
|
$address->Country = $country; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$address->write(); |
156
|
|
|
|
157
|
|
|
if (!$member->DefaultShippingAddressID) { |
158
|
|
|
$member->DefaultShippingAddressID = $address->ID; |
159
|
|
|
$member->write(); |
160
|
|
|
} |
161
|
|
|
if (!$member->DefaultBillingAddressID) { |
162
|
|
|
$member->DefaultBillingAddressID = $address->ID; |
163
|
|
|
$member->write(); |
164
|
|
|
} |
165
|
|
|
$form->sessionMessage(_t('SilverShop\Model\Address.AddressSaved', 'Your address has been saved'), 'good'); |
166
|
|
|
|
167
|
|
|
$this->extend('updateCreateAddressFormResponse', $form, $data, $response); |
|
|
|
|
168
|
|
|
|
169
|
|
|
return $response ?: $this->redirect($this->Link('addressbook')); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function editprofile() |
173
|
|
|
{ |
174
|
|
|
return []; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param HTTPRequest $req |
179
|
|
|
* @return HTTPResponse |
180
|
|
|
*/ |
181
|
|
|
function deleteaddress($req) |
182
|
|
|
{ |
183
|
|
|
// NOTE: we don't want to fully delete the address because it's presumably still |
184
|
|
|
// attached to an order. Setting MemberID to 0 means it won't show up in the address |
185
|
|
|
// book any longer. |
186
|
|
|
$address = $this->member->AddressBook()->byID($req->param('ID')); |
187
|
|
|
if ($address) { |
188
|
|
|
$address->MemberID = 0; |
189
|
|
|
$address->write(); |
190
|
|
|
} else { |
191
|
|
|
return $this->httpError(404, 'Address not found'); |
192
|
|
|
} |
193
|
|
|
return $this->redirectBack(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param HTTPRequest $req |
198
|
|
|
* @return HTTPResponse |
199
|
|
|
*/ |
200
|
|
|
function setdefaultbilling($req) |
201
|
|
|
{ |
202
|
|
|
$this->member->DefaultBillingAddressID = $req->param('ID'); |
203
|
|
|
$this->member->write(); |
204
|
|
|
return $this->redirectBack(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param HTTPRequest $req |
209
|
|
|
* @return HTTPResponse |
210
|
|
|
*/ |
211
|
|
|
function setdefaultshipping($req) |
212
|
|
|
{ |
213
|
|
|
$this->member->DefaultShippingAddressID = $req->param('ID'); |
214
|
|
|
$this->member->write(); |
215
|
|
|
return $this->redirectBack(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Return a form allowing the user to edit their details. |
220
|
|
|
* |
221
|
|
|
* @return ShopAccountForm |
222
|
|
|
*/ |
223
|
|
|
public function EditAccountForm() |
224
|
|
|
{ |
225
|
|
|
return ShopAccountForm::create($this, 'EditAccountForm'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function ChangePasswordForm() |
229
|
|
|
{ |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @var ChangePasswordHandler $handler |
233
|
|
|
*/ |
234
|
|
|
$handler = Injector::inst()->get(MemberAuthenticator::class)->getChangePasswordHandler($this->Link()); |
235
|
|
|
$handler->setRequest($this->getRequest()); |
236
|
|
|
/** |
237
|
|
|
* @var ChangePasswordForm $form |
238
|
|
|
*/ |
239
|
|
|
$form = $handler->changePasswordForm(); |
240
|
|
|
|
241
|
|
|
// The default form tries to redirect to /account/login which doesn't exist |
242
|
|
|
$backURL = $form->Fields()->fieldByName('BackURL'); |
|
|
|
|
243
|
|
|
if (!$backURL) { |
|
|
|
|
244
|
|
|
$backURL = new HiddenField('BackURL', 'BackURL'); |
245
|
|
|
$form->Fields()->push($backURL); |
246
|
|
|
} |
247
|
|
|
$backURL->setValue($this->Link('editprofile')); |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
$this->extend('updateChangePasswordForm', $form); |
251
|
|
|
|
252
|
|
|
return $form; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* By default, ChangePasswordForm redirects to /account/changepassword when it's done. |
257
|
|
|
* This catches that and sends it back to editprofile, which seems easier and less error-prone |
258
|
|
|
* than the alternative of trying to manipulate the BackURL field. |
259
|
|
|
*/ |
260
|
|
|
public function changepassword() |
261
|
|
|
{ |
262
|
|
|
$this->redirect($this->Link('editprofile')); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths