diff --git a/app/Import/Importer/CsvImporter.php b/app/Import/Importer/CsvImporter.php index 5722cab569..85ee7359dc 100644 --- a/app/Import/Importer/CsvImporter.php +++ b/app/Import/Importer/CsvImporter.php @@ -11,6 +11,15 @@ declare(strict_types = 1); namespace FireflyIII\Import\Importer; +use FireflyIII\Crud\Account\AccountCrud; +use FireflyIII\Import\Converter\ConverterInterface; +use FireflyIII\Import\ImportEntry; +use FireflyIII\Import\ImportResult; +use FireflyIII\Models\Account; +use FireflyIII\Models\ImportJob; +use League\Csv\Reader; +use Log; + /** * Class CsvImporter * @@ -18,5 +27,99 @@ namespace FireflyIII\Import\Importer; */ class CsvImporter implements ImporterInterface { + /** @var ImportJob */ + public $job; + /** @var Account */ + public $defaultImportAccount; + + /** + * @param ImportJob $job + */ + public function setJob(ImportJob $job) + { + $this->job = $job; + } + + /** + * Run the actual import + * + * @return bool + */ + public function start(): bool + { + $config = $this->job->configuration; + $content = $this->job->uploadFileContents(); + + if ($config['import-account'] != 0) { + $repository = app(AccountCrud::class, [$this->job->user]); + $this->defaultImportAccount = $repository->find($config['import-account']); + } + + + // create CSV reader. + $reader = Reader::createFromString($content); + $start = $config['has-headers'] ? 1 : 0; + $results = $reader->fetch(); + foreach ($results as $index => $row) { + if ($index >= $start) { + Log::debug(sprintf('Now going to import row %d.', $index)); + $this->importSingleRow($row); + } + } + + Log::debug('This call should be intercepted somehow.'); + + return true; + } + + + /** + * @param array $row + * + * @return ImportResult + */ + private function importSingleRow(array $row): ImportResult + { + $object = new ImportEntry; + $object->setUser($this->job->user); + $config = $this->job->configuration; + $result = new ImportResult; + + foreach ($row as $index => $value) { + // find the role for this column: + $role = $config['column-roles'][$index] ?? '_ignore'; + $doMap = $config['column-do-mapping'][$index] ?? false; + $converterClass = config('csv.import_roles.' . $role . '.converter'); + $mapping = $config['column-mapping-config'][$index] ?? []; + /** @var ConverterInterface $converter */ + $converter = app('FireflyIII\\Import\\Converter\\' . $converterClass); + // set some useful values for the converter: + $converter->setMapping($mapping); + $converter->setDoMap($doMap); + $converter->setUser($this->job->user); + $converter->setConfig($config); + + // run the converter for this value: + $convertedValue = $converter->convert($value); + $certainty = $converter->getCertainty(); + + + // log it. + Log::debug('Value ', ['index' => $index, 'value' => $value, 'role' => $role]); + + // store in import entry: + $object->importValue($role, $value, $certainty, $convertedValue); + // $object->fromRawValue($role, $value); + } + + $result = $object->import(); + if ($result->failed()) { + Log::error('Import of row has failed.', $result->errors->toArray()); + } + + exit; + + return true; + } } \ No newline at end of file diff --git a/app/Import/Importer/ImporterInterface.php b/app/Import/Importer/ImporterInterface.php index f5a4883a9d..308d495040 100644 --- a/app/Import/Importer/ImporterInterface.php +++ b/app/Import/Importer/ImporterInterface.php @@ -10,9 +10,25 @@ declare(strict_types = 1); namespace FireflyIII\Import\Importer; +use FireflyIII\Models\ImportJob; - +/** + * Interface ImporterInterface + * + * @package FireflyIII\Import\Importer + */ interface ImporterInterface { + /** + * Run the actual import + * + * @return bool + */ + public function start(): bool; + /** + * @param ImportJob $job + * + */ + public function setJob(ImportJob $job); } \ No newline at end of file diff --git a/app/Import/Setup/CsvSetup.php b/app/Import/Setup/CsvSetup.php index 967b9deeee..53d445037f 100644 --- a/app/Import/Setup/CsvSetup.php +++ b/app/Import/Setup/CsvSetup.php @@ -14,9 +14,6 @@ namespace FireflyIII\Import\Setup; use ExpandedForm; use FireflyIII\Crud\Account\AccountCrud; -use FireflyIII\Import\Converter\ConverterInterface; -use FireflyIII\Import\ImportEntry; -use FireflyIII\Import\ImportResult; use FireflyIII\Import\Mapper\MapperInterface; use FireflyIII\Import\MapperPreProcess\PreProcessorInterface; use FireflyIII\Models\Account; @@ -233,38 +230,6 @@ class CsvSetup implements SetupInterface $this->job = $job; } - /** - * Run the actual import - * - * @return bool - */ - public function start(): bool - { - $config = $this->job->configuration; - $content = $this->job->uploadFileContents(); - - if ($config['import-account'] != 0) { - $repository = app(AccountCrud::class, [auth()->user()]); - $this->defaultImportAccount = $repository->find($config['csv_import_account']); - } - - - // create CSV reader. - $reader = Reader::createFromString($content); - $start = $config['has-headers'] ? 1 : 0; - $results = $reader->fetch(); - foreach ($results as $index => $row) { - if ($index >= $start) { - Log::debug(sprintf('Now going to import row %d.', $index)); - $this->importSingleRow($row); - } - } - - Log::debug('This call should be intercepted somehow.'); - - return true; - } - /** * Store the settings filled in by the user, if applicable. * @@ -476,53 +441,4 @@ class CsvSetup implements SetupInterface } - - /** - * @param array $row - * - * @return ImportResult - */ - private function importSingleRow(array $row): ImportResult - { - $object = new ImportEntry; - $object->setUser($this->job->user); - $config = $this->job->configuration; - $result = new ImportResult; - - foreach ($row as $index => $value) { - // find the role for this column: - $role = $config['column-roles'][$index] ?? '_ignore'; - $doMap = $config['column-do-mapping'][$index] ?? false; - $converterClass = config('csv.import_roles.' . $role . '.converter'); - $mapping = $config['column-mapping-config'][$index] ?? []; - /** @var ConverterInterface $converter */ - $converter = app('FireflyIII\\Import\\Converter\\' . $converterClass); - // set some useful values for the converter: - $converter->setMapping($mapping); - $converter->setDoMap($doMap); - $converter->setUser($this->job->user); - $converter->setConfig($config); - - // run the converter for this value: - $convertedValue = $converter->convert($value); - $certainty = $converter->getCertainty(); - - - // log it. - Log::debug('Value ', ['index' => $index, 'value' => $value, 'role' => $role]); - - // store in import entry: - $object->importValue($role, $value, $certainty, $convertedValue); - // $object->fromRawValue($role, $value); - } - - $result = $object->import(); - if ($result->failed()) { - Log::error('Import of row has failed.', $result->errors->toArray()); - } - - exit; - - return true; - } } \ No newline at end of file diff --git a/app/Import/Setup/SetupInterface.php b/app/Import/Setup/SetupInterface.php index 497c8e4769..9055c5f98e 100644 --- a/app/Import/Setup/SetupInterface.php +++ b/app/Import/Setup/SetupInterface.php @@ -24,13 +24,6 @@ use Symfony\Component\HttpFoundation\FileBag; interface SetupInterface { - /** - * Run the actual import - * - * @return bool - */ - public function start(): bool; - /** * After uploading, and after setJob(), prepare anything that is * necessary for the configure() line.