123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Block\Address;
- use Magento\Customer\Api\AddressRepositoryInterface;
- use Magento\Customer\Model\Address\Mapper;
- use Magento\Customer\Block\Address\Grid as AddressesGrid;
- /**
- * Customer address book block
- *
- * @api
- * @author Magento Core Team <core@magentocommerce.com>
- * @since 100.0.2
- */
- class Book extends \Magento\Framework\View\Element\Template
- {
- /**
- * @var \Magento\Customer\Helper\Session\CurrentCustomer
- */
- protected $currentCustomer;
- /**
- * @var \Magento\Customer\Api\CustomerRepositoryInterface
- */
- protected $customerRepository;
- /**
- * @var AddressRepositoryInterface
- */
- protected $addressRepository;
- /**
- * @var \Magento\Customer\Model\Address\Config
- */
- protected $_addressConfig;
- /**
- * @var Mapper
- */
- protected $addressMapper;
- /**
- * @var AddressesGrid
- */
- private $addressesGrid;
- /**
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param CustomerRepositoryInterface|null $customerRepository
- * @param AddressRepositoryInterface $addressRepository
- * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
- * @param \Magento\Customer\Model\Address\Config $addressConfig
- * @param Mapper $addressMapper
- * @param array $data
- * @param AddressesGrid|null $addressesGrid
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function __construct(
- \Magento\Framework\View\Element\Template\Context $context,
- \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository = null,
- AddressRepositoryInterface $addressRepository,
- \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
- \Magento\Customer\Model\Address\Config $addressConfig,
- Mapper $addressMapper,
- array $data = [],
- Grid $addressesGrid = null
- ) {
- $this->currentCustomer = $currentCustomer;
- $this->addressRepository = $addressRepository;
- $this->_addressConfig = $addressConfig;
- $this->addressMapper = $addressMapper;
- $this->addressesGrid = $addressesGrid ?: \Magento\Framework\App\ObjectManager::getInstance()
- ->get(AddressesGrid::class);
- parent::__construct($context, $data);
- }
- /**
- * Prepare the Address Book section layout
- *
- * @return $this
- */
- protected function _prepareLayout()
- {
- $this->pageConfig->getTitle()->set(__('Address Book'));
- return parent::_prepareLayout();
- }
- /**
- * Generate and return "New Address" URL
- *
- * @return string
- * @deprecated 102.0.1 not used in this block
- * @see \Magento\Customer\Block\Address\Grid::getAddAddressUrl
- */
- public function getAddAddressUrl()
- {
- return $this->addressesGrid->getAddAddressUrl();
- }
- /**
- * Generate and return "Back" URL
- *
- * @return string
- */
- public function getBackUrl()
- {
- if ($this->getRefererUrl()) {
- return $this->getRefererUrl();
- }
- return $this->getUrl('customer/account/', ['_secure' => true]);
- }
- /**
- * Generate and return "Delete" URL
- *
- * @return string
- * @deprecated 102.0.1 not used in this block
- * @see \Magento\Customer\Block\Address\Grid::getDeleteUrl
- */
- public function getDeleteUrl()
- {
- return $this->addressesGrid->getDeleteUrl();
- }
- /**
- * Generate and return "Edit Address" URL.
- *
- * Address ID passed in parameters
- *
- * @param int $addressId
- * @return string
- * @deprecated 102.0.1 not used in this block
- * @see \Magento\Customer\Block\Address\Grid::getAddressEditUrl
- */
- public function getAddressEditUrl($addressId)
- {
- return $this->addressesGrid->getAddressEditUrl($addressId);
- }
- /**
- * Determines is the address primary (billing or shipping)
- *
- * @return bool
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function hasPrimaryAddress()
- {
- return $this->getDefaultBilling() || $this->getDefaultShipping();
- }
- /**
- * Get current additional customer addresses
- *
- * Will return array of address interfaces if customer have additional addresses and false in other case.
- *
- * @return \Magento\Customer\Api\Data\AddressInterface[]|bool
- * @throws \Magento\Framework\Exception\LocalizedException
- * @deprecated 102.0.1 not used in this block
- * @see \Magento\Customer\Block\Address\Grid::getAdditionalAddresses
- */
- public function getAdditionalAddresses()
- {
- try {
- $addresses = $this->addressesGrid->getAdditionalAddresses();
- } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
- }
- return empty($addresses) ? false : $addresses;
- }
- /**
- * Render an address as HTML and return the result
- *
- * @param \Magento\Customer\Api\Data\AddressInterface $address
- * @return string
- */
- public function getAddressHtml(\Magento\Customer\Api\Data\AddressInterface $address = null)
- {
- if ($address !== null) {
- /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
- $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
- return $renderer->renderArray($this->addressMapper->toFlatArray($address));
- }
- return '';
- }
- /**
- * Get current customer
- *
- * @return \Magento\Customer\Api\Data\CustomerInterface|null
- */
- public function getCustomer()
- {
- $customer = null;
- try {
- $customer = $this->currentCustomer->getCustomer();
- } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
- }
- return $customer;
- }
- /**
- * Get customer's default billing address
- *
- * @return int|null
- */
- public function getDefaultBilling()
- {
- $customer = $this->getCustomer();
- if ($customer === null) {
- return null;
- } else {
- return $customer->getDefaultBilling();
- }
- }
- /**
- * Get customer address by ID
- *
- * @param int $addressId
- * @return \Magento\Customer\Api\Data\AddressInterface|null
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getAddressById($addressId)
- {
- try {
- return $this->addressRepository->getById($addressId);
- } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
- return null;
- }
- }
- /**
- * Get customer's default shipping address
- *
- * @return int|null
- */
- public function getDefaultShipping()
- {
- $customer = $this->getCustomer();
- if ($customer === null) {
- return null;
- } else {
- return $customer->getDefaultShipping();
- }
- }
- }
|