Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Copyright © 2013-2017 Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. /**
  8. * Import model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12.  
  13. namespace AARImportModelImportScheduleImportExport;
  14.  
  15. use MagentoFrameworkAppFilesystemDirectoryList;
  16. use MagentoImportExportModelImportErrorProcessingProcessingErrorAggregatorInterface;
  17. use MagentoFrameworkFilesystem;
  18. use MagentoScheduledImportExportModelScheduledOperationData;
  19.  
  20. class Import extends MagentoScheduledImportExportModelImport implements
  21. MagentoScheduledImportExportModelScheduledOperationOperationInterface {
  22.  
  23. /**
  24. * Run single import through cron
  25. *
  26. * @param MagentoScheduledImportExportModelScheduledOperation $operation
  27. * @param $fileInfo
  28. * @return bool
  29. */
  30. public function runSingleSchedule(MagentoScheduledImportExportModelScheduledOperation $operation, $fileInfo)
  31. {
  32. $sourceFile = $fileInfo['file_path'];
  33. $result = false;
  34. if ($sourceFile) {
  35. $this->createHistoryReport($sourceFile, $operation->getEntityType());
  36. $result = $this->validateSource(
  37. MagentoImportExportModelImportAdapter::findAdapterFor(
  38. $sourceFile,
  39. $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR),
  40. $this->getData(MagentoImportExportModelImport::FIELD_FIELD_SEPARATOR)
  41. )
  42. );
  43. }
  44. $isAllowedForcedImport = $operation->getForceImport() &&
  45. $this->getProcessedRowsCount() != $this->getInvalidRowsCount();
  46. if ($isAllowedForcedImport || $result) {
  47. $this->setData(
  48. self::FIELD_NAME_VALIDATION_STRATEGY,
  49. $operation->getForceImport()
  50. ? ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_SKIP_ERRORS
  51. : ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_STOP_ON_ERROR
  52. );
  53. $result = $this->importSource();
  54. }
  55.  
  56. if ($result) {
  57. $this->invalidateIndex();
  58. }
  59. return (bool)$result;
  60. }
  61.  
  62.  
  63.  
  64. /**
  65. * Run multiple import through cron
  66. *
  67. * @param MagentoScheduledImportExportModelScheduledOperation $operation
  68. * @return bool
  69. */
  70. public function runSchedule(MagentoScheduledImportExportModelScheduledOperation $operation) {
  71. $result = false;
  72. $filesArray = [];
  73. $file = $operation->getFileInfo();
  74. $isMultiple = (strpos($file['file_name'], '*') !== false);
  75. $rootDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::ROOT);
  76.  
  77. if(strpos($file['file_name'], '*') !== false){
  78. $filePath = rtrim($file['file_path'], '\/') . '/' . strstr($file['file_name'], '.', true);
  79. // Get all files matching a pattern
  80. $paths = glob($rootDirectory->getAbsolutePath($filePath));
  81. foreach ($paths AS $path) {
  82. $updatedFile = $file;
  83. $updatedFile['file_path'] = $path;
  84. $updatedFile['file_name'] = basename($path);
  85. $mergedFile = array_merge(
  86. $updatedFile,
  87. [
  88. 'entity' => $operation->getEntityType(),
  89. 'behavior' => $operation->getBehavior(),
  90. 'operation_type' => $operation->getOperationType(),
  91. 'run_at' => $operation->getStartTime(),
  92. 'scheduled_operation_id' => $operation->getId(),
  93. ]
  94. );
  95. $filesArray[] = $mergedFile;
  96. }
  97. } else {
  98. $filesArray[] = $file;
  99. }
  100. //echo '<pre>',print_r(var_dump($filesArray)),'</pre>';
  101. // exit;
  102. //print_r($filesArray); exit;
  103. if (count($filesArray > 0)){
  104. foreach($filesArray AS $fileInfo){
  105. $result = $this->runSingleSchedule($operation, $fileInfo);
  106.  
  107. if ($result) {
  108. // Move file to archive
  109. $archiveDir = $rootDirectory->getAbsolutePath(rtrim($file['file_path'], '\/') . '/archive');
  110. if (!file_exists($archiveDir) && !is_dir($archiveDir)) {
  111. mkdir($archiveDir);
  112. }
  113. rename($fileInfo['file_path'], $archiveDir . '/' . basename($fileInfo['file_name']));
  114. } else {
  115. // Move file to fail
  116. $failDir = $rootDirectory->getAbsolutePath(rtrim($file['file_path'], '\/') . '/fail');
  117. if (!file_exists($failDir) && !is_dir($failDir)) {
  118. mkdir($failDir);
  119. }
  120. copy($fileInfo['file_path'], $failDir . '/' . basename($fileInfo['file_name']));
  121. break;
  122. }
  123. }
  124. }
  125.  
  126. return (bool)$result;
  127.  
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement