1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Controller\Adminhtml\Index;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Address\Mapper;
use Magento\Customer\Model\AddressRegistry;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Framework\DataObjectFactory as ObjectFactory;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Controller\RegistryConstants;
use Magento\Customer\Model\EmailNotificationInterface;
use Magento\Customer\Model\Metadata\Form;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\App\ObjectManager;
/**
* Save customer action.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Save extends \Magento\Customer\Controller\Adminhtml\Index implements HttpPostActionInterface
{
/**
* @var EmailNotificationInterface
*/
private $emailNotification;
/**
* @var AddressRegistry
*/
private $addressRegistry;
/**
* Constructor
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
* @param \Magento\Customer\Model\AddressFactory $addressFactory
* @param \Magento\Customer\Model\Metadata\FormFactory $formFactory
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
* @param \Magento\Customer\Helper\View $viewHelper
* @param \Magento\Framework\Math\Random $random
* @param CustomerRepositoryInterface $customerRepository
* @param \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter
* @param Mapper $addressMapper
* @param AccountManagementInterface $customerAccountManagement
* @param AddressRepositoryInterface $addressRepository
* @param CustomerInterfaceFactory $customerDataFactory
* @param AddressInterfaceFactory $addressDataFactory
* @param \Magento\Customer\Model\Customer\Mapper $customerMapper
* @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
* @param DataObjectHelper $dataObjectHelper
* @param ObjectFactory $objectFactory
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
* @param \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
* @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
* @param AddressRegistry|null $addressRegistry
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\AddressFactory $addressFactory,
\Magento\Customer\Model\Metadata\FormFactory $formFactory,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
\Magento\Customer\Helper\View $viewHelper,
\Magento\Framework\Math\Random $random,
CustomerRepositoryInterface $customerRepository,
\Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
Mapper $addressMapper,
AccountManagementInterface $customerAccountManagement,
AddressRepositoryInterface $addressRepository,
CustomerInterfaceFactory $customerDataFactory,
AddressInterfaceFactory $addressDataFactory,
\Magento\Customer\Model\Customer\Mapper $customerMapper,
\Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
DataObjectHelper $dataObjectHelper,
ObjectFactory $objectFactory,
\Magento\Framework\View\LayoutFactory $layoutFactory,
\Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
AddressRegistry $addressRegistry = null
) {
parent::__construct(
$context,
$coreRegistry,
$fileFactory,
$customerFactory,
$addressFactory,
$formFactory,
$subscriberFactory,
$viewHelper,
$random,
$customerRepository,
$extensibleDataObjectConverter,
$addressMapper,
$customerAccountManagement,
$addressRepository,
$customerDataFactory,
$addressDataFactory,
$customerMapper,
$dataObjectProcessor,
$dataObjectHelper,
$objectFactory,
$layoutFactory,
$resultLayoutFactory,
$resultPageFactory,
$resultForwardFactory,
$resultJsonFactory
);
$this->addressRegistry = $addressRegistry ?: ObjectManager::getInstance()->get(AddressRegistry::class);
}
/**
* Reformat customer account data to be compatible with customer service interface
*
* @return array
*/
protected function _extractCustomerData()
{
$customerData = [];
if ($this->getRequest()->getPost('customer')) {
$additionalAttributes = [
CustomerInterface::DEFAULT_BILLING,
CustomerInterface::DEFAULT_SHIPPING,
'confirmation',
'sendemail_store_id',
'extension_attributes',
];
$customerData = $this->_extractData(
'adminhtml_customer',
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
$additionalAttributes,
'customer'
);
}
if (isset($customerData['disable_auto_group_change'])) {
$customerData['disable_auto_group_change'] = (int) filter_var(
$customerData['disable_auto_group_change'],
FILTER_VALIDATE_BOOLEAN
);
}
return $customerData;
}
/**
* Perform customer data filtration based on form code and form object
*
* @param string $formCode The code of EAV form to take the list of attributes from
* @param string $entityType entity type for the form
* @param string[] $additionalAttributes The list of attribute codes to skip filtration for
* @param string $scope scope of the request
* @return array
*/
protected function _extractData(
$formCode,
$entityType,
$additionalAttributes = [],
$scope = null
) {
$metadataForm = $this->getMetadataForm($entityType, $formCode, $scope);
$formData = $metadataForm->extractData($this->getRequest(), $scope);
$formData = $metadataForm->compactData($formData);
// Initialize additional attributes
/** @var \Magento\Framework\DataObject $object */
$object = $this->_objectFactory->create(['data' => $this->getRequest()->getPostValue()]);
$requestData = $object->getData($scope);
foreach ($additionalAttributes as $attributeCode) {
$formData[$attributeCode] = isset($requestData[$attributeCode]) ? $requestData[$attributeCode] : false;
}
// Unset unused attributes
$formAttributes = $metadataForm->getAttributes();
foreach ($formAttributes as $attribute) {
/** @var \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute */
$attributeCode = $attribute->getAttributeCode();
if ($attribute->getFrontendInput() != 'boolean'
&& $formData[$attributeCode] === false
) {
unset($formData[$attributeCode]);
}
}
if (empty($formData['extension_attributes'])) {
unset($formData['extension_attributes']);
}
return $formData;
}
/**
* Saves default_billing and default_shipping flags for customer address
*
* @deprecated 102.0.1 must be removed because addresses are save separately for now
* @param array $addressIdList
* @param array $extractedCustomerData
* @return array
*/
protected function saveDefaultFlags(array $addressIdList, array & $extractedCustomerData)
{
$result = [];
$extractedCustomerData[CustomerInterface::DEFAULT_BILLING] = null;
$extractedCustomerData[CustomerInterface::DEFAULT_SHIPPING] = null;
foreach ($addressIdList as $addressId) {
$scope = sprintf('address/%s', $addressId);
$addressData = $this->_extractData(
'adminhtml_customer_address',
AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
['default_billing', 'default_shipping'],
$scope
);
if (is_numeric($addressId)) {
$addressData['id'] = $addressId;
}
// Set default billing and shipping flags to customer
if (!empty($addressData['default_billing']) && $addressData['default_billing'] === 'true') {
$extractedCustomerData[CustomerInterface::DEFAULT_BILLING] = $addressId;
$addressData['default_billing'] = true;
} else {
$addressData['default_billing'] = false;
}
if (!empty($addressData['default_shipping']) && $addressData['default_shipping'] === 'true') {
$extractedCustomerData[CustomerInterface::DEFAULT_SHIPPING] = $addressId;
$addressData['default_shipping'] = true;
} else {
$addressData['default_shipping'] = false;
}
$result[] = $addressData;
}
return $result;
}
/**
* Reformat customer addresses data to be compatible with customer service interface
*
* @deprecated 102.0.1 addresses are saved separately for now
* @param array $extractedCustomerData
* @return array
*/
protected function _extractCustomerAddressData(array & $extractedCustomerData)
{
$addresses = $this->getRequest()->getPost('address');
$result = [];
if (is_array($addresses)) {
if (isset($addresses['_template_'])) {
unset($addresses['_template_']);
}
$addressIdList = array_keys($addresses);
$result = $this->saveDefaultFlags($addressIdList, $extractedCustomerData);
}
return $result;
}
/**
* Save customer action
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
$returnToEdit = false;
$originalRequestData = $this->getRequest()->getPostValue();
$customerId = $this->getCurrentCustomerId();
if ($originalRequestData) {
try {
// optional fields might be set in request for future processing by observers in other modules
$customerData = $this->_extractCustomerData();
if ($customerId) {
$currentCustomer = $this->_customerRepository->getById($customerId);
// No need to validate customer address while editing customer profile
$this->disableAddressValidation($currentCustomer);
$customerData = array_merge(
$this->customerMapper->toFlatArray($currentCustomer),
$customerData
);
$customerData['id'] = $customerId;
}
/** @var CustomerInterface $customer */
$customer = $this->customerDataFactory->create();
$this->dataObjectHelper->populateWithArray(
$customer,
$customerData,
\Magento\Customer\Api\Data\CustomerInterface::class
);
$this->_eventManager->dispatch(
'adminhtml_customer_prepare_save',
['customer' => $customer, 'request' => $this->getRequest()]
);
if (isset($customerData['sendemail_store_id'])) {
$customer->setStoreId($customerData['sendemail_store_id']);
}
// Save customer
if ($customerId) {
$this->_customerRepository->save($customer);
$this->getEmailNotification()->credentialsChanged($customer, $currentCustomer->getEmail());
} else {
$customer = $this->customerAccountManagement->createAccount($customer);
$customerId = $customer->getId();
}
$isSubscribed = null;
if ($this->_authorization->isAllowed(null)) {
$isSubscribed = $this->getRequest()->getPost('subscription');
}
if ($isSubscribed !== null) {
if ($isSubscribed !== '0') {
$this->_subscriberFactory->create()->subscribeCustomerById($customerId);
} else {
$this->_subscriberFactory->create()->unsubscribeCustomerById($customerId);
}
}
// After save
$this->_eventManager->dispatch(
'adminhtml_customer_save_after',
['customer' => $customer, 'request' => $this->getRequest()]
);
$this->_getSession()->unsCustomerFormData();
// Done Saving customer, finish save action
$this->_coreRegistry->register(RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
$this->messageManager->addSuccess(__('You saved the customer.'));
$returnToEdit = (bool)$this->getRequest()->getParam('back', false);
} catch (\Magento\Framework\Validator\Exception $exception) {
$messages = $exception->getMessages();
if (empty($messages)) {
$messages = $exception->getMessage();
}
$this->_addSessionErrorMessages($messages);
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
} catch (\Magento\Framework\Exception\AbstractAggregateException $exception) {
$errors = $exception->getErrors();
$messages = [];
foreach ($errors as $error) {
$messages[] = $error->getMessage();
}
$this->_addSessionErrorMessages($messages);
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
} catch (LocalizedException $exception) {
$this->_addSessionErrorMessages($exception->getMessage());
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
} catch (\Exception $exception) {
$this->messageManager->addException($exception, __('Something went wrong while saving the customer.'));
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
}
}
$resultRedirect = $this->resultRedirectFactory->create();
if ($returnToEdit) {
if ($customerId) {
$resultRedirect->setPath(
'customer/*/edit',
['id' => $customerId, '_current' => true]
);
} else {
$resultRedirect->setPath(
'customer/*/new',
['_current' => true]
);
}
} else {
$resultRedirect->setPath('customer/index');
}
return $resultRedirect;
}
/**
* Get email notification
*
* @return EmailNotificationInterface
* @deprecated 100.1.0
*/
private function getEmailNotification()
{
if (!($this->emailNotification instanceof EmailNotificationInterface)) {
return \Magento\Framework\App\ObjectManager::getInstance()->get(
EmailNotificationInterface::class
);
} else {
return $this->emailNotification;
}
}
/**
* Get metadata form
*
* @param string $entityType
* @param string $formCode
* @param string $scope
* @return Form
*/
private function getMetadataForm($entityType, $formCode, $scope)
{
$attributeValues = [];
if ($entityType == CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER) {
$customerId = $this->getCurrentCustomerId();
if ($customerId) {
$customer = $this->_customerRepository->getById($customerId);
$attributeValues = $this->customerMapper->toFlatArray($customer);
}
}
if ($entityType == AddressMetadataInterface::ENTITY_TYPE_ADDRESS) {
$scopeData = explode('/', $scope);
if (isset($scopeData[1]) && is_numeric($scopeData[1])) {
$customerAddress = $this->addressRepository->getById($scopeData[1]);
$attributeValues = $this->addressMapper->toFlatArray($customerAddress);
}
}
$metadataForm = $this->_formFactory->create(
$entityType,
$formCode,
$attributeValues,
false,
Form::DONT_IGNORE_INVISIBLE
);
return $metadataForm;
}
/**
* Retrieve current customer ID
*
* @return int
*/
private function getCurrentCustomerId()
{
$originalRequestData = $this->getRequest()->getPostValue(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
$customerId = isset($originalRequestData['entity_id'])
? $originalRequestData['entity_id']
: null;
return $customerId;
}
/**
* Disable Customer Address Validation
*
* @param CustomerInterface $customer
* @throws NoSuchEntityException
*/
private function disableAddressValidation($customer)
{
foreach ($customer->getAddresses() as $address) {
$addressModel = $this->addressRegistry->retrieve($address->getId());
$addressModel->setShouldIgnoreValidation(true);
}
}
}