1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\ContactAdmin\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\i18n\i18n; |
6
|
|
|
use SilverStripe\Forms\Form; |
7
|
|
|
use SilverStripe\Core\Extension; |
8
|
|
|
use SilverStripe\View\ArrayData; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\Forms\TextField; |
11
|
|
|
use SilverStripe\Forms\FormAction; |
12
|
|
|
use SilverStripe\Forms\HeaderField; |
13
|
|
|
use SilverStripe\Forms\HiddenField; |
14
|
|
|
use SilverStripe\ORM\PaginatedList; |
15
|
|
|
use SilverStripe\Security\Security; |
16
|
|
|
use SilverStripe\Forms\LiteralField; |
17
|
|
|
use SilverStripe\Forms\CheckboxField; |
18
|
|
|
use SilverStripe\Forms\DropdownField; |
19
|
|
|
use SilverStripe\Forms\CompositeField; |
20
|
|
|
use SilverStripe\Forms\RequiredFields; |
21
|
|
|
use SilverStripe\ORM\ValidationResult; |
22
|
|
|
use SilverStripe\Core\Injector\Injector; |
23
|
|
|
use SilverCommerce\ContactAdmin\Model\ContactLocation; |
24
|
|
|
use ilateral\SilverStripe\Users\Control\AccountController; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Add extra fields to a user account (if the users module is |
28
|
|
|
* installed) to allow logged in users to see their invoices. |
29
|
|
|
* |
30
|
|
|
* @package orders |
31
|
|
|
*/ |
32
|
|
|
class AccountControllerExtension extends Extension |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Add extra URL endpoints |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private static $allowed_actions = [ |
|
|
|
|
40
|
|
|
"addresses", |
41
|
|
|
"addaddress", |
42
|
|
|
"editaddress", |
43
|
|
|
"removeaddress", |
44
|
|
|
"AddressForm" |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Display all addresses associated with the current user |
49
|
|
|
* |
50
|
|
|
* @return HTMLText |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
public function addresses() |
53
|
|
|
{ |
54
|
|
|
$member = Security::getCurrentUser(); |
55
|
|
|
|
56
|
|
|
$this |
57
|
|
|
->getOwner() |
58
|
|
|
->customise([ |
59
|
|
|
"Title" => _t( |
60
|
|
|
"SilverCommerce\ContactAdmin.YourAddresses", |
61
|
|
|
"Your Addresses" |
62
|
|
|
), |
63
|
|
|
"MetaTitle" => _t( |
64
|
|
|
"SilverCommerce\ContactAdmin.YourAddresses", |
65
|
|
|
"Your Addresses" |
66
|
|
|
), |
67
|
|
|
"Content" => $this->getOwner()->renderWith( |
68
|
|
|
"SilverCommerce\\ContactAdmin\\Includes\\Addresses", |
69
|
|
|
["Contact" => $member->Contact()] |
70
|
|
|
) |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
$this->getOwner()->extend("updateAddresses"); |
74
|
|
|
|
75
|
|
|
return $this |
76
|
|
|
->getOwner() |
77
|
|
|
->renderWith([ |
78
|
|
|
'AccountController_addresses', |
79
|
|
|
AccountController::class . '_addresses', |
80
|
|
|
'AccountController', |
81
|
|
|
AccountController::class, |
82
|
|
|
'Page' |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Display all addresses associated with the current user |
88
|
|
|
* |
89
|
|
|
* @return HTMLText |
90
|
|
|
*/ |
91
|
|
|
public function addaddress() |
92
|
|
|
{ |
93
|
|
|
$form = $this->getOwner()->AddressForm(); |
94
|
|
|
$member = Security::getCurrentUser(); |
95
|
|
|
|
96
|
|
|
$form |
97
|
|
|
->Fields() |
98
|
|
|
->dataFieldByName("ContactID") |
99
|
|
|
->setValue($member->Contact()->ID); |
100
|
|
|
|
101
|
|
|
$this |
102
|
|
|
->getOwner() |
103
|
|
|
->customise([ |
104
|
|
|
"Title" => _t( |
105
|
|
|
"SilverCommerce\ContactAdmin.AddAddress", |
106
|
|
|
"Add Address" |
107
|
|
|
), |
108
|
|
|
"MetaTitle" => _t( |
109
|
|
|
"SilverCommerce\ContactAdmin.AddAddress", |
110
|
|
|
"Add Address" |
111
|
|
|
), |
112
|
|
|
"Form" => $form |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
$this->getOwner()->extend("updateAddAddress"); |
116
|
|
|
|
117
|
|
|
return $this |
118
|
|
|
->getOwner() |
119
|
|
|
->renderWith([ |
120
|
|
|
'AccountController_addaddress', |
121
|
|
|
AccountController::class . '_addaddress', |
122
|
|
|
'AccountController', |
123
|
|
|
AccountController::class, |
124
|
|
|
'Page' |
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Display all addresses associated with the current user |
130
|
|
|
* |
131
|
|
|
* @return HTMLText |
132
|
|
|
*/ |
133
|
|
|
public function editaddress() |
134
|
|
|
{ |
135
|
|
|
$member = Security::getCurrentUser(); |
136
|
|
|
$id = $this->getOwner()->request->param("ID"); |
137
|
|
|
$address = ContactLocation::get()->byID($id); |
138
|
|
|
|
139
|
|
|
if ($address && $address->canEdit($member)) { |
140
|
|
|
$form = $this->AddressForm(); |
141
|
|
|
$form->loadDataFrom($address); |
142
|
|
|
$form |
143
|
|
|
->Actions() |
144
|
|
|
->dataFieldByName("action_doSaveAddress") |
145
|
|
|
->setTitle(_t("SilverCommerce\ContactAdmin.Save", "Save")); |
146
|
|
|
|
147
|
|
|
$this |
148
|
|
|
->getOwner() |
149
|
|
|
->customise([ |
150
|
|
|
"Title" => _t( |
151
|
|
|
"SilverCommerce\ContactAdmin.EditAddress", |
152
|
|
|
"Edit Address" |
153
|
|
|
), |
154
|
|
|
"MenuTitle" => _t( |
155
|
|
|
"SilverCommerce\ContactAdmin.EditAddress", |
156
|
|
|
"Edit Address" |
157
|
|
|
), |
158
|
|
|
"Form" => $form |
159
|
|
|
]); |
160
|
|
|
|
161
|
|
|
$this->getOwner()->extend("updateEditAddress"); |
162
|
|
|
|
163
|
|
|
return $this |
164
|
|
|
->getOwner() |
165
|
|
|
->renderWith([ |
166
|
|
|
'AccountController_editaddress', |
167
|
|
|
AccountController::class . '_editaddress', |
168
|
|
|
'AccountController', |
169
|
|
|
AccountController::class, |
170
|
|
|
'Page' |
171
|
|
|
]); |
172
|
|
|
} else { |
173
|
|
|
return $this->getOwner()->httpError(404); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Remove an addresses by the given ID (if allowed) |
179
|
|
|
* |
180
|
|
|
* @return HTMLText |
181
|
|
|
*/ |
182
|
|
|
public function removeaddress() |
183
|
|
|
{ |
184
|
|
|
$member = Security::getCurrentUser(); |
185
|
|
|
$id = $this->getOwner()->request->param("ID"); |
186
|
|
|
$address = ContactLocation::get()->byID($id); |
187
|
|
|
|
188
|
|
|
if ($address && $address->canDelete($member)) { |
189
|
|
|
$address->delete(); |
190
|
|
|
$this |
191
|
|
|
->owner |
192
|
|
|
->customise([ |
193
|
|
|
"Title" => _t( |
194
|
|
|
"SilverCommerce\ContactAdmin.AddressRemoved", |
195
|
|
|
"Address Removed" |
196
|
|
|
), |
197
|
|
|
"MenuTitle" => _t( |
198
|
|
|
"SilverCommerce\ContactAdmin.AddressRemoved", |
199
|
|
|
"Address Removed" |
200
|
|
|
) |
201
|
|
|
]); |
202
|
|
|
|
203
|
|
|
$this->getOwner()->extend("updateEditAddress"); |
204
|
|
|
|
205
|
|
|
return $this |
206
|
|
|
->getOwner() |
207
|
|
|
->renderWith([ |
208
|
|
|
'AccountController_removeaddress', |
209
|
|
|
AccountController::class . '_removeaddress', |
210
|
|
|
'AccountController', |
211
|
|
|
AccountController::class, |
212
|
|
|
'Page' |
213
|
|
|
]); |
214
|
|
|
} else { |
215
|
|
|
return $this->getOwner()->httpError(404); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Form used for adding or editing addresses |
221
|
|
|
* |
222
|
|
|
* @return Form |
223
|
|
|
*/ |
224
|
|
|
public function AddressForm() |
225
|
|
|
{ |
226
|
|
|
$location = Injector::inst()->get(ContactLocation::class); |
227
|
|
|
|
228
|
|
|
$fields = FieldList::create( |
229
|
|
|
HiddenField::create("ID") |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
$fields->merge($location->getFrontEndFields()); |
233
|
|
|
|
234
|
|
|
$fields->replaceField( |
235
|
|
|
"Country", |
236
|
|
|
DropdownField::create( |
237
|
|
|
'Country', |
238
|
|
|
$location->fieldLabel("Country"), |
239
|
|
|
i18n::getData()->getCountries() |
240
|
|
|
)->setEmptyString("") |
241
|
|
|
); |
242
|
|
|
|
243
|
|
|
$fields->replaceField( |
244
|
|
|
"ContactID", |
245
|
|
|
HiddenField::create('ContactID') |
246
|
|
|
); |
247
|
|
|
|
248
|
|
|
$back_btn = '<a href="'; |
249
|
|
|
$back_btn .= $this->getOwner()->Link('addresses'); |
250
|
|
|
$back_btn .= '" class="btn btn-link">'; |
251
|
|
|
$back_btn .= _t('SilverCommerce\ContactAdmin.Cancel', 'Cancel'); |
252
|
|
|
$back_btn .= '</a>'; |
253
|
|
|
|
254
|
|
|
$form = Form::create( |
255
|
|
|
$this->getOwner(), |
256
|
|
|
"AddressForm", |
257
|
|
|
$fields, |
258
|
|
|
FieldList::create( |
259
|
|
|
LiteralField::create( |
260
|
|
|
'BackButton', |
261
|
|
|
$back_btn |
262
|
|
|
), |
263
|
|
|
FormAction::create( |
264
|
|
|
'doSaveAddress', |
265
|
|
|
_t('SilverCommerce\ContactAdmin.Add', 'Add') |
266
|
|
|
)->addExtraClass('btn btn-success') |
267
|
|
|
), |
268
|
|
|
RequiredFields::create( |
269
|
|
|
[ |
270
|
|
|
'FirstName', |
271
|
|
|
'Surname', |
272
|
|
|
'Address1', |
273
|
|
|
'City', |
274
|
|
|
'PostCode', |
275
|
|
|
'Country', |
276
|
|
|
] |
277
|
|
|
) |
278
|
|
|
); |
279
|
|
|
|
280
|
|
|
$this->getOwner()->extend("updateAddressForm", $form); |
281
|
|
|
|
282
|
|
|
return $form; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Method responsible for saving (or adding) an address. |
287
|
|
|
* If the ID field is set, the method assums we are saving |
288
|
|
|
* an address. |
289
|
|
|
* |
290
|
|
|
* If the ID field is not set, we assume a new address is being |
291
|
|
|
* created. |
292
|
|
|
* |
293
|
|
|
*/ |
294
|
|
|
public function doSaveAddress($data, $form) |
295
|
|
|
{ |
296
|
|
|
if (!$data["ID"]) { |
297
|
|
|
$address = ContactLocation::create(); |
298
|
|
|
} else { |
299
|
|
|
$address = ContactLocation::get()->byID($data["ID"]); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
if ($address) { |
303
|
|
|
$form->saveInto($address); |
304
|
|
|
$address->write(); |
305
|
|
|
$form->sessionMessage( |
306
|
|
|
_t("SilverCommerce\ContactAdmin.AddressSaved", "Address Saved"), |
307
|
|
|
ValidationResult::TYPE_GOOD |
308
|
|
|
); |
309
|
|
|
} else { |
310
|
|
|
$form->sessionMessage( |
311
|
|
|
_t("SilverCommerce\ContactAdmin.Error", "There was an error"), |
312
|
|
|
ValidationResult::TYPE_ERROR |
313
|
|
|
); |
314
|
|
|
} |
315
|
|
|
return $this |
316
|
|
|
->getOwner() |
317
|
|
|
->redirect($this->getOwner()->Link("addresses")); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Add commerce specific links to account menu |
322
|
|
|
* |
323
|
|
|
* @param ArrayList $menu |
|
|
|
|
324
|
|
|
*/ |
325
|
|
|
public function updateAccountMenu($menu) |
326
|
|
|
{ |
327
|
|
|
$curr_action = $this |
328
|
|
|
->owner |
329
|
|
|
->getRequest() |
330
|
|
|
->param("Action"); |
331
|
|
|
|
332
|
|
|
$menu->add(ArrayData::create([ |
333
|
|
|
"ID" => 11, |
334
|
|
|
"Title" => _t('SilverCommerce\ContactAdmin.Addresses', 'Addresses'), |
335
|
|
|
"Link" => $this->getOwner()->Link("addresses"), |
336
|
|
|
"LinkingMode" => ($curr_action == "addresses") ? "current" : "link" |
337
|
|
|
])); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
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