mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Towards managing mapping for file imports.
This commit is contained in:
@@ -30,6 +30,15 @@ use Illuminate\Support\MessageBag;
|
||||
*/
|
||||
interface ConfigurationInterface
|
||||
{
|
||||
/**
|
||||
* Store data associated with current stage.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function configureJob(array $data): MessageBag;
|
||||
|
||||
/**
|
||||
* Get the data necessary to show the configuration screen.
|
||||
*
|
||||
@@ -39,17 +48,6 @@ interface ConfigurationInterface
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*
|
||||
* @return ConfigurationInterface
|
||||
*/
|
||||
public function setJob(ImportJob $job);
|
||||
|
||||
/**
|
||||
* Store data associated with current stage.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function configureJob(array $data): MessageBag;
|
||||
public function setJob(ImportJob $job): void;
|
||||
}
|
||||
|
@@ -0,0 +1,296 @@
|
||||
<?php
|
||||
/**
|
||||
* ConfigureMappingHandler.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Import\Configuration\File;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
|
||||
use FireflyIII\Import\Mapper\MapperInterface;
|
||||
use FireflyIII\Models\Attachment;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use League\Csv\Exception;
|
||||
use League\Csv\Reader;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class ConfigureMappingHandler
|
||||
*/
|
||||
class ConfigureMappingHandler implements ConfigurationInterface
|
||||
{
|
||||
/** @var AttachmentHelperInterface */
|
||||
private $attachments;
|
||||
/** @var array */
|
||||
private $columnConfig;
|
||||
/** @var ImportJob */
|
||||
private $importJob;
|
||||
/** @var ImportJobRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Store data associated with current stage.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function configureJob(array $data): MessageBag
|
||||
{
|
||||
return new MessageBag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data necessary to show the configuration screen.
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getNextData(): array
|
||||
{
|
||||
$config = $this->importJob->configuration;
|
||||
$columnConfig = $this->doColumnConfig($config);
|
||||
|
||||
// in order to actually map we also need to read the FULL file.
|
||||
try {
|
||||
$reader = $this->getReader();
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
throw new FireflyException('Cannot get reader: ' . $e->getMessage());
|
||||
}
|
||||
//
|
||||
// if ($config['has-headers']) {
|
||||
// $offset = 1;
|
||||
// }
|
||||
// $stmt = (new Statement)->offset($offset);
|
||||
// $results = $stmt->process($reader);
|
||||
// $this->validSpecifics = array_keys(config('csv.import_specifics'));
|
||||
// $indexes = array_keys($this->data);
|
||||
// $rowIndex = 0;
|
||||
// foreach ($results as $rowIndex => $row) {
|
||||
// $row = $this->runSpecifics($row);
|
||||
//
|
||||
// //do something here
|
||||
// foreach ($indexes as $index) { // this is simply 1, 2, 3, etc.
|
||||
// if (!isset($row[$index])) {
|
||||
// // don't really know how to handle this. Just skip, for now.
|
||||
// continue;
|
||||
// }
|
||||
// $value = trim($row[$index]);
|
||||
// if (\strlen($value) > 0) {
|
||||
// // we can do some preprocessing here,
|
||||
// // which is exclusively to fix the tags:
|
||||
// if (null !== $this->data[$index]['preProcessMap'] && \strlen($this->data[$index]['preProcessMap']) > 0) {
|
||||
// /** @var PreProcessorInterface $preProcessor */
|
||||
// $preProcessor = app($this->data[$index]['preProcessMap']);
|
||||
// $result = $preProcessor->run($value);
|
||||
// $this->data[$index]['values'] = array_merge($this->data[$index]['values'], $result);
|
||||
//
|
||||
// Log::debug($rowIndex . ':' . $index . 'Value before preprocessor', ['value' => $value]);
|
||||
// Log::debug($rowIndex . ':' . $index . 'Value after preprocessor', ['value-new' => $result]);
|
||||
// Log::debug($rowIndex . ':' . $index . 'Value after joining', ['value-complete' => $this->data[$index]['values']]);
|
||||
//
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// $this->data[$index]['values'][] = $value;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// $setIndexes = array_keys($this->data);
|
||||
// foreach ($setIndexes as $index) {
|
||||
// $this->data[$index]['values'] = array_unique($this->data[$index]['values']);
|
||||
// asort($this->data[$index]['values']);
|
||||
// // if the count of this array is zero, there is nothing to map.
|
||||
// if (\count($this->data[$index]['values']) === 0) {
|
||||
// unset($this->data[$index]);
|
||||
// }
|
||||
// }
|
||||
// unset($setIndexes);
|
||||
//
|
||||
// // save number of rows, thus number of steps, in job:
|
||||
// $steps = $rowIndex * 5;
|
||||
// $extended = $this->job->extended_status;
|
||||
// $extended['steps'] = $steps;
|
||||
// $this->job->extended_status = $extended;
|
||||
// $this->job->save();
|
||||
//
|
||||
// return $this->data;
|
||||
// */
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*/
|
||||
public function setJob(ImportJob $job): void
|
||||
{
|
||||
$this->importJob = $job;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
$this->repository->setUser($job->user);
|
||||
$this->attachments = app(AttachmentHelperInterface::class);
|
||||
$this->columnConfig = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the "mapper" class that will eventually return the correct data for the user
|
||||
* to map against. For example: a list of asset accounts. A list of budgets. A list of tags.
|
||||
*
|
||||
* @param string $column
|
||||
*
|
||||
* @return MapperInterface
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function createMapper(string $column): MapperInterface
|
||||
{
|
||||
$mapperClass = config('csv.import_roles.' . $column . '.mapper');
|
||||
$mapperName = sprintf('\\FireflyIII\\Import\Mapper\\%s', $mapperClass);
|
||||
if (!class_exists($mapperName)) {
|
||||
throw new FireflyException(sprintf('Class "%s" does not exist. Cannot map "%s"', $mapperName, $column));
|
||||
}
|
||||
|
||||
return app($mapperName);
|
||||
}
|
||||
|
||||
/**
|
||||
* For each column in the configuration of the job, will:
|
||||
* - validate the role.
|
||||
* - validate if it can be used for mapping
|
||||
* - if so, create an entry in $columnConfig
|
||||
*
|
||||
* @param array $config
|
||||
*
|
||||
* @return array the column configuration.
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function doColumnConfig(array $config): array
|
||||
{
|
||||
/** @var array $requestMapping */
|
||||
$requestMapping = $config['column-do-mapping'] ?? [];
|
||||
$columnConfig = [];
|
||||
/**
|
||||
* @var int
|
||||
* @var bool $mustBeMapped
|
||||
*/
|
||||
foreach ($requestMapping as $index => $requested) {
|
||||
// sanitize column name, so we're sure it's valid.
|
||||
$column = $this->sanitizeColumnName($config['column-roles'][$index] ?? '_ignore');
|
||||
$doMapping = $this->doMapOfColumn($column, $requested);
|
||||
if ($doMapping) {
|
||||
// user want to map this column. And this is possible.
|
||||
$columnConfig[$index] = [
|
||||
'name' => $column,
|
||||
'options' => $this->createMapper($column)->getMap(),
|
||||
'preProcessMap' => $this->getPreProcessorName($column),
|
||||
'values' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $columnConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* For each $name given, and if the user wants to map the column, will return
|
||||
* true when the column can also be mapped.
|
||||
*
|
||||
* Unmappable columns will always return false.
|
||||
* Mappable columns will return $requested.
|
||||
*
|
||||
* @param string $name
|
||||
* @param bool $requested
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function doMapOfColumn(string $name, bool $requested): bool
|
||||
{
|
||||
$canBeMapped = config('csv.import_roles.' . $name . '.mappable');
|
||||
|
||||
return $canBeMapped && $requested;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return the name of the pre-processor: a special class that will clean up any input that may be found
|
||||
* in the users input (aka the file uploaded). Only two examples exist at this time: a space or comma separated
|
||||
* list of tags.
|
||||
*
|
||||
* @param string $column
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getPreProcessorName(string $column): string
|
||||
{
|
||||
$name = '';
|
||||
$hasPreProcess = config(sprintf('csv.import_roles.%s.pre-process-map', $column));
|
||||
$preProcessClass = config(sprintf('csv.import_roles.%s.pre-process-mapper', $column));
|
||||
|
||||
if (null !== $hasPreProcess && true === $hasPreProcess && null !== $preProcessClass) {
|
||||
$name = sprintf('\\FireflyIII\\Import\\MapperPreProcess\\%s', $preProcessClass);
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of a CSV file reader so content of the file can be read.
|
||||
*
|
||||
* @throws \League\Csv\Exception
|
||||
*/
|
||||
private function getReader(): Reader
|
||||
{
|
||||
$content = '';
|
||||
/** @var Collection $collection */
|
||||
$collection = $this->importJob->attachments;
|
||||
/** @var Attachment $attachment */
|
||||
foreach ($collection as $attachment) {
|
||||
if ($attachment->filename === 'import_file') {
|
||||
$content = $this->attachments->getAttachmentContent($attachment);
|
||||
break;
|
||||
}
|
||||
}
|
||||
$config = $this->repository->getConfiguration($this->importJob);
|
||||
$reader = Reader::createFromString($content);
|
||||
$reader->setDelimiter($config['delimiter']);
|
||||
|
||||
return $reader;
|
||||
}
|
||||
|
||||
/**
|
||||
* For each given column name, will return either the name (when it's a valid one)
|
||||
* or return the _ignore column.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function sanitizeColumnName(string $name): string
|
||||
{
|
||||
/** @var array $validColumns */
|
||||
$validColumns = array_keys(config('csv.import_roles'));
|
||||
if (!\in_array($name, $validColumns, true)) {
|
||||
$name = '_ignore';
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
}
|
394
app/Support/Import/Configuration/File/ConfigureRolesHandler.php
Normal file
394
app/Support/Import/Configuration/File/ConfigureRolesHandler.php
Normal file
@@ -0,0 +1,394 @@
|
||||
<?php
|
||||
/**
|
||||
* ConfigureRolesHandler.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Import\Configuration\File;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
|
||||
use FireflyIII\Import\Specifics\SpecificInterface;
|
||||
use FireflyIII\Models\Attachment;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use League\Csv\Exception;
|
||||
use League\Csv\Reader;
|
||||
use League\Csv\Statement;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class ConfigureRolesHandler
|
||||
*/
|
||||
class ConfigureRolesHandler implements ConfigurationInterface
|
||||
{
|
||||
/** @var AttachmentHelperInterface */
|
||||
private $attachments;
|
||||
/** @var array */
|
||||
private $examples;
|
||||
/** @var ImportJob */
|
||||
private $importJob;
|
||||
/** @var ImportJobRepositoryInterface */
|
||||
private $repository;
|
||||
/** @var int */
|
||||
private $totalColumns;
|
||||
|
||||
/**
|
||||
* Store data associated with current stage.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function configureJob(array $data): MessageBag
|
||||
{
|
||||
$config = $this->importJob->configuration;
|
||||
$count = $config['column-count'];
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$role = $data['role'][$i] ?? '_ignore';
|
||||
$mapping = (isset($data['map'][$i]) && $data['map'][$i] === '1');
|
||||
$config['column-roles'][$i] = $role;
|
||||
$config['column-do-mapping'][$i] = $mapping;
|
||||
Log::debug(sprintf('Column %d has been given role %s (mapping: %s)', $i, $role, var_export($mapping, true)));
|
||||
}
|
||||
$config = $this->ignoreUnmappableColumns($config);
|
||||
$messages = $this->configurationComplete($config);
|
||||
|
||||
if ($messages->count() === 0) {
|
||||
$this->repository->setStage($this->importJob, 'ready_to_run');
|
||||
if ($this->isMappingNecessary($config)) {
|
||||
$this->repository->setStage($this->importJob, 'map');
|
||||
}
|
||||
$this->repository->setConfiguration($this->importJob, $config);
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data necessary to show the configuration screen.
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getNextData(): array
|
||||
{
|
||||
try {
|
||||
$reader = $this->getReader();
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
throw new FireflyException($e->getMessage());
|
||||
}
|
||||
$headers = $this->getHeaders($reader);
|
||||
|
||||
// get example rows:
|
||||
$this->getExamples($reader);
|
||||
|
||||
return [
|
||||
'examples' => $this->examples,
|
||||
'roles' => $this->getRoles(),
|
||||
'total' => $this->totalColumns,
|
||||
'headers' => $headers,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set job and some start values.
|
||||
*
|
||||
* @param ImportJob $job
|
||||
*/
|
||||
public function setJob(ImportJob $job): void
|
||||
{
|
||||
$this->importJob = $job;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
$this->repository->setUser($job->user);
|
||||
$this->attachments = app(AttachmentHelperInterface::class);
|
||||
$this->totalColumns = 0;
|
||||
$this->examples = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the configuration of the job is actually complete, and valid.
|
||||
*
|
||||
* @param array $config
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
private function configurationComplete(array $config): MessageBag
|
||||
{
|
||||
$count = $config['column-count'];
|
||||
$assigned = 0;
|
||||
|
||||
// check if data actually contains amount column (foreign amount does not count)
|
||||
$hasAmount = false;
|
||||
$hasForeignAmount = false;
|
||||
$hasForeignCode = false;
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$role = $config['column-roles'][$i] ?? '_ignore';
|
||||
if ('_ignore' !== $role) {
|
||||
++$assigned;
|
||||
}
|
||||
if (\in_array($role, ['amount', 'amount_credit', 'amount_debit'])) {
|
||||
$hasAmount = true;
|
||||
}
|
||||
if ($role === 'foreign-currency-code') {
|
||||
$hasForeignCode = true;
|
||||
}
|
||||
if ($role === 'amount_foreign') {
|
||||
$hasForeignAmount = true;
|
||||
}
|
||||
}
|
||||
|
||||
// all assigned and correct foreign info
|
||||
if ($assigned > 0 && $hasAmount && ($hasForeignCode === $hasForeignAmount)) {
|
||||
return new MessageBag;
|
||||
}
|
||||
if (0 === $assigned || !$hasAmount) {
|
||||
$message = (string)trans('import.job_config_roles_rwarning');
|
||||
$messages = new MessageBag();
|
||||
$messages->add('error', $message);
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
// warn if has foreign amount but no currency code:
|
||||
if ($hasForeignAmount && !$hasForeignCode) {
|
||||
$message = (string)trans('import.job_config_roles_fa_warning');
|
||||
$messages = new MessageBag();
|
||||
$messages->add('error', $message);
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return new MessageBag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts example data from a single row and store it in the class.
|
||||
*
|
||||
* @param array $row
|
||||
*/
|
||||
private function getExampleFromRow(array $row): void
|
||||
{
|
||||
foreach ($row as $index => $value) {
|
||||
$value = trim($value);
|
||||
if (\strlen($value) > 0) {
|
||||
$this->examples[$index][] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a bunch of examples from the CSV file the user has uploaded.
|
||||
*
|
||||
* @param Reader $reader
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getExamples(Reader $reader): void
|
||||
{
|
||||
// configure example data:
|
||||
$config = $this->importJob->configuration;
|
||||
$limit = (int)config('csv.example_rows', 5);
|
||||
$offset = isset($config['has-headers']) && $config['has-headers'] === true ? 1 : 0;
|
||||
|
||||
// make statement.
|
||||
try {
|
||||
$stmt = (new Statement)->limit($limit)->offset($offset);
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
throw new FireflyException($e->getMessage());
|
||||
}
|
||||
|
||||
// grab the records:
|
||||
$records = $stmt->process($reader);
|
||||
/** @var array $row */
|
||||
foreach ($records as $row) {
|
||||
$row = array_values($row);
|
||||
$row = $this->processSpecifics($row);
|
||||
$count = \count($row);
|
||||
$this->totalColumns = $count > $this->totalColumns ? $count : $this->totalColumns;
|
||||
$this->getExampleFromRow($row);
|
||||
}
|
||||
// save column count:
|
||||
$this->saveColumCount();
|
||||
$this->makeExamplesUnique();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the header row, if one is present.
|
||||
*
|
||||
* @param Reader $reader
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getHeaders(Reader $reader): array
|
||||
{
|
||||
$headers = [];
|
||||
$config = $this->importJob->configuration;
|
||||
if ($config['has-headers']) {
|
||||
try {
|
||||
$stmt = (new Statement)->limit(1)->offset(0);
|
||||
$records = $stmt->process($reader);
|
||||
$headers = $records->fetchOne(0);
|
||||
} catch (Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
throw new FireflyException($e->getMessage());
|
||||
}
|
||||
Log::debug('Detected file headers:', $headers);
|
||||
}
|
||||
|
||||
return $headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of a CSV file reader so content of the file can be read.
|
||||
*
|
||||
* @throws \League\Csv\Exception
|
||||
*/
|
||||
private function getReader(): Reader
|
||||
{
|
||||
$content = '';
|
||||
/** @var Collection $collection */
|
||||
$collection = $this->importJob->attachments;
|
||||
/** @var Attachment $attachment */
|
||||
foreach ($collection as $attachment) {
|
||||
if ($attachment->filename === 'import_file') {
|
||||
$content = $this->attachments->getAttachmentContent($attachment);
|
||||
break;
|
||||
}
|
||||
}
|
||||
$config = $this->repository->getConfiguration($this->importJob);
|
||||
$reader = Reader::createFromString($content);
|
||||
$reader->setDelimiter($config['delimiter']);
|
||||
|
||||
return $reader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all possible roles and translate their name. Then sort them.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getRoles(): array
|
||||
{
|
||||
$roles = [];
|
||||
foreach (array_keys(config('csv.import_roles')) as $role) {
|
||||
$roles[$role] = trans('import.column_' . $role);
|
||||
}
|
||||
asort($roles);
|
||||
|
||||
return $roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the user has checked columns that cannot be mapped to any value, this function will
|
||||
* uncheck them and return the configuration again.
|
||||
*
|
||||
* @param array $config
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function ignoreUnmappableColumns(array $config): array
|
||||
{
|
||||
$count = $config['column-count'];
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$role = $config['column-roles'][$i] ?? '_ignore';
|
||||
$mapping = $config['column-do-mapping'][$i] ?? false;
|
||||
// if the column can be mapped depends on the config:
|
||||
$canMap = (bool)config(sprintf('csv.import_roles.%s.mappable', $role));
|
||||
$mapping = $mapping && $canMap;
|
||||
$config['column-do-mapping'][$i] = $mapping;
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns false when it's not necessary to map values. This saves time and is user friendly
|
||||
* (will skip to the next screen).
|
||||
*
|
||||
* @param array $config
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isMappingNecessary(array $config): bool
|
||||
{
|
||||
$count = $config['column-count'];
|
||||
$toBeMapped = 0;
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$mapping = $config['column-do-mapping'][$i] ?? false;
|
||||
if (true === $mapping) {
|
||||
++$toBeMapped;
|
||||
}
|
||||
}
|
||||
|
||||
return !(0 === $toBeMapped);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that the examples do not contain double data values.
|
||||
*/
|
||||
private function makeExamplesUnique(): void
|
||||
{
|
||||
foreach ($this->examples as $index => $values) {
|
||||
$this->examples[$index] = array_unique($values);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* if the user has configured specific fixes to be applied, they must be applied to the example data as well.
|
||||
*
|
||||
* @param array $row
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function processSpecifics(array $row): array
|
||||
{
|
||||
$config = $this->importJob->configuration;
|
||||
$specifics = $config['specifics'] ?? [];
|
||||
$names = array_keys($specifics);
|
||||
foreach ($names as $name) {
|
||||
/** @var SpecificInterface $specific */
|
||||
$specific = app('FireflyIII\Import\Specifics\\' . $name);
|
||||
$row = $specific->run($row);
|
||||
}
|
||||
|
||||
return $row;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the column count in the job. It's used in a later stage.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function saveColumCount(): void
|
||||
{
|
||||
$config = $this->importJob->configuration;
|
||||
$config['column-count'] = $this->totalColumns;
|
||||
$this->repository->setConfiguration($this->importJob, $config);
|
||||
}
|
||||
}
|
@@ -83,10 +83,8 @@ class ConfigureUploadHandler implements ConfigurationInterface
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*
|
||||
* @return ConfigurationInterface
|
||||
*/
|
||||
public function setJob(ImportJob $job)
|
||||
public function setJob(ImportJob $job): void
|
||||
{
|
||||
$this->importJob = $job;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
|
Reference in New Issue
Block a user