mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 01:42:32 +00:00
New view for tags
This commit is contained in:
@@ -168,41 +168,23 @@ class TagController extends Controller
|
||||
*/
|
||||
public function index(TagRepositoryInterface $repository)
|
||||
{
|
||||
$title = 'Tags';
|
||||
$mainTitleIcon = 'fa-tags';
|
||||
$types = ['nothing', 'balancingAct', 'advancePayment'];
|
||||
$hasTypes = 0; // which types of tag the user actually has.
|
||||
$counts = []; // how many of each type?
|
||||
$count = $repository->count();
|
||||
|
||||
// loop each types and get the tags, group them by year.
|
||||
$collection = [];
|
||||
foreach ($types as $type) {
|
||||
// collect tags by year:
|
||||
/** @var Carbon $start */
|
||||
$start = clone(session('first'));
|
||||
$now = new Carbon;
|
||||
$clouds = [];
|
||||
$clouds['no-date'] = $repository->tagCloud(null);
|
||||
while ($now > $start) {
|
||||
$year = $now->year;
|
||||
$clouds[$year] = $repository->tagCloud($year);
|
||||
|
||||
/** @var Collection $tags */
|
||||
$tags = $repository->getByType($type);
|
||||
$tags = $tags->sortBy(
|
||||
function (Tag $tag) {
|
||||
$date = !is_null($tag->date) ? $tag->date->format('Ymd') : '000000';
|
||||
|
||||
return strtolower($date . $tag->tag);
|
||||
}
|
||||
);
|
||||
if ($tags->count() > 0) {
|
||||
$hasTypes++;
|
||||
}
|
||||
$counts[$type] = $tags->count();
|
||||
|
||||
/** @var Tag $tag */
|
||||
foreach ($tags as $tag) {
|
||||
$year = is_null($tag->date) ? trans('firefly.no_year') : $tag->date->year;
|
||||
$monthFormatted = is_null($tag->date) ? trans('firefly.no_month') : $tag->date->formatLocalized($this->monthFormat);
|
||||
|
||||
$collection[$type][$year][$monthFormatted][] = $tag;
|
||||
}
|
||||
$now->subYear();
|
||||
}
|
||||
$count = $repository->count();
|
||||
|
||||
return view('tags.index', compact('title', 'mainTitleIcon', 'counts', 'hasTypes', 'types', 'collection', 'count'));
|
||||
|
||||
return view('tags.index', compact('clouds', 'count'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -274,7 +256,6 @@ class TagController extends Controller
|
||||
return view('tags.show', compact('apiKey', 'tag', 'periods', 'subTitle', 'subTitleIcon', 'journals', 'sum', 'start', 'end', 'moment'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param TagFormRequest $request
|
||||
*
|
||||
@@ -371,4 +352,6 @@ class TagController extends Controller
|
||||
return $collection;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -253,11 +253,61 @@ class TagRepository implements TagRepositoryInterface
|
||||
}
|
||||
|
||||
$collector->setAllAssetAccounts()->setTag($tag);
|
||||
$sum = $collector->getJournals()->sum('transaction_amount');
|
||||
$journals = $collector->getJournals();
|
||||
$sum = '0';
|
||||
foreach ($journals as $journal) {
|
||||
$sum = bcadd($sum, app('steam')->positive(strval($journal->transaction_amount)));
|
||||
}
|
||||
|
||||
return strval($sum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a tag cloud.
|
||||
*
|
||||
* @param int|null $year
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tagCloud(?int $year): array
|
||||
{
|
||||
$min = null;
|
||||
$max = 0;
|
||||
$query = $this->user->tags();
|
||||
$return = [];
|
||||
if (!is_null($year)) {
|
||||
$start = $year . '-01-01';
|
||||
$end = $year . '-12-31';
|
||||
$query->where('date', '>=', $start)->where('date', '<=', $end);
|
||||
}
|
||||
if (is_null($year)) {
|
||||
$query->whereNull('date');
|
||||
}
|
||||
$tags = $query->orderBy('id','desc')->get();
|
||||
$temporary = [];
|
||||
foreach ($tags as $tag) {
|
||||
$amount = floatval($this->sumOfTag($tag, null, null));
|
||||
$min = $amount < $min || is_null($min) ? $amount : $min;
|
||||
$max = $amount > $max ? $amount : $max;
|
||||
$temporary[] = [
|
||||
'amount' => $amount,
|
||||
'tag' => $tag,
|
||||
];
|
||||
}
|
||||
/** @var array $entry */
|
||||
foreach ($temporary as $entry) {
|
||||
$scale = $this->cloudScale([12, 20], $entry['amount'], $min, $max);
|
||||
$tagId = $entry['tag']->id;
|
||||
$return[$tagId] = [
|
||||
'scale' => $scale,
|
||||
'tag' => $entry['tag'],
|
||||
];
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Tag $tag
|
||||
* @param array $data
|
||||
@@ -277,4 +327,21 @@ class TagRepository implements TagRepositoryInterface
|
||||
return $tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $range
|
||||
* @param float $amount
|
||||
* @param float $min
|
||||
* @param float $max
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function cloudScale(array $range, float $amount, float $min, float $max): int
|
||||
{
|
||||
$amountDiff = $max - $min;
|
||||
$diff = $range[1] - $range[0];
|
||||
$step = $amountDiff / $diff;
|
||||
$extra = round($amount / $step);
|
||||
|
||||
return intval($range[0] + $extra);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,6 +135,15 @@ interface TagRepositoryInterface
|
||||
*/
|
||||
public function sumOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): string;
|
||||
|
||||
/**
|
||||
* Generates a tag cloud.
|
||||
*
|
||||
* @param int|null $year
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tagCloud(?int $year): array;
|
||||
|
||||
/**
|
||||
* Update a tag.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user