Address.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Helper;
  7. use Magento\Customer\Api\AddressMetadataInterface;
  8. use Magento\Customer\Api\CustomerMetadataInterface;
  9. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  10. use Magento\Directory\Model\Country\Format;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Magento\Framework\View\Element\BlockInterface;
  13. use Magento\Store\Model\ScopeInterface;
  14. /**
  15. * Customer address helper
  16. *
  17. * @api
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. * @since 100.0.2
  20. */
  21. class Address extends \Magento\Framework\App\Helper\AbstractHelper
  22. {
  23. /**
  24. * VAT Validation parameters XML paths
  25. */
  26. const XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT = 'customer/create_account/viv_disable_auto_group_assign_default';
  27. const XML_PATH_VIV_ON_EACH_TRANSACTION = 'customer/create_account/viv_on_each_transaction';
  28. const XML_PATH_VAT_VALIDATION_ENABLED = 'customer/create_account/auto_group_assign';
  29. const XML_PATH_VIV_TAX_CALCULATION_ADDRESS_TYPE = 'customer/create_account/tax_calculation_address_type';
  30. const XML_PATH_VAT_FRONTEND_VISIBILITY = 'customer/create_account/vat_frontend_visibility';
  31. /**
  32. * Possible customer address types
  33. */
  34. const TYPE_BILLING = 'billing';
  35. const TYPE_SHIPPING = 'shipping';
  36. /**
  37. * Array of Customer Address Attributes
  38. *
  39. * @var AttributeMetadataInterface[]
  40. */
  41. protected $_attributes;
  42. /**
  43. * Customer address config node per website
  44. *
  45. * @var array
  46. */
  47. protected $_config = [];
  48. /**
  49. * Customer Number of Lines in a Street Address per website
  50. *
  51. * @var array
  52. */
  53. protected $_streetLines = [];
  54. /**
  55. * @var array
  56. */
  57. protected $_formatTemplate = [];
  58. /**
  59. * @var \Magento\Framework\View\Element\BlockFactory
  60. */
  61. protected $_blockFactory;
  62. /**
  63. * @var \Magento\Store\Model\StoreManagerInterface
  64. */
  65. protected $_storeManager;
  66. /**
  67. * @var CustomerMetadataInterface
  68. *
  69. * @deprecated 101.0.0
  70. */
  71. protected $_customerMetadataService;
  72. /**
  73. * @var \Magento\Customer\Api\AddressMetadataInterface
  74. */
  75. protected $_addressMetadataService;
  76. /**
  77. * @var \Magento\Customer\Model\Address\Config
  78. */
  79. protected $_addressConfig;
  80. /**
  81. * Address constructor.
  82. *
  83. * @param \Magento\Framework\App\Helper\Context $context
  84. * @param \Magento\Framework\View\Element\BlockFactory $blockFactory
  85. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  86. * @param CustomerMetadataInterface $customerMetadataService
  87. * @param AddressMetadataInterface $addressMetadataService
  88. * @param \Magento\Customer\Model\Address\Config $addressConfig
  89. */
  90. public function __construct(
  91. \Magento\Framework\App\Helper\Context $context,
  92. \Magento\Framework\View\Element\BlockFactory $blockFactory,
  93. \Magento\Store\Model\StoreManagerInterface $storeManager,
  94. CustomerMetadataInterface $customerMetadataService,
  95. AddressMetadataInterface $addressMetadataService,
  96. \Magento\Customer\Model\Address\Config $addressConfig
  97. ) {
  98. $this->_blockFactory = $blockFactory;
  99. $this->_storeManager = $storeManager;
  100. $this->_customerMetadataService = $customerMetadataService;
  101. $this->_addressMetadataService = $addressMetadataService;
  102. $this->_addressConfig = $addressConfig;
  103. parent::__construct($context);
  104. }
  105. /**
  106. * Addresses url
  107. *
  108. * @return void
  109. */
  110. public function getBookUrl()
  111. {
  112. }
  113. /**
  114. * Retrieve edit url.
  115. *
  116. * @return void
  117. */
  118. public function getEditUrl()
  119. {
  120. }
  121. /**
  122. * Retrieve delete url.
  123. *
  124. * @return void
  125. */
  126. public function getDeleteUrl()
  127. {
  128. }
  129. /**
  130. * Retrieve create url.
  131. *
  132. * @return void
  133. */
  134. public function getCreateUrl()
  135. {
  136. }
  137. /**
  138. * Retrieve block renderer.
  139. *
  140. * @param string $renderer
  141. * @return \Magento\Framework\View\Element\BlockInterface
  142. */
  143. public function getRenderer($renderer)
  144. {
  145. if (is_string($renderer) && $renderer) {
  146. return $this->_blockFactory->createBlock($renderer, []);
  147. } else {
  148. return $renderer;
  149. }
  150. }
  151. /**
  152. * Return customer address config value by key and store
  153. *
  154. * @param string $key
  155. * @param \Magento\Store\Model\Store|int|string $store
  156. *
  157. * @return string|null
  158. * @throws NoSuchEntityException
  159. */
  160. public function getConfig($key, $store = null)
  161. {
  162. $store = $this->_storeManager->getStore($store);
  163. $websiteId = $store->getWebsiteId();
  164. if (!isset($this->_config[$websiteId])) {
  165. $this->_config[$websiteId] = $this->scopeConfig->getValue(
  166. 'customer/address',
  167. ScopeInterface::SCOPE_STORE,
  168. $store
  169. );
  170. }
  171. return isset($this->_config[$websiteId][$key]) ? (string)$this->_config[$websiteId][$key] : null;
  172. }
  173. /**
  174. * Return Number of Lines in a Street Address for store
  175. *
  176. * @param \Magento\Store\Model\Store|int|string $store
  177. *
  178. * @return int
  179. * @throws NoSuchEntityException
  180. * @throws \Magento\Framework\Exception\LocalizedException
  181. */
  182. public function getStreetLines($store = null)
  183. {
  184. $websiteId = $this->_storeManager->getStore($store)->getWebsiteId();
  185. if (!isset($this->_streetLines[$websiteId])) {
  186. $attribute = $this->_addressMetadataService->getAttributeMetadata('street');
  187. $lines = $attribute->getMultilineCount();
  188. if ($lines <= 0) {
  189. $lines = 2;
  190. }
  191. $this->_streetLines[$websiteId] = min($lines, 20);
  192. }
  193. return $this->_streetLines[$websiteId];
  194. }
  195. /**
  196. * Retrieve address format.
  197. *
  198. * @param string $code
  199. * @return Format|string
  200. */
  201. public function getFormat($code)
  202. {
  203. $format = $this->_addressConfig->getFormatByCode($code);
  204. return $format->getRenderer() ? $format->getRenderer()->getFormatArray() : '';
  205. }
  206. /**
  207. * Retrieve renderer by code
  208. *
  209. * @param string $code
  210. * @return \Magento\Customer\Block\Address\Renderer\RendererInterface|null
  211. */
  212. public function getFormatTypeRenderer($code)
  213. {
  214. $formatType = $this->_addressConfig->getFormatByCode($code);
  215. if (!$formatType || !$formatType->getRenderer()) {
  216. return null;
  217. }
  218. return $formatType->getRenderer();
  219. }
  220. /**
  221. * Determine if specified address config value can be shown
  222. *
  223. * @param string $key
  224. *
  225. * @return bool
  226. * @throws NoSuchEntityException
  227. */
  228. public function canShowConfig($key)
  229. {
  230. return (bool)$this->getConfig($key);
  231. }
  232. /**
  233. * Get string with frontend validation classes for attribute
  234. *
  235. * @param string $attributeCode
  236. *
  237. * @return string
  238. *
  239. * @SuppressWarnings(PHPMD.NPathComplexity)
  240. * @throws \Magento\Framework\Exception\LocalizedException
  241. */
  242. public function getAttributeValidationClass($attributeCode)
  243. {
  244. $class = '';
  245. try {
  246. /** @var $attribute AttributeMetadataInterface */
  247. $attribute = isset($this->_attributes[$attributeCode])
  248. ? $this->_attributes[$attributeCode]
  249. : $this->_addressMetadataService->getAttributeMetadata($attributeCode);
  250. $class = $attribute ? $attribute->getFrontendClass() : '';
  251. } catch (NoSuchEntityException $e) {
  252. // the attribute does not exist so just return an empty string
  253. }
  254. return $class;
  255. }
  256. /**
  257. * Convert streets array to new street lines count
  258. * Examples of use:
  259. * $origStreets = array('street1', 'street2', 'street3', 'street4')
  260. * $toCount = 3
  261. * Result:
  262. * array('street1 street2', 'street3', 'street4')
  263. * $toCount = 2
  264. * Result:
  265. * array('street1 street2', 'street3 street4')
  266. *
  267. * @param string[] $origStreets
  268. * @param int $toCount
  269. * @return string[]
  270. */
  271. public function convertStreetLines($origStreets, $toCount)
  272. {
  273. $lines = [];
  274. if (!empty($origStreets) && $toCount > 0) {
  275. $countArgs = (int)floor(count($origStreets) / $toCount);
  276. $modulo = count($origStreets) % $toCount;
  277. $offset = 0;
  278. $neededLinesCount = 0;
  279. for ($i = 0; $i < $toCount; $i++) {
  280. $offset += $neededLinesCount;
  281. $neededLinesCount = $countArgs;
  282. if ($modulo > 0) {
  283. ++$neededLinesCount;
  284. --$modulo;
  285. }
  286. $values = array_slice($origStreets, $offset, $neededLinesCount);
  287. if (is_array($values)) {
  288. $lines[] = implode(' ', $values);
  289. }
  290. }
  291. }
  292. return $lines;
  293. }
  294. /**
  295. * Check whether VAT ID validation is enabled
  296. *
  297. * @param \Magento\Store\Model\Store|string|int $store
  298. * @return bool
  299. */
  300. public function isVatValidationEnabled($store = null)
  301. {
  302. return $this->scopeConfig->isSetFlag(
  303. self::XML_PATH_VAT_VALIDATION_ENABLED,
  304. ScopeInterface::SCOPE_STORE,
  305. $store
  306. );
  307. }
  308. /**
  309. * Retrieve disable auto group assign default value
  310. *
  311. * @return bool
  312. */
  313. public function isDisableAutoGroupAssignDefaultValue()
  314. {
  315. return $this->scopeConfig->isSetFlag(
  316. self::XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT,
  317. ScopeInterface::SCOPE_STORE
  318. );
  319. }
  320. /**
  321. * Retrieve 'validate on each transaction' value
  322. *
  323. * @param \Magento\Store\Model\Store|string|int $store
  324. * @return bool
  325. */
  326. public function hasValidateOnEachTransaction($store = null)
  327. {
  328. return $this->scopeConfig->isSetFlag(
  329. self::XML_PATH_VIV_ON_EACH_TRANSACTION,
  330. ScopeInterface::SCOPE_STORE,
  331. $store
  332. );
  333. }
  334. /**
  335. * Retrieve customer address type on which tax calculation must be based
  336. *
  337. * @param \Magento\Store\Model\Store|string|int|null $store
  338. * @return string
  339. */
  340. public function getTaxCalculationAddressType($store = null)
  341. {
  342. return (string)$this->scopeConfig->getValue(
  343. self::XML_PATH_VIV_TAX_CALCULATION_ADDRESS_TYPE,
  344. ScopeInterface::SCOPE_STORE,
  345. $store
  346. );
  347. }
  348. /**
  349. * Check if VAT ID address attribute has to be shown on frontend (on Customer Address management forms)
  350. *
  351. * @return boolean
  352. */
  353. public function isVatAttributeVisible()
  354. {
  355. return $this->scopeConfig->isSetFlag(
  356. self::XML_PATH_VAT_FRONTEND_VISIBILITY,
  357. ScopeInterface::SCOPE_STORE
  358. );
  359. }
  360. /**
  361. * Retrieve attribute visibility
  362. *
  363. * @param string $code
  364. *
  365. * @return bool
  366. * @throws NoSuchEntityException
  367. * @throws \Magento\Framework\Exception\LocalizedException
  368. * @since 101.0.0
  369. */
  370. public function isAttributeVisible($code)
  371. {
  372. $attributeMetadata = $this->_addressMetadataService->getAttributeMetadata($code);
  373. if ($attributeMetadata) {
  374. return $attributeMetadata->isVisible();
  375. }
  376. return false;
  377. }
  378. }