1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TagController.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 Carbon\Carbon; |
27
|
|
|
use FireflyIII\Api\V1\Requests\DateRequest; |
28
|
|
|
use FireflyIII\Api\V1\Requests\TagStoreRequest; |
29
|
|
|
use FireflyIII\Api\V1\Requests\TagUpdateRequest; |
30
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface; |
31
|
|
|
use FireflyIII\Models\Tag; |
32
|
|
|
use FireflyIII\Repositories\Tag\TagRepositoryInterface; |
33
|
|
|
use FireflyIII\Support\Http\Api\TransactionFilter; |
34
|
|
|
use FireflyIII\Transformers\AttachmentTransformer; |
35
|
|
|
use FireflyIII\Transformers\TagTransformer; |
36
|
|
|
use FireflyIII\Transformers\TransactionGroupTransformer; |
37
|
|
|
use FireflyIII\User; |
38
|
|
|
use Illuminate\Http\JsonResponse; |
39
|
|
|
use Illuminate\Http\Request; |
40
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
41
|
|
|
use Illuminate\Support\Collection; |
42
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
43
|
|
|
use League\Fractal\Resource\Collection as FractalCollection; |
44
|
|
|
use League\Fractal\Resource\Item; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Class TagController |
48
|
|
|
*/ |
49
|
|
|
class TagController extends Controller |
50
|
|
|
{ |
51
|
|
|
use TransactionFilter; |
52
|
|
|
|
53
|
|
|
/** @var TagRepositoryInterface The tag repository */ |
54
|
|
|
private $repository; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* TagController constructor. |
58
|
|
|
* |
59
|
|
|
* @codeCoverageIgnore |
60
|
|
|
*/ |
61
|
|
|
public function __construct() |
62
|
|
|
{ |
63
|
|
|
parent::__construct(); |
64
|
|
|
$this->middleware( |
65
|
|
|
function ($request, $next) { |
66
|
|
|
/** @var User $user */ |
67
|
|
|
$user = auth()->user(); |
68
|
|
|
|
69
|
|
|
$this->repository = app(TagRepositoryInterface::class); |
70
|
|
|
$this->repository->setUser($user); |
71
|
|
|
|
72
|
|
|
return $next($request); |
73
|
|
|
} |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param DateRequest $request |
79
|
|
|
* |
80
|
|
|
* @return JsonResponse |
81
|
|
|
*/ |
82
|
|
|
public function cloud(DateRequest $request): JsonResponse |
83
|
|
|
{ |
84
|
|
|
// parameters for boxes: |
85
|
|
|
$dates = $request->getAll(); |
86
|
|
|
$start = $dates['start']; |
87
|
|
|
$end = $dates['end']; |
88
|
|
|
|
89
|
|
|
// get all tags: |
90
|
|
|
$tags = $this->repository->get(); |
91
|
|
|
$cloud = $this->getTagCloud($tags, $start, $end); |
|
|
|
|
92
|
|
|
|
93
|
|
|
return response()->json($cloud); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Delete the resource. |
98
|
|
|
* |
99
|
|
|
* @param Tag $tag |
100
|
|
|
* |
101
|
|
|
* @return JsonResponse |
102
|
|
|
* @codeCoverageIgnore |
103
|
|
|
*/ |
104
|
|
|
public function delete(Tag $tag): JsonResponse |
105
|
|
|
{ |
106
|
|
|
$this->repository->destroy($tag); |
107
|
|
|
|
108
|
|
|
return response()->json([], 204); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* List all of them. |
113
|
|
|
* |
114
|
|
|
* @return JsonResponse |
115
|
|
|
* @codeCoverageIgnore |
116
|
|
|
*/ |
117
|
|
|
public function index(): JsonResponse |
118
|
|
|
{ |
119
|
|
|
$manager = $this->getManager(); |
120
|
|
|
// types to get, page size: |
121
|
|
|
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
122
|
|
|
|
123
|
|
|
// get list of budgets. Count it and split it. |
124
|
|
|
$collection = $this->repository->get(); |
125
|
|
|
$count = $collection->count(); |
126
|
|
|
$rules = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
127
|
|
|
|
128
|
|
|
// make paginator: |
129
|
|
|
$paginator = new LengthAwarePaginator($rules, $count, $pageSize, $this->parameters->get('page')); |
130
|
|
|
$paginator->setPath(route('api.v1.tags.index') . $this->buildParams()); |
131
|
|
|
|
132
|
|
|
/** @var TagTransformer $transformer */ |
133
|
|
|
$transformer = app(TagTransformer::class); |
134
|
|
|
$transformer->setParameters($this->parameters); |
135
|
|
|
|
136
|
|
|
$resource = new FractalCollection($rules, $transformer, 'tags'); |
137
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
138
|
|
|
|
139
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Tag $tag |
145
|
|
|
* |
146
|
|
|
* @return JsonResponse |
147
|
|
|
* @codeCoverageIgnore |
148
|
|
|
*/ |
149
|
|
|
public function attachments(Tag $tag): JsonResponse |
150
|
|
|
{ |
151
|
|
|
$manager = $this->getManager(); |
152
|
|
|
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
153
|
|
|
$collection = $this->repository->getAttachments($tag); |
154
|
|
|
|
155
|
|
|
$count = $collection->count(); |
156
|
|
|
$attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
157
|
|
|
|
158
|
|
|
// make paginator: |
159
|
|
|
$paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page')); |
160
|
|
|
$paginator->setPath(route('api.v1.tags.attachments', [$tag->id]) . $this->buildParams()); |
161
|
|
|
|
162
|
|
|
/** @var AttachmentTransformer $transformer */ |
163
|
|
|
$transformer = app(AttachmentTransformer::class); |
164
|
|
|
$transformer->setParameters($this->parameters); |
165
|
|
|
|
166
|
|
|
$resource = new FractalCollection($attachments, $transformer, 'attachments'); |
167
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
168
|
|
|
|
169
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* List single resource. |
174
|
|
|
* |
175
|
|
|
* @param Tag $tag |
176
|
|
|
* |
177
|
|
|
* @return JsonResponse |
178
|
|
|
* @codeCoverageIgnore |
179
|
|
|
*/ |
180
|
|
|
public function show(Tag $tag): JsonResponse |
181
|
|
|
{ |
182
|
|
|
$manager = $this->getManager(); |
183
|
|
|
/** @var TagTransformer $transformer */ |
184
|
|
|
$transformer = app(TagTransformer::class); |
185
|
|
|
$transformer->setParameters($this->parameters); |
186
|
|
|
|
187
|
|
|
$resource = new Item($tag, $transformer, 'tags'); |
188
|
|
|
|
189
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Store new object. |
195
|
|
|
* |
196
|
|
|
* @param TagStoreRequest $request |
197
|
|
|
* |
198
|
|
|
* @return JsonResponse |
199
|
|
|
*/ |
200
|
|
|
public function store(TagStoreRequest $request): JsonResponse |
201
|
|
|
{ |
202
|
|
|
$rule = $this->repository->store($request->getAll()); |
203
|
|
|
$manager = $this->getManager(); |
204
|
|
|
/** @var TagTransformer $transformer */ |
205
|
|
|
$transformer = app(TagTransformer::class); |
206
|
|
|
$transformer->setParameters($this->parameters); |
207
|
|
|
|
208
|
|
|
$resource = new Item($rule, $transformer, 'tags'); |
209
|
|
|
|
210
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Show all transactions. |
215
|
|
|
* |
216
|
|
|
* @param Request $request |
217
|
|
|
* @param Tag $tag |
218
|
|
|
* |
219
|
|
|
* @return JsonResponse |
220
|
|
|
* @codeCoverageIgnore |
221
|
|
|
*/ |
222
|
|
|
public function transactions(Request $request, Tag $tag): JsonResponse |
223
|
|
|
{ |
224
|
|
|
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
225
|
|
|
$type = $request->get('type') ?? 'default'; |
226
|
|
|
$this->parameters->set('type', $type); |
227
|
|
|
|
228
|
|
|
$types = $this->mapTransactionTypes($this->parameters->get('type')); |
229
|
|
|
$manager = $this->getManager(); |
230
|
|
|
/** @var User $admin */ |
231
|
|
|
$admin = auth()->user(); |
232
|
|
|
|
233
|
|
|
// use new group collector: |
234
|
|
|
/** @var GroupCollectorInterface $collector */ |
235
|
|
|
$collector = app(GroupCollectorInterface::class); |
236
|
|
|
$collector |
237
|
|
|
->setUser($admin) |
238
|
|
|
// filter on tag. |
239
|
|
|
->setTag($tag) |
240
|
|
|
// all info needed for the API: |
241
|
|
|
->withAPIInformation() |
242
|
|
|
// set page size: |
243
|
|
|
->setLimit($pageSize) |
244
|
|
|
// set page to retrieve |
245
|
|
|
->setPage($this->parameters->get('page')) |
246
|
|
|
// set types of transactions to return. |
247
|
|
|
->setTypes($types); |
248
|
|
|
|
249
|
|
|
if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) { |
250
|
|
|
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); |
251
|
|
|
} |
252
|
|
|
$paginator = $collector->getPaginatedGroups(); |
253
|
|
|
$paginator->setPath(route('api.v1.transactions.index') . $this->buildParams()); |
254
|
|
|
$transactions = $paginator->getCollection(); |
255
|
|
|
|
256
|
|
|
/** @var TransactionGroupTransformer $transformer */ |
257
|
|
|
$transformer = app(TransactionGroupTransformer::class); |
258
|
|
|
$transformer->setParameters($this->parameters); |
259
|
|
|
|
260
|
|
|
$resource = new FractalCollection($transactions, $transformer, 'transactions'); |
261
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
262
|
|
|
|
263
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Update a rule. |
268
|
|
|
* |
269
|
|
|
* @param TagUpdateRequest $request |
270
|
|
|
* @param Tag $tag |
271
|
|
|
* |
272
|
|
|
* @return JsonResponse |
273
|
|
|
*/ |
274
|
|
|
public function update(TagUpdateRequest $request, Tag $tag): JsonResponse |
275
|
|
|
{ |
276
|
|
|
$rule = $this->repository->update($tag, $request->getAll()); |
277
|
|
|
$manager = $this->getManager(); |
278
|
|
|
/** @var TagTransformer $transformer */ |
279
|
|
|
$transformer = app(TagTransformer::class); |
280
|
|
|
$transformer->setParameters($this->parameters); |
281
|
|
|
|
282
|
|
|
$resource = new Item($rule, $transformer, 'tags'); |
283
|
|
|
|
284
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
285
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param array $cloud |
290
|
|
|
* @param float $min |
291
|
|
|
* @param float $max |
292
|
|
|
* |
293
|
|
|
* @return array |
294
|
|
|
*/ |
295
|
|
|
private function analyseTagCloud(array $cloud, float $min, float $max): array |
296
|
|
|
{ |
297
|
|
|
foreach (array_keys($cloud['tags']) as $index) { |
298
|
|
|
$cloud['tags'][$index]['relative'] = round($cloud['tags'][$index]['size'] / $max, 4); |
299
|
|
|
} |
300
|
|
|
$cloud['min'] = $min; |
301
|
|
|
$cloud['max'] = $max; |
302
|
|
|
|
303
|
|
|
return $cloud; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param Collection $tags |
308
|
|
|
* @param Carbon $start |
309
|
|
|
* @param Carbon $end |
310
|
|
|
* |
311
|
|
|
* @return array |
312
|
|
|
*/ |
313
|
|
|
private function getTagCloud(Collection $tags, Carbon $start, Carbon $end): array |
314
|
|
|
{ |
315
|
|
|
$min = null; |
316
|
|
|
$max = 0; |
317
|
|
|
$cloud = [ |
318
|
|
|
'tags' => [], |
319
|
|
|
]; |
320
|
|
|
/** @var Tag $tag */ |
321
|
|
|
foreach ($tags as $tag) { |
322
|
|
|
$earned = (float) $this->repository->earnedInPeriod($tag, $start, $end); |
323
|
|
|
$spent = (float) $this->repository->spentInPeriod($tag, $start, $end); |
324
|
|
|
$size = ($spent * -1) + $earned; |
325
|
|
|
$min = $min ?? $size; |
326
|
|
|
if ($size > 0) { |
327
|
|
|
$max = $size > $max ? $size : $max; |
328
|
|
|
$cloud['tags'][] = [ |
329
|
|
|
'tag' => $tag->tag, |
330
|
|
|
'id' => $tag->id, |
331
|
|
|
'size' => $size, |
332
|
|
|
]; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
$cloud = $this->analyseTagCloud($cloud, $min, $max); |
|
|
|
|
336
|
|
|
|
337
|
|
|
return $cloud; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|