175 private $importHistoryModel;
201 \Psr\Log\LoggerInterface
$logger,
204 \
Magento\Framework\
App\Config\ScopeConfigInterface $coreConfig,
210 \
Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
217 $this->_importExportData = $importExportData;
218 $this->_coreConfig = $coreConfig;
219 $this->_importConfig = $importConfig;
220 $this->_entityFactory = $entityFactory;
221 $this->_importData = $importData;
222 $this->_csvFactory = $csvFactory;
223 $this->_httpFactory = $httpFactory;
224 $this->_uploaderFactory = $uploaderFactory;
226 $this->_behaviorFactory = $behaviorFactory;
228 $this->importHistoryModel = $importHistoryModel;
229 $this->localeDate = $localeDate;
241 if (!$this->_entityAdapter) {
242 $entities = $this->_importConfig->getEntities();
243 if (isset($entities[$this->
getEntity()])) {
245 $this->_entityAdapter = $this->_entityFactory->create($entities[$this->
getEntity()][
'model']);
246 }
catch (\Exception $e) {
247 $this->_logger->critical($e);
248 throw new \Magento\Framework\Exception\LocalizedException(
249 __(
'Please enter a correct entity model.')
255 throw new \Magento\Framework\Exception\LocalizedException(
257 'The entity adapter object must be an instance of %1 or %2.',
265 if ($this->
getEntity() != $this->_entityAdapter->getEntityTypeCode()) {
266 throw new \Magento\Framework\Exception\LocalizedException(
267 __(
'The input entity code is not equal to entity adapter code.')
271 throw new \Magento\Framework\Exception\LocalizedException(
__(
'Please enter a correct entity.'));
273 $this->_entityAdapter->setParameters($this->
getData());
287 return \Magento\ImportExport\Model\Import\Adapter::findAdapterFor(
290 $this->
getData(self::FIELD_FIELD_SEPARATOR)
306 $messages[] =
__(
'Data validation failed. Please fix the following errors and upload the file again.');
310 $error = $errorMessage .
' ' .
__(
'in row(s)') .
': ' . implode(
', ', $rows);
311 $messages[] = $error;
315 $messages[] =
__(
'The validation is complete.');
317 $messages[] =
__(
'The file is valid, but we can\'t import it for some reason.');
322 'Checked rows: %1, checked entities: %2, invalid rows: %3, total errors: %4',
334 $messages[] =
__(
'This file does not contain any data.');
347 $frontendInput =
$attribute->getFrontendInput();
348 if (
$attribute->usesSource() && in_array($frontendInput, [
'select',
'multiselect',
'boolean'])) {
349 return $frontendInput;
351 return $frontendInput ==
'date' ?
'datetime' :
'varchar';
386 if (empty($this->_data[
'entity'])) {
387 throw new \Magento\Framework\Exception\LocalizedException(
__(
'Entity is unknown'));
389 return $this->_data[
'entity'];
421 return $this->_varDirectory->getAbsolutePath(
'importexport/');
434 $this->importHistoryModel->updateReport($this);
444 'Checked rows: %1, checked entities: %2, invalid rows: %3, total errors: %4',
450 __(
'The import was successful.'),
453 $this->importHistoryModel->updateReport($this,
true);
455 $this->importHistoryModel->invalidateReport($this);
500 public function uploadSource()
503 $adapter = $this->_httpFactory->create();
504 if (!
$adapter->isValid(self::FIELD_NAME_SOURCE_FILE)) {
507 $errorMessage = $this->_importExportData->getMaxUploadSizeMessage();
509 $errorMessage =
__(
'The file was not uploaded.');
511 throw new \Magento\Framework\Exception\LocalizedException($errorMessage);
516 $uploader = $this->_uploaderFactory->create([
'fileId' => self::FIELD_NAME_SOURCE_FILE]);
517 $uploader->skipDbProcessing(
true);
523 $this->_varDirectory->delete($uploadedFile);
524 throw new \Magento\Framework\Exception\LocalizedException(
__(
'The file you uploaded has no extension.'));
529 $sourceFileRelative = $this->_varDirectory->getRelativePath($sourceFile);
531 if (strtolower($uploadedFile) != strtolower($sourceFile)) {
532 if ($this->_varDirectory->isExist($sourceFileRelative)) {
533 $this->_varDirectory->delete($sourceFileRelative);
537 $this->_varDirectory->renameFile(
538 $this->_varDirectory->getRelativePath($uploadedFile),
541 }
catch (\
Magento\Framework\Exception\FileSystemException $e) {
542 throw new \Magento\Framework\Exception\LocalizedException(
__(
'The source file moving process failed.'));
559 $sourceFile = $this->uploadSource();
562 }
catch (\Exception $e) {
563 $this->_varDirectory->delete($this->_varDirectory->getRelativePath($sourceFile));
564 throw new \Magento\Framework\Exception\LocalizedException(
__($e->getMessage()));
579 $string = $this->_varDirectory->readFile($this->_varDirectory->getRelativePath($sourceFile));
580 if ($string !==
false && substr($string, 0, 3) == pack(
"CCC", 0xef, 0xbb, 0xbf)) {
581 $string = substr($string, 3);
582 $this->_varDirectory->writeFile($this->_varDirectory->getRelativePath($sourceFile), $string);
602 $errorAggregator->initValidationStrategy(
603 $this->
getData(self::FIELD_NAME_VALIDATION_STRATEGY),
604 $this->
getData(self::FIELD_NAME_ALLOWED_ERROR_COUNT)
610 }
catch (\Exception $e) {
611 $errorAggregator->addError(
623 $result = !$errorAggregator->getErrorsCount();
638 $relatedIndexers = $this->_importConfig->getRelatedIndexers($this->
getEntity());
639 if (empty($relatedIndexers)) {
643 foreach (array_keys($relatedIndexers) as $indexerId) {
645 $indexer = $this->indexerRegistry->get($indexerId);
650 }
catch (\InvalidArgumentException $e) {
670 public function getEntityBehaviors()
673 $entities = $this->_importConfig->getEntities();
674 foreach ($entities as $entityCode => $entityData) {
675 $behaviorClassName = isset($entityData[
'behaviorModel']) ? $entityData[
'behaviorModel'] :
null;
676 if ($behaviorClassName &&
class_exists($behaviorClassName)) {
678 $behavior = $this->_behaviorFactory->create($behaviorClassName);
679 $behaviourData[$entityCode] = [
680 'token' => $behaviorClassName,
681 'code' => $behavior->getCode() .
'_behavior',
682 'notes' => $behavior->getNotes($entityCode),
685 throw new \Magento\Framework\Exception\LocalizedException(
686 __(
'The behavior token for %1 is invalid.', $entityCode)
690 return $behaviourData;
705 $uniqueBehaviors = [];
706 $behaviourData = $this->getEntityBehaviors();
707 foreach ($behaviourData as $behavior) {
708 $behaviorCode = $behavior[
'code'];
709 if (!isset($uniqueBehaviors[$behaviorCode])) {
710 $uniqueBehaviors[$behaviorCode] = $behavior[
'token'];
713 return $uniqueBehaviors;
730 $entities = $this->_importConfig->getEntities();
731 if (isset($entities[
$entity])) {
734 }
catch (\Exception $e) {
735 throw new \Magento\Framework\Exception\LocalizedException(
736 __(
'Please enter a correct entity model')
740 throw new \Magento\Framework\Exception\LocalizedException(
__(
'Please enter a correct entity model'));
761 if (is_array($sourceFileRelative)) {
762 $fileName = $sourceFileRelative[
'file_name'];
763 $sourceFileRelative = $this->_varDirectory->getRelativePath(self::IMPORT_DIR .
$fileName);
769 $fileName = basename($sourceFileRelative);
771 $copyName = $this->localeDate->gmtTimestamp() .
'_' .
$fileName;
772 $copyFile = self::IMPORT_HISTORY_DIR . $copyName;
774 if ($this->_varDirectory->isExist($sourceFileRelative)) {
775 $this->_varDirectory->copyFile($sourceFileRelative, $copyFile);
777 $content = $this->_varDirectory->getDriver()->fileGetContents($sourceFileRelative);
778 $this->_varDirectory->writeFile($copyFile,
$content);
780 }
catch (\
Magento\Framework\Exception\FileSystemException $e) {
781 throw new \Magento\Framework\Exception\LocalizedException(
__(
'Source file coping failed'));
783 $this->importHistoryModel->addReport($copyName);
const FIELD_NAME_SOURCE_FILE
const ERROR_CODE_SYSTEM_EXCEPTION
static getAttributeType(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute)
const ERROR_LEVEL_NOT_CRITICAL
getData($key='', $index=null)
elseif(isset( $params[ 'redirect_parent']))
isReportEntityType($entity=null)
const FIELD_NAME_ALLOWED_ERROR_COUNT
const BEHAVIOR_ADD_UPDATE
const DEFAULT_EMPTY_ATTRIBUTE_VALUE_CONSTANT
const FIELD_NAME_IMG_FILE_DIR
getErrorsCount(array $errorLevels=[ProcessingError::ERROR_LEVEL_CRITICAL, ProcessingError::ERROR_LEVEL_NOT_CRITICAL])
getOperationResultMessages(ProcessingErrorAggregatorInterface $validationResult)
const DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR
const FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR
_getSourceAdapter($sourceFile)
const ERROR_LEVEL_CRITICAL
createHistoryReport($sourceFileRelative, $entity, $extension=null, $result=null)
static getDefaultBehavior()
getUniqueEntityBehaviors()
const FIELD_NAME_IMG_ARCHIVE_FILE
getProcessedEntitiesCount()
validateSource(\Magento\ImportExport\Model\Import\AbstractSource $source)
const FIELD_NAME_VALIDATION_STRATEGY
const FIELD_EMPTY_ATTRIBUTE_VALUE_CONSTANT
__construct(\Psr\Log\LoggerInterface $logger, \Magento\Framework\Filesystem $filesystem, \Magento\ImportExport\Helper\Data $importExportData, \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig, \Magento\ImportExport\Model\Import\ConfigInterface $importConfig, \Magento\ImportExport\Model\Import\Entity\Factory $entityFactory, \Magento\ImportExport\Model\ResourceModel\Import\Data $importData, \Magento\ImportExport\Model\Export\Adapter\CsvFactory $csvFactory, \Magento\Framework\HTTP\Adapter\FileTransferFactory $httpFactory, \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory, \Magento\ImportExport\Model\Source\Import\Behavior\Factory $behaviorFactory, \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry, \Magento\ImportExport\Model\History $importHistoryModel, DateTime $localeDate, array $data=[])
setData($key, $value=null)
addLogComment($debugData)
const FIELD_FIELD_SEPARATOR
getRowsGroupedByErrorCode(array $errorCode=[], array $excludedCodes=[], $replaceCodeWithMessage=true)