Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerComposite.php
Go to the documentation of this file.
1 <?php
7 
9 
15 class CustomerComposite extends \Magento\ImportExport\Model\Import\AbstractEntity
16 {
23  const COLUMN_ADDRESS_PREFIX = '_address_';
24 
25  const COLUMN_DEFAULT_BILLING = '_address_default_billing_';
26 
27  const COLUMN_DEFAULT_SHIPPING = '_address_default_shipping_';
28 
34  const SCOPE_DEFAULT = 1;
35 
36  const SCOPE_ADDRESS = -1;
37 
43  const COMPONENT_ENTITY_CUSTOMER = 'customer';
44 
45  const COMPONENT_ENTITY_ADDRESS = 'address';
46 
52  const ERROR_ROW_IS_ORPHAN = 'rowIsOrphan';
53 
57  protected $_customerEntity;
58 
62  protected $_addressEntity;
63 
69  protected $_specialAttributes = [
74  ];
75 
81  protected $_permanentAttributes = [
84  ];
85 
91  protected $_customerAttributes = [];
92 
98  protected $_addressAttributes = [];
99 
106 
112  protected $_currentEmail;
113 
119  protected $_nextCustomerId;
120 
127 
133  protected $needColumnCheck = true;
134 
140  protected $validColumnNames = [
144  ];
145 
149  protected $masterAttributeCode = 'email';
150 
166  public function __construct(
167  \Magento\Framework\Stdlib\StringUtils $string,
168  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
169  \Magento\ImportExport\Model\ImportFactory $importFactory,
170  \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
173  \Magento\CustomerImportExport\Model\ResourceModel\Import\CustomerComposite\DataFactory $dataFactory,
174  \Magento\CustomerImportExport\Model\Import\CustomerFactory $customerFactory,
175  \Magento\CustomerImportExport\Model\Import\AddressFactory $addressFactory,
176  array $data = []
177  ) {
178  parent::__construct($string, $scopeConfig, $importFactory, $resourceHelper, $resource, $errorAggregator, $data);
179 
180  $this->addMessageTemplate(
181  self::ERROR_ROW_IS_ORPHAN,
182  __('Orphan rows that will be skipped due default row errors')
183  );
184 
185  $this->_availableBehaviors = [
188  ];
189 
190  // customer entity stuff
191  if (isset($data['customer_data_source_model'])) {
192  $this->_dataSourceModels['customer'] = $data['customer_data_source_model'];
193  } else {
194  $arguments = [
196  ];
197  $this->_dataSourceModels['customer'] = $dataFactory->create(['arguments' => $arguments]);
198  }
199  if (isset($data['customer_entity'])) {
200  $this->_customerEntity = $data['customer_entity'];
201  } else {
202  $data['data_source_model'] = $this->_dataSourceModels['customer'];
203  $this->_customerEntity = $customerFactory->create(['data' => $data]);
204  unset($data['data_source_model']);
205  }
206  $this->_initCustomerAttributes();
207 
208  // address entity stuff
209  if (isset($data['address_data_source_model'])) {
210  $this->_dataSourceModels['address'] = $data['address_data_source_model'];
211  } else {
212  $arguments = [
214  'customer_attributes' => $this->_customerAttributes,
215  ];
216  $this->_dataSourceModels['address'] = $dataFactory->create(['arguments' => $arguments]);
217  }
218  if (isset($data['address_entity'])) {
219  $this->_addressEntity = $data['address_entity'];
220  } else {
221  $data['data_source_model'] = $this->_dataSourceModels['address'];
222  $this->_addressEntity = $addressFactory->create(['data' => $data]);
223  unset($data['data_source_model']);
224  }
225  $this->_initAddressAttributes();
226 
227  // next customer id
228  if (isset($data['next_customer_id'])) {
229  $this->_nextCustomerId = $data['next_customer_id'];
230  } else {
231  $this->_nextCustomerId = $resourceHelper->getNextAutoincrement($this->_customerEntity->getEntityTable());
232  }
233  }
234 
240  protected function _initCustomerAttributes()
241  {
243  foreach ($this->_customerEntity->getAttributeCollection() as $attribute) {
244  $this->_customerAttributes[] = $attribute->getAttributeCode();
245  }
246 
247  return $this;
248  }
249 
255  protected function _initAddressAttributes()
256  {
258  foreach ($this->_addressEntity->getAttributeCollection() as $attribute) {
259  $this->_addressAttributes[] = $attribute->getAttributeCode();
260  }
261 
262  return $this;
263  }
264 
270  protected function _importData()
271  {
272  $result = $this->_customerEntity->importData();
273  if ($this->getBehavior() != \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE) {
274  return $result && $this->_addressEntity->setCustomerAttributes($this->_customerAttributes)->importData();
275  }
276 
277  return $result;
278  }
279 
285  public function getEntityTypeCode()
286  {
287  return 'customer_composite';
288  }
289 
293  public function validateData()
294  {
295  //Preparing both customer and address imports for mass validation.
296  $source = $this->getSource();
297  $this->_customerEntity->prepareCustomerData($source);
298  $source->rewind();
299  $rows = [];
300  foreach ($source as $row) {
301  $rows[] = [
304  ];
305  }
306  $source->rewind();
307  $this->_addressEntity->prepareCustomerData($rows);
308 
309  return parent::validateData();
310  }
311 
319  public function validateRow(array $rowData, $rowNumber)
320  {
321  $rowScope = $this->_getRowScope($rowData);
322  if ($rowScope == self::SCOPE_DEFAULT) {
323  if ($this->_customerEntity->validateRow($rowData, $rowNumber)) {
324  $this->_currentWebsiteCode =
325  $rowData[Customer::COLUMN_WEBSITE];
326  $this->_currentEmail = strtolower(
327  $rowData[Customer::COLUMN_EMAIL]
328  );
329 
330  // Add new customer data into customer storage for address entity instance
331  $websiteId = $this->_customerEntity->getWebsiteId($this->_currentWebsiteCode);
332  if (!$this->_addressEntity->getCustomerStorage()->getCustomerId($this->_currentEmail, $websiteId)) {
333  $this->_addressEntity->getCustomerStorage()->addCustomerByArray(
334  [
335  'entity_id' => $this->_nextCustomerId,
336  'email' => $this->_currentEmail,
337  'website_id' => $websiteId,
338  ]
339  );
340  $this->_nextCustomerId++;
341  }
342 
343  return $this->_validateAddressRow($rowData, $rowNumber);
344  } else {
345  $this->_currentWebsiteCode = null;
346  $this->_currentEmail = null;
347  }
348  } else {
349  if (!empty($this->_currentWebsiteCode) && !empty($this->_currentEmail)) {
350  return $this->_validateAddressRow($rowData, $rowNumber);
351  } else {
352  $this->addRowError(self::ERROR_ROW_IS_ORPHAN, $rowNumber);
353  }
354  }
355 
356  return false;
357  }
358 
366  protected function _validateAddressRow(array $rowData, $rowNumber)
367  {
368  if ($this->getBehavior() == \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE) {
369  return true;
370  }
371 
372  $rowData = $this->_prepareAddressRowData($rowData);
373  if (empty($rowData)) {
374  return true;
375  } else {
376  $rowData[Address::COLUMN_WEBSITE] =
378  $rowData[Address::COLUMN_EMAIL] =
380  $rowData[Address::COLUMN_ADDRESS_ID] = null;
381 
382  return $this->_addressEntity->validateRow($rowData, $rowNumber);
383  }
384  }
385 
392  protected function _prepareAddressRowData(array $rowData)
393  {
395 
396  unset(
397  $rowData[Customer::COLUMN_WEBSITE],
398  $rowData[Customer::COLUMN_STORE]
399  );
400 
401  $result = [];
402  foreach ($rowData as $key => $value) {
403  if (!in_array($key, $this->_customerAttributes) && !empty($value)) {
404  if (!in_array($key, $excludedAttributes)) {
405  $key = str_replace(self::COLUMN_ADDRESS_PREFIX, '', $key);
406  }
407  $result[$key] = $value;
408  }
409  }
410 
411  return $result;
412  }
413 
420  protected function _getRowScope(array $rowData)
421  {
422  if (!isset($rowData[Customer::COLUMN_EMAIL])) {
423  return self::SCOPE_ADDRESS;
424  }
425  return strlen(
426  trim($rowData[Customer::COLUMN_EMAIL])
428  }
429 
436  public function setParameters(array $parameters)
437  {
438  parent::setParameters($parameters);
439 
440  if ($this->getBehavior() == \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) {
442  }
443 
444  $this->_customerEntity->setParameters($parameters);
445  $this->_addressEntity->setParameters($parameters);
446 
447  return $this;
448  }
449 
457  {
458  $this->_customerEntity->setSource($source);
459  $this->_addressEntity->setSource($source);
460 
461  return parent::setSource($source);
462  }
463 
469  public function getProcessedEntitiesCount()
470  {
471  return $this->_customerEntity->getProcessedEntitiesCount() +
472  $this->_addressEntity->getProcessedEntitiesCount();
473  }
474 
482  {
483  if (in_array(str_replace(self::COLUMN_ADDRESS_PREFIX, '', $attributeCode), $this->_addressAttributes)) {
484  return true;
485  } else {
486  return parent::isAttributeParticular($attributeCode);
487  }
488  }
489 
496  protected function _prepareRowForDb(array $rowData)
497  {
498  $rowData['_scope'] = $this->_getRowScope($rowData);
499  $rowData[Address::COLUMN_WEBSITE] =
502  $rowData[Address::COLUMN_ADDRESS_ID] = null;
503 
504  return parent::_prepareRowForDb($rowData);
505  }
506 
510  public function getValidColumnNames()
511  {
512  return array_unique(
513  array_merge(
514  $this->validColumnNames,
515  $this->_customerAttributes,
516  $this->_addressAttributes,
517  $this->_customerEntity->getValidColumnNames()
518  )
519  );
520  }
521 }
$source
Definition: source.php:23
addRowError( $errorCode, $errorRowNum, $colName=null, $errorMessage=null, $errorLevel=ProcessingError::ERROR_LEVEL_CRITICAL, $errorDescription=null)
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$addressFactory
Definition: quote.php:20
$value
Definition: gender.phtml:16
setSource(\Magento\ImportExport\Model\Import\AbstractSource $source)
$attributeCode
Definition: extend.phtml:12
$arguments
__construct(\Magento\Framework\Stdlib\StringUtils $string, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\ImportExport\Model\ImportFactory $importFactory, \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper, \Magento\Framework\App\ResourceConnection $resource, ProcessingErrorAggregatorInterface $errorAggregator, \Magento\CustomerImportExport\Model\ResourceModel\Import\CustomerComposite\DataFactory $dataFactory, \Magento\CustomerImportExport\Model\Import\CustomerFactory $customerFactory, \Magento\CustomerImportExport\Model\Import\AddressFactory $addressFactory, array $data=[])