Edit.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Address;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. /**
  10. * Customer address edit block
  11. *
  12. * @api
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. * @since 100.0.2
  15. */
  16. class Edit extends \Magento\Directory\Block\Data
  17. {
  18. /**
  19. * @var \Magento\Customer\Api\Data\AddressInterface|null
  20. */
  21. protected $_address = null;
  22. /**
  23. * @var \Magento\Customer\Model\Session
  24. */
  25. protected $_customerSession;
  26. /**
  27. * @var \Magento\Customer\Api\AddressRepositoryInterface
  28. */
  29. protected $_addressRepository;
  30. /**
  31. * @var \Magento\Customer\Api\Data\AddressInterfaceFactory
  32. */
  33. protected $addressDataFactory;
  34. /**
  35. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  36. */
  37. protected $currentCustomer;
  38. /**
  39. * @var \Magento\Framework\Api\DataObjectHelper
  40. */
  41. protected $dataObjectHelper;
  42. /**
  43. * @var \Magento\Customer\Api\AddressMetadataInterface
  44. */
  45. private $addressMetadata;
  46. /**
  47. * Constructor
  48. *
  49. * @param \Magento\Framework\View\Element\Template\Context $context
  50. * @param \Magento\Directory\Helper\Data $directoryHelper
  51. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  52. * @param \Magento\Framework\App\Cache\Type\Config $configCacheType
  53. * @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
  54. * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory
  55. * @param \Magento\Customer\Model\Session $customerSession
  56. * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  57. * @param \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory
  58. * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
  59. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  60. * @param array $data
  61. * @param \Magento\Customer\Api\AddressMetadataInterface|null $addressMetadata
  62. *
  63. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  64. */
  65. public function __construct(
  66. \Magento\Framework\View\Element\Template\Context $context,
  67. \Magento\Directory\Helper\Data $directoryHelper,
  68. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  69. \Magento\Framework\App\Cache\Type\Config $configCacheType,
  70. \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory,
  71. \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
  72. \Magento\Customer\Model\Session $customerSession,
  73. \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
  74. \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory,
  75. \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
  76. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  77. array $data = [],
  78. \Magento\Customer\Api\AddressMetadataInterface $addressMetadata = null
  79. ) {
  80. $this->_customerSession = $customerSession;
  81. $this->_addressRepository = $addressRepository;
  82. $this->addressDataFactory = $addressDataFactory;
  83. $this->currentCustomer = $currentCustomer;
  84. $this->dataObjectHelper = $dataObjectHelper;
  85. $this->addressMetadata = $addressMetadata;
  86. parent::__construct(
  87. $context,
  88. $directoryHelper,
  89. $jsonEncoder,
  90. $configCacheType,
  91. $regionCollectionFactory,
  92. $countryCollectionFactory,
  93. $data
  94. );
  95. }
  96. /**
  97. * Prepare the layout of the address edit block.
  98. *
  99. * @return $this
  100. */
  101. protected function _prepareLayout()
  102. {
  103. parent::_prepareLayout();
  104. $this->initAddressObject();
  105. $this->pageConfig->getTitle()->set($this->getTitle());
  106. if ($postedData = $this->_customerSession->getAddressFormData(true)) {
  107. $postedData['region'] = [
  108. 'region_id' => isset($postedData['region_id']) ? $postedData['region_id'] : null,
  109. 'region' => $postedData['region'],
  110. ];
  111. $this->dataObjectHelper->populateWithArray(
  112. $this->_address,
  113. $postedData,
  114. \Magento\Customer\Api\Data\AddressInterface::class
  115. );
  116. }
  117. $this->precheckRequiredAttributes();
  118. return $this;
  119. }
  120. /**
  121. * Initialize address object.
  122. *
  123. * @return void
  124. */
  125. private function initAddressObject()
  126. {
  127. // Init address object
  128. if ($addressId = $this->getRequest()->getParam('id')) {
  129. try {
  130. $this->_address = $this->_addressRepository->getById($addressId);
  131. if ($this->_address->getCustomerId() != $this->_customerSession->getCustomerId()) {
  132. $this->_address = null;
  133. }
  134. } catch (NoSuchEntityException $e) {
  135. $this->_address = null;
  136. }
  137. }
  138. if ($this->_address === null || !$this->_address->getId()) {
  139. $this->_address = $this->addressDataFactory->create();
  140. $customer = $this->getCustomer();
  141. $this->_address->setPrefix($customer->getPrefix());
  142. $this->_address->setFirstname($customer->getFirstname());
  143. $this->_address->setMiddlename($customer->getMiddlename());
  144. $this->_address->setLastname($customer->getLastname());
  145. $this->_address->setSuffix($customer->getSuffix());
  146. }
  147. }
  148. /**
  149. * Precheck attributes that may be required in attribute configuration.
  150. *
  151. * @return void
  152. */
  153. private function precheckRequiredAttributes()
  154. {
  155. $precheckAttributes = $this->getData('check_attributes_on_render');
  156. $requiredAttributesPrechecked = [];
  157. if (!empty($precheckAttributes) && is_array($precheckAttributes)) {
  158. foreach ($precheckAttributes as $attributeCode) {
  159. $attributeMetadata = $this->addressMetadata->getAttributeMetadata($attributeCode);
  160. if ($attributeMetadata && $attributeMetadata->isRequired()) {
  161. $requiredAttributesPrechecked[$attributeCode] = $attributeCode;
  162. }
  163. }
  164. }
  165. $this->setData('required_attributes_prechecked', $requiredAttributesPrechecked);
  166. }
  167. /**
  168. * Generate name block html.
  169. *
  170. * @return string
  171. */
  172. public function getNameBlockHtml()
  173. {
  174. $nameBlock = $this->getLayout()
  175. ->createBlock(\Magento\Customer\Block\Widget\Name::class)
  176. ->setObject($this->getAddress());
  177. return $nameBlock->toHtml();
  178. }
  179. /**
  180. * Return the title, either editing an existing address, or adding a new one.
  181. *
  182. * @return string
  183. */
  184. public function getTitle()
  185. {
  186. if ($title = $this->getData('title')) {
  187. return $title;
  188. }
  189. if ($this->getAddress()->getId()) {
  190. $title = __('Edit Address');
  191. } else {
  192. $title = __('Add New Address');
  193. }
  194. return $title;
  195. }
  196. /**
  197. * Return the Url to go back.
  198. *
  199. * @return string
  200. */
  201. public function getBackUrl()
  202. {
  203. if ($this->getData('back_url')) {
  204. return $this->getData('back_url');
  205. }
  206. if ($this->getCustomerAddressCount()) {
  207. return $this->getUrl('customer/address');
  208. } else {
  209. return $this->getUrl('customer/account/');
  210. }
  211. }
  212. /**
  213. * Return the Url for saving.
  214. *
  215. * @return string
  216. */
  217. public function getSaveUrl()
  218. {
  219. return $this->_urlBuilder->getUrl(
  220. 'customer/address/formPost',
  221. ['_secure' => true, 'id' => $this->getAddress()->getId()]
  222. );
  223. }
  224. /**
  225. * Return the associated address.
  226. *
  227. * @return \Magento\Customer\Api\Data\AddressInterface
  228. */
  229. public function getAddress()
  230. {
  231. return $this->_address;
  232. }
  233. /**
  234. * Return the specified numbered street line.
  235. *
  236. * @param int $lineNumber
  237. * @return string
  238. */
  239. public function getStreetLine($lineNumber)
  240. {
  241. $street = $this->_address->getStreet();
  242. return isset($street[$lineNumber - 1]) ? $street[$lineNumber - 1] : '';
  243. }
  244. /**
  245. * Return the country Id.
  246. *
  247. * @return int|null|string
  248. */
  249. public function getCountryId()
  250. {
  251. if ($countryId = $this->getAddress()->getCountryId()) {
  252. return $countryId;
  253. }
  254. return parent::getCountryId();
  255. }
  256. /**
  257. * Return the name of the region for the address being edited.
  258. *
  259. * @return string region name
  260. */
  261. public function getRegion()
  262. {
  263. $region = $this->getAddress()->getRegion();
  264. return $region === null ? '' : $region->getRegion();
  265. }
  266. /**
  267. * Return the id of the region being edited.
  268. *
  269. * @return int region id
  270. */
  271. public function getRegionId()
  272. {
  273. $region = $this->getAddress()->getRegion();
  274. return $region === null ? 0 : $region->getRegionId();
  275. }
  276. /**
  277. * Retrieve the number of addresses associated with the customer given a customer Id.
  278. *
  279. * @return int
  280. */
  281. public function getCustomerAddressCount()
  282. {
  283. return count($this->getCustomer()->getAddresses());
  284. }
  285. /**
  286. * Determine if the address can be set as the default billing address.
  287. *
  288. * @return bool|int
  289. */
  290. public function canSetAsDefaultBilling()
  291. {
  292. if (!$this->getAddress()->getId()) {
  293. return $this->getCustomerAddressCount();
  294. }
  295. return !$this->isDefaultBilling();
  296. }
  297. /**
  298. * Determine if the address can be set as the default shipping address.
  299. *
  300. * @return bool|int
  301. */
  302. public function canSetAsDefaultShipping()
  303. {
  304. if (!$this->getAddress()->getId()) {
  305. return $this->getCustomerAddressCount();
  306. }
  307. return !$this->isDefaultShipping();
  308. }
  309. /**
  310. * Is the address the default billing address?
  311. *
  312. * @return bool
  313. */
  314. public function isDefaultBilling()
  315. {
  316. return (bool)$this->getAddress()->isDefaultBilling();
  317. }
  318. /**
  319. * Is the address the default shipping address?
  320. *
  321. * @return bool
  322. */
  323. public function isDefaultShipping()
  324. {
  325. return (bool)$this->getAddress()->isDefaultShipping();
  326. }
  327. /**
  328. * Retrieve the Customer Data using the customer Id from the customer session.
  329. *
  330. * @return \Magento\Customer\Api\Data\CustomerInterface
  331. */
  332. public function getCustomer()
  333. {
  334. return $this->currentCustomer->getCustomer();
  335. }
  336. /**
  337. * Return back button Url, either to customer address or account.
  338. *
  339. * @return string
  340. */
  341. public function getBackButtonUrl()
  342. {
  343. if ($this->getCustomerAddressCount()) {
  344. return $this->getUrl('customer/address');
  345. } else {
  346. return $this->getUrl('customer/account/');
  347. }
  348. }
  349. /**
  350. * Get config value.
  351. *
  352. * @param string $path
  353. * @return string|null
  354. */
  355. public function getConfig($path)
  356. {
  357. return $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
  358. }
  359. }