Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractCustomer.php
Go to the documentation of this file.
1 <?php
8 
12 
21 abstract class AbstractCustomer extends \Magento\ImportExport\Model\Import\Entity\AbstractEav
22 {
29  const COLUMN_WEBSITE = '_website';
30 
31  const COLUMN_EMAIL = '_email';
32 
33  const COLUMN_DEFAULT_BILLING = 'default_billing';
34 
35  const COLUMN_DEFAULT_SHIPPING = 'default_shipping';
36 
42  const ERROR_WEBSITE_IS_EMPTY = 'websiteIsEmpty';
43 
44  const ERROR_EMAIL_IS_EMPTY = 'emailIsEmpty';
45 
46  const ERROR_INVALID_WEBSITE = 'invalidWebsite';
47 
48  const ERROR_INVALID_EMAIL = 'invalidEmail';
49 
50  const ERROR_VALUE_IS_REQUIRED = 'valueIsRequired';
51 
52  const ERROR_CUSTOMER_NOT_FOUND = 'customerNotFound';
53 
57  protected $_ignoredAttributes = ['website_id', 'store_id',
59 
65  protected $_customerStorage;
66 
70  protected $_storageFactory;
71 
77  protected $needColumnCheck = true;
78 
82  protected $masterAttributeCode = '_email';
83 
98  public function __construct(
99  \Magento\Framework\Stdlib\StringUtils $string,
100  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
101  \Magento\ImportExport\Model\ImportFactory $importFactory,
102  \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
105  \Magento\Store\Model\StoreManagerInterface $storeManager,
106  \Magento\ImportExport\Model\Export\Factory $collectionFactory,
107  \Magento\Eav\Model\Config $eavConfig,
108  \Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory $storageFactory,
109  array $data = []
110  ) {
111  $this->_storageFactory = $storageFactory;
112  parent::__construct(
113  $string,
114  $scopeConfig,
115  $importFactory,
116  $resourceHelper,
117  $resource,
120  $collectionFactory,
121  $eavConfig,
122  $data
123  );
124 
125  $this->addMessageTemplate(self::ERROR_WEBSITE_IS_EMPTY, __('Please specify a website.'));
126  $this->addMessageTemplate(
127  self::ERROR_EMAIL_IS_EMPTY,
128  __("An email wasn't specified. Enter the email and try again.")
129  );
130  $this->addMessageTemplate(self::ERROR_INVALID_WEBSITE, __('We found an invalid value in a website column.'));
131  $this->addMessageTemplate(self::ERROR_INVALID_EMAIL, __('Please enter a valid email.'));
132  $this->addMessageTemplate(self::ERROR_VALUE_IS_REQUIRED, __('Please make sure attribute "%s" is not empty.'));
133  $this->addMessageTemplate(
134  self::ERROR_CUSTOMER_NOT_FOUND,
135  __('We can\'t find a customer who matches this email and website code.')
136  );
137 
138  $this->_initCustomers($data)->_initWebsites(true);
139  }
140 
147  protected function _initCustomers(array $data)
148  {
149  if (!isset($data['page_size'])) {
150  $data['page_size'] = $this->_pageSize;
151  }
152  $this->_customerStorage = isset(
153  $data['customer_storage']
154  ) ? $data['customer_storage'] : $this->_storageFactory->create(
155  ['data' => $data]
156  );
157 
158  return $this;
159  }
160 
168  protected function _getCustomerId($email, $websiteCode)
169  {
170  $email = strtolower(trim($email));
171  if (isset($this->_websiteCodeToId[$websiteCode])) {
172  $websiteId = $this->_websiteCodeToId[$websiteCode];
173  return $this->_customerStorage->getCustomerId($email, $websiteId);
174  }
175 
176  return false;
177  }
178 
186  public function validateRow(array $rowData, $rowNumber)
187  {
188  if (isset($this->_validatedRows[$rowNumber])) {
189  // check that row is already validated
190  return !$this->getErrorAggregator()->isRowInvalid($rowNumber);
191  }
192  $this->_validatedRows[$rowNumber] = true;
193  $this->_processedEntitiesCount++;
194  if ($this->getBehavior($rowData) == \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE) {
195  $this->_validateRowForUpdate($rowData, $rowNumber);
196  } elseif ($this->getBehavior($rowData) == \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE) {
197  $this->_validateRowForDelete($rowData, $rowNumber);
198  }
199 
200  return !$this->getErrorAggregator()->isRowInvalid($rowNumber);
201  }
202 
210  abstract protected function _validateRowForUpdate(array $rowData, $rowNumber);
211 
219  abstract protected function _validateRowForDelete(array $rowData, $rowNumber);
220 
228  protected function _checkUniqueKey(array $rowData, $rowNumber)
229  {
230  if (empty($rowData[static::COLUMN_WEBSITE])) {
231  $this->addRowError(static::ERROR_WEBSITE_IS_EMPTY, $rowNumber, static::COLUMN_WEBSITE);
232  } elseif (empty($rowData[static::COLUMN_EMAIL])) {
233  $this->addRowError(static::ERROR_EMAIL_IS_EMPTY, $rowNumber, static::COLUMN_EMAIL);
234  } else {
235  $email = strtolower($rowData[static::COLUMN_EMAIL]);
236  $website = $rowData[static::COLUMN_WEBSITE];
237 
238  if (!\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
239  $this->addRowError(static::ERROR_INVALID_EMAIL, $rowNumber, static::COLUMN_EMAIL);
240  } elseif (!isset($this->_websiteCodeToId[$website])) {
241  $this->addRowError(static::ERROR_INVALID_WEBSITE, $rowNumber, static::COLUMN_WEBSITE);
242  }
243  }
244  return !$this->getErrorAggregator()->isRowInvalid($rowNumber);
245  }
246 
252  public function getCustomerStorage()
253  {
255  }
256 
265  protected function getSelectAttrIdByValue(array $attributeParameters, $value)
266  {
267  return isset($attributeParameters['options'][strtolower($value)])
268  ? $attributeParameters['options'][strtolower($value)]
269  : 0;
270  }
271 
278  protected function getMultipleValueSeparator()
279  {
280  return isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR])
283  }
284 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$email
Definition: details.phtml:13
addRowError( $errorCode, $errorRowNum, $colName=null, $errorMessage=null, $errorLevel=ProcessingError::ERROR_LEVEL_CRITICAL, $errorDescription=null)
$storeManager
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
getSelectAttrIdByValue(array $attributeParameters, $value)
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195
if(!isset($_GET['website_code'])) $websiteCode
Definition: website.php:11
__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\Store\Model\StoreManagerInterface $storeManager, \Magento\ImportExport\Model\Export\Factory $collectionFactory, \Magento\Eav\Model\Config $eavConfig, \Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory $storageFactory, array $data=[])