1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Initial.php |
4
|
|
|
* Copyright (c) 2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III. |
7
|
|
|
* |
8
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* Firefly III is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace FireflyIII\Support\Import\Configuration\File; |
24
|
|
|
|
25
|
|
|
use FireflyIII\Models\ImportJob; |
26
|
|
|
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; |
27
|
|
|
use FireflyIII\Support\Import\Configuration\ConfigurationInterface; |
28
|
|
|
use Log; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class Initial. |
32
|
|
|
*/ |
33
|
|
|
class Initial implements ConfigurationInterface |
34
|
|
|
{ |
35
|
|
|
/** @var ImportJob */ |
36
|
|
|
private $job; |
37
|
|
|
|
38
|
|
|
/** @var ImportJobRepositoryInterface */ |
39
|
|
|
private $repository; |
40
|
|
|
|
41
|
|
|
/** @var string */ |
42
|
|
|
private $warning = ''; |
43
|
|
|
|
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
Log::debug('Constructed Initial.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the data necessary to show the configuration screen. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function getData(): array |
55
|
|
|
{ |
56
|
|
|
$importFileTypes = []; |
57
|
|
|
$defaultImportType = config('import.options.file.default_import_format'); |
58
|
|
|
|
59
|
|
|
foreach (config('import.options.file.import_formats') as $type) { |
60
|
|
|
$importFileTypes[$type] = trans('import.import_file_type_' . $type); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return [ |
64
|
|
|
'default_type' => $defaultImportType, |
65
|
|
|
'file_types' => $importFileTypes, |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return possible warning to user. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getWarningMessage(): string |
75
|
|
|
{ |
76
|
|
|
return $this->warning; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param ImportJob $job |
81
|
|
|
* |
82
|
|
|
* @return ConfigurationInterface |
83
|
|
|
*/ |
84
|
|
|
public function setJob(ImportJob $job): ConfigurationInterface |
85
|
|
|
{ |
86
|
|
|
$this->job = $job; |
87
|
|
|
$this->repository = app(ImportJobRepositoryInterface::class); |
88
|
|
|
$this->repository->setUser($job->user); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Store the result. |
95
|
|
|
* |
96
|
|
|
* @param array $data |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function storeConfiguration(array $data): bool |
101
|
|
|
{ |
102
|
|
|
Log::debug('Now in storeConfiguration for file Upload.'); |
103
|
|
|
$config = $this->getConfig(); |
104
|
|
|
$type = $data['import_file_type'] ?? 'csv'; // assume it's a CSV file. |
105
|
|
|
$config['file-type'] = in_array($type, config('import.options.file.import_formats')) ? $type : 'csv'; |
106
|
|
|
|
107
|
|
|
// update config: |
108
|
|
|
$this->repository->setConfiguration($this->job, $config); |
109
|
|
|
|
110
|
|
|
// make repository process file: |
111
|
|
|
$uploaded = $this->repository->processFile($this->job, $data['import_file'] ?? null); |
112
|
|
|
Log::debug(sprintf('Result of upload is %s', var_export($uploaded, true))); |
113
|
|
|
|
114
|
|
|
// process config, if present: |
115
|
|
|
if (isset($data['configuration_file'])) { |
116
|
|
|
$this->repository->processConfiguration($this->job, $data['configuration_file']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (false === $uploaded) { |
120
|
|
|
$this->warning = 'No valid upload.'; |
121
|
|
|
|
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// if file was upload safely, assume we can go to the next stage: |
126
|
|
|
$config = $this->getConfig(); |
127
|
|
|
$config['stage'] = 'upload-config'; |
128
|
|
|
$this->repository->setConfiguration($this->job, $config); |
129
|
|
|
|
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Short hand method. |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
private function getConfig(): array |
139
|
|
|
{ |
140
|
|
|
return $this->repository->getConfiguration($this->job); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|