Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImportResult.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\ImportExport\Model\History as ModelHistory;
12 
16 abstract class ImportResult extends Import
17 {
18  const IMPORT_HISTORY_FILE_DOWNLOAD_ROUTE = '*/history/download';
19 
23  const LIMIT_ERRORS_MESSAGE = 100;
24 
28  protected $reportProcessor;
29 
33  protected $historyModel;
34 
38  protected $reportHelper;
39 
46  public function __construct(
47  \Magento\Backend\App\Action\Context $context,
48  \Magento\ImportExport\Model\Report\ReportProcessorInterface $reportProcessor,
50  \Magento\ImportExport\Helper\Report $reportHelper
51  ) {
52  parent::__construct($context);
53  $this->reportProcessor = $reportProcessor;
54  $this->historyModel = $historyModel;
55  $this->reportHelper = $reportHelper;
56  }
57 
63  protected function addErrorMessages(
64  \Magento\Framework\View\Element\AbstractBlock $resultBlock,
65  ProcessingErrorAggregatorInterface $errorAggregator
66  ) {
67  if ($errorAggregator->getErrorsCount()) {
68  $message = '';
69  $counter = 0;
70  foreach ($this->getErrorMessages($errorAggregator) as $error) {
71  $message .= ++$counter . '. ' . $error . '<br>';
72  if ($counter >= self::LIMIT_ERRORS_MESSAGE) {
73  break;
74  }
75  }
76  if ($errorAggregator->hasFatalExceptions()) {
77  foreach ($this->getSystemExceptions($errorAggregator) as $error) {
78  $message .= $error->getErrorMessage()
79  . ' <a href="#" onclick="$(this).next().show();$(this).hide();return false;">'
80  . __('Show more') . '</a><div style="display:none;">' . __('Additional data') . ': '
81  . $error->getErrorDescription() . '</div>';
82  }
83  }
84  try {
85  $resultBlock->addNotice(
86  '<strong>' . __('Following Error(s) has been occurred during importing process:') . '</strong><br>'
87  . '<div class="import-error-wrapper">' . __('Only the first 100 errors are shown. ')
88  . '<a href="'
89  . $this->createDownloadUrlImportHistoryFile($this->createErrorReport($errorAggregator))
90  . '">' . __('Download full report') . '</a><br>'
91  . '<div class="import-error-list">' . $message . '</div></div>'
92  );
93  } catch (\Exception $e) {
94  foreach ($this->getErrorMessages($errorAggregator) as $errorMessage) {
95  $resultBlock->addError($errorMessage);
96  }
97  }
98  }
99 
100  return $this;
101  }
102 
107  protected function getErrorMessages(ProcessingErrorAggregatorInterface $errorAggregator)
108  {
109  $messages = [];
110  $rowMessages = $errorAggregator->getRowsGroupedByErrorCode([], [AbstractEntity::ERROR_CODE_SYSTEM_EXCEPTION]);
111  foreach ($rowMessages as $errorCode => $rows) {
112  $messages[] = $errorCode . ' ' . __('in row(s):') . ' ' . implode(', ', $rows);
113  }
114  return $messages;
115  }
116 
121  protected function getSystemExceptions(ProcessingErrorAggregatorInterface $errorAggregator)
122  {
124  }
125 
130  protected function createErrorReport(ProcessingErrorAggregatorInterface $errorAggregator)
131  {
132  $this->historyModel->loadLastInsertItem();
133  $sourceFile = $this->reportHelper->getReportAbsolutePath($this->historyModel->getImportedFile());
134  $writeOnlyErrorItems = true;
135  if ($this->historyModel->getData('execution_time') == ModelHistory::IMPORT_VALIDATION) {
136  $writeOnlyErrorItems = false;
137  }
138  $fileName = $this->reportProcessor->createReport($sourceFile, $errorAggregator, $writeOnlyErrorItems);
139  $this->historyModel->addErrorReportFile($fileName);
140  return $fileName;
141  }
142 
148  {
149  return $this->getUrl(self::IMPORT_HISTORY_FILE_DOWNLOAD_ROUTE, ['filename' => $fileName]);
150  }
151 }
addErrorMessages(\Magento\Framework\View\Element\AbstractBlock $resultBlock, ProcessingErrorAggregatorInterface $errorAggregator)
__()
Definition: __.php:13
$message
getErrorsCount(array $errorLevels=[ProcessingError::ERROR_LEVEL_CRITICAL, ProcessingError::ERROR_LEVEL_NOT_CRITICAL])
$fileName
Definition: translate.phtml:15
__construct(\Magento\Backend\App\Action\Context $context, \Magento\ImportExport\Model\Report\ReportProcessorInterface $reportProcessor, \Magento\ImportExport\Model\History $historyModel, \Magento\ImportExport\Helper\Report $reportHelper)
createErrorReport(ProcessingErrorAggregatorInterface $errorAggregator)
getSystemExceptions(ProcessingErrorAggregatorInterface $errorAggregator)
getErrorMessages(ProcessingErrorAggregatorInterface $errorAggregator)
getRowsGroupedByErrorCode(array $errorCode=[], array $excludedCodes=[], $replaceCodeWithMessage=true)