Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractEntity.php
Go to the documentation of this file.
1 <?php
7 
11 
20 abstract class AbstractEntity
21 {
25  const ATTRIBUTE_COLLECTION_NAME = \Magento\Framework\Data\Collection::class;
26 
32  const XML_PATH_PAGE_SIZE = '';
33 
37  protected $_storeManager;
38 
44  protected $_errors = [];
45 
51  protected $_errorsCount = 0;
52 
58  protected $_errorsLimit = 100;
59 
65  protected $_invalidRows = [];
66 
72  protected $_messageTemplates = [];
73 
79  protected $_parameters = [];
80 
86  protected $_processedEntitiesCount = 0;
87 
93  protected $_processedRowsCount = 0;
94 
100  protected $_writer;
101 
107  protected $_storeIdToCode = [];
108 
114  protected $_websiteIdToCode = [];
115 
121  protected $_disabledAttributes = [];
122 
128  protected $_fileName = null;
129 
136 
142  protected $_pageSize;
143 
149  protected $_byPagesIterator;
150 
156  protected $_scopeConfig;
157 
163  protected $_attributeCodes = null;
164 
170  protected $_permanentAttributes = [];
171 
180  public function __construct(
181  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
182  \Magento\Store\Model\StoreManagerInterface $storeManager,
183  \Magento\ImportExport\Model\Export\Factory $collectionFactory,
184  \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory,
185  array $data = []
186  ) {
187  $this->_scopeConfig = $scopeConfig;
188  $this->_storeManager = $storeManager;
189  $this->_attributeCollection = isset(
190  $data['attribute_collection']
191  ) ? $data['attribute_collection'] : $collectionFactory->create(
192  static::ATTRIBUTE_COLLECTION_NAME
193  );
194  $this->_pageSize = isset(
195  $data['page_size']
196  ) ? $data['page_size'] : (static::XML_PATH_PAGE_SIZE ? (int)$this->_scopeConfig->getValue(
197  static::XML_PATH_PAGE_SIZE,
198  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
199  ) : 0);
200  $this->_byPagesIterator = isset(
201  $data['collection_by_pages_iterator']
202  ) ? $data['collection_by_pages_iterator'] : $resourceColFactory->create();
203  }
204 
210  protected function _initStores()
211  {
213  foreach ($this->_storeManager->getStores(true) as $store) {
214  $this->_storeIdToCode[$store->getId()] = $store->getCode();
215  }
216  ksort($this->_storeIdToCode);
217  // to ensure that 'admin' store (ID is zero) goes first
218 
219  return $this;
220  }
221 
228  protected function _initWebsites($withDefault = false)
229  {
231  foreach ($this->_storeManager->getWebsites($withDefault) as $website) {
232  $this->_websiteIdToCode[$website->getId()] = $website->getCode();
233  }
234  return $this;
235  }
236 
244  public function addRowError($errorCode, $errorRowNum)
245  {
246  $errorCode = (string)$errorCode;
247  $this->_errors[$errorCode][] = $errorRowNum + 1;
248  // one added for human readability
249  $this->_invalidRows[$errorRowNum] = true;
250  $this->_errorsCount++;
251 
252  return $this;
253  }
254 
262  public function addMessageTemplate($errorCode, $message)
263  {
264  $this->_messageTemplates[$errorCode] = $message;
265 
266  return $this;
267  }
268 
275  public function retrieveMessageTemplate($errorCode)
276  {
277  if (isset($this->_messageTemplates[$errorCode])) {
278  return $this->_messageTemplates[$errorCode];
279  }
280  return null;
281  }
282 
288  abstract public function export();
289 
296  abstract public function exportItem($item);
297 
304  protected function _exportCollectionByPages(\Magento\Framework\Data\Collection\AbstractDb $collection)
305  {
306  $this->_byPagesIterator->iterate($collection, $this->_pageSize, [[$this, 'exportItem']]);
307  }
308 
314  protected function _getExportAttributeCodes()
315  {
316  if (null === $this->_attributeCodes) {
317  if (!empty($this->_parameters[Export::FILTER_ELEMENT_SKIP])
318  && is_array($this->_parameters[Export::FILTER_ELEMENT_SKIP])
319  ) {
320  $skippedAttributes = array_flip(
321  $this->_parameters[Export::FILTER_ELEMENT_SKIP]
322  );
323  } else {
324  $skippedAttributes = [];
325  }
326  $attributeCodes = [];
327 
329  foreach ($this->filterAttributeCollection($this->getAttributeCollection()) as $attribute) {
330  if (!isset($skippedAttributes[$attribute->getAttributeId()])
331  || in_array($attribute->getAttributeCode(), $this->_permanentAttributes)
332  ) {
333  $attributeCodes[] = $attribute->getAttributeCode();
334  }
335  }
336  $this->_attributeCodes = $attributeCodes;
337  }
338  return $this->_attributeCodes;
339  }
340 
347  abstract public function getEntityTypeCode();
348 
354  abstract protected function _getHeaderColumns();
355 
361  abstract protected function _getEntityCollection();
362 
368  public function getAttributeCollection()
369  {
371  }
372 
379  public function filterAttributeCollection(\Magento\Framework\Data\Collection $collection)
380  {
382  foreach ($collection as $attribute) {
383  if (in_array($attribute->getAttributeCode(), $this->_disabledAttributes)) {
384  $collection->removeItemByKey($attribute->getId());
385  }
386  }
387 
388  return $collection;
389  }
390 
396  public function getErrorMessages()
397  {
398  $messages = [];
399  foreach ($this->_errors as $errorCode => $errorRows) {
400  $message = isset(
401  $this->_messageTemplates[$errorCode]
402  ) ? __(
403  $this->_messageTemplates[$errorCode]
404  ) : __(
405  'Please correct the value for "%1" column.',
406  $errorCode
407  );
408  $message = (string)$message;
409  $messages[$message] = $errorRows;
410  }
411 
412  return $messages;
413  }
414 
420  public function getErrorsCount()
421  {
422  return $this->_errorsCount;
423  }
424 
430  public function getInvalidRowsCount()
431  {
432  return count($this->_invalidRows);
433  }
434 
440  public function getProcessedEntitiesCount()
441  {
443  }
444 
450  public function getProcessedRowsCount()
451  {
453  }
454 
461  public function getWriter()
462  {
463  if (!$this->_writer) {
464  throw new \Magento\Framework\Exception\LocalizedException(__('Please specify the writer.'));
465  }
466 
467  return $this->_writer;
468  }
469 
476  public function setParameters(array $parameters)
477  {
478  $this->_parameters = $parameters;
479 
480  return $this;
481  }
482 
489  public function setWriter(AbstractAdapter $writer)
490  {
491  $this->_writer = $writer;
492 
493  return $this;
494  }
495 
502  public function setFileName($fileName)
503  {
504  $this->_fileName = $fileName;
505  }
506 
512  public function getFileName()
513  {
514  return $this->_fileName;
515  }
516 
522  public function getDisabledAttributes()
523  {
525  }
526 }
$storeManager
__()
Definition: __.php:13
$message
$fileName
Definition: translate.phtml:15
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\ImportExport\Model\Export\Factory $collectionFactory, \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory $resourceColFactory, array $data=[])
filterAttributeCollection(\Magento\Framework\Data\Collection $collection)
Definition: Export.php:215
_exportCollectionByPages(\Magento\Framework\Data\Collection\AbstractDb $collection)