2017-06-10 15:09:41 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2017-12-16 08:03:35 +01:00
|
|
|
* FileConfigurator.php
|
2017-06-10 15:09:41 +02:00
|
|
|
* 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\Import\Configuration;
|
2017-06-10 15:09:41 +02:00
|
|
|
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Models\ImportJob;
|
|
|
|
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
|
2017-12-16 08:03:35 +01:00
|
|
|
use FireflyIII\Support\Import\Configuration\File\Initial;
|
|
|
|
use FireflyIII\Support\Import\Configuration\File\Map;
|
|
|
|
use FireflyIII\Support\Import\Configuration\File\Roles;
|
|
|
|
use FireflyIII\Support\Import\Configuration\File\Upload;
|
2017-06-10 15:09:41 +02:00
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
2017-12-16 08:03:35 +01:00
|
|
|
* Class FileConfigurator.
|
2017-06-10 15:09:41 +02:00
|
|
|
*/
|
2017-12-16 08:03:35 +01:00
|
|
|
class FileConfigurator implements ConfiguratorInterface
|
2017-06-10 15:09:41 +02:00
|
|
|
{
|
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-24 12:38:24 +02:00
|
|
|
/**
|
2018-01-03 07:35:29 +01:00
|
|
|
* FileConfigurator constructor.
|
2017-06-24 12:38:24 +02:00
|
|
|
*/
|
|
|
|
public function __construct()
|
2017-06-10 15:09:41 +02:00
|
|
|
{
|
2018-01-03 07:35:29 +01:00
|
|
|
Log::debug('Created FileConfigurator');
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store any data from the $data array into the job.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return bool
|
2017-11-15 12:25:49 +01:00
|
|
|
*
|
2017-06-10 15:09:41 +02:00
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function configureJob(array $data): bool
|
|
|
|
{
|
|
|
|
$class = $this->getConfigurationClass();
|
2017-06-12 19:12:07 +02:00
|
|
|
$job = $this->job;
|
2017-06-10 15:09:41 +02:00
|
|
|
/** @var ConfigurationInterface $object */
|
|
|
|
$object = new $class($this->job);
|
2017-06-12 19:12:07 +02:00
|
|
|
$object->setJob($job);
|
2017-07-29 08:27:39 +02:00
|
|
|
$result = $object->storeConfiguration($data);
|
|
|
|
$this->warning = $object->getWarningMessage();
|
2017-06-10 15:09:41 +02:00
|
|
|
|
2017-07-29 08:27:39 +02:00
|
|
|
return $result;
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the data required for the next step in the job configuration.
|
|
|
|
*
|
|
|
|
* @return array
|
2017-11-15 12:25:49 +01:00
|
|
|
*
|
2017-06-10 15:09:41 +02:00
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function getNextData(): array
|
|
|
|
{
|
|
|
|
$class = $this->getConfigurationClass();
|
2017-06-12 19:12:07 +02:00
|
|
|
$job = $this->job;
|
2017-06-10 15:09:41 +02:00
|
|
|
/** @var ConfigurationInterface $object */
|
2017-06-12 19:12:07 +02:00
|
|
|
$object = app($class);
|
|
|
|
$object->setJob($job);
|
2017-06-10 15:09:41 +02:00
|
|
|
|
|
|
|
return $object->getData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
2017-11-15 12:25:49 +01:00
|
|
|
*
|
2017-06-10 15:09:41 +02:00
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function getNextView(): string
|
|
|
|
{
|
2017-12-16 08:03:35 +01:00
|
|
|
if (!$this->job->configuration['has-file-upload']) {
|
|
|
|
return 'import.file.upload';
|
|
|
|
}
|
2017-06-10 15:09:41 +02:00
|
|
|
if (!$this->job->configuration['initial-config-complete']) {
|
2017-12-16 08:03:35 +01:00
|
|
|
return 'import.file.initial';
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
|
|
|
if (!$this->job->configuration['column-roles-complete']) {
|
2017-12-16 08:03:35 +01:00
|
|
|
return 'import.file.roles';
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
|
|
|
if (!$this->job->configuration['column-mapping-complete']) {
|
2017-12-16 08:03:35 +01:00
|
|
|
return 'import.file.map';
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new FireflyException('No view for state');
|
|
|
|
}
|
|
|
|
|
2017-07-29 08:27:39 +02:00
|
|
|
/**
|
|
|
|
* Return possible warning to user.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getWarningMessage(): string
|
|
|
|
{
|
|
|
|
return $this->warning;
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:09:41 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isJobConfigured(): bool
|
|
|
|
{
|
2017-06-24 08:37:09 +02:00
|
|
|
$config = $this->job->configuration;
|
2017-12-16 08:03:35 +01:00
|
|
|
$config['has-file-upload'] = $config['has-file-upload'] ?? false;
|
2017-06-24 08:37:09 +02:00
|
|
|
$config['initial-config-complete'] = $config['initial-config-complete'] ?? false;
|
|
|
|
$config['column-roles-complete'] = $config['column-roles-complete'] ?? false;
|
|
|
|
$config['column-mapping-complete'] = $config['column-mapping-complete'] ?? false;
|
|
|
|
$this->job->configuration = $config;
|
|
|
|
$this->job->save();
|
|
|
|
|
2017-12-16 08:03:35 +01:00
|
|
|
if ($config['initial-config-complete']
|
|
|
|
&& $config['column-roles-complete']
|
|
|
|
&& $config['column-mapping-complete']
|
|
|
|
&& $config['has-file-upload']
|
2017-06-10 15:09:41 +02:00
|
|
|
) {
|
2018-01-03 07:35:29 +01:00
|
|
|
Log::debug('isJobConfigured returns true');
|
|
|
|
|
2017-06-10 15:09:41 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-03 07:35:29 +01:00
|
|
|
Log::debug('isJobConfigured returns false');
|
2017-06-10 15:09:41 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-24 12:38:24 +02:00
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
*/
|
|
|
|
public function setJob(ImportJob $job)
|
|
|
|
{
|
|
|
|
$this->job = $job;
|
2018-01-03 07:35:29 +01:00
|
|
|
// give job default config:
|
|
|
|
$defaultConfig = [
|
|
|
|
'initial-config-complete' => false,
|
|
|
|
'has-headers' => false, // assume
|
|
|
|
'date-format' => 'Ymd', // assume
|
|
|
|
'delimiter' => ',', // assume
|
|
|
|
'import-account' => 0, // none,
|
|
|
|
'specifics' => [], // none
|
|
|
|
'column-count' => 0, // unknown
|
|
|
|
'column-roles' => [], // unknown
|
|
|
|
'column-do-mapping' => [], // not yet set which columns must be mapped
|
|
|
|
'column-roles-complete' => false, // not yet configured roles for columns
|
|
|
|
'column-mapping-config' => [], // no mapping made yet.
|
|
|
|
'column-mapping-complete' => false, // so mapping is not complete.
|
|
|
|
'apply-rules' => true,
|
|
|
|
'match-bills' => false,
|
|
|
|
];
|
|
|
|
$config = $this->job->configuration ?? [];
|
|
|
|
$finalConfig = array_merge($defaultConfig, $config);
|
|
|
|
$this->job->configuration = $finalConfig;
|
|
|
|
$this->job->save();
|
2017-06-24 12:38:24 +02:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:09:41 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
2017-11-15 12:25:49 +01:00
|
|
|
*
|
2017-06-10 15:09:41 +02:00
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
private function getConfigurationClass(): string
|
|
|
|
{
|
2018-01-03 07:35:29 +01:00
|
|
|
|
2017-06-10 15:09:41 +02:00
|
|
|
$class = false;
|
|
|
|
switch (true) {
|
2017-12-16 08:03:35 +01:00
|
|
|
case !$this->job->configuration['has-file-upload']:
|
|
|
|
$class = Upload::class;
|
|
|
|
break;
|
2017-11-15 12:25:49 +01:00
|
|
|
case !$this->job->configuration['initial-config-complete']:
|
2018-01-03 07:35:29 +01:00
|
|
|
Log::debug(sprintf('Class is %s', Initial::class));
|
|
|
|
Log::debug(sprintf('initial-config-complete is %s', var_export($this->job->configuration['initial-config-complete'], true)));
|
2017-06-14 20:13:19 +02:00
|
|
|
$class = Initial::class;
|
2017-06-10 15:09:41 +02:00
|
|
|
break;
|
2017-11-15 12:25:49 +01:00
|
|
|
case !$this->job->configuration['column-roles-complete']:
|
2017-06-14 20:13:19 +02:00
|
|
|
$class = Roles::class;
|
2017-06-10 15:09:41 +02:00
|
|
|
break;
|
2017-11-15 12:25:49 +01:00
|
|
|
case !$this->job->configuration['column-mapping-complete']:
|
2017-06-14 20:13:19 +02:00
|
|
|
$class = Map::class;
|
2017-06-10 15:09:41 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-15 12:25:49 +01:00
|
|
|
if (false === $class || 0 === strlen($class)) {
|
2017-06-10 15:09:41 +02:00
|
|
|
throw new FireflyException('Cannot handle current job state in getConfigurationClass().');
|
|
|
|
}
|
|
|
|
if (!class_exists($class)) {
|
|
|
|
throw new FireflyException(sprintf('Class %s does not exist in getConfigurationClass().', $class));
|
|
|
|
}
|
2018-01-03 07:35:29 +01:00
|
|
|
Log::debug(sprintf('Configuration class is "%s"', $class));
|
2017-06-10 15:09:41 +02:00
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
2017-07-07 08:09:42 +02:00
|
|
|
}
|