Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Import.php
Go to the documentation of this file.
1 <?php
8 
14 
27 {
31  const BEHAVIOR_APPEND = 'append';
32 
33  const BEHAVIOR_ADD_UPDATE = 'add_update';
34 
35  const BEHAVIOR_REPLACE = 'replace';
36 
37  const BEHAVIOR_DELETE = 'delete';
38 
39  const BEHAVIOR_CUSTOM = 'custom';
40 
50  const FIELD_NAME_SOURCE_FILE = 'import_file';
51 
55  const FIELD_NAME_IMG_ARCHIVE_FILE = 'import_image_archive';
56 
60  const FIELD_NAME_IMG_FILE_DIR = 'import_images_file_dir';
61 
65  const FIELD_NAME_ALLOWED_ERROR_COUNT = 'allowed_error_count';
66 
70  const FIELD_NAME_VALIDATION_STRATEGY = 'validation_strategy';
71 
75  const FIELD_FIELD_SEPARATOR = '_import_field_separator';
76 
80  const FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR = '_import_multiple_value_separator';
81 
85  const FIELD_EMPTY_ATTRIBUTE_VALUE_CONSTANT = '_import_empty_attribute_value_constant';
86 
90  const FIELDS_ENCLOSURE = 'fields_enclosure';
91 
98 
102  const DEFAULT_EMPTY_ATTRIBUTE_VALUE_CONSTANT = '__EMPTY__VALUE__';
103 
107  const DEFAULT_SIZE = 50;
108 
109  const MAX_IMPORT_CHUNKS = 4;
110 
111  const IMPORT_HISTORY_DIR = 'import_history/';
112 
113  const IMPORT_DIR = 'import/';
114 
118  protected $_entityAdapter;
119 
125  protected $_importExportData = null;
126 
130  protected $_importConfig;
131 
135  protected $_entityFactory;
136 
140  protected $_importData;
141 
145  protected $_csvFactory;
146 
150  protected $_httpFactory;
151 
155  protected $_uploaderFactory;
156 
160  protected $indexerRegistry;
161 
165  protected $_behaviorFactory;
166 
170  protected $_filesystem;
171 
175  private $importHistoryModel;
176 
180  private $localeDate;
181 
200  public function __construct(
201  \Psr\Log\LoggerInterface $logger,
202  \Magento\Framework\Filesystem $filesystem,
203  \Magento\ImportExport\Helper\Data $importExportData,
204  \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig,
205  \Magento\ImportExport\Model\Import\ConfigInterface $importConfig,
206  \Magento\ImportExport\Model\Import\Entity\Factory $entityFactory,
207  \Magento\ImportExport\Model\ResourceModel\Import\Data $importData,
208  \Magento\ImportExport\Model\Export\Adapter\CsvFactory $csvFactory,
209  \Magento\Framework\HTTP\Adapter\FileTransferFactory $httpFactory,
210  \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
211  \Magento\ImportExport\Model\Source\Import\Behavior\Factory $behaviorFactory,
212  \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
213  \Magento\ImportExport\Model\History $importHistoryModel,
214  DateTime $localeDate,
215  array $data = []
216  ) {
217  $this->_importExportData = $importExportData;
218  $this->_coreConfig = $coreConfig;
219  $this->_importConfig = $importConfig;
220  $this->_entityFactory = $entityFactory;
221  $this->_importData = $importData;
222  $this->_csvFactory = $csvFactory;
223  $this->_httpFactory = $httpFactory;
224  $this->_uploaderFactory = $uploaderFactory;
225  $this->indexerRegistry = $indexerRegistry;
226  $this->_behaviorFactory = $behaviorFactory;
227  $this->_filesystem = $filesystem;
228  $this->importHistoryModel = $importHistoryModel;
229  $this->localeDate = $localeDate;
230  parent::__construct($logger, $filesystem, $data);
231  }
232 
239  protected function _getEntityAdapter()
240  {
241  if (!$this->_entityAdapter) {
242  $entities = $this->_importConfig->getEntities();
243  if (isset($entities[$this->getEntity()])) {
244  try {
245  $this->_entityAdapter = $this->_entityFactory->create($entities[$this->getEntity()]['model']);
246  } catch (\Exception $e) {
247  $this->_logger->critical($e);
248  throw new \Magento\Framework\Exception\LocalizedException(
249  __('Please enter a correct entity model.')
250  );
251  }
252  if (!$this->_entityAdapter instanceof \Magento\ImportExport\Model\Import\Entity\AbstractEntity &&
253  !$this->_entityAdapter instanceof \Magento\ImportExport\Model\Import\AbstractEntity
254  ) {
255  throw new \Magento\Framework\Exception\LocalizedException(
256  __(
257  'The entity adapter object must be an instance of %1 or %2.',
258  \Magento\ImportExport\Model\Import\Entity\AbstractEntity::class,
259  \Magento\ImportExport\Model\Import\AbstractEntity::class
260  )
261  );
262  }
263 
264  // check for entity codes integrity
265  if ($this->getEntity() != $this->_entityAdapter->getEntityTypeCode()) {
266  throw new \Magento\Framework\Exception\LocalizedException(
267  __('The input entity code is not equal to entity adapter code.')
268  );
269  }
270  } else {
271  throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a correct entity.'));
272  }
273  $this->_entityAdapter->setParameters($this->getData());
274  }
275  return $this->_entityAdapter;
276  }
277 
285  protected function _getSourceAdapter($sourceFile)
286  {
287  return \Magento\ImportExport\Model\Import\Adapter::findAdapterFor(
288  $sourceFile,
289  $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT),
290  $this->getData(self::FIELD_FIELD_SEPARATOR)
291  );
292  }
293 
302  {
303  $messages = [];
304  if ($this->getProcessedRowsCount()) {
305  if ($validationResult->getErrorsCount()) {
306  $messages[] = __('Data validation failed. Please fix the following errors and upload the file again.');
307 
308  // errors info
309  foreach ($validationResult->getRowsGroupedByErrorCode() as $errorMessage => $rows) {
310  $error = $errorMessage . ' ' . __('in row(s)') . ': ' . implode(', ', $rows);
311  $messages[] = $error;
312  }
313  } else {
314  if ($this->isImportAllowed()) {
315  $messages[] = __('The validation is complete.');
316  } else {
317  $messages[] = __('The file is valid, but we can\'t import it for some reason.');
318  }
319  }
320 
321  $messages[] = __(
322  'Checked rows: %1, checked entities: %2, invalid rows: %3, total errors: %4',
323  $this->getProcessedRowsCount(),
324  $this->getProcessedEntitiesCount(),
325  $validationResult->getInvalidRowsCount(),
326  $validationResult->getErrorsCount(
327  [
330  ]
331  )
332  );
333  } else {
334  $messages[] = __('This file does not contain any data.');
335  }
336  return $messages;
337  }
338 
345  public static function getAttributeType(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute)
346  {
347  $frontendInput = $attribute->getFrontendInput();
348  if ($attribute->usesSource() && in_array($frontendInput, ['select', 'multiselect', 'boolean'])) {
349  return $frontendInput;
350  } elseif ($attribute->isStatic()) {
351  return $frontendInput == 'date' ? 'datetime' : 'varchar';
352  } else {
353  return $attribute->getBackendType();
354  }
355  }
356 
362  public function getDataSourceModel()
363  {
364  return $this->_importData;
365  }
366 
373  public static function getDefaultBehavior()
374  {
375  return self::BEHAVIOR_APPEND;
376  }
377 
384  public function getEntity()
385  {
386  if (empty($this->_data['entity'])) {
387  throw new \Magento\Framework\Exception\LocalizedException(__('Entity is unknown'));
388  }
389  return $this->_data['entity'];
390  }
391 
398  public function getProcessedEntitiesCount()
399  {
400  return $this->_getEntityAdapter()->getProcessedEntitiesCount();
401  }
402 
409  public function getProcessedRowsCount()
410  {
411  return $this->_getEntityAdapter()->getProcessedRowsCount();
412  }
413 
419  public function getWorkingDir()
420  {
421  return $this->_varDirectory->getAbsolutePath('importexport/');
422  }
423 
430  public function importSource()
431  {
432  $this->setData('entity', $this->getDataSourceModel()->getEntityTypeCode());
433  $this->setData('behavior', $this->getDataSourceModel()->getBehavior());
434  $this->importHistoryModel->updateReport($this);
435 
436  $this->addLogComment(__('Begin import of "%1" with "%2" behavior', $this->getEntity(), $this->getBehavior()));
437 
438  $result = $this->processImport();
439 
440  if ($result) {
441  $this->addLogComment(
442  [
443  __(
444  'Checked rows: %1, checked entities: %2, invalid rows: %3, total errors: %4',
445  $this->getProcessedRowsCount(),
446  $this->getProcessedEntitiesCount(),
447  $this->getErrorAggregator()->getInvalidRowsCount(),
448  $this->getErrorAggregator()->getErrorsCount()
449  ),
450  __('The import was successful.'),
451  ]
452  );
453  $this->importHistoryModel->updateReport($this, true);
454  } else {
455  $this->importHistoryModel->invalidateReport($this);
456  }
457 
458  return $result;
459  }
460 
467  protected function processImport()
468  {
469  return $this->_getEntityAdapter()->importData();
470  }
471 
478  public function isImportAllowed()
479  {
480  return $this->_getEntityAdapter()->isImportAllowed();
481  }
482 
489  public function getErrorAggregator()
490  {
491  return $this->_getEntityAdapter()->getErrorAggregator();
492  }
493 
500  public function uploadSource()
501  {
503  $adapter = $this->_httpFactory->create();
504  if (!$adapter->isValid(self::FIELD_NAME_SOURCE_FILE)) {
505  $errors = $adapter->getErrors();
507  $errorMessage = $this->_importExportData->getMaxUploadSizeMessage();
508  } else {
509  $errorMessage = __('The file was not uploaded.');
510  }
511  throw new \Magento\Framework\Exception\LocalizedException($errorMessage);
512  }
513 
514  $entity = $this->getEntity();
516  $uploader = $this->_uploaderFactory->create(['fileId' => self::FIELD_NAME_SOURCE_FILE]);
517  $uploader->skipDbProcessing(true);
518  $result = $uploader->save($this->getWorkingDir());
519  $extension = pathinfo($result['file'], PATHINFO_EXTENSION);
520 
521  $uploadedFile = $result['path'] . $result['file'];
522  if (!$extension) {
523  $this->_varDirectory->delete($uploadedFile);
524  throw new \Magento\Framework\Exception\LocalizedException(__('The file you uploaded has no extension.'));
525  }
526  $sourceFile = $this->getWorkingDir() . $entity;
527 
528  $sourceFile .= '.' . $extension;
529  $sourceFileRelative = $this->_varDirectory->getRelativePath($sourceFile);
530 
531  if (strtolower($uploadedFile) != strtolower($sourceFile)) {
532  if ($this->_varDirectory->isExist($sourceFileRelative)) {
533  $this->_varDirectory->delete($sourceFileRelative);
534  }
535 
536  try {
537  $this->_varDirectory->renameFile(
538  $this->_varDirectory->getRelativePath($uploadedFile),
539  $sourceFileRelative
540  );
541  } catch (\Magento\Framework\Exception\FileSystemException $e) {
542  throw new \Magento\Framework\Exception\LocalizedException(__('The source file moving process failed.'));
543  }
544  }
545  $this->_removeBom($sourceFile);
546  $this->createHistoryReport($sourceFileRelative, $entity, $extension, $result);
547  return $sourceFile;
548  }
549 
557  public function uploadFileAndGetSource()
558  {
559  $sourceFile = $this->uploadSource();
560  try {
561  $source = $this->_getSourceAdapter($sourceFile);
562  } catch (\Exception $e) {
563  $this->_varDirectory->delete($this->_varDirectory->getRelativePath($sourceFile));
564  throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
565  }
566 
567  return $source;
568  }
569 
577  protected function _removeBom($sourceFile)
578  {
579  $string = $this->_varDirectory->readFile($this->_varDirectory->getRelativePath($sourceFile));
580  if ($string !== false && substr($string, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
581  $string = substr($string, 3);
582  $this->_varDirectory->writeFile($this->_varDirectory->getRelativePath($sourceFile), $string);
583  }
584  return $this;
585  }
586 
597  public function validateSource(\Magento\ImportExport\Model\Import\AbstractSource $source)
598  {
599  $this->addLogComment(__('Begin data validation'));
600 
601  $errorAggregator = $this->getErrorAggregator();
602  $errorAggregator->initValidationStrategy(
603  $this->getData(self::FIELD_NAME_VALIDATION_STRATEGY),
604  $this->getData(self::FIELD_NAME_ALLOWED_ERROR_COUNT)
605  );
606 
607  try {
608  $adapter = $this->_getEntityAdapter()->setSource($source);
609  $adapter->validateData();
610  } catch (\Exception $e) {
611  $errorAggregator->addError(
614  null,
615  null,
616  $e->getMessage()
617  );
618  }
619 
620  $messages = $this->getOperationResultMessages($errorAggregator);
621  $this->addLogComment($messages);
622 
623  $result = !$errorAggregator->getErrorsCount();
624  if ($result) {
625  $this->addLogComment(__('Import data validation is complete.'));
626  }
627  return $result;
628  }
629 
636  public function invalidateIndex()
637  {
638  $relatedIndexers = $this->_importConfig->getRelatedIndexers($this->getEntity());
639  if (empty($relatedIndexers)) {
640  return $this;
641  }
642 
643  foreach (array_keys($relatedIndexers) as $indexerId) {
644  try {
645  $indexer = $this->indexerRegistry->get($indexerId);
646 
647  if (!$indexer->isScheduled()) {
648  $indexer->invalidate();
649  }
650  } catch (\InvalidArgumentException $e) {
651  }
652  }
653 
654  return $this;
655  }
656 
670  public function getEntityBehaviors()
671  {
672  $behaviourData = [];
673  $entities = $this->_importConfig->getEntities();
674  foreach ($entities as $entityCode => $entityData) {
675  $behaviorClassName = isset($entityData['behaviorModel']) ? $entityData['behaviorModel'] : null;
676  if ($behaviorClassName && class_exists($behaviorClassName)) {
678  $behavior = $this->_behaviorFactory->create($behaviorClassName);
679  $behaviourData[$entityCode] = [
680  'token' => $behaviorClassName,
681  'code' => $behavior->getCode() . '_behavior',
682  'notes' => $behavior->getNotes($entityCode),
683  ];
684  } else {
685  throw new \Magento\Framework\Exception\LocalizedException(
686  __('The behavior token for %1 is invalid.', $entityCode)
687  );
688  }
689  }
690  return $behaviourData;
691  }
692 
703  public function getUniqueEntityBehaviors()
704  {
705  $uniqueBehaviors = [];
706  $behaviourData = $this->getEntityBehaviors();
707  foreach ($behaviourData as $behavior) {
708  $behaviorCode = $behavior['code'];
709  if (!isset($uniqueBehaviors[$behaviorCode])) {
710  $uniqueBehaviors[$behaviorCode] = $behavior['token'];
711  }
712  }
713  return $uniqueBehaviors;
714  }
715 
723  public function isReportEntityType($entity = null)
724  {
725  $result = false;
726  if (!$entity) {
727  $entity = $this->getEntity();
728  }
729  if ($entity !== null && $this->_getEntityAdapter()->getEntityTypeCode() != $entity) {
730  $entities = $this->_importConfig->getEntities();
731  if (isset($entities[$entity])) {
732  try {
733  $result = $this->_getEntityAdapter()->isNeedToLogInHistory();
734  } catch (\Exception $e) {
735  throw new \Magento\Framework\Exception\LocalizedException(
736  __('Please enter a correct entity model')
737  );
738  }
739  } else {
740  throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a correct entity model'));
741  }
742  } else {
743  $result = $this->_getEntityAdapter()->isNeedToLogInHistory();
744  }
745  return $result;
746  }
747 
758  protected function createHistoryReport($sourceFileRelative, $entity, $extension = null, $result = null)
759  {
760  if ($this->isReportEntityType($entity)) {
761  if (is_array($sourceFileRelative)) {
762  $fileName = $sourceFileRelative['file_name'];
763  $sourceFileRelative = $this->_varDirectory->getRelativePath(self::IMPORT_DIR . $fileName);
764  } elseif (isset($result['name'])) {
765  $fileName = $result['name'];
766  } elseif ($extension !== null) {
768  } else {
769  $fileName = basename($sourceFileRelative);
770  }
771  $copyName = $this->localeDate->gmtTimestamp() . '_' . $fileName;
772  $copyFile = self::IMPORT_HISTORY_DIR . $copyName;
773  try {
774  if ($this->_varDirectory->isExist($sourceFileRelative)) {
775  $this->_varDirectory->copyFile($sourceFileRelative, $copyFile);
776  } else {
777  $content = $this->_varDirectory->getDriver()->fileGetContents($sourceFileRelative);
778  $this->_varDirectory->writeFile($copyFile, $content);
779  }
780  } catch (\Magento\Framework\Exception\FileSystemException $e) {
781  throw new \Magento\Framework\Exception\LocalizedException(__('Source file coping failed'));
782  }
783  $this->importHistoryModel->addReport($copyName);
784  }
785  return $this;
786  }
787 
794  public function getCreatedItemsCount()
795  {
796  return $this->_getEntityAdapter()->getCreatedItemsCount();
797  }
798 
805  public function getUpdatedItemsCount()
806  {
807  return $this->_getEntityAdapter()->getUpdatedItemsCount();
808  }
809 
816  public function getDeletedItemsCount()
817  {
818  return $this->_getEntityAdapter()->getDeletedItemsCount();
819  }
820 }
static getAttributeType(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute)
Definition: Import.php:345
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
isReportEntityType($entity=null)
Definition: Import.php:723
$source
Definition: source.php:23
__()
Definition: __.php:13
getErrorsCount(array $errorLevels=[ProcessingError::ERROR_LEVEL_CRITICAL, ProcessingError::ERROR_LEVEL_NOT_CRITICAL])
$logger
getOperationResultMessages(ProcessingErrorAggregatorInterface $validationResult)
Definition: Import.php:301
$adapter
Definition: webapi_user.php:16
$fileName
Definition: translate.phtml:15
createHistoryReport($sourceFileRelative, $entity, $extension=null, $result=null)
Definition: Import.php:758
$entity
Definition: element.phtml:22
validateSource(\Magento\ImportExport\Model\Import\AbstractSource $source)
Definition: Import.php:597
__construct(\Psr\Log\LoggerInterface $logger, \Magento\Framework\Filesystem $filesystem, \Magento\ImportExport\Helper\Data $importExportData, \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig, \Magento\ImportExport\Model\Import\ConfigInterface $importConfig, \Magento\ImportExport\Model\Import\Entity\Factory $entityFactory, \Magento\ImportExport\Model\ResourceModel\Import\Data $importData, \Magento\ImportExport\Model\Export\Adapter\CsvFactory $csvFactory, \Magento\Framework\HTTP\Adapter\FileTransferFactory $httpFactory, \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory, \Magento\ImportExport\Model\Source\Import\Behavior\Factory $behaviorFactory, \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry, \Magento\ImportExport\Model\History $importHistoryModel, DateTime $localeDate, array $data=[])
Definition: Import.php:200
setData($key, $value=null)
Definition: DataObject.php:72
$filesystem
getRowsGroupedByErrorCode(array $errorCode=[], array $excludedCodes=[], $replaceCodeWithMessage=true)
$errors
Definition: overview.phtml:9