Customer.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * Export entity customer model
  9. *
  10. * @api
  11. *
  12. * @method \Magento\Customer\Model\ResourceModel\Attribute\Collection getAttributeCollection() getAttributeCollection()
  13. * @since 100.0.2
  14. */
  15. class Customer extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
  16. {
  17. /**
  18. * Permanent column names.
  19. *
  20. * Names that begins with underscore is not an attribute. This name convention is for
  21. * to avoid interference with same attribute name.
  22. */
  23. const COLUMN_EMAIL = 'email';
  24. const COLUMN_WEBSITE = '_website';
  25. const COLUMN_STORE = '_store';
  26. /**
  27. * Attribute collection name
  28. */
  29. const ATTRIBUTE_COLLECTION_NAME = \Magento\Customer\Model\ResourceModel\Attribute\Collection::class;
  30. /**
  31. * XML path to page size parameter
  32. */
  33. const XML_PATH_PAGE_SIZE = 'export/customer_page_size/customer';
  34. /**
  35. * @var array
  36. */
  37. protected $_attributeOverrides = [
  38. 'created_at' => ['backend_type' => 'datetime'],
  39. 'reward_update_notification' => ['source_model' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class],
  40. 'reward_warning_notification' => ['source_model' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class],
  41. ];
  42. /**
  43. * Array of attributes codes which are disabled for export
  44. *
  45. * @var string[]
  46. */
  47. protected $_disabledAttributes = ['default_billing', 'default_shipping'];
  48. /**
  49. * Attributes with index (not label) value.
  50. *
  51. * @var string[]
  52. */
  53. protected $_indexValueAttributes = ['group_id', 'website_id', 'store_id'];
  54. /**
  55. * Permanent entity columns.
  56. *
  57. * @var string[]
  58. */
  59. protected $_permanentAttributes = [self::COLUMN_EMAIL, self::COLUMN_WEBSITE, self::COLUMN_STORE];
  60. /**
  61. * Customers whose data is exported
  62. *
  63. * @var \Magento\Customer\Model\ResourceModel\Customer\Collection
  64. */
  65. protected $_customerCollection;
  66. /**
  67. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  68. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  69. * @param \Magento\ImportExport\Model\Export\Factory $collectionFactory
  70. * @param \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory
  71. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  72. * @param \Magento\Eav\Model\Config $eavConfig
  73. * @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerColFactory
  74. * @param array $data
  75. */
  76. public function __construct(
  77. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  78. \Magento\Store\Model\StoreManagerInterface $storeManager,
  79. \Magento\ImportExport\Model\Export\Factory $collectionFactory,
  80. \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory,
  81. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  82. \Magento\Eav\Model\Config $eavConfig,
  83. \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerColFactory,
  84. array $data = []
  85. ) {
  86. parent::__construct(
  87. $scopeConfig,
  88. $storeManager,
  89. $collectionFactory,
  90. $resourceColFactory,
  91. $localeDate,
  92. $eavConfig,
  93. $data
  94. );
  95. $this->_customerCollection = isset(
  96. $data['customer_collection']
  97. ) ? $data['customer_collection'] : $customerColFactory->create();
  98. $this->_initAttributeValues()->_initAttributeTypes()->_initStores()->_initWebsites(true);
  99. }
  100. /**
  101. * Export process.
  102. *
  103. * @return string
  104. */
  105. public function export()
  106. {
  107. $this->_prepareEntityCollection($this->_getEntityCollection());
  108. $writer = $this->getWriter();
  109. // create export file
  110. $writer->setHeaderCols($this->_getHeaderColumns());
  111. $this->_exportCollectionByPages($this->_getEntityCollection());
  112. return $writer->getContents();
  113. }
  114. /**
  115. * Get customers collection
  116. *
  117. * @return \Magento\Customer\Model\ResourceModel\Customer\Collection
  118. */
  119. protected function _getEntityCollection()
  120. {
  121. return $this->_customerCollection;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. protected function _getHeaderColumns()
  127. {
  128. $validAttributeCodes = $this->_getExportAttributeCodes();
  129. return array_merge($this->_permanentAttributes, $validAttributeCodes, ['password']);
  130. }
  131. /**
  132. * Export given customer data
  133. *
  134. * @param \Magento\Customer\Model\Customer $item
  135. * @return void
  136. */
  137. public function exportItem($item)
  138. {
  139. $row = $this->_addAttributeValuesToRow($item);
  140. $row[self::COLUMN_WEBSITE] = $this->_websiteIdToCode[$item->getWebsiteId()];
  141. $row[self::COLUMN_STORE] = $this->_storeIdToCode[$item->getStoreId()];
  142. $this->getWriter()->writeRow($row);
  143. }
  144. /**
  145. * Clean up already loaded attribute collection.
  146. *
  147. * @param \Magento\Framework\Data\Collection $collection
  148. * @return \Magento\Framework\Data\Collection
  149. */
  150. public function filterAttributeCollection(\Magento\Framework\Data\Collection $collection)
  151. {
  152. /** @var $attribute \Magento\Customer\Model\Attribute */
  153. foreach (parent::filterAttributeCollection($collection) as $attribute) {
  154. if (!empty($this->_attributeOverrides[$attribute->getAttributeCode()])) {
  155. $data = $this->_attributeOverrides[$attribute->getAttributeCode()];
  156. if (isset($data['options_method']) && method_exists($this, $data['options_method'])) {
  157. $data['filter_options'] = $this->{$data['options_method']}();
  158. }
  159. $attribute->addData($data);
  160. }
  161. }
  162. return $collection;
  163. }
  164. /**
  165. * EAV entity type code getter.
  166. *
  167. * @return string
  168. */
  169. public function getEntityTypeCode()
  170. {
  171. return $this->getAttributeCollection()->getEntityTypeCode();
  172. }
  173. /**
  174. * Retrieve list of overridden attributes
  175. *
  176. * @return array
  177. */
  178. public function getOverriddenAttributes()
  179. {
  180. return $this->_attributeOverrides;
  181. }
  182. }