ImportResult.php 5.6 KB

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