Some updates to the tag overview for #3066 and #3067

This commit is contained in:
James Cole 2020-01-30 18:56:08 +01:00
parent a6fab50c20
commit b697b71e59
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 127 additions and 103 deletions

View File

@ -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();

View File

@ -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'));
}
/**

View File

@ -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();
}
}

View File

@ -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
*

View File

@ -17,7 +17,7 @@
</div>
<form action="{{ route('tags.mass-destroy') }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
{% for period,entries in clouds %}
{% for period, entries in tags %}
{% if entries|length > 0 %}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
@ -28,19 +28,30 @@
</h3>
</div>
<div class="box-body">
<p class="tagcloud">
<div class="row">
{% for tagInfo in entries %}
<input type="checkbox" name="tags[]" value="{{ tagInfo.id }}">
<a style="font-size:{{ tagInfo.size }}px;" class="label label-success"
title="{{ tagInfo.created_at.formatLocalized(monthAndDayFormat) }}"
href="{{ route('tags.show',tagInfo.id) }}">
{% if tagInfo.location %}
<i class="fa fa-fw fa-map-marker"></i>
{% endif %}
<i class="fa fa-fw fa-tag"></i>
{{ tagInfo.tag }}</a>
<div class="col-xs-6 col-sm-4 col-md-3 col-lg-2"
style="line-height: 1.7em;"
><input type="checkbox" name="tags[]" value="{{ tagInfo.id }}"> <a
class="label label-success"
style="font-weight: normal;font-size:0.9em;"
title="{{ tagInfo.created_at.formatLocalized(monthAndDayFormat) }}"
href="{{ route('tags.show',tagInfo.id) }}">{% if tagInfo.location %}<i
class="fa fa-fw fa-map-marker"></i>{% endif %}<i class="fa fa-fw fa-tag"></i>{{ tagInfo.tag }}</a></div>
{% endfor %}
</p>
</div>
<ul class="list-inline">
{% for tagInfo in entries %}
<li>
<!--&nbsp;-->
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
@ -51,9 +62,9 @@
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<p>
<a class="btn btn-success" href="{{ route('tags.create') }}"><i class="fa fa-plus fa-fw"></i> {{ ('no_tags_create_default')|_ }}</a>
<button type="submit" class="btn pull-right btn-danger" onclick="return confirm('{{ 'are_you_sure'|_|escape('js') }}');">
<i class="fa fa-fw fa-trash"></i> {{ 'delete_all_selected_tags'|_ }}
</button>
<button type="submit" class="btn pull-right btn-danger" onclick="return confirm('{{ 'are_you_sure'|_|escape('js') }}');">
<i class="fa fa-fw fa-trash"></i> {{ 'delete_all_selected_tags'|_ }}
</button>
</p>
</div>
</div>