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

306 lines
8.4 KiB
PHP
Raw Normal View History

<?php
/**
* Roles.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\Import\Specifics\SpecificInterface;
use FireflyIII\Models\ImportJob;
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
use League\Csv\Reader;
2017-12-12 20:53:16 +01:00
use League\Csv\Statement;
use Log;
/**
2017-11-15 12:25:49 +01:00
* Class Roles.
*/
class Roles implements ConfigurationInterface
{
2017-12-17 14:30:53 +01:00
/**
* @var array
*/
private $data = [];
2017-11-15 12:25:49 +01:00
/** @var ImportJob */
private $job;
2017-07-29 08:27:39 +02:00
/** @var string */
private $warning = '';
/**
* Get the data necessary to show the configuration screen.
*
* @return array
* @throws \League\Csv\Exception
2017-12-17 14:30:53 +01:00
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function getData(): array
{
$config = $this->job->configuration;
$content = $this->job->uploadFileContents();
$config['has-headers'] = true;
$headers = [];
$offset = 0;
// create CSV reader.
$reader = Reader::createFromString($content);
$reader->setDelimiter($config['delimiter']);
2017-12-12 20:53:16 +01:00
if ($config['has-headers']) {
$offset = 1;
// get headers:
$stmt = (new Statement)->limit(1)->offset(0);
$records = $stmt->process($reader);
$headers = $records->fetchOne();
2017-12-12 20:53:16 +01:00
}
// example rows:
$stmt = (new Statement)->limit(intval(config('csv.example_rows', 5)))->offset($offset);
// set data:
2017-08-13 08:49:45 +02:00
$roles = $this->getRoles();
asort($roles);
$this->data = [
'examples' => [],
2017-08-13 08:49:45 +02:00
'roles' => $roles,
'total' => 0,
'headers' => $headers,
];
2017-12-12 20:53:16 +01:00
$records = $stmt->process($reader);
foreach ($records as $row) {
$row = $this->processSpecifics($row);
$count = count($row);
$this->data['total'] = $count > $this->data['total'] ? $count : $this->data['total'];
$this->processRow($row);
}
$this->updateColumCount();
$this->makeExamplesUnique();
return $this->data;
}
2017-07-29 08:27:39 +02:00
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return $this->warning;
}
2017-07-07 08:04:21 +02:00
/**
* @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
{
Log::debug('Now in storeConfiguration of Roles.');
$config = $this->job->configuration;
$count = $config['column-count'];
2017-11-15 12:25:49 +01:00
for ($i = 0; $i < $count; ++$i) {
$role = $data['role'][$i] ?? '_ignore';
$mapping = isset($data['map'][$i]) && $data['map'][$i] === '1' ? true : false;
$config['column-roles'][$i] = $role;
$config['column-do-mapping'][$i] = $mapping;
Log::debug(sprintf('Column %d has been given role %s', $i, $role));
}
$this->job->configuration = $config;
$this->job->save();
$this->ignoreUnmappableColumns();
$this->setRolesComplete();
$this->isMappingNecessary();
return true;
}
/**
* @return array
*/
private function getRoles(): array
{
$roles = [];
foreach (array_keys(config('csv.import_roles')) as $role) {
$roles[$role] = trans('import.column_' . $role);
}
return $roles;
}
/**
* @return bool
*/
private function ignoreUnmappableColumns(): bool
{
$config = $this->job->configuration;
$count = $config['column-count'];
2017-11-15 12:25:49 +01:00
for ($i = 0; $i < $count; ++$i) {
$role = $config['column-roles'][$i] ?? '_ignore';
2017-07-23 08:32:51 +02:00
$mapping = $config['column-do-mapping'][$i] ?? false;
2017-11-15 12:25:49 +01:00
if ('_ignore' === $role && true === $mapping) {
$mapping = false;
Log::debug(sprintf('Column %d has type %s so it cannot be mapped.', $i, $role));
}
$config['column-do-mapping'][$i] = $mapping;
}
$this->job->configuration = $config;
$this->job->save();
return true;
}
/**
* @return bool
*/
private function isMappingNecessary()
{
$config = $this->job->configuration;
$count = $config['column-count'];
$toBeMapped = 0;
2017-11-15 12:25:49 +01:00
for ($i = 0; $i < $count; ++$i) {
2017-07-23 08:32:51 +02:00
$mapping = $config['column-do-mapping'][$i] ?? false;
2017-11-15 12:25:49 +01:00
if (true === $mapping) {
++$toBeMapped;
}
}
Log::debug(sprintf('Found %d columns that need mapping.', $toBeMapped));
2017-11-15 12:25:49 +01:00
if (0 === $toBeMapped) {
// skip setting of map, because none need to be mapped:
$config['column-mapping-complete'] = true;
$this->job->configuration = $config;
$this->job->save();
}
return true;
}
/**
2017-11-15 12:25:49 +01:00
* make unique example data.
*/
private function makeExamplesUnique(): bool
{
foreach ($this->data['examples'] as $index => $values) {
$this->data['examples'][$index] = array_unique($values);
}
return true;
}
/**
* @param array $row
*
* @return bool
*/
private function processRow(array $row): bool
{
foreach ($row as $index => $value) {
$value = trim($value);
if (strlen($value) > 0) {
$this->data['examples'][$index][] = $value;
}
}
return true;
}
/**
* run specifics here:
* and this is the point where the specifix go to work.
*
* @param array $row
*
* @return array
*/
private function processSpecifics(array $row): array
{
$names = array_keys($this->job->configuration['specifics'] ?? []);
2017-08-12 10:27:45 +02:00
foreach ($names as $name) {
/** @var SpecificInterface $specific */
$specific = app('FireflyIII\Import\Specifics\\' . $name);
$row = $specific->run($row);
}
return $row;
}
/**
* @return bool
*/
private function setRolesComplete(): bool
{
2017-07-07 08:04:21 +02:00
$config = $this->job->configuration;
$count = $config['column-count'];
$assigned = 0;
$hasAmount = false;
2017-11-15 12:25:49 +01:00
for ($i = 0; $i < $count; ++$i) {
$role = $config['column-roles'][$i] ?? '_ignore';
2017-11-15 12:25:49 +01:00
if ('_ignore' !== $role) {
++$assigned;
}
2017-11-22 21:12:27 +01:00
if (in_array($role, ['amount', 'amount_credit', 'amount_debet'])) {
2017-07-07 08:04:21 +02:00
$hasAmount = true;
}
}
2017-07-07 08:04:21 +02:00
if ($assigned > 0 && $hasAmount) {
$config['column-roles-complete'] = true;
$this->job->configuration = $config;
$this->job->save();
2017-07-29 08:27:39 +02:00
$this->warning = '';
}
2017-11-15 12:25:49 +01:00
if (0 === $assigned || !$hasAmount) {
$this->warning = strval(trans('import.roles_warning'));
}
return true;
}
/**
* @return bool
*/
private function updateColumCount(): bool
{
$config = $this->job->configuration;
$count = $this->data['total'];
$config['column-count'] = $count;
$this->job->configuration = $config;
$this->job->save();
return true;
}
2017-07-07 08:09:42 +02:00
}