123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Controller\Address;
- use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
- use Magento\Customer\Api\AddressRepositoryInterface;
- use Magento\Customer\Api\Data\AddressInterfaceFactory;
- use Magento\Customer\Api\Data\RegionInterface;
- use Magento\Customer\Api\Data\RegionInterfaceFactory;
- use Magento\Customer\Model\Address\Mapper;
- use Magento\Customer\Model\Metadata\FormFactory;
- use Magento\Customer\Model\Session;
- use Magento\Directory\Helper\Data as HelperData;
- use Magento\Directory\Model\RegionFactory;
- use Magento\Framework\Api\DataObjectHelper;
- use Magento\Framework\App\Action\Context;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\Controller\Result\ForwardFactory;
- use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
- use Magento\Framework\Exception\InputException;
- use Magento\Framework\Reflection\DataObjectProcessor;
- use Magento\Framework\View\Result\PageFactory;
- /**
- * Customer Address Form Post Controller
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class FormPost extends \Magento\Customer\Controller\Address implements HttpPostActionInterface
- {
- /**
- * @var RegionFactory
- */
- protected $regionFactory;
- /**
- * @var HelperData
- */
- protected $helperData;
- /**
- * @var Mapper
- */
- private $customerAddressMapper;
- /**
- * @param Context $context
- * @param Session $customerSession
- * @param FormKeyValidator $formKeyValidator
- * @param FormFactory $formFactory
- * @param AddressRepositoryInterface $addressRepository
- * @param AddressInterfaceFactory $addressDataFactory
- * @param RegionInterfaceFactory $regionDataFactory
- * @param DataObjectProcessor $dataProcessor
- * @param DataObjectHelper $dataObjectHelper
- * @param ForwardFactory $resultForwardFactory
- * @param PageFactory $resultPageFactory
- * @param RegionFactory $regionFactory
- * @param HelperData $helperData
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- Context $context,
- Session $customerSession,
- FormKeyValidator $formKeyValidator,
- FormFactory $formFactory,
- AddressRepositoryInterface $addressRepository,
- AddressInterfaceFactory $addressDataFactory,
- RegionInterfaceFactory $regionDataFactory,
- DataObjectProcessor $dataProcessor,
- DataObjectHelper $dataObjectHelper,
- ForwardFactory $resultForwardFactory,
- PageFactory $resultPageFactory,
- RegionFactory $regionFactory,
- HelperData $helperData
- ) {
- $this->regionFactory = $regionFactory;
- $this->helperData = $helperData;
- parent::__construct(
- $context,
- $customerSession,
- $formKeyValidator,
- $formFactory,
- $addressRepository,
- $addressDataFactory,
- $regionDataFactory,
- $dataProcessor,
- $dataObjectHelper,
- $resultForwardFactory,
- $resultPageFactory
- );
- }
- /**
- * Extract address from request
- *
- * @return \Magento\Customer\Api\Data\AddressInterface
- */
- protected function _extractAddress()
- {
- $existingAddressData = $this->getExistingAddressData();
- /** @var \Magento\Customer\Model\Metadata\Form $addressForm */
- $addressForm = $this->_formFactory->create(
- 'customer_address',
- 'customer_address_edit',
- $existingAddressData
- );
- $addressData = $addressForm->extractData($this->getRequest());
- $attributeValues = $addressForm->compactData($addressData);
- $this->updateRegionData($attributeValues);
- $addressDataObject = $this->addressDataFactory->create();
- $this->dataObjectHelper->populateWithArray(
- $addressDataObject,
- array_merge($existingAddressData, $attributeValues),
- \Magento\Customer\Api\Data\AddressInterface::class
- );
- $addressDataObject->setCustomerId($this->_getSession()->getCustomerId())
- ->setIsDefaultBilling(
- $this->getRequest()->getParam(
- 'default_billing',
- isset($existingAddressData['default_billing']) ? $existingAddressData['default_billing'] : false
- )
- )
- ->setIsDefaultShipping(
- $this->getRequest()->getParam(
- 'default_shipping',
- isset($existingAddressData['default_shipping']) ? $existingAddressData['default_shipping'] : false
- )
- );
- return $addressDataObject;
- }
- /**
- * Retrieve existing address data
- *
- * @return array
- * @throws \Exception
- */
- protected function getExistingAddressData()
- {
- $existingAddressData = [];
- if ($addressId = $this->getRequest()->getParam('id')) {
- $existingAddress = $this->_addressRepository->getById($addressId);
- if ($existingAddress->getCustomerId() !== $this->_getSession()->getCustomerId()) {
- throw new \Exception();
- }
- $existingAddressData = $this->getCustomerAddressMapper()->toFlatArray($existingAddress);
- }
- return $existingAddressData;
- }
- /**
- * Update region data
- *
- * @param array $attributeValues
- * @return void
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- protected function updateRegionData(&$attributeValues)
- {
- if (!empty($attributeValues['region_id'])) {
- $newRegion = $this->regionFactory->create()->load($attributeValues['region_id']);
- $attributeValues['region_code'] = $newRegion->getCode();
- $attributeValues['region'] = $newRegion->getDefaultName();
- }
- $regionData = [
- RegionInterface::REGION_ID => !empty($attributeValues['region_id']) ? $attributeValues['region_id'] : null,
- RegionInterface::REGION => !empty($attributeValues['region']) ? $attributeValues['region'] : null,
- RegionInterface::REGION_CODE => !empty($attributeValues['region_code'])
- ? $attributeValues['region_code']
- : null,
- ];
- $region = $this->regionDataFactory->create();
- $this->dataObjectHelper->populateWithArray(
- $region,
- $regionData,
- \Magento\Customer\Api\Data\RegionInterface::class
- );
- $attributeValues['region'] = $region;
- }
- /**
- * Process address form save
- *
- * @return \Magento\Framework\Controller\Result\Redirect
- */
- public function execute()
- {
- $redirectUrl = null;
- if (!$this->_formKeyValidator->validate($this->getRequest())) {
- return $this->resultRedirectFactory->create()->setPath('*/*/');
- }
- if (!$this->getRequest()->isPost()) {
- $this->_getSession()->setAddressFormData($this->getRequest()->getPostValue());
- return $this->resultRedirectFactory->create()->setUrl(
- $this->_redirect->error($this->_buildUrl('*/*/edit'))
- );
- }
- try {
- $address = $this->_extractAddress();
- $this->_addressRepository->save($address);
- $this->messageManager->addSuccessMessage(__('You saved the address.'));
- $url = $this->_buildUrl('*/*/index', ['_secure' => true]);
- return $this->resultRedirectFactory->create()->setUrl($this->_redirect->success($url));
- } catch (InputException $e) {
- $this->messageManager->addErrorMessage($e->getMessage());
- foreach ($e->getErrors() as $error) {
- $this->messageManager->addErrorMessage($error->getMessage());
- }
- } catch (\Exception $e) {
- $redirectUrl = $this->_buildUrl('*/*/index');
- $this->messageManager->addExceptionMessage($e, __('We can\'t save the address.'));
- }
- $url = $redirectUrl;
- if (!$redirectUrl) {
- $this->_getSession()->setAddressFormData($this->getRequest()->getPostValue());
- $url = $this->_buildUrl('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
- }
- return $this->resultRedirectFactory->create()->setUrl($this->_redirect->error($url));
- }
- /**
- * Get Customer Address Mapper instance
- *
- * @return Mapper
- *
- * @deprecated 100.1.3
- */
- private function getCustomerAddressMapper()
- {
- if ($this->customerAddressMapper === null) {
- $this->customerAddressMapper = ObjectManager::getInstance()->get(
- \Magento\Customer\Model\Address\Mapper::class
- );
- }
- return $this->customerAddressMapper;
- }
- }
|