Address.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CustomerImportExport\Model\Export;
  7. /**
  8. * Customer address export
  9. *
  10. * @api
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. * @since 100.0.2
  14. */
  15. class Address extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
  16. {
  17. /**#@+
  18. * Permanent column names
  19. *
  20. * Names that begins with underscore is not an attribute.
  21. * This name convention is for to avoid interference with same attribute name.
  22. */
  23. const COLUMN_EMAIL = '_email';
  24. const COLUMN_WEBSITE = '_website';
  25. const COLUMN_ADDRESS_ID = '_entity_id';
  26. /**#@-*/
  27. /**
  28. * Country column name for index value
  29. */
  30. const COLUMN_COUNTRY_ID = 'country_id';
  31. /**
  32. * Name of region id column
  33. */
  34. const COLUMN_REGION_ID = 'region_id';
  35. /**#@+
  36. * Particular columns that contains of customer default addresses
  37. */
  38. const COLUMN_NAME_DEFAULT_BILLING = '_address_default_billing_';
  39. const COLUMN_NAME_DEFAULT_SHIPPING = '_address_default_shipping_';
  40. /**#@-*/
  41. /**#@+
  42. * Attribute collection name
  43. */
  44. const ATTRIBUTE_COLLECTION_NAME = \Magento\Customer\Model\ResourceModel\Address\Attribute\Collection::class;
  45. /**#@-*/
  46. /**#@+
  47. * XML path to page size parameter
  48. */
  49. const XML_PATH_PAGE_SIZE = 'export/customer_page_size/address';
  50. /**#@-*/
  51. /**#@-*/
  52. protected $_permanentAttributes = [self::COLUMN_WEBSITE, self::COLUMN_EMAIL, self::COLUMN_ADDRESS_ID];
  53. /**
  54. * Attributes with index (not label) value
  55. *
  56. * @var string[]
  57. * @since 100.2.0
  58. */
  59. protected $_indexValueAttributes = [self::COLUMN_COUNTRY_ID];
  60. /**
  61. * Default addresses column names to appropriate customer attribute code
  62. *
  63. * @var array
  64. */
  65. protected static $_defaultAddressAttributeMapping = [
  66. self::COLUMN_NAME_DEFAULT_BILLING => 'default_billing',
  67. self::COLUMN_NAME_DEFAULT_SHIPPING => 'default_shipping',
  68. ];
  69. /**
  70. * Customers whose addresses are exported
  71. *
  72. * @var \Magento\Customer\Model\ResourceModel\Customer\Collection
  73. */
  74. protected $_customerCollection;
  75. /**
  76. * Customer addresses collection
  77. *
  78. * @var \Magento\Customer\Model\ResourceModel\Address\Collection
  79. */
  80. protected $_addressCollection;
  81. /**
  82. * Customers whose address are exported
  83. *
  84. * @var Customer
  85. */
  86. protected $_customerEntity;
  87. /**
  88. * Existing customers information.
  89. *
  90. * In form of:
  91. *
  92. * [customer email] => array(
  93. * [website id 1] => customer_id 1,
  94. * [website id 2] => customer_id 2,
  95. * ... => ... ,
  96. * [website id n] => customer_id n,
  97. * )
  98. *
  99. * @var array
  100. */
  101. protected $_customers = [];
  102. /**
  103. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  104. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  105. * @param \Magento\ImportExport\Model\Export\Factory $collectionFactory
  106. * @param \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory
  107. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  108. * @param \Magento\Eav\Model\Config $eavConfig
  109. * @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerColFactory
  110. * @param \Magento\CustomerImportExport\Model\Export\CustomerFactory $eavCustomerFactory
  111. * @param \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory
  112. * @param array $data
  113. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  114. */
  115. public function __construct(
  116. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  117. \Magento\Store\Model\StoreManagerInterface $storeManager,
  118. \Magento\ImportExport\Model\Export\Factory $collectionFactory,
  119. \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory,
  120. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  121. \Magento\Eav\Model\Config $eavConfig,
  122. \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerColFactory,
  123. \Magento\CustomerImportExport\Model\Export\CustomerFactory $eavCustomerFactory,
  124. \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory,
  125. array $data = []
  126. ) {
  127. parent::__construct(
  128. $scopeConfig,
  129. $storeManager,
  130. $collectionFactory,
  131. $resourceColFactory,
  132. $localeDate,
  133. $eavConfig,
  134. $data
  135. );
  136. $this->_customerCollection = isset(
  137. $data['customer_collection']
  138. ) ? $data['customer_collection'] : $customerColFactory->create();
  139. $this->_customerEntity = isset(
  140. $data['customer_entity']
  141. ) ? $data['customer_entity'] : $eavCustomerFactory->create();
  142. $this->_addressCollection = isset(
  143. $data['address_collection']
  144. ) ? $data['address_collection'] : $addressColFactory->create();
  145. $this->_initAttributeValues()->_initAttributeTypes()->_initWebsites(true);
  146. $this->setFileName($this->getEntityTypeCode());
  147. }
  148. /**
  149. * Initialize existent customers data
  150. *
  151. * @return $this
  152. */
  153. protected function _initCustomers()
  154. {
  155. if (empty($this->_customers)) {
  156. // add customer default addresses column name to customer attribute mapping array
  157. $this->_customerCollection->addAttributeToSelect(self::$_defaultAddressAttributeMapping);
  158. // filter customer collection
  159. $this->_customerCollection = $this->_customerEntity->filterEntityCollection($this->_customerCollection);
  160. $customers = [];
  161. $addCustomer = function (\Magento\Customer\Model\Customer $customer) use (&$customers) {
  162. $customers[$customer->getId()] = $customer->getData();
  163. };
  164. $this->_byPagesIterator->iterate($this->_customerCollection, $this->_pageSize, [$addCustomer]);
  165. $this->_customers = $customers;
  166. }
  167. return $this;
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. protected function _getHeaderColumns()
  173. {
  174. return array_merge(
  175. $this->_permanentAttributes,
  176. $this->_getExportAttributeCodes(),
  177. array_keys(self::$_defaultAddressAttributeMapping)
  178. );
  179. }
  180. /**
  181. * Get customers collection
  182. *
  183. * @return \Magento\Customer\Model\ResourceModel\Address\Collection
  184. */
  185. protected function _getEntityCollection()
  186. {
  187. return $this->_addressCollection;
  188. }
  189. /**
  190. * Export process
  191. *
  192. * @return string
  193. */
  194. public function export()
  195. {
  196. // skip and filter by customer address attributes
  197. $this->_prepareEntityCollection($this->_getEntityCollection());
  198. $this->_getEntityCollection()->setCustomerFilter(array_keys($this->_customers));
  199. // prepare headers
  200. $this->getWriter()->setHeaderCols($this->_getHeaderColumns());
  201. $this->_exportCollectionByPages($this->_getEntityCollection());
  202. return $this->getWriter()->getContents();
  203. }
  204. /**
  205. * Export given customer address data plus related customer data (required for import)
  206. *
  207. * @param \Magento\Customer\Model\Address $item
  208. * @return void
  209. */
  210. public function exportItem($item)
  211. {
  212. $row = $this->_addAttributeValuesToRow($item);
  213. /** @var $customer \Magento\Customer\Model\Customer */
  214. $customer = $this->_customers[$item->getParentId()];
  215. // Fill row with default address attributes values
  216. foreach (self::$_defaultAddressAttributeMapping as $columnName => $attributeCode) {
  217. if (!empty($customer[$attributeCode]) && $customer[$attributeCode] == $item->getId()) {
  218. $row[$columnName] = 1;
  219. }
  220. }
  221. // Unique key
  222. $row[self::COLUMN_ADDRESS_ID] = $item['entity_id'];
  223. $row[self::COLUMN_EMAIL] = $customer['email'];
  224. $row[self::COLUMN_WEBSITE] = $this->_websiteIdToCode[$customer['website_id']];
  225. $row[self::COLUMN_REGION_ID] = $item->getRegionId();
  226. $this->getWriter()->writeRow($row);
  227. }
  228. /**
  229. * Set parameters (push filters from post into export customer model)
  230. *
  231. * @param array $parameters
  232. * @return $this
  233. */
  234. public function setParameters(array $parameters)
  235. {
  236. // push filters from post into export customer model
  237. $this->_customerEntity->setParameters($parameters);
  238. $this->_initCustomers();
  239. return parent::setParameters($parameters);
  240. }
  241. /**
  242. * EAV entity type code getter.
  243. *
  244. * @return string
  245. */
  246. public function getEntityTypeCode()
  247. {
  248. return $this->getAttributeCollection()->getEntityTypeCode();
  249. }
  250. }