Files
firefly-iii/app/Http/Controllers/Category/ShowController.php

163 lines
5.9 KiB
PHP
Raw Normal View History

2018-07-14 22:48:22 +02:00
<?php
/**
* ShowController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-14 22:48:22 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-14 22:48:22 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-07-14 22:48:22 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-07-14 22:48:22 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-07-14 22:48:22 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-07-14 22:48:22 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Category;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2018-07-14 22:48:22 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Support\Http\Controllers\PeriodOverview;
use Illuminate\Contracts\View\Factory;
2018-07-14 22:48:22 +02:00
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\View\View;
2022-12-29 19:41:57 +01:00
use JsonException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2018-07-14 22:48:22 +02:00
/**
*
* Class ShowController
*
*/
class ShowController extends Controller
{
use PeriodOverview;
2021-03-28 11:46:23 +02:00
2018-07-21 08:06:24 +02:00
/** @var CategoryRepositoryInterface The category repository */
2018-07-14 22:48:22 +02:00
private $repository;
/**
* CategoryController constructor.
*
* @codeCoverageIgnore
2018-07-14 22:48:22 +02:00
*/
public function __construct()
{
parent::__construct();
2019-09-04 07:51:31 +02:00
app('view')->share('showBudget', true);
2018-07-14 22:48:22 +02:00
$this->middleware(
function ($request, $next) {
2022-12-29 19:41:57 +01:00
app('view')->share('title', (string)trans('firefly.categories'));
app('view')->share('mainTitleIcon', 'fa-bookmark');
2019-02-13 17:38:41 +01:00
$this->repository = app(CategoryRepositoryInterface::class);
2018-07-14 22:48:22 +02:00
return $next($request);
}
);
}
2021-03-28 11:46:23 +02:00
2018-07-14 22:48:22 +02:00
/**
2018-07-21 08:06:24 +02:00
* Show a single category.
*
2022-12-29 19:41:57 +01:00
* @param Request $request
* @param Category $category
* @param Carbon|null $start
* @param Carbon|null $end
2018-07-14 22:48:22 +02:00
*
* @return Factory|View
* @throws FireflyException
2022-12-29 19:41:57 +01:00
* @throws JsonException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2018-07-14 22:48:22 +02:00
*/
2022-12-31 06:57:05 +01:00
public function show(Request $request, Category $category, Carbon $start = null, Carbon $end = null)
2018-07-14 22:48:22 +02:00
{
/** @var Carbon $start */
2018-08-04 17:30:06 +02:00
$start = $start ?? session('start', Carbon::now()->startOfMonth());
2018-07-14 22:48:22 +02:00
/** @var Carbon $end */
$end = $end ?? session('end', Carbon::now()->endOfMonth());
$subTitleIcon = 'fa-bookmark';
2022-12-29 19:41:57 +01:00
$page = (int)$request->get('page');
2020-03-19 18:37:57 +01:00
$attachments = $this->repository->getAttachments($category);
2022-12-29 19:41:57 +01:00
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
2019-09-23 20:25:32 +02:00
$oldest = $this->repository->firstUseDate($category) ?? Carbon::now()->startOfYear();
$periods = $this->getCategoryPeriodOverview($category, $oldest, $end);
2018-07-14 22:48:22 +02:00
$path = route('categories.show', [$category->id, $start->format('Y-m-d'), $end->format('Y-m-d')]);
$subTitle = trans(
'firefly.journals_in_period_for_category',
2022-12-29 19:41:57 +01:00
[
'name' => $category->name,
'start' => $start->isoFormat($this->monthAndDayFormat),
'end' => $end->isoFormat($this->monthAndDayFormat),
]
2018-07-14 22:48:22 +02:00
);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)->setLimit($pageSize)->setPage($page)
->withAccountInformation()
2018-07-14 22:48:22 +02:00
->setCategory($category)->withBudgetInformation()->withCategoryInformation();
$groups = $collector->getPaginatedGroups();
$groups->setPath($path);
2018-07-14 22:48:22 +02:00
return view('categories.show', compact('category', 'attachments', 'groups', 'periods', 'subTitle', 'subTitleIcon', 'start', 'end'));
2018-07-14 22:48:22 +02:00
}
/**
2018-07-21 08:06:24 +02:00
* Show all transactions within a category.
*
2022-12-29 19:41:57 +01:00
* @param Request $request
* @param Category $category
2018-07-14 22:48:22 +02:00
*
* @return Factory|View
* @throws FireflyException
2022-12-29 19:41:57 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2018-07-14 22:48:22 +02:00
*/
public function showAll(Request $request, Category $category)
{
// default values:
$subTitleIcon = 'fa-bookmark';
2022-12-29 19:41:57 +01:00
$page = (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
2018-07-14 22:48:22 +02:00
$start = null;
$end = null;
2022-10-30 14:24:19 +01:00
$periods = new Collection();
2018-07-14 22:48:22 +02:00
2022-12-29 19:41:57 +01:00
$subTitle = (string)trans('firefly.all_journals_for_category', ['name' => $category->name]);
2018-07-14 22:48:22 +02:00
$first = $this->repository->firstUseDate($category);
/** @var Carbon $start */
2021-03-28 11:46:23 +02:00
$start = $first ?? today(config('app.timezone'));
$end = today(config('app.timezone'));
$path = route('categories.show.all', [$category->id]);
$attachments = $this->repository->getAttachments($category);
2018-07-14 22:48:22 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)->setLimit($pageSize)->setPage($page)
->withAccountInformation()
2018-07-14 22:48:22 +02:00
->setCategory($category)->withBudgetInformation()->withCategoryInformation();
$groups = $collector->getPaginatedGroups();
$groups->setPath($path);
return view('categories.show', compact('category', 'attachments', 'groups', 'periods', 'subTitle', 'subTitleIcon', 'start', 'end'));
2018-07-14 22:48:22 +02:00
}
}