Edit.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Adminhtml\Index;
  7. use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
  8. use Magento\Customer\Api\Data\CustomerInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. class Edit extends \Magento\Customer\Controller\Adminhtml\Index implements HttpGetActionInterface
  11. {
  12. /**
  13. * Customer edit action
  14. *
  15. * @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Redirect
  16. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  17. * @SuppressWarnings(PHPMD.NPathComplexity)
  18. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  19. */
  20. public function execute()
  21. {
  22. $customerId = $this->initCurrentCustomer();
  23. $customerData = [];
  24. $customerData['account'] = [];
  25. $customerData['address'] = [];
  26. $customer = null;
  27. $isExistingCustomer = (bool)$customerId;
  28. if ($isExistingCustomer) {
  29. try {
  30. $customer = $this->_customerRepository->getById($customerId);
  31. $customerData['account'] = $this->customerMapper->toFlatArray($customer);
  32. $customerData['account'][CustomerInterface::ID] = $customerId;
  33. try {
  34. $addresses = $customer->getAddresses();
  35. foreach ($addresses as $address) {
  36. $customerData['address'][$address->getId()] = $this->addressMapper->toFlatArray($address);
  37. $customerData['address'][$address->getId()]['id'] = $address->getId();
  38. }
  39. } catch (NoSuchEntityException $e) {
  40. //do nothing
  41. }
  42. } catch (NoSuchEntityException $e) {
  43. $this->messageManager->addException($e, __('Something went wrong while editing the customer.'));
  44. $resultRedirect = $this->resultRedirectFactory->create();
  45. $resultRedirect->setPath('customer/*/index');
  46. return $resultRedirect;
  47. }
  48. }
  49. $customerData['customer_id'] = $customerId;
  50. $this->_getSession()->setCustomerData($customerData);
  51. $resultPage = $this->resultPageFactory->create();
  52. $resultPage->setActiveMenu('Magento_Customer::customer_manage');
  53. $this->prepareDefaultCustomerTitle($resultPage);
  54. $resultPage->setActiveMenu('Magento_Customer::customer');
  55. if ($isExistingCustomer) {
  56. $resultPage->getConfig()->getTitle()->prepend($this->_viewHelper->getCustomerName($customer));
  57. } else {
  58. $resultPage->getConfig()->getTitle()->prepend(__('New Customer'));
  59. }
  60. return $resultPage;
  61. }
  62. }