This commit is contained in:
James Cole
2018-06-01 13:11:10 +02:00
parent 3654e75b8c
commit 4c04415e80
5 changed files with 27 additions and 18 deletions

View File

@@ -145,27 +145,18 @@ class TagController extends Controller
public function index(TagRepositoryInterface $repository) public function index(TagRepositoryInterface $repository)
{ {
// start with oldest tag // start with oldest tag
$oldestTag = $repository->oldestTag(); $oldestTagDate = null === $repository->oldestTag() ? clone session('first') : $repository->oldestTag()->date;
/** @var Carbon $start */ $newestTagDate = null === $repository->newestTag() ? new Carbon : $repository->newestTag()->date;
$start = new Carbon; $oldestTagDate->startOfYear();
if (null !== $oldestTag) { $newestTagDate->endOfYear();
/** @var Carbon $start */
$start = $oldestTag->date; // @codeCoverageIgnore
}
if (null === $oldestTag) {
/** @var Carbon $start */
$start = clone session('first');
}
$now = new Carbon;
$clouds = []; $clouds = [];
$clouds['no-date'] = $repository->tagCloud(null); $clouds['no-date'] = $repository->tagCloud(null);
while ($now > $start) { while ($newestTagDate > $oldestTagDate) {
$year = $now->year; $year = $newestTagDate->year;
$clouds[$year] = $repository->tagCloud($year); $clouds[$year] = $repository->tagCloud($year);
$now->subYear(); $newestTagDate->subYear();
} }
$count = $repository->count(); $count = $repository->count();

View File

@@ -32,9 +32,11 @@ use FireflyIII\Models\TransactionJournal;
/** /**
* Class Tag. * Class Tag.
*
* @property Collection $transactionJournals * @property Collection $transactionJournals
* @property string $tag * @property string $tag
* @property int $id * @property int $id
* @property \Carbon\Carbon $date
*/ */
class Tag extends Model class Tag extends Model
{ {

View File

@@ -336,7 +336,6 @@ class TagRepository implements TagRepositoryInterface
} }
) )
->groupBy(['tags.id', 'tags.tag']); ->groupBy(['tags.id', 'tags.tag']);
// add date range (or not): // add date range (or not):
if (null === $year) { if (null === $year) {
Log::debug('Get tags without a date.'); Log::debug('Get tags without a date.');
@@ -464,4 +463,13 @@ class TagRepository implements TagRepositoryInterface
{ {
return $this->user->tags()->find($tagId); return $this->user->tags()->find($tagId);
} }
}
/**
* Will return the newest tag (if known) or NULL.
*
* @return Tag|null
*/
public function newestTag(): ?Tag
{
return $this->user->tags()->whereNotNull('date')->orderBy('date', 'DESC')->first();
}}

View File

@@ -116,10 +116,16 @@ interface TagRepositoryInterface
public function lastUseDate(Tag $tag): Carbon; public function lastUseDate(Tag $tag): Carbon;
/** /**
* Will return the newest tag (if known) or NULL.
* @return Tag|null * @return Tag|null
*/ */
public function oldestTag(): ?Tag; public function oldestTag(): ?Tag;
/**
* Will return the newest tag (if known) or NULL.
* @return Tag|null
*/
public function newestTag(): ?Tag;
/** /**
* @param User $user * @param User $user
*/ */

View File

@@ -130,6 +130,8 @@ class TagControllerTest extends TestCase
$repository->shouldReceive('count')->andReturn(0); $repository->shouldReceive('count')->andReturn(0);
$repository->shouldReceive('tagCloud')->andReturn([]); $repository->shouldReceive('tagCloud')->andReturn([]);
$repository->shouldReceive('oldestTag')->andReturn(null)->once(); $repository->shouldReceive('oldestTag')->andReturn(null)->once();
$repository->shouldReceive('newestTag')->andReturn(null)->once();
$this->be($this->user()); $this->be($this->user());
$response = $this->get(route('tags.index')); $response = $this->get(route('tags.index'));