7
7
8
8
use Magento \ImportExport \Controller \Adminhtml \ImportResult as ImportResultController ;
9
9
use Magento \ImportExport \Model \Import ;
10
- use Magento \ImportExport \Block \Adminhtml \Import \Frame \Result as ImportResultBlock ;
10
+ use Magento \ImportExport \Block \Adminhtml \Import \Frame \Result ;
11
11
use Magento \Framework \Controller \ResultFactory ;
12
12
use Magento \Framework \App \Filesystem \DirectoryList ;
13
13
use Magento \ImportExport \Model \Import \Adapter as ImportAdapter ;
14
+ use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingErrorAggregatorInterface ;
14
15
15
16
class Validate extends ImportResultController
16
17
{
@@ -29,7 +30,7 @@ public function execute()
29
30
$ data = $ this ->getRequest ()->getPostValue ();
30
31
/** @var \Magento\Framework\View\Result\Layout $resultLayout */
31
32
$ resultLayout = $ this ->resultFactory ->create (ResultFactory::TYPE_LAYOUT );
32
- /** @var $resultBlock ImportResultBlock */
33
+ /** @var $resultBlock Result */
33
34
$ resultBlock = $ resultLayout ->getLayout ()->getBlock ('import.frame.result ' );
34
35
if ($ data ) {
35
36
// common actions
@@ -66,36 +67,27 @@ public function execute()
66
67
}
67
68
68
69
/**
70
+ * Process validation result and add required error or success messages to Result block
71
+ *
69
72
* @param bool $validationResult
70
- * @param ImportResultBlock $resultBlock
73
+ * @param Result $resultBlock
71
74
* @return void
72
75
*/
73
76
private function processValidationResult ($ validationResult , $ resultBlock )
74
77
{
75
78
$ import = $ this ->getImport ();
76
- if (!$ import ->getProcessedRowsCount ()) {
77
- if (!$ import ->getErrorAggregator ()->getErrorsCount ()) {
78
- $ resultBlock ->addError (__ ('This file is empty. Please try another one. ' ));
79
+ $ errorAggregator = $ import ->getErrorAggregator ();
80
+
81
+ if ($ import ->getProcessedRowsCount ()) {
82
+ if ($ validationResult ) {
83
+ $ this ->addMessageForValidResult ($ resultBlock );
79
84
} else {
80
- foreach ($ import ->getErrorAggregator ()->getAllErrors () as $ error ) {
81
- $ resultBlock ->addError ($ error ->getErrorMessage ());
82
- }
83
- }
84
- } else {
85
- $ errorAggregator = $ import ->getErrorAggregator ();
86
- if (!$ validationResult ) {
87
85
$ resultBlock ->addError (
88
86
__ ('Data validation failed. Please fix the following errors and upload the file again. ' )
89
87
);
90
88
$ this ->addErrorMessages ($ resultBlock , $ errorAggregator );
91
- } else {
92
- if ($ import ->isImportAllowed ()) {
93
- $ resultBlock ->addSuccess (
94
- __ ('File is valid! To start import process press "Import" button ' ),
95
- true
96
- );
97
- } else {
98
- $ resultBlock ->addError (__ ('The file is valid, but we can \'t import it for some reason. ' ));
89
+ if ($ errorAggregator ->getErrorsCount ()) {
90
+ $ this ->addMessageToSkipErrors ($ resultBlock );
99
91
}
100
92
}
101
93
$ resultBlock ->addNotice (
@@ -107,6 +99,12 @@ private function processValidationResult($validationResult, $resultBlock)
107
99
$ errorAggregator ->getErrorsCount ()
108
100
)
109
101
);
102
+ } else {
103
+ if ($ errorAggregator ->getErrorsCount ()) {
104
+ $ this ->collectErrors ($ resultBlock );
105
+ } else {
106
+ $ resultBlock ->addError (__ ('This file is empty. Please try another one. ' ));
107
+ }
110
108
}
111
109
}
112
110
@@ -121,4 +119,60 @@ private function getImport()
121
119
}
122
120
return $ this ->import ;
123
121
}
122
+
123
+ /**
124
+ * Add error message to Result block and allow 'Import' button
125
+ *
126
+ * If validation strategy is equal to 'validation-skip-errors' and validation error limit is not exceeded,
127
+ * then add error message and allow 'Import' button.
128
+ *
129
+ * @param Result $resultBlock
130
+ * @return void
131
+ */
132
+ private function addMessageToSkipErrors (Result $ resultBlock )
133
+ {
134
+ $ import = $ this ->getImport ();
135
+ if (!$ import ->getErrorAggregator ()->hasFatalExceptions ()) {
136
+ $ resultBlock ->addSuccess (
137
+ __ ('Please fix errors and re-upload file or simply press "Import" button to skip rows with errors ' ),
138
+ true
139
+ );
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Add success message to Result block
145
+ *
146
+ * 1. Add message for case when imported data was checked and result is valid.
147
+ * 2. Add message for case when imported data was checked and result is valid, but import is not allowed.
148
+ *
149
+ * @param Result $resultBlock
150
+ * @return void
151
+ */
152
+ private function addMessageForValidResult (Result $ resultBlock )
153
+ {
154
+ if ($ this ->getImport ()->isImportAllowed ()) {
155
+ $ resultBlock ->addSuccess (__ ('File is valid! To start import process press "Import" button ' ), true );
156
+ } else {
157
+ $ resultBlock ->addError (__ ('The file is valid, but we can \'t import it for some reason. ' ));
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Collect errors and add error messages to Result block
163
+ *
164
+ * Get all errors from ProcessingErrorAggregatorInterface and add appropriated error messages
165
+ * to Result block.
166
+ *
167
+ * @param Result $resultBlock
168
+ * @return void
169
+ */
170
+ private function collectErrors (Result $ resultBlock )
171
+ {
172
+ $ import = $ this ->getImport ();
173
+ $ errors = $ import ->getErrorAggregator ()->getAllErrors ();
174
+ foreach ($ errors as $ error ) {
175
+ $ resultBlock ->addError ($ error ->getErrorMessage ());
176
+ }
177
+ }
124
178
}
0 commit comments