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