123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- declare(strict_types=1);
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Controller\Adminhtml\Address;
- use Magento\Backend\App\Action;
- use Magento\Framework\App\Action\HttpPostActionInterface;
- use Magento\Framework\Controller\Result\Json;
- use Magento\Framework\Controller\Result\JsonFactory;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Psr\Log\LoggerInterface;
- /**
- * Class for saving of customer address
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Save extends Action implements HttpPostActionInterface
- {
- /**
- * Authorization level of a basic admin session
- *
- * @see _isAllowed()
- */
- public const ADMIN_RESOURCE = 'Magento_Customer::manage';
- /**
- * @var \Magento\Customer\Api\AddressRepositoryInterface
- */
- private $addressRepository;
- /**
- * @var \Magento\Customer\Model\Metadata\FormFactory
- */
- private $formFactory;
- /**
- * @var \Magento\Customer\Api\CustomerRepositoryInterface
- */
- private $customerRepository;
- /**
- * @var \Magento\Framework\Api\DataObjectHelper
- */
- private $dataObjectHelper;
- /**
- * @var \Magento\Customer\Api\Data\AddressInterfaceFactory
- */
- private $addressDataFactory;
- /**
- * @var LoggerInterface
- */
- private $logger;
- /**
- * @var JsonFactory
- */
- private $resultJsonFactory;
- /**
- * @param Action\Context $context
- * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
- * @param \Magento\Customer\Model\Metadata\FormFactory $formFactory
- * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
- * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
- * @param \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory
- * @param LoggerInterface $logger
- * @param JsonFactory $resultJsonFactory
- */
- public function __construct(
- Action\Context $context,
- \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
- \Magento\Customer\Model\Metadata\FormFactory $formFactory,
- \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
- \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
- \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory,
- LoggerInterface $logger,
- JsonFactory $resultJsonFactory
- ) {
- parent::__construct($context);
- $this->addressRepository = $addressRepository;
- $this->formFactory = $formFactory;
- $this->customerRepository = $customerRepository;
- $this->dataObjectHelper = $dataObjectHelper;
- $this->addressDataFactory = $addressDataFactory;
- $this->logger = $logger;
- $this->resultJsonFactory = $resultJsonFactory;
- }
- /**
- * Save customer address action
- *
- * @return Json
- */
- public function execute(): Json
- {
- $customerId = $this->getRequest()->getParam('parent_id', false);
- $addressId = $this->getRequest()->getParam('entity_id', false);
- $error = false;
- try {
- /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
- $customer = $this->customerRepository->getById($customerId);
- $addressForm = $this->formFactory->create(
- 'customer_address',
- 'adminhtml_customer_address',
- [],
- false,
- false
- );
- $addressData = $addressForm->extractData($this->getRequest());
- $addressData = $addressForm->compactData($addressData);
- $addressData['region'] = [
- 'region' => $addressData['region'] ?? null,
- 'region_id' => $addressData['region_id'] ?? null,
- ];
- $addressToSave = $this->addressDataFactory->create();
- $this->dataObjectHelper->populateWithArray(
- $addressToSave,
- $addressData,
- \Magento\Customer\Api\Data\AddressInterface::class
- );
- $addressToSave->setCustomerId($customer->getId());
- $addressToSave->setIsDefaultBilling(
- (bool)$this->getRequest()->getParam('default_billing', false)
- );
- $addressToSave->setIsDefaultShipping(
- (bool)$this->getRequest()->getParam('default_shipping', false)
- );
- if ($addressId) {
- $addressToSave->setId($addressId);
- $message = __('Customer address has been updated.');
- } else {
- $addressToSave->setId(null);
- $message = __('New customer address has been added.');
- }
- $savedAddress = $this->addressRepository->save($addressToSave);
- $addressId = $savedAddress->getId();
- } catch (NoSuchEntityException $e) {
- $this->logger->critical($e);
- $error = true;
- $message = __('There is no customer with such id.');
- } catch (LocalizedException $e) {
- $error = true;
- $message = __($e->getMessage());
- $this->logger->critical($e);
- } catch (\Exception $e) {
- $error = true;
- $message = __('We can\'t change customer address right now.');
- $this->logger->critical($e);
- }
- $addressId = empty($addressId) ? null : $addressId;
- $resultJson = $this->resultJsonFactory->create();
- $resultJson->setData(
- [
- 'message' => $message,
- 'error' => $error,
- 'data' => [
- 'entity_id' => $addressId
- ]
- ]
- );
- return $resultJson;
- }
- }
|