2016-08-06 06:21:25 +02:00
|
|
|
<?php
|
|
|
|
|
/**
|
2016-08-06 06:21:46 +02:00
|
|
|
* CsvSetup.php
|
2016-08-06 06:21:25 +02:00
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
|
*
|
|
|
|
|
* See the LICENSE file for details.
|
2016-08-06 06:21:25 +02:00
|
|
|
*/
|
|
|
|
|
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-08-06 06:21:25 +02:00
|
|
|
|
2016-08-06 06:21:46 +02:00
|
|
|
namespace FireflyIII\Import\Setup;
|
2016-08-06 06:21:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
use ExpandedForm;
|
2016-09-25 08:20:17 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-08-06 06:21:25 +02:00
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
|
use FireflyIII\Models\AccountType;
|
|
|
|
|
use FireflyIII\Models\ImportJob;
|
2016-10-10 07:16:05 +02:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2017-06-07 11:13:04 +02:00
|
|
|
use FireflyIII\Support\Import\CsvImportSupportTrait;
|
2016-08-06 06:21:25 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Log;
|
|
|
|
|
use Symfony\Component\HttpFoundation\FileBag;
|
|
|
|
|
|
|
|
|
|
/**
|
2016-08-06 06:21:46 +02:00
|
|
|
* Class CsvSetup
|
2016-08-06 06:21:25 +02:00
|
|
|
*
|
|
|
|
|
* @package FireflyIII\Import\Importer
|
|
|
|
|
*/
|
2016-08-06 06:21:46 +02:00
|
|
|
class CsvSetup implements SetupInterface
|
2016-08-06 06:21:25 +02:00
|
|
|
{
|
2017-06-07 11:13:04 +02:00
|
|
|
use CsvImportSupportTrait;
|
2016-08-06 06:21:25 +02:00
|
|
|
/** @var Account */
|
|
|
|
|
public $defaultImportAccount;
|
|
|
|
|
/** @var ImportJob */
|
|
|
|
|
public $job;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* CsvImporter constructor.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->defaultImportAccount = new Account;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create initial (empty) configuration array.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function configure(): bool
|
|
|
|
|
{
|
|
|
|
|
if (is_null($this->job->configuration) || (is_array($this->job->configuration) && count($this->job->configuration) === 0)) {
|
|
|
|
|
Log::debug('No config detected, will create empty one.');
|
2016-09-25 08:32:53 +02:00
|
|
|
$this->job->configuration = config('csv.default_config');
|
2016-08-06 06:21:25 +02:00
|
|
|
$this->job->save();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// need to do nothing, for now.
|
|
|
|
|
Log::debug('Detected config in upload, will use that one. ', $this->job->configuration);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getConfigurationData(): array
|
|
|
|
|
{
|
2016-10-10 07:49:39 +02:00
|
|
|
/** @var AccountRepositoryInterface $accountRepository */
|
|
|
|
|
$accountRepository = app(AccountRepositoryInterface::class);
|
|
|
|
|
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
|
|
|
|
$delimiters = [
|
2016-08-06 06:21:25 +02:00
|
|
|
',' => trans('form.csv_comma'),
|
|
|
|
|
';' => trans('form.csv_semicolon'),
|
|
|
|
|
'tab' => trans('form.csv_tab'),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$specifics = [];
|
|
|
|
|
|
|
|
|
|
// collect specifics.
|
|
|
|
|
foreach (config('csv.import_specifics') as $name => $className) {
|
|
|
|
|
$specifics[$name] = [
|
|
|
|
|
'name' => $className::getName(),
|
|
|
|
|
'description' => $className::getDescription(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'accounts' => ExpandedForm::makeSelectList($accounts),
|
|
|
|
|
'specifix' => [],
|
|
|
|
|
'delimiters' => $delimiters,
|
|
|
|
|
'upload_path' => storage_path('upload'),
|
|
|
|
|
'is_upload_possible' => is_writable(storage_path('upload')),
|
|
|
|
|
'specifics' => $specifics,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method returns the data required for the view that will let the user add settings to the import job.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getDataForSettings(): array
|
|
|
|
|
{
|
2016-11-20 11:40:05 +01:00
|
|
|
Log::debug('Now in getDataForSettings()');
|
2016-08-06 06:21:25 +02:00
|
|
|
if ($this->doColumnRoles()) {
|
2016-11-20 11:40:05 +01:00
|
|
|
Log::debug('doColumnRoles() is true.');
|
2016-08-06 06:21:25 +02:00
|
|
|
$data = $this->getDataForColumnRoles();
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->doColumnMapping()) {
|
2016-11-20 11:40:05 +01:00
|
|
|
Log::debug('doColumnMapping() is true.');
|
2016-08-06 06:21:25 +02:00
|
|
|
$data = $this->getDataForColumnMapping();
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo 'no settings to do.';
|
|
|
|
|
exit;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method returns the name of the view that will be shown to the user to further configure
|
|
|
|
|
* the import job.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2016-09-25 08:32:53 +02:00
|
|
|
* @throws FireflyException
|
2016-08-06 06:21:25 +02:00
|
|
|
*/
|
|
|
|
|
public function getViewForSettings(): string
|
|
|
|
|
{
|
|
|
|
|
if ($this->doColumnRoles()) {
|
|
|
|
|
return 'import.csv.roles';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->doColumnMapping()) {
|
|
|
|
|
return 'import.csv.map';
|
|
|
|
|
}
|
2016-09-25 08:32:53 +02:00
|
|
|
throw new FireflyException('There is no view for the current CSV import step.');
|
2016-08-06 06:21:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method returns whether or not the user must configure this import
|
|
|
|
|
* job further.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function requireUserSettings(): bool
|
|
|
|
|
{
|
2016-09-25 08:32:53 +02:00
|
|
|
Log::debug(sprintf('doColumnMapping is %s', $this->doColumnMapping()));
|
|
|
|
|
Log::debug(sprintf('doColumnRoles is %s', $this->doColumnRoles()));
|
2016-08-06 06:21:25 +02:00
|
|
|
if ($this->doColumnMapping() || $this->doColumnRoles()) {
|
|
|
|
|
Log::debug('Return true');
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
Log::debug('Return false');
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-08-26 09:30:52 +02:00
|
|
|
* @param array $data
|
|
|
|
|
*
|
|
|
|
|
* @param FileBag $files
|
2016-08-06 06:21:25 +02:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function saveImportConfiguration(array $data, FileBag $files): bool
|
|
|
|
|
{
|
2016-10-10 07:16:05 +02:00
|
|
|
/** @var AccountRepositoryInterface $repository */
|
2017-02-05 16:16:15 +01:00
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
2017-02-17 06:42:36 +01:00
|
|
|
$importId = $data['csv_import_account'] ?? 0;
|
|
|
|
|
$account = $repository->find(intval($importId));
|
2016-08-06 06:21:25 +02:00
|
|
|
|
|
|
|
|
$hasHeaders = isset($data['has_headers']) && intval($data['has_headers']) === 1 ? true : false;
|
|
|
|
|
$config = $this->job->configuration;
|
|
|
|
|
$config['has-headers'] = $hasHeaders;
|
|
|
|
|
$config['date-format'] = $data['date_format'];
|
|
|
|
|
$config['delimiter'] = $data['csv_delimiter'];
|
2016-08-12 12:55:52 +02:00
|
|
|
$config['delimiter'] = $config['delimiter'] === 'tab' ? "\t" : $config['delimiter'];
|
2016-08-06 06:21:25 +02:00
|
|
|
|
2016-08-11 08:00:02 +02:00
|
|
|
Log::debug('Entered import account.', ['id' => $importId]);
|
2016-08-06 06:21:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!is_null($account->id)) {
|
|
|
|
|
Log::debug('Found account.', ['id' => $account->id, 'name' => $account->name]);
|
|
|
|
|
$config['import-account'] = $account->id;
|
|
|
|
|
} else {
|
2016-08-11 08:00:02 +02:00
|
|
|
Log::error('Could not find anything for csv_import_account.', ['id' => $importId]);
|
2016-08-06 06:21:25 +02:00
|
|
|
}
|
|
|
|
|
// loop specifics.
|
|
|
|
|
if (isset($data['specifics']) && is_array($data['specifics'])) {
|
|
|
|
|
foreach ($data['specifics'] as $name => $enabled) {
|
2016-09-17 09:50:40 +02:00
|
|
|
// verify their content.
|
|
|
|
|
$className = sprintf('FireflyIII\Import\Specifics\%s', $name);
|
|
|
|
|
if (class_exists($className)) {
|
|
|
|
|
$config['specifics'][$name] = 1;
|
|
|
|
|
}
|
2016-08-06 06:21:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->job->configuration = $config;
|
|
|
|
|
$this->job->save();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ImportJob $job
|
|
|
|
|
*/
|
|
|
|
|
public function setJob(ImportJob $job)
|
|
|
|
|
{
|
|
|
|
|
$this->job = $job;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store the settings filled in by the user, if applicable.
|
|
|
|
|
*
|
|
|
|
|
* @param Request $request
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function storeSettings(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$config = $this->job->configuration;
|
|
|
|
|
$all = $request->all();
|
|
|
|
|
if ($request->get('settings') == 'roles') {
|
|
|
|
|
$count = $config['column-count'];
|
|
|
|
|
|
|
|
|
|
$roleSet = 0; // how many roles have been defined
|
|
|
|
|
$mapSet = 0; // how many columns must be mapped
|
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
|
|
|
$selectedRole = $all['role'][$i] ?? '_ignore';
|
|
|
|
|
$doMapping = isset($all['map'][$i]) && $all['map'][$i] == '1' ? true : false;
|
|
|
|
|
if ($selectedRole == '_ignore' && $doMapping === true) {
|
|
|
|
|
$doMapping = false; // cannot map ignored columns.
|
|
|
|
|
}
|
|
|
|
|
if ($selectedRole != '_ignore') {
|
|
|
|
|
$roleSet++;
|
|
|
|
|
}
|
|
|
|
|
if ($doMapping === true) {
|
|
|
|
|
$mapSet++;
|
|
|
|
|
}
|
|
|
|
|
$config['column-roles'][$i] = $selectedRole;
|
|
|
|
|
$config['column-do-mapping'][$i] = $doMapping;
|
|
|
|
|
}
|
|
|
|
|
if ($roleSet > 0) {
|
|
|
|
|
$config['column-roles-complete'] = true;
|
|
|
|
|
$this->job->configuration = $config;
|
|
|
|
|
$this->job->save();
|
|
|
|
|
}
|
|
|
|
|
if ($mapSet === 0) {
|
|
|
|
|
// skip setting of map:
|
|
|
|
|
$config['column-mapping-complete'] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($request->get('settings') == 'map') {
|
|
|
|
|
if (isset($all['mapping'])) {
|
|
|
|
|
foreach ($all['mapping'] as $index => $data) {
|
|
|
|
|
$config['column-mapping-config'][$index] = [];
|
|
|
|
|
foreach ($data as $value => $mapId) {
|
|
|
|
|
$mapId = intval($mapId);
|
|
|
|
|
if ($mapId !== 0) {
|
|
|
|
|
$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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-12 15:10:03 +02:00
|
|
|
}
|