123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Block\Address;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Exception\NoSuchEntityException;
- /**
- * Customer address edit block
- *
- * @api
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @since 100.0.2
- */
- class Edit extends \Magento\Directory\Block\Data
- {
- /**
- * @var \Magento\Customer\Api\Data\AddressInterface|null
- */
- protected $_address = null;
- /**
- * @var \Magento\Customer\Model\Session
- */
- protected $_customerSession;
- /**
- * @var \Magento\Customer\Api\AddressRepositoryInterface
- */
- protected $_addressRepository;
- /**
- * @var \Magento\Customer\Api\Data\AddressInterfaceFactory
- */
- protected $addressDataFactory;
- /**
- * @var \Magento\Customer\Helper\Session\CurrentCustomer
- */
- protected $currentCustomer;
- /**
- * @var \Magento\Framework\Api\DataObjectHelper
- */
- protected $dataObjectHelper;
- /**
- * @var \Magento\Customer\Api\AddressMetadataInterface
- */
- private $addressMetadata;
- /**
- * Constructor
- *
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param \Magento\Directory\Helper\Data $directoryHelper
- * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
- * @param \Magento\Framework\App\Cache\Type\Config $configCacheType
- * @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
- * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory
- * @param \Magento\Customer\Model\Session $customerSession
- * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
- * @param \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory
- * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
- * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
- * @param array $data
- * @param \Magento\Customer\Api\AddressMetadataInterface|null $addressMetadata
- *
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- \Magento\Framework\View\Element\Template\Context $context,
- \Magento\Directory\Helper\Data $directoryHelper,
- \Magento\Framework\Json\EncoderInterface $jsonEncoder,
- \Magento\Framework\App\Cache\Type\Config $configCacheType,
- \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory,
- \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
- \Magento\Customer\Model\Session $customerSession,
- \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
- \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory,
- \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
- \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
- array $data = [],
- \Magento\Customer\Api\AddressMetadataInterface $addressMetadata = null
- ) {
- $this->_customerSession = $customerSession;
- $this->_addressRepository = $addressRepository;
- $this->addressDataFactory = $addressDataFactory;
- $this->currentCustomer = $currentCustomer;
- $this->dataObjectHelper = $dataObjectHelper;
- $this->addressMetadata = $addressMetadata;
- parent::__construct(
- $context,
- $directoryHelper,
- $jsonEncoder,
- $configCacheType,
- $regionCollectionFactory,
- $countryCollectionFactory,
- $data
- );
- }
- /**
- * Prepare the layout of the address edit block.
- *
- * @return $this
- */
- protected function _prepareLayout()
- {
- parent::_prepareLayout();
- $this->initAddressObject();
- $this->pageConfig->getTitle()->set($this->getTitle());
- if ($postedData = $this->_customerSession->getAddressFormData(true)) {
- $postedData['region'] = [
- 'region_id' => isset($postedData['region_id']) ? $postedData['region_id'] : null,
- 'region' => $postedData['region'],
- ];
- $this->dataObjectHelper->populateWithArray(
- $this->_address,
- $postedData,
- \Magento\Customer\Api\Data\AddressInterface::class
- );
- }
- $this->precheckRequiredAttributes();
- return $this;
- }
- /**
- * Initialize address object.
- *
- * @return void
- */
- private function initAddressObject()
- {
- // Init address object
- if ($addressId = $this->getRequest()->getParam('id')) {
- try {
- $this->_address = $this->_addressRepository->getById($addressId);
- if ($this->_address->getCustomerId() != $this->_customerSession->getCustomerId()) {
- $this->_address = null;
- }
- } catch (NoSuchEntityException $e) {
- $this->_address = null;
- }
- }
- if ($this->_address === null || !$this->_address->getId()) {
- $this->_address = $this->addressDataFactory->create();
- $customer = $this->getCustomer();
- $this->_address->setPrefix($customer->getPrefix());
- $this->_address->setFirstname($customer->getFirstname());
- $this->_address->setMiddlename($customer->getMiddlename());
- $this->_address->setLastname($customer->getLastname());
- $this->_address->setSuffix($customer->getSuffix());
- }
- }
- /**
- * Precheck attributes that may be required in attribute configuration.
- *
- * @return void
- */
- private function precheckRequiredAttributes()
- {
- $precheckAttributes = $this->getData('check_attributes_on_render');
- $requiredAttributesPrechecked = [];
- if (!empty($precheckAttributes) && is_array($precheckAttributes)) {
- foreach ($precheckAttributes as $attributeCode) {
- $attributeMetadata = $this->addressMetadata->getAttributeMetadata($attributeCode);
- if ($attributeMetadata && $attributeMetadata->isRequired()) {
- $requiredAttributesPrechecked[$attributeCode] = $attributeCode;
- }
- }
- }
- $this->setData('required_attributes_prechecked', $requiredAttributesPrechecked);
- }
- /**
- * Generate name block html.
- *
- * @return string
- */
- public function getNameBlockHtml()
- {
- $nameBlock = $this->getLayout()
- ->createBlock(\Magento\Customer\Block\Widget\Name::class)
- ->setObject($this->getAddress());
- return $nameBlock->toHtml();
- }
- /**
- * Return the title, either editing an existing address, or adding a new one.
- *
- * @return string
- */
- public function getTitle()
- {
- if ($title = $this->getData('title')) {
- return $title;
- }
- if ($this->getAddress()->getId()) {
- $title = __('Edit Address');
- } else {
- $title = __('Add New Address');
- }
- return $title;
- }
- /**
- * Return the Url to go back.
- *
- * @return string
- */
- public function getBackUrl()
- {
- if ($this->getData('back_url')) {
- return $this->getData('back_url');
- }
- if ($this->getCustomerAddressCount()) {
- return $this->getUrl('customer/address');
- } else {
- return $this->getUrl('customer/account/');
- }
- }
- /**
- * Return the Url for saving.
- *
- * @return string
- */
- public function getSaveUrl()
- {
- return $this->_urlBuilder->getUrl(
- 'customer/address/formPost',
- ['_secure' => true, 'id' => $this->getAddress()->getId()]
- );
- }
- /**
- * Return the associated address.
- *
- * @return \Magento\Customer\Api\Data\AddressInterface
- */
- public function getAddress()
- {
- return $this->_address;
- }
- /**
- * Return the specified numbered street line.
- *
- * @param int $lineNumber
- * @return string
- */
- public function getStreetLine($lineNumber)
- {
- $street = $this->_address->getStreet();
- return isset($street[$lineNumber - 1]) ? $street[$lineNumber - 1] : '';
- }
- /**
- * Return the country Id.
- *
- * @return int|null|string
- */
- public function getCountryId()
- {
- if ($countryId = $this->getAddress()->getCountryId()) {
- return $countryId;
- }
- return parent::getCountryId();
- }
- /**
- * Return the name of the region for the address being edited.
- *
- * @return string region name
- */
- public function getRegion()
- {
- $region = $this->getAddress()->getRegion();
- return $region === null ? '' : $region->getRegion();
- }
- /**
- * Return the id of the region being edited.
- *
- * @return int region id
- */
- public function getRegionId()
- {
- $region = $this->getAddress()->getRegion();
- return $region === null ? 0 : $region->getRegionId();
- }
- /**
- * Retrieve the number of addresses associated with the customer given a customer Id.
- *
- * @return int
- */
- public function getCustomerAddressCount()
- {
- return count($this->getCustomer()->getAddresses());
- }
- /**
- * Determine if the address can be set as the default billing address.
- *
- * @return bool|int
- */
- public function canSetAsDefaultBilling()
- {
- if (!$this->getAddress()->getId()) {
- return $this->getCustomerAddressCount();
- }
- return !$this->isDefaultBilling();
- }
- /**
- * Determine if the address can be set as the default shipping address.
- *
- * @return bool|int
- */
- public function canSetAsDefaultShipping()
- {
- if (!$this->getAddress()->getId()) {
- return $this->getCustomerAddressCount();
- }
- return !$this->isDefaultShipping();
- }
- /**
- * Is the address the default billing address?
- *
- * @return bool
- */
- public function isDefaultBilling()
- {
- return (bool)$this->getAddress()->isDefaultBilling();
- }
- /**
- * Is the address the default shipping address?
- *
- * @return bool
- */
- public function isDefaultShipping()
- {
- return (bool)$this->getAddress()->isDefaultShipping();
- }
- /**
- * Retrieve the Customer Data using the customer Id from the customer session.
- *
- * @return \Magento\Customer\Api\Data\CustomerInterface
- */
- public function getCustomer()
- {
- return $this->currentCustomer->getCustomer();
- }
- /**
- * Return back button Url, either to customer address or account.
- *
- * @return string
- */
- public function getBackButtonUrl()
- {
- if ($this->getCustomerAddressCount()) {
- return $this->getUrl('customer/address');
- } else {
- return $this->getUrl('customer/account/');
- }
- }
- /**
- * Get config value.
- *
- * @param string $path
- * @return string|null
- */
- public function getConfig($path)
- {
- return $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
- }
- }
|