1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ImportExport\Controller\Adminhtml;
use Magento\Backend\App\Action;
use Magento\ImportExport\Model\Import\Entity\AbstractEntity;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
use Magento\ImportExport\Model\History as ModelHistory;
/**
* Import controller
*/
abstract class ImportResult extends Import
{
const IMPORT_HISTORY_FILE_DOWNLOAD_ROUTE = '*/history/download';
/**
* Limit view errors
*/
const LIMIT_ERRORS_MESSAGE = 100;
/**
* @var \Magento\ImportExport\Model\Report\ReportProcessorInterface
*/
protected $reportProcessor;
/**
* @var \Magento\ImportExport\Model\History
*/
protected $historyModel;
/**
* @var \Magento\ImportExport\Helper\Report
*/
protected $reportHelper;
/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\ImportExport\Model\Report\ReportProcessorInterface $reportProcessor
* @param \Magento\ImportExport\Model\History $historyModel
* @param \Magento\ImportExport\Helper\Report $reportHelper
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\ImportExport\Model\Report\ReportProcessorInterface $reportProcessor,
\Magento\ImportExport\Model\History $historyModel,
\Magento\ImportExport\Helper\Report $reportHelper
) {
parent::__construct($context);
$this->reportProcessor = $reportProcessor;
$this->historyModel = $historyModel;
$this->reportHelper = $reportHelper;
}
/**
* @param \Magento\Framework\View\Element\AbstractBlock $resultBlock
* @param ProcessingErrorAggregatorInterface $errorAggregator
* @return $this
*/
protected function addErrorMessages(
\Magento\Framework\View\Element\AbstractBlock $resultBlock,
ProcessingErrorAggregatorInterface $errorAggregator
) {
if ($errorAggregator->getErrorsCount()) {
$message = '';
$counter = 0;
foreach ($this->getErrorMessages($errorAggregator) as $error) {
$message .= ++$counter . '. ' . $error . '<br>';
if ($counter >= self::LIMIT_ERRORS_MESSAGE) {
break;
}
}
if ($errorAggregator->hasFatalExceptions()) {
foreach ($this->getSystemExceptions($errorAggregator) as $error) {
$message .= $error->getErrorMessage()
. ' <a href="#" onclick="$(this).next().show();$(this).hide();return false;">'
. __('Show more') . '</a><div style="display:none;">' . __('Additional data') . ': '
. $error->getErrorDescription() . '</div>';
}
}
try {
$resultBlock->addNotice(
'<strong>' . __('Following Error(s) has been occurred during importing process:') . '</strong><br>'
. '<div class="import-error-wrapper">' . __('Only the first 100 errors are shown. ')
. '<a href="'
. $this->createDownloadUrlImportHistoryFile($this->createErrorReport($errorAggregator))
. '">' . __('Download full report') . '</a><br>'
. '<div class="import-error-list">' . $message . '</div></div>'
);
} catch (\Exception $e) {
foreach ($this->getErrorMessages($errorAggregator) as $errorMessage) {
$resultBlock->addError($errorMessage);
}
}
}
return $this;
}
/**
* @param \Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface $errorAggregator
* @return array
*/
protected function getErrorMessages(ProcessingErrorAggregatorInterface $errorAggregator)
{
$messages = [];
$rowMessages = $errorAggregator->getRowsGroupedByErrorCode([], [AbstractEntity::ERROR_CODE_SYSTEM_EXCEPTION]);
foreach ($rowMessages as $errorCode => $rows) {
$messages[] = $errorCode . ' ' . __('in row(s):') . ' ' . implode(', ', $rows);
}
return $messages;
}
/**
* @param ProcessingErrorAggregatorInterface $errorAggregator
* @return \Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError[]
*/
protected function getSystemExceptions(ProcessingErrorAggregatorInterface $errorAggregator)
{
return $errorAggregator->getErrorsByCode([AbstractEntity::ERROR_CODE_SYSTEM_EXCEPTION]);
}
/**
* @param ProcessingErrorAggregatorInterface $errorAggregator
* @return string
*/
protected function createErrorReport(ProcessingErrorAggregatorInterface $errorAggregator)
{
$this->historyModel->loadLastInsertItem();
$sourceFile = $this->reportHelper->getReportAbsolutePath($this->historyModel->getImportedFile());
$writeOnlyErrorItems = true;
if ($this->historyModel->getData('execution_time') == ModelHistory::IMPORT_VALIDATION) {
$writeOnlyErrorItems = false;
}
$fileName = $this->reportProcessor->createReport($sourceFile, $errorAggregator, $writeOnlyErrorItems);
$this->historyModel->addErrorReportFile($fileName);
return $fileName;
}
/**
* @param string $fileName
* @return string
*/
protected function createDownloadUrlImportHistoryFile($fileName)
{
return $this->getUrl(self::IMPORT_HISTORY_FILE_DOWNLOAD_ROUTE, ['filename' => $fileName]);
}
}