From b697b71e59f316114b8e23a19470ef4f964e903a Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 30 Jan 2020 18:56:08 +0100 Subject: [PATCH] Some updates to the tag overview for #3066 and #3067 --- .../Controllers/Chart/ReportController.php | 4 +- app/Http/Controllers/TagController.php | 9 +- app/Repositories/Tag/TagRepository.php | 123 +++++++++--------- .../Tag/TagRepositoryInterface.php | 53 ++++---- resources/views/v1/tags/index.twig | 41 +++--- 5 files changed, 127 insertions(+), 103 deletions(-) diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php index 6f240e45c0..4213fa12fb 100644 --- a/app/Http/Controllers/Chart/ReportController.php +++ b/app/Http/Controllers/Chart/ReportController.php @@ -159,7 +159,9 @@ class ReportController extends Controller // get journals for entire period: $data = []; - $chartData = []; + $chartData = [ + + ]; /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); $collector->setRange($start, $end)->withAccountInformation(); diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php index d439656e47..bb8cea2936 100644 --- a/app/Http/Controllers/TagController.php +++ b/app/Http/Controllers/TagController.php @@ -201,18 +201,17 @@ class TagController extends Controller $newestTagDate = null === $repository->newestTag() ? new Carbon : $repository->newestTag()->date; $oldestTagDate->startOfYear(); $newestTagDate->endOfYear(); - $clouds = []; - $clouds['no-date'] = $repository->tagCloud(null); + $tags = []; + $tags['no-date'] = $repository->getTagsInYear(null); while ($newestTagDate > $oldestTagDate) { $year = $newestTagDate->year; - $clouds[$year] = $repository->tagCloud($year); - + $tags[$year] = $repository->getTagsInYear($year); $newestTagDate->subYear(); } $count = $repository->count(); - return view('tags.index', compact('clouds', 'count')); + return view('tags.index', compact('tags', 'count')); } /** diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index 2f5c8d0ce6..983b08948c 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -30,7 +30,6 @@ use FireflyIII\Models\Location; use FireflyIII\Models\Tag; use FireflyIII\Models\TransactionType; use FireflyIII\User; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; use Log; @@ -75,6 +74,19 @@ class TagRepository implements TagRepositoryInterface return true; } + /** + * Destroy all tags. + */ + public function destroyAll(): void + { + $tags = $this->get(); + /** @var Tag $tag */ + foreach ($tags as $tag) { + DB::table('tag_transaction_journal')->where('tag_id', $tag->id)->delete(); + $tag->delete(); + } + } + /** * @param Tag $tag * @param Carbon $start @@ -157,6 +169,50 @@ class TagRepository implements TagRepositoryInterface return $tags; } + /** + * @inheritDoc + */ + public function getLocation(Tag $tag): ?Location + { + return $tag->locations()->first(); + } + + /** + * @param int|null $year + * + * @return Collection + */ + public function getTagsInYear(?int $year): array + { + // get all tags in the year (if present): + $tagQuery = $this->user->tags()->with(['locations'])->orderBy('tags.tag'); + + // add date range (or not): + if (null === $year) { + Log::debug('Get tags without a date.'); + $tagQuery->whereNull('tags.date'); + } + + if (null !== $year) { + Log::debug(sprintf('Get tags with year %s.', $year)); + $tagQuery->where('tags.date', '>=', $year . '-01-01 00:00:00')->where('tags.date', '<=', $year . '-12-31 23:59:59'); + } + $collection = $tagQuery->get(); + $return = []; + /** @var Tag $tag */ + foreach ($collection as $tag) { + // return value for tag cloud: + $return[$tag->id] = [ + 'tag' => $tag->tag, + 'id' => $tag->id, + 'created_at' => $tag->created_at, + 'location' => $tag->locations->first(), + ]; + } + + return $return; + } + /** * @param Tag $tag * @param Carbon $start @@ -328,11 +384,13 @@ class TagRepository implements TagRepositoryInterface * @param int|null $year * * @return array + * @deprecated */ public function tagCloud(?int $year): array { // Some vars - $tags = $this->getTagsInYear($year); + $tags = $this->getTagsInYear($year); + $max = $this->getMaxAmount($tags); $min = $this->getMinAmount($tags); $diff = bcsub($max, $min); @@ -364,7 +422,7 @@ class TagRepository implements TagRepositoryInterface 'tag' => $tag->tag, 'id' => $tag->id, 'created_at' => $tag->created_at, - 'location' => $this->getLocation($tag), + 'location' => $this->getLocation($tag), ]; } @@ -422,8 +480,8 @@ class TagRepository implements TagRepositoryInterface $location->locatable()->associate($tag); } - $location->latitude = $data['latitude'] ?? config('firefly.default_location.latitude'); - $location->longitude = $data['longitude'] ?? config('firefly.default_location.longitude'); + $location->latitude = $data['latitude'] ?? config('firefly.default_location.latitude'); + $location->longitude = $data['longitude'] ?? config('firefly.default_location.longitude'); $location->zoom_level = $data['zoom_level'] ?? config('firefly.default_location.zoom_level'); $location->save(); } @@ -481,59 +539,4 @@ class TagRepository implements TagRepositoryInterface return $min; } - - /** - * @param int|null $year - * - * @return Collection - */ - private function getTagsInYear(?int $year): Collection - { - // get all tags in the year (if present): - $tagQuery = $this->user->tags() - ->leftJoin('tag_transaction_journal', 'tag_transaction_journal.tag_id', '=', 'tags.id') - ->leftJoin('transaction_journals', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - ->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') - ->where( - static function (Builder $query) { - $query->where('transactions.amount', '>', 0); - $query->orWhereNull('transactions.amount'); - } - ) - ->groupBy(['tags.id', 'tags.tag', 'tags.created_at']); - - // add date range (or not): - if (null === $year) { - Log::debug('Get tags without a date.'); - $tagQuery->whereNull('tags.date'); - } - if (null !== $year) { - Log::debug(sprintf('Get tags with year %s.', $year)); - $tagQuery->where('tags.date', '>=', $year . '-01-01 00:00:00')->where('tags.date', '<=', $year . '-12-31 23:59:59'); - } - - return $tagQuery->get(['tags.id', 'tags.tag','tags.created_at', DB::raw('SUM(transactions.amount) as amount_sum')]); - - } - - /** - * Destroy all tags. - */ - public function destroyAll(): void - { - $tags = $this->get(); - /** @var Tag $tag */ - foreach ($tags as $tag) { - DB::table('tag_transaction_journal')->where('tag_id', $tag->id)->delete(); - $tag->delete(); - } - } - - /** - * @inheritDoc - */ - public function getLocation(Tag $tag): ?Location - { - return $tag->locations()->first(); - } } diff --git a/app/Repositories/Tag/TagRepositoryInterface.php b/app/Repositories/Tag/TagRepositoryInterface.php index ef0abbc57d..01d32c26b9 100644 --- a/app/Repositories/Tag/TagRepositoryInterface.php +++ b/app/Repositories/Tag/TagRepositoryInterface.php @@ -34,20 +34,6 @@ use Illuminate\Support\Collection; interface TagRepositoryInterface { - /** - * Return location, or NULL. - * - * @param Tag $tag - * - * @return Location|null - */ - public function getLocation(Tag $tag): ?Location; - - /** - * Destroy all tags. - */ - public function destroyAll(): void; - /** * @return int */ @@ -62,6 +48,11 @@ interface TagRepositoryInterface */ public function destroy(Tag $tag): bool; + /** + * Destroy all tags. + */ + public function destroyAll(): void; + /** * @param Tag $tag * @param Carbon $start @@ -87,14 +78,6 @@ interface TagRepositoryInterface */ public function findByTag(string $tag): ?Tag; - /** - * Find one or more tags based on the query. - * @param string $query - * - * @return Collection - */ - public function searchTag(string $query): Collection; - /** * @param int $tagId * @@ -116,6 +99,22 @@ interface TagRepositoryInterface */ public function get(): Collection; + /** + * Return location, or NULL. + * + * @param Tag $tag + * + * @return Location|null + */ + public function getLocation(Tag $tag): ?Location; + + /** + * @param int|null $year + * + * @return Collection + */ + public function getTagsInYear(?int $year): array; + /** * @param Tag $tag * @param Carbon $start @@ -146,6 +145,15 @@ interface TagRepositoryInterface */ public function oldestTag(): ?Tag; + /** + * Find one or more tags based on the query. + * + * @param string $query + * + * @return Collection + */ + public function searchTag(string $query): Collection; + /** * Search the users tags. * @@ -191,6 +199,7 @@ interface TagRepositoryInterface /** * Generates a tag cloud. + * @deprecated * * @param int|null $year * diff --git a/resources/views/v1/tags/index.twig b/resources/views/v1/tags/index.twig index 8bcd5fa0da..95b3e798cd 100644 --- a/resources/views/v1/tags/index.twig +++ b/resources/views/v1/tags/index.twig @@ -17,7 +17,7 @@
- {% for period,entries in clouds %} + {% for period, entries in tags %} {% if entries|length > 0 %}
@@ -28,19 +28,30 @@
-

+

+ +
    + {% for tagInfo in entries %} +
  • + + +
  • + {% endfor %} +
+
@@ -51,9 +62,9 @@

{{ ('no_tags_create_default')|_ }} - +