1 | <?php |
||
2 | /** |
||
3 | * PreferencesController.php |
||
4 | * Copyright (c) 2018 [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 | |||
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\Http\Request; |
||
34 | use Illuminate\Support\Collection; |
||
35 | use League\Fractal\Manager; |
||
36 | use League\Fractal\Resource\Collection as FractalCollection; |
||
37 | use League\Fractal\Resource\Item; |
||
38 | use League\Fractal\Serializer\JsonApiSerializer; |
||
39 | |||
40 | /** |
||
41 | * |
||
42 | * Class PreferenceController |
||
43 | */ |
||
44 | class PreferenceController extends Controller |
||
45 | { |
||
46 | /** |
||
47 | * LinkTypeController constructor. |
||
48 | */ |
||
49 | public function __construct() |
||
50 | { |
||
51 | parent::__construct(); |
||
52 | $this->middleware( |
||
53 | function ($request, $next) { |
||
54 | /** @var User $user */ |
||
55 | $user = auth()->user(); |
||
56 | $repository = app(AccountRepositoryInterface::class); |
||
57 | $repository->setUser($user); |
||
58 | |||
59 | // an important fallback is that the frontPageAccount array gets refilled automatically |
||
60 | // when it turns up empty. |
||
61 | $frontPageAccounts = app('preferences')->getForUser($user, 'frontPageAccounts', [])->data; |
||
62 | if (\count($frontPageAccounts) === 0) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
63 | /** @var Collection $accounts */ |
||
64 | $accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); |
||
65 | $accountIds = $accounts->pluck('id')->toArray(); |
||
66 | app('preferences')->setForUser($user, 'frontPageAccounts', $accountIds); |
||
67 | } |
||
68 | |||
69 | return $next($request); |
||
70 | } |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * List all of them. |
||
76 | * |
||
77 | * @param Request $request |
||
78 | * |
||
79 | * @return JsonResponse] |
||
80 | */ |
||
81 | public function index(Request $request): JsonResponse |
||
82 | { |
||
83 | /** @var User $user */ |
||
84 | $user = auth()->user(); |
||
85 | $available = [ |
||
86 | 'language', 'customFiscalYear', 'fiscalYearStart', 'currencyPreference', |
||
87 | 'transaction_journal_optional_fields', 'frontPageAccounts', 'viewRange', |
||
88 | 'listPageSize, twoFactorAuthEnabled', |
||
89 | ]; |
||
90 | |||
91 | $preferences = new Collection; |
||
92 | foreach ($available as $name) { |
||
93 | $pref = app('preferences')->getForUser($user, $name); |
||
94 | if (null !== $pref) { |
||
95 | $preferences->push($pref); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | // create some objects: |
||
100 | $manager = new Manager; |
||
101 | $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
||
102 | |||
103 | // present to user. |
||
104 | $manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
105 | |||
106 | /** @var PreferenceTransformer $transformer */ |
||
107 | $transformer = app(PreferenceTransformer::class); |
||
108 | $transformer->setParameters($this->parameters); |
||
109 | |||
110 | $resource = new FractalCollection($preferences, $transformer, 'preferences'); |
||
111 | |||
112 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
113 | |||
114 | |||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Return a single preference by name. |
||
119 | * |
||
120 | * @param Request $request |
||
121 | * @param Preference $preference |
||
122 | * |
||
123 | * @return JsonResponse |
||
124 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
125 | */ |
||
126 | public function show(Request $request, Preference $preference): JsonResponse |
||
127 | { |
||
128 | // create some objects: |
||
129 | $manager = new Manager; |
||
130 | $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
||
131 | |||
132 | // present to user. |
||
133 | $manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
134 | /** @var PreferenceTransformer $transformer */ |
||
135 | $transformer = app(PreferenceTransformer::class); |
||
136 | $transformer->setParameters($this->parameters); |
||
137 | |||
138 | $resource = new Item($preference, $transformer, 'preferences'); |
||
139 | |||
140 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Update a preference. |
||
145 | * |
||
146 | * @param PreferenceRequest $request |
||
147 | * @param Preference $preference |
||
148 | * |
||
149 | * @return JsonResponse |
||
150 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
151 | */ |
||
152 | public function update(PreferenceRequest $request, Preference $preference): JsonResponse |
||
153 | { |
||
154 | |||
155 | $data = $request->getAll(); |
||
156 | $newValue = $data['data']; |
||
157 | switch ($preference->name) { |
||
158 | default: |
||
159 | break; |
||
160 | case 'transaction_journal_optional_fields': |
||
161 | case 'frontPageAccounts': |
||
162 | $newValue = explode(',', $data['data']); |
||
163 | break; |
||
164 | case 'listPageSize': |
||
165 | $newValue = (int)$data['data']; |
||
166 | break; |
||
167 | case 'customFiscalYear': |
||
168 | case 'twoFactorAuthEnabled': |
||
169 | $newValue = 1 === (int)$data['data']; |
||
170 | break; |
||
171 | } |
||
172 | $result = app('preferences')->set($preference->name, $newValue); |
||
173 | |||
174 | // create some objects: |
||
175 | $manager = new Manager; |
||
176 | $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
||
177 | |||
178 | // present to user. |
||
179 | $manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
180 | /** @var PreferenceTransformer $transformer */ |
||
181 | $transformer = app(PreferenceTransformer::class); |
||
182 | $transformer->setParameters($this->parameters); |
||
183 | |||
184 | $resource = new Item($result, $transformer, 'preferences'); |
||
185 | |||
186 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
187 | |||
188 | } |
||
189 | } |
||
190 |