1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PreferenceController.php |
4
|
|
|
* Copyright (c) 2019 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii). |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program 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 Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace FireflyIII\Api\V1\Controllers; |
25
|
|
|
|
26
|
|
|
use FireflyIII\Api\V1\Requests\PreferenceRequest; |
27
|
|
|
use FireflyIII\Models\AccountType; |
28
|
|
|
use FireflyIII\Models\Preference; |
29
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
30
|
|
|
use FireflyIII\Transformers\PreferenceTransformer; |
31
|
|
|
use FireflyIII\User; |
32
|
|
|
use Illuminate\Http\JsonResponse; |
33
|
|
|
use Illuminate\Support\Collection; |
34
|
|
|
use League\Fractal\Resource\Collection as FractalCollection; |
35
|
|
|
use League\Fractal\Resource\Item; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
* Class PreferenceController |
40
|
|
|
*/ |
41
|
|
|
class PreferenceController extends Controller |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* LinkTypeController constructor. |
45
|
|
|
* |
46
|
|
|
* @codeCoverageIgnore |
47
|
|
|
*/ |
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
parent::__construct(); |
51
|
|
|
$this->middleware( |
52
|
|
|
static function ($request, $next) { |
53
|
|
|
/** @var User $user */ |
54
|
|
|
$user = auth()->user(); |
55
|
|
|
$repository = app(AccountRepositoryInterface::class); |
56
|
|
|
$repository->setUser($user); |
57
|
|
|
|
58
|
|
|
// an important fallback is that the frontPageAccount array gets refilled automatically |
59
|
|
|
// when it turns up empty. |
60
|
|
|
$frontPageAccounts = app('preferences')->getForUser($user, 'frontPageAccounts', [])->data; |
61
|
|
|
if (0 === count($frontPageAccounts)) { |
|
|
|
|
62
|
|
|
/** @var Collection $accounts */ |
63
|
|
|
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); |
64
|
|
|
$accountIds = $accounts->pluck('id')->toArray(); |
65
|
|
|
app('preferences')->setForUser($user, 'frontPageAccounts', $accountIds); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $next($request); |
69
|
|
|
} |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* List all of them. |
75
|
|
|
* |
76
|
|
|
* @return JsonResponse |
77
|
|
|
* @codeCoverageIgnore |
78
|
|
|
*/ |
79
|
|
|
public function index(): JsonResponse |
80
|
|
|
{ |
81
|
|
|
/** @var User $user */ |
82
|
|
|
$user = auth()->user(); |
83
|
|
|
$available = [ |
84
|
|
|
'language', 'customFiscalYear', 'fiscalYearStart', 'currencyPreference', |
85
|
|
|
'transaction_journal_optional_fields', 'frontPageAccounts', 'viewRange', |
86
|
|
|
'listPageSize', |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
$preferences = new Collection; |
90
|
|
|
foreach ($available as $name) { |
91
|
|
|
$pref = app('preferences')->getForUser($user, $name); |
92
|
|
|
if (null !== $pref) { |
93
|
|
|
$preferences->push($pref); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$manager = $this->getManager(); |
98
|
|
|
|
99
|
|
|
/** @var PreferenceTransformer $transformer */ |
100
|
|
|
$transformer = app(PreferenceTransformer::class); |
101
|
|
|
$transformer->setParameters($this->parameters); |
102
|
|
|
|
103
|
|
|
$resource = new FractalCollection($preferences, $transformer, 'preferences'); |
104
|
|
|
|
105
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return a single preference by name. |
111
|
|
|
* |
112
|
|
|
* @param Preference $preference |
113
|
|
|
* |
114
|
|
|
* @return JsonResponse |
115
|
|
|
* @codeCoverageIgnore |
116
|
|
|
*/ |
117
|
|
|
public function show(Preference $preference): JsonResponse |
118
|
|
|
{ |
119
|
|
|
$manager = $this->getManager(); |
120
|
|
|
/** @var PreferenceTransformer $transformer */ |
121
|
|
|
$transformer = app(PreferenceTransformer::class); |
122
|
|
|
$transformer->setParameters($this->parameters); |
123
|
|
|
|
124
|
|
|
$resource = new Item($preference, $transformer, 'preferences'); |
125
|
|
|
|
126
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Update a preference. |
131
|
|
|
* |
132
|
|
|
* @param PreferenceRequest $request |
133
|
|
|
* @param Preference $preference |
134
|
|
|
* |
135
|
|
|
* @return JsonResponse |
136
|
|
|
*/ |
137
|
|
|
public function update(PreferenceRequest $request, Preference $preference): JsonResponse |
138
|
|
|
{ |
139
|
|
|
|
140
|
|
|
$data = $request->getAll(); |
141
|
|
|
$newValue = $data['data']; |
142
|
|
|
switch ($preference->name) { |
143
|
|
|
default: |
144
|
|
|
break; |
145
|
|
|
case 'transaction_journal_optional_fields': |
146
|
|
|
case 'frontPageAccounts': |
147
|
|
|
$newValue = explode(',', $data['data']); |
148
|
|
|
break; |
149
|
|
|
case 'listPageSize': |
150
|
|
|
$newValue = (int) $data['data']; |
151
|
|
|
break; |
152
|
|
|
case 'customFiscalYear': |
153
|
|
|
$newValue = 1 === (int) $data['data']; |
154
|
|
|
break; |
155
|
|
|
} |
156
|
|
|
$result = app('preferences')->set($preference->name, $newValue); |
157
|
|
|
|
158
|
|
|
$manager = $this->getManager(); |
159
|
|
|
/** @var PreferenceTransformer $transformer */ |
160
|
|
|
$transformer = app(PreferenceTransformer::class); |
161
|
|
|
$transformer->setParameters($this->parameters); |
162
|
|
|
|
163
|
|
|
$resource = new Item($result, $transformer, 'preferences'); |
164
|
|
|
|
165
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|