2017-06-10 15:09:41 +02:00
|
|
|
<?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
|
2017-12-17 14:44:05 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2017-06-10 15:09:41 +02:00
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-12-16 08:03:35 +01:00
|
|
|
namespace FireflyIII\Support\Import\Configuration\File;
|
2017-06-10 15:09:41 +02:00
|
|
|
|
|
|
|
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;
|
2017-06-10 15:09:41 +02:00
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class Roles.
|
2017-06-10 15:09:41 +02:00
|
|
|
*/
|
|
|
|
class Roles implements ConfigurationInterface
|
|
|
|
{
|
2017-12-17 14:30:53 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-06-10 15:09:41 +02:00
|
|
|
private $data = [];
|
2017-11-15 12:25:49 +01:00
|
|
|
/** @var ImportJob */
|
2017-06-10 15:09:41 +02:00
|
|
|
private $job;
|
|
|
|
|
2017-07-29 08:27:39 +02:00
|
|
|
/** @var string */
|
|
|
|
private $warning = '';
|
|
|
|
|
2017-06-10 15:09:41 +02:00
|
|
|
/**
|
|
|
|
* Get the data necessary to show the configuration screen.
|
|
|
|
*
|
|
|
|
* @return array
|
2017-12-16 17:19:10 +01:00
|
|
|
* @throws \League\Csv\Exception
|
2017-12-17 14:30:53 +01:00
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
2017-06-10 15:09:41 +02:00
|
|
|
*/
|
|
|
|
public function getData(): array
|
|
|
|
{
|
2017-12-16 17:19:10 +01:00
|
|
|
$config = $this->job->configuration;
|
|
|
|
$content = $this->job->uploadFileContents();
|
|
|
|
$headers = [];
|
|
|
|
$offset = 0;
|
2017-06-10 15:09:41 +02:00
|
|
|
// create CSV reader.
|
|
|
|
$reader = Reader::createFromString($content);
|
|
|
|
$reader->setDelimiter($config['delimiter']);
|
2017-12-16 17:19:10 +01:00
|
|
|
|
2017-12-12 20:53:16 +01:00
|
|
|
if ($config['has-headers']) {
|
2017-12-16 17:19:10 +01:00
|
|
|
$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
|
|
|
}
|
2017-12-16 17:19:10 +01:00
|
|
|
// example rows:
|
|
|
|
$stmt = (new Statement)->limit(intval(config('csv.example_rows', 5)))->offset($offset);
|
2017-06-10 15:09:41 +02:00
|
|
|
// set data:
|
2017-08-13 08:49:45 +02:00
|
|
|
$roles = $this->getRoles();
|
|
|
|
asort($roles);
|
2017-06-10 15:09:41 +02:00
|
|
|
$this->data = [
|
|
|
|
'examples' => [],
|
2017-08-13 08:49:45 +02:00
|
|
|
'roles' => $roles,
|
2017-06-10 15:09:41 +02:00
|
|
|
'total' => 0,
|
2017-12-16 17:19:10 +01:00
|
|
|
'headers' => $headers,
|
2017-06-10 15:09:41 +02:00
|
|
|
];
|
|
|
|
|
2017-12-12 20:53:16 +01:00
|
|
|
$records = $stmt->process($reader);
|
|
|
|
foreach ($records as $row) {
|
2017-06-10 15:09:41 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:09:41 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
2017-06-10 15:09:41 +02:00
|
|
|
$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) {
|
2017-12-16 17:19:10 +01:00
|
|
|
$roles[$role] = trans('import.column_' . $role);
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-06-10 15:09:41 +02:00
|
|
|
$role = $config['column-roles'][$i] ?? '_ignore';
|
2017-07-23 08:32:51 +02:00
|
|
|
$mapping = $config['column-do-mapping'][$i] ?? false;
|
2017-06-10 15:09:41 +02:00
|
|
|
|
2017-11-15 12:25:49 +01:00
|
|
|
if ('_ignore' === $role && true === $mapping) {
|
2017-06-10 15:09:41 +02:00
|
|
|
$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;
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Log::debug(sprintf('Found %d columns that need mapping.', $toBeMapped));
|
2017-11-15 12:25:49 +01:00
|
|
|
if (0 === $toBeMapped) {
|
2017-06-10 15:09:41 +02:00
|
|
|
// 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.
|
2017-06-10 15:09:41 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
{
|
2017-12-16 17:19:10 +01:00
|
|
|
$names = array_keys($this->job->configuration['specifics'] ?? []);
|
2017-08-12 10:27:45 +02:00
|
|
|
foreach ($names as $name) {
|
2017-06-10 15:09:41 +02:00
|
|
|
/** @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) {
|
2017-06-10 15:09:41 +02:00
|
|
|
$role = $config['column-roles'][$i] ?? '_ignore';
|
2017-11-15 12:25:49 +01:00
|
|
|
if ('_ignore' !== $role) {
|
|
|
|
++$assigned;
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
2017-12-19 18:53:50 +01:00
|
|
|
if (in_array($role, ['amount', 'amount_credit', 'amount_debit'])) {
|
2017-07-07 08:04:21 +02:00
|
|
|
$hasAmount = true;
|
|
|
|
}
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
2017-07-07 08:04:21 +02:00
|
|
|
if ($assigned > 0 && $hasAmount) {
|
2017-06-10 15:09:41 +02:00
|
|
|
$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) {
|
2017-12-16 17:19:10 +01:00
|
|
|
$this->warning = strval(trans('import.roles_warning'));
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|