Files
firefly-iii/app/Support/Import/Configuration/File/Map.php

290 lines
9.2 KiB
PHP
Raw Normal View History

<?php
/**
2017-10-21 08:40:00 +02:00
* Map.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* 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\Import\Mapper\MapperInterface;
use FireflyIII\Import\MapperPreProcess\PreProcessorInterface;
use FireflyIII\Import\Specifics\SpecificInterface;
use FireflyIII\Models\ImportJob;
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
use League\Csv\Reader;
use Log;
/**
2017-11-15 12:25:49 +01:00
* Class Mapping.
*/
class Map implements ConfigurationInterface
{
/** @var array */
private $configuration = [];
/** @var array that holds each column to be mapped by the user */
private $data = [];
2017-11-15 12:25:49 +01:00
/** @var ImportJob */
private $job;
/** @var array */
private $validSpecifics = [];
/**
* @return array
2017-11-15 12:25:49 +01:00
*
* @throws FireflyException
*/
public function getData(): array
{
$this->configuration = $this->job->configuration;
$this->getMappableColumns();
// in order to actually map we also need all possible values from the CSV file.
$content = $this->job->uploadFileContents();
/** @var Reader $reader */
$reader = Reader::createFromString($content);
$reader->setDelimiter($this->configuration['delimiter']);
2017-12-12 20:53:16 +01:00
if($this->configuration['has-headers']) {
$reader->setHeaderOffset(0);
}
$results = $reader->getRecords();
$this->validSpecifics = array_keys(config('csv.import_specifics'));
$indexes = array_keys($this->data);
2017-06-24 13:04:41 +02:00
$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 = $row[$index];
if (strlen($value) > 0) {
// we can do some preprocessing here,
// which is exclusively to fix the tags:
2017-11-15 12:25:49 +01:00
if (null !== $this->data[$index]['preProcessMap'] && strlen($this->data[$index]['preProcessMap']) > 0) {
/** @var PreProcessorInterface $preProcessor */
2017-06-24 13:04:41 +02:00
$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]);
2017-06-24 13:04:41 +02:00
Log::debug($rowIndex . ':' . $index . 'Value after joining', ['value-complete' => $this->data[$index]['values']]);
continue;
}
$this->data[$index]['values'][] = $value;
}
}
}
2017-08-12 10:27:45 +02:00
$setIndexes = array_keys($this->data);
foreach ($setIndexes as $index) {
$this->data[$index]['values'] = array_unique($this->data[$index]['values']);
asort($this->data[$index]['values']);
}
2017-08-12 10:27:45 +02:00
unset($setIndexes);
2017-06-22 21:50:10 +02:00
// save number of rows, thus number of steps, in job:
$steps = $rowIndex * 5;
$extended = $this->job->extended_status;
2017-06-24 13:04:41 +02:00
$extended['steps'] = $steps;
2017-06-22 21:50:10 +02:00
$this->job->extended_status = $extended;
$this->job->save();
return $this->data;
}
2017-08-12 07:47:42 +02:00
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return '';
}
/**
* @param ImportJob $job
*
* @return ConfigurationInterface
*/
public function setJob(ImportJob $job): ConfigurationInterface
{
$this->job = $job;
return $this;
}
/**
* Store the result.
*
* @param array $data
*
* @return bool
*/
public function storeConfiguration(array $data): bool
{
$config = $this->job->configuration;
if (isset($data['mapping'])) {
foreach ($data['mapping'] as $index => $data) {
$config['column-mapping-config'][$index] = [];
foreach ($data as $value => $mapId) {
$mapId = intval($mapId);
2017-11-15 12:25:49 +01:00
if (0 !== $mapId) {
$config['column-mapping-config'][$index][$value] = intval($mapId);
}
}
}
}
// set thing to be completed.
$config['column-mapping-complete'] = true;
$this->job->configuration = $config;
$this->job->save();
return true;
}
/**
* @param string $column
*
* @return MapperInterface
*/
private function createMapper(string $column): MapperInterface
{
$mapperClass = config('csv.import_roles.' . $column . '.mapper');
$mapperName = sprintf('\\FireflyIII\\Import\Mapper\\%s', $mapperClass);
/** @var MapperInterface $mapper */
$mapper = new $mapperName;
return $mapper;
}
/**
* @return bool
*/
private function getMappableColumns(): bool
{
$config = $this->job->configuration;
/**
2017-11-15 12:25:49 +01:00
* @var int
* @var bool $mustBeMapped
*/
foreach ($config['column-do-mapping'] as $index => $mustBeMapped) {
$column = $this->validateColumnName($config['column-roles'][$index] ?? '_ignore');
$shouldMap = $this->shouldMapColumn($column, $mustBeMapped);
if ($shouldMap) {
// create configuration entry for this specific column and add column to $this->data array for later processing.
$this->data[$index] = [
'name' => $column,
'index' => $index,
'options' => $this->createMapper($column)->getMap(),
'preProcessMap' => $this->getPreProcessorName($column),
'values' => [],
];
}
}
return true;
}
/**
* @param string $column
*
* @return string
*/
private function getPreProcessorName(string $column): string
{
$name = '';
$hasPreProcess = config('csv.import_roles.' . $column . '.pre-process-map');
$preProcessClass = config('csv.import_roles.' . $column . '.pre-process-mapper');
2017-11-15 12:25:49 +01:00
if (null !== $hasPreProcess && true === $hasPreProcess && null !== $preProcessClass) {
$name = sprintf('\\FireflyIII\\Import\\MapperPreProcess\\%s', $preProcessClass);
}
return $name;
}
/**
* @param array $row
*
* @return array
2017-11-15 12:25:49 +01:00
*
* @throws FireflyException
*/
private function runSpecifics(array $row): array
{
// run specifics here:
// and this is the point where the specifix go to work.
2017-08-12 16:41:05 +02:00
$names = array_keys($this->job->configuration['specifics']);
2017-08-12 10:27:45 +02:00
foreach ($names as $name) {
if (!in_array($name, $this->validSpecifics)) {
throw new FireflyException(sprintf('"%s" is not a valid class name', $name));
}
$class = config('csv.import_specifics.' . $name);
/** @var SpecificInterface $specific */
$specific = app($class);
// it returns the row, possibly modified:
$row = $specific->run($row);
}
return $row;
}
/**
* @param string $column
* @param bool $mustBeMapped
*
* @return bool
*/
private function shouldMapColumn(string $column, bool $mustBeMapped): bool
{
$canBeMapped = config('csv.import_roles.' . $column . '.mappable');
2017-11-15 12:25:49 +01:00
return $canBeMapped && $mustBeMapped;
}
/**
* @param string $column
*
* @return string
2017-11-15 12:25:49 +01:00
*
* @throws FireflyException
*/
private function validateColumnName(string $column): string
{
// is valid column?
$validColumns = array_keys(config('csv.import_roles'));
if (!in_array($column, $validColumns)) {
throw new FireflyException(sprintf('"%s" is not a valid column.', $column));
}
return $column;
}
2017-07-08 06:28:44 +02:00
}