New view for tags

This commit is contained in:
James Cole 2017-08-20 12:40:14 +02:00
parent 1d6f3fc57f
commit 40639dfa37
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
6 changed files with 168 additions and 78 deletions

View File

@ -168,41 +168,23 @@ class TagController extends Controller
*/ */
public function index(TagRepositoryInterface $repository) 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. // collect tags by year:
$collection = []; /** @var Carbon $start */
foreach ($types as $type) { $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 */ $now->subYear();
$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;
}
} }
$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')); return view('tags.show', compact('apiKey', 'tag', 'periods', 'subTitle', 'subTitleIcon', 'journals', 'sum', 'start', 'end', 'moment'));
} }
/** /**
* @param TagFormRequest $request * @param TagFormRequest $request
* *
@ -371,4 +352,6 @@ class TagController extends Controller
return $collection; return $collection;
} }
} }

View File

@ -253,11 +253,61 @@ class TagRepository implements TagRepositoryInterface
} }
$collector->setAllAssetAccounts()->setTag($tag); $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); 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 Tag $tag
* @param array $data * @param array $data
@ -277,4 +327,21 @@ class TagRepository implements TagRepositoryInterface
return $tag; 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);
}
} }

View File

@ -135,6 +135,15 @@ interface TagRepositoryInterface
*/ */
public function sumOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): string; 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. * Update a tag.
* *

View File

@ -17,6 +17,10 @@
background: url('/images/error.png') no-repeat center center; background: url('/images/error.png') no-repeat center center;
} }
p.tagcloud .label {
line-height:2;
}
.handle { .handle {
cursor: move; cursor: move;
} }

View File

@ -8,54 +8,81 @@
{% if count == 0 %} {% if count == 0 %}
{% include 'partials.empty' with {what: 'default', type: 'tags',route: route('tags.create')} %} {% include 'partials.empty' with {what: 'default', type: 'tags',route: route('tags.create')} %}
{% else %} {% else %}
<div class="row"> {% for period,entries in clouds %}
{% for type in types %} {% if entries|length > 0 %}
{% if counts[type] > 0 %} <div class="row">
<div class=" <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
{% if hasTypes == 1 %}col-lg-12 col-md-12 col-sm-12 col-xs-12{% endif %} <div class="box">
{% if hasTypes == 2 %}col-lg-6 col-md-6 col-sm-12 col-xs-12{% endif %} <div class="box-header with-border">
{% if hasTypes == 3 %}col-lg-4 col-md-4 col-sm-12 col-xs-12{% endif %} <h3 class="box-title">
"> {% if period == 'no-date' %}{{ 'without_date'|_ }}{% else %}{{ period }}{% endif %}
<div class="box"> </h3>
<div class="box-header with-border"> </div>
<h3 class="box-title">{{ ('tag_title_'~type)|_ }}</h3> <div class="box-body">
</div> <p class="tagcloud">
<div class="box-body"> {% for tagInfo in entries %}
{% for year,months in collection[type] %} <a style="font-size:{{ tagInfo.scale }}px;" class="label label-success" href="{{ route('tags.show',tagInfo.tag.id) }}"><i class="fa fa-fw fa-tag"></i> {{ tagInfo.tag.tag }}</a>
<h4>{{ year }}</h4>
{% for month,tags in months %}
<h5>{{ month }}</h5>
<p style="line-height: 200%;">
{% for tag in tags %}
<span style="display: inline;"><a style="font-size:100%;font-weight:normal;" class="label label-success"
href="{{ route('tags.show',tag.id) }}">
{% if tag.tagMode == 'nothing' %}
<i class="fa fa-fw fa-tag"></i>
{% endif %}
{% if tag.tagMode == 'balancingAct' %}
<i class="fa fa-fw fa-refresh"></i>
{% endif %}
{% if tag.tagMode == 'advancePayment' %}
<i class="fa fa-fw fa-sort-numeric-desc"></i>
{% endif %}
{{ tag.tag }}</a>
</span>
{% endfor %}
</p>
{% endfor %}
{% endfor %} {% endfor %}
</p>
</div>
<div class="box-footer">
<a href="{{ route('tags.create') }}" title="New tag" class="btn btn-info pull-right">{{ 'new_tag'|_ }}</a>
</div>
</div> </div>
</div> </div>
</div>
</div>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{#
<div class="row">
{% for type in types %}
{% if counts[type] > 0 %}
<div class="
{% if hasTypes == 1 %}col-lg-12 col-md-12 col-sm-12 col-xs-12{% endif %}
{% if hasTypes == 2 %}col-lg-6 col-md-6 col-sm-12 col-xs-12{% endif %}
{% if hasTypes == 3 %}col-lg-4 col-md-4 col-sm-12 col-xs-12{% endif %}
">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ ('tag_title_'~type)|_ }}</h3>
</div>
<div class="box-body">
{% for year,months in collection[type] %}
<h4>{{ year }}</h4>
{% for month,tags in months %}
<h5>{{ month }}</h5>
<p style="line-height: 200%;">
{% for tag in tags %}
<span style="display: inline;"><a style="font-size:100%;font-weight:normal;" class="label label-success"
href="{{ route('tags.show',tag.id) }}">
{% if tag.tagMode == 'nothing' %}
<i class="fa fa-fw fa-tag"></i>
{% endif %}
{% if tag.tagMode == 'balancingAct' %}
<i class="fa fa-fw fa-refresh"></i>
{% endif %}
{% if tag.tagMode == 'advancePayment' %}
<i class="fa fa-fw fa-sort-numeric-desc"></i>
{% endif %}
{{ tag.tag }}</a>
</span>
{% endfor %}
</p>
{% endfor %}
{% endfor %}
</div>
<div class="box-footer">
<a href="{{ route('tags.create') }}" title="New tag" class="btn btn-info pull-right">{{ 'new_tag'|_ }}</a>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div> </div>
#}
{% endif %} {% endif %}
{% endblock %} {% endblock %}

View File

@ -109,14 +109,14 @@
{% if periods.count > 0 %} {% if periods.count > 0 %}
<p> <p>
<i class="fa fa-calendar" aria-hidden="true"></i> <i class="fa fa-calendar"></i>
<a href="{{ route('tags.show', [tag.id,'all']) }}"> <a href="{{ route('tags.show', [tag.id,'all']) }}">
{{ 'show_all_no_filter'|_ }} {{ 'show_all_no_filter'|_ }}
</a> </a>
</p> </p>
{% else %} {% else %}
<p> <p>
<i class="fa fa-calendar" aria-hidden="true"></i> <i class="fa fa-calendar"></i>
<a href="{{ route('tags.show', [tag.id]) }}"> <a href="{{ route('tags.show', [tag.id]) }}">
{{ 'show_the_current_period_and_overview'|_ }} {{ 'show_the_current_period_and_overview'|_ }}
</a> </a>