2017-06-10 15:09:41 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2017-10-21 08:40:00 +02:00
|
|
|
* Initial.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\Support\Import\Configuration\File;
|
2017-06-10 15:09:41 +02:00
|
|
|
|
|
|
|
use FireflyIII\Models\ImportJob;
|
2018-01-04 18:34:51 +01:00
|
|
|
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
2017-06-10 15:09:41 +02:00
|
|
|
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
|
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
2018-01-04 18:34:51 +01:00
|
|
|
* Class Initial.
|
2017-06-10 15:09:41 +02:00
|
|
|
*/
|
|
|
|
class Initial implements ConfigurationInterface
|
|
|
|
{
|
2018-01-04 18:34:51 +01:00
|
|
|
/** @var ImportJob */
|
2017-06-10 15:09:41 +02:00
|
|
|
private $job;
|
2017-07-07 13:50:15 +02:00
|
|
|
|
2018-01-04 19:33:16 +01:00
|
|
|
/** @var ImportJobRepositoryInterface */
|
|
|
|
private $repository;
|
|
|
|
|
2018-01-04 18:34:51 +01:00
|
|
|
/** @var string */
|
|
|
|
private $warning = '';
|
|
|
|
|
2018-01-04 19:33:16 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
Log::debug('Constructed Initial.');
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:09:41 +02:00
|
|
|
/**
|
2018-01-04 18:34:51 +01:00
|
|
|
* Get the data necessary to show the configuration screen.
|
|
|
|
*
|
2017-06-10 15:09:41 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getData(): array
|
|
|
|
{
|
2018-01-04 18:34:51 +01:00
|
|
|
$importFileTypes = [];
|
|
|
|
$defaultImportType = config('import.options.file.default_import_format');
|
2017-06-10 15:09:41 +02:00
|
|
|
|
2018-01-04 18:34:51 +01:00
|
|
|
foreach (config('import.options.file.import_formats') as $type) {
|
|
|
|
$importFileTypes[$type] = trans('import.import_file_type_' . $type);
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 18:34:51 +01:00
|
|
|
return [
|
|
|
|
'default_type' => $defaultImportType,
|
|
|
|
'file_types' => $importFileTypes,
|
2017-06-10 15:09:41 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-08-12 07:47:42 +02:00
|
|
|
/**
|
|
|
|
* Return possible warning to user.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getWarningMessage(): string
|
|
|
|
{
|
2018-01-04 18:34:51 +01:00
|
|
|
return $this->warning;
|
2017-08-12 07:47:42 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 19:12:07 +02:00
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
*
|
|
|
|
* @return ConfigurationInterface
|
|
|
|
*/
|
|
|
|
public function setJob(ImportJob $job): ConfigurationInterface
|
|
|
|
{
|
2018-01-04 19:33:16 +01:00
|
|
|
$this->job = $job;
|
|
|
|
$this->repository = app(ImportJobRepositoryInterface::class);
|
|
|
|
$this->repository->setUser($job->user);
|
2017-06-12 19:12:07 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:09:41 +02:00
|
|
|
/**
|
|
|
|
* Store the result.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function storeConfiguration(array $data): bool
|
|
|
|
{
|
2018-01-04 18:34:51 +01:00
|
|
|
Log::debug('Now in storeConfiguration for file Upload.');
|
2018-01-04 19:33:16 +01:00
|
|
|
$config = $this->getConfig();
|
|
|
|
$type = $data['import_file_type'] ?? 'csv'; // assume it's a CSV file.
|
|
|
|
$config['file-type'] = in_array($type, config('import.options.file.import_formats')) ? $type : 'csv';
|
|
|
|
|
|
|
|
// update config:
|
|
|
|
$this->repository->setConfiguration($this->job, $config);
|
|
|
|
|
|
|
|
// make repository process file:
|
|
|
|
$uploaded = $this->repository->processFile($this->job, $data['import_file'] ?? null);
|
2018-01-04 18:34:51 +01:00
|
|
|
Log::debug(sprintf('Result of upload is %s', var_export($uploaded, true)));
|
2018-01-04 19:33:16 +01:00
|
|
|
|
2018-01-04 18:34:51 +01:00
|
|
|
// process config, if present:
|
|
|
|
if (isset($data['configuration_file'])) {
|
2018-01-04 19:33:16 +01:00
|
|
|
$this->repository->processConfiguration($this->job, $data['configuration_file']);
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
2017-06-14 20:13:19 +02:00
|
|
|
|
2018-01-04 18:34:51 +01:00
|
|
|
if (false === $uploaded) {
|
|
|
|
$this->warning = 'No valid upload.';
|
2018-01-04 19:33:16 +01:00
|
|
|
|
|
|
|
return true;
|
2017-06-10 15:09:41 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 19:33:16 +01:00
|
|
|
// if file was upload safely, assume we can go to the next stage:
|
|
|
|
$config = $this->getConfig();
|
|
|
|
$config['stage'] = 'upload-config';
|
|
|
|
$this->repository->setConfiguration($this->job, $config);
|
|
|
|
|
2017-07-07 13:50:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-04 19:33:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Short hand method.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getConfig(): array
|
|
|
|
{
|
|
|
|
return $this->repository->getConfiguration($this->job);
|
|
|
|
}
|
2017-07-07 08:09:42 +02:00
|
|
|
}
|