Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Copyright © 2013-2017 Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Import model
- *
- * @author Magento Core Team <core@magentocommerce.com>
- */
- namespace AARImportModelImportScheduleImportExport;
- use MagentoFrameworkAppFilesystemDirectoryList;
- use MagentoImportExportModelImportErrorProcessingProcessingErrorAggregatorInterface;
- use MagentoFrameworkFilesystem;
- use MagentoScheduledImportExportModelScheduledOperationData;
- class Import extends MagentoScheduledImportExportModelImport implements
- MagentoScheduledImportExportModelScheduledOperationOperationInterface {
- /**
- * Run single import through cron
- *
- * @param MagentoScheduledImportExportModelScheduledOperation $operation
- * @param $fileInfo
- * @return bool
- */
- public function runSingleSchedule(MagentoScheduledImportExportModelScheduledOperation $operation, $fileInfo)
- {
- $sourceFile = $fileInfo['file_path'];
- $result = false;
- if ($sourceFile) {
- $this->createHistoryReport($sourceFile, $operation->getEntityType());
- $result = $this->validateSource(
- MagentoImportExportModelImportAdapter::findAdapterFor(
- $sourceFile,
- $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR),
- $this->getData(MagentoImportExportModelImport::FIELD_FIELD_SEPARATOR)
- )
- );
- }
- $isAllowedForcedImport = $operation->getForceImport() &&
- $this->getProcessedRowsCount() != $this->getInvalidRowsCount();
- if ($isAllowedForcedImport || $result) {
- $this->setData(
- self::FIELD_NAME_VALIDATION_STRATEGY,
- $operation->getForceImport()
- ? ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_SKIP_ERRORS
- : ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_STOP_ON_ERROR
- );
- $result = $this->importSource();
- }
- if ($result) {
- $this->invalidateIndex();
- }
- return (bool)$result;
- }
- /**
- * Run multiple import through cron
- *
- * @param MagentoScheduledImportExportModelScheduledOperation $operation
- * @return bool
- */
- public function runSchedule(MagentoScheduledImportExportModelScheduledOperation $operation) {
- $result = false;
- $filesArray = [];
- $file = $operation->getFileInfo();
- $isMultiple = (strpos($file['file_name'], '*') !== false);
- $rootDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::ROOT);
- if(strpos($file['file_name'], '*') !== false){
- $filePath = rtrim($file['file_path'], '\/') . '/' . strstr($file['file_name'], '.', true);
- // Get all files matching a pattern
- $paths = glob($rootDirectory->getAbsolutePath($filePath));
- foreach ($paths AS $path) {
- $updatedFile = $file;
- $updatedFile['file_path'] = $path;
- $updatedFile['file_name'] = basename($path);
- $mergedFile = array_merge(
- $updatedFile,
- [
- 'entity' => $operation->getEntityType(),
- 'behavior' => $operation->getBehavior(),
- 'operation_type' => $operation->getOperationType(),
- 'run_at' => $operation->getStartTime(),
- 'scheduled_operation_id' => $operation->getId(),
- ]
- );
- $filesArray[] = $mergedFile;
- }
- } else {
- $filesArray[] = $file;
- }
- //echo '<pre>',print_r(var_dump($filesArray)),'</pre>';
- // exit;
- //print_r($filesArray); exit;
- if (count($filesArray > 0)){
- foreach($filesArray AS $fileInfo){
- $result = $this->runSingleSchedule($operation, $fileInfo);
- if ($result) {
- // Move file to archive
- $archiveDir = $rootDirectory->getAbsolutePath(rtrim($file['file_path'], '\/') . '/archive');
- if (!file_exists($archiveDir) && !is_dir($archiveDir)) {
- mkdir($archiveDir);
- }
- rename($fileInfo['file_path'], $archiveDir . '/' . basename($fileInfo['file_name']));
- } else {
- // Move file to fail
- $failDir = $rootDirectory->getAbsolutePath(rtrim($file['file_path'], '\/') . '/fail');
- if (!file_exists($failDir) && !is_dir($failDir)) {
- mkdir($failDir);
- }
- copy($fileInfo['file_path'], $failDir . '/' . basename($fileInfo['file_name']));
- break;
- }
- }
- }
- return (bool)$result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement