1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* UploadConfig.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\AccountType; |
26
|
|
|
use FireflyIII\Models\ImportJob; |
27
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
28
|
|
|
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; |
29
|
|
|
use FireflyIII\Support\Import\Configuration\ConfigurationInterface; |
30
|
|
|
use Log; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class UploadConfig. |
34
|
|
|
*/ |
35
|
|
|
class UploadConfig implements ConfigurationInterface |
36
|
|
|
{ |
37
|
|
|
/** @var AccountRepositoryInterface */ |
38
|
|
|
private $accountRepository; |
39
|
|
|
/** |
40
|
|
|
* @var ImportJob |
41
|
|
|
*/ |
42
|
|
|
private $job; |
43
|
|
|
/** @var ImportJobRepositoryInterface */ |
44
|
|
|
private $repository; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getData(): array |
50
|
|
|
{ |
51
|
|
|
$accounts = $this->accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); |
52
|
|
|
$delimiters = [ |
53
|
|
|
',' => trans('form.csv_comma'), |
54
|
|
|
';' => trans('form.csv_semicolon'), |
55
|
|
|
'tab' => trans('form.csv_tab'), |
56
|
|
|
]; |
57
|
|
|
$config = $this->getConfig(); |
58
|
|
|
$config['date-format'] = $config['date-format'] ?? 'Ymd'; |
59
|
|
|
$specifics = []; |
60
|
|
|
$this->saveConfig($config); |
61
|
|
|
|
62
|
|
|
// collect specifics. |
63
|
|
|
foreach (config('csv.import_specifics') as $name => $className) { |
64
|
|
|
$specifics[$name] = [ |
65
|
|
|
'name' => $className::getName(), |
66
|
|
|
'description' => $className::getDescription(), |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$data = [ |
71
|
|
|
'accounts' => app('expandedform')->makeSelectList($accounts), |
72
|
|
|
'specifix' => [], |
73
|
|
|
'delimiters' => $delimiters, |
74
|
|
|
'specifics' => $specifics, |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
return $data; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return possible warning to user. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getWarningMessage(): string |
86
|
|
|
{ |
87
|
|
|
return ''; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ImportJob $job |
92
|
|
|
* |
93
|
|
|
* @return ConfigurationInterface |
94
|
|
|
*/ |
95
|
|
|
public function setJob(ImportJob $job): ConfigurationInterface |
96
|
|
|
{ |
97
|
|
|
$this->job = $job; |
98
|
|
|
$this->repository = app(ImportJobRepositoryInterface::class); |
99
|
|
|
$this->accountRepository = app(AccountRepositoryInterface::class); |
100
|
|
|
$this->repository->setUser($job->user); |
101
|
|
|
$this->accountRepository->setUser($job->user); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Store the result. |
108
|
|
|
* |
109
|
|
|
* @param array $data |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function storeConfiguration(array $data): bool |
114
|
|
|
{ |
115
|
|
|
Log::debug('Now in Initial::storeConfiguration()'); |
116
|
|
|
$config = $this->getConfig(); |
117
|
|
|
$importId = intval($data['csv_import_account'] ?? 0); |
118
|
|
|
$account = $this->accountRepository->find($importId); |
119
|
|
|
$delimiter = strval($data['csv_delimiter']); |
120
|
|
|
|
121
|
|
|
// set "headers": |
122
|
|
|
$config['has-headers'] = intval($data['has_headers'] ?? 0) === 1; |
123
|
|
|
$config['date-format'] = strval($data['date_format']); |
124
|
|
|
$config['delimiter'] = 'tab' === $delimiter ? "\t" : $config['delimiter']; |
125
|
|
|
$config['apply-rules'] = intval($data['apply_rules'] ?? 0) === 1; |
126
|
|
|
$config['match-bills'] = intval($data['match_bills'] ?? 0) === 1; |
127
|
|
|
|
128
|
|
|
Log::debug('Entered import account.', ['id' => $importId]); |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
if (null !== $account->id) { |
132
|
|
|
Log::debug('Found account.', ['id' => $account->id, 'name' => $account->name]); |
133
|
|
|
$config['import-account'] = $account->id; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (null === $account->id) { |
137
|
|
|
Log::error('Could not find anything for csv_import_account.', ['id' => $importId]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$config = $this->storeSpecifics($data, $config); |
141
|
|
|
Log::debug('Final config is ', $config); |
142
|
|
|
|
143
|
|
|
// onto the next stage! |
144
|
|
|
|
145
|
|
|
$config['stage'] = 'roles'; |
146
|
|
|
$this->saveConfig($config); |
147
|
|
|
|
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Short hand method. |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
private function getConfig(): array |
157
|
|
|
{ |
158
|
|
|
return $this->repository->getConfiguration($this->job); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param array $array |
163
|
|
|
*/ |
164
|
|
|
private function saveConfig(array $array) |
165
|
|
|
{ |
166
|
|
|
$this->repository->setConfiguration($this->job, $array); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param array $data |
171
|
|
|
* @param array $config |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
private function storeSpecifics(array $data, array $config): array |
176
|
|
|
{ |
177
|
|
|
// loop specifics. |
178
|
|
|
if (isset($data['specifics']) && is_array($data['specifics'])) { |
179
|
|
|
$names = array_keys($data['specifics']); |
180
|
|
|
foreach ($names as $name) { |
181
|
|
|
// verify their content. |
182
|
|
|
$className = sprintf('FireflyIII\Import\Specifics\%s', $name); |
183
|
|
|
if (class_exists($className)) { |
184
|
|
|
$config['specifics'][$name] = 1; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $config; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|