2016-05-20 08:57:45 +02:00
|
|
|
<?php
|
2016-05-20 12:27:31 +02:00
|
|
|
/**
|
|
|
|
* CategoryController.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 12:27:31 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 14:41:58 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 12:27:31 +02:00
|
|
|
*/
|
2017-04-08 09:00:37 +02:00
|
|
|
declare(strict_types=1);
|
2016-05-20 08:57:45 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-02-22 16:19:32 +01:00
|
|
|
|
|
|
|
use FireflyIII\Http\Requests\CategoryFormRequest;
|
|
|
|
use FireflyIII\Models\Category;
|
2016-12-27 10:46:11 +01:00
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
2016-12-28 13:02:56 +01:00
|
|
|
use Illuminate\Http\Request;
|
2017-12-28 09:53:21 +01:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2015-09-25 16:00:14 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2015-02-22 16:19:32 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class CategoryController.
|
2015-02-22 16:19:32 +01:00
|
|
|
*/
|
|
|
|
class CategoryController extends Controller
|
|
|
|
{
|
2018-07-21 08:06:24 +02:00
|
|
|
/** @var CategoryRepositoryInterface The category repository */
|
2018-01-14 19:36:24 +01:00
|
|
|
private $repository;
|
|
|
|
|
2015-02-24 22:53:38 +01:00
|
|
|
/**
|
2018-07-08 12:08:53 +02:00
|
|
|
* CategoryController constructor.
|
2015-02-24 22:53:38 +01:00
|
|
|
*/
|
2015-02-22 16:19:32 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
2015-04-28 15:26:30 +02:00
|
|
|
parent::__construct();
|
2016-10-29 07:44:46 +02:00
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2018-07-15 09:38:49 +02:00
|
|
|
app('view')->share('title', (string)trans('firefly.categories'));
|
2017-12-16 19:46:36 +01:00
|
|
|
app('view')->share('mainTitleIcon', 'fa-bar-chart');
|
2018-07-14 22:48:22 +02:00
|
|
|
$this->repository = app(CategoryRepositoryInterface::class);
|
2016-10-29 07:44:46 +02:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2015-02-22 16:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Create category.
|
|
|
|
*
|
2017-02-17 20:14:22 +01:00
|
|
|
* @param Request $request
|
|
|
|
*
|
2018-07-08 12:08:53 +02:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2015-02-22 16:19:32 +01:00
|
|
|
*/
|
2017-02-17 20:14:22 +01:00
|
|
|
public function create(Request $request)
|
2015-02-22 16:19:32 +01:00
|
|
|
{
|
2017-11-15 12:25:49 +01:00
|
|
|
if (true !== session('categories.create.fromStore')) {
|
2017-02-05 08:26:54 +01:00
|
|
|
$this->rememberPreviousUri('categories.create.uri');
|
2015-04-01 09:43:19 +02:00
|
|
|
}
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->forget('categories.create.fromStore');
|
2018-07-15 15:45:45 +02:00
|
|
|
$subTitle = (string)trans('firefly.create_new_category');
|
2015-04-01 09:43:19 +02:00
|
|
|
|
2015-05-05 10:23:01 +02:00
|
|
|
return view('categories.create', compact('subTitle'));
|
2015-02-22 16:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Delete a category.
|
|
|
|
*
|
2015-02-22 16:19:32 +01:00
|
|
|
* @param Category $category
|
|
|
|
*
|
2018-07-08 12:08:53 +02:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2015-02-22 16:19:32 +01:00
|
|
|
*/
|
2017-12-29 09:05:35 +01:00
|
|
|
public function delete(Category $category)
|
2015-02-22 16:19:32 +01:00
|
|
|
{
|
2018-07-15 09:38:49 +02:00
|
|
|
$subTitle = (string)trans('firefly.delete_category', ['name' => $category->name]);
|
2015-02-22 16:19:32 +01:00
|
|
|
|
2015-04-01 09:43:19 +02:00
|
|
|
// put previous url in session
|
2017-02-05 08:26:54 +01:00
|
|
|
$this->rememberPreviousUri('categories.delete.uri');
|
2015-04-01 09:43:19 +02:00
|
|
|
|
2015-02-22 16:19:32 +01:00
|
|
|
return view('categories.delete', compact('category', 'subTitle'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Destroy a category.
|
|
|
|
*
|
2018-01-14 19:36:24 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Category $category
|
2015-02-22 16:19:32 +01:00
|
|
|
*
|
2016-12-27 10:46:11 +01:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-22 16:19:32 +01:00
|
|
|
*/
|
2018-01-14 19:36:24 +01:00
|
|
|
public function destroy(Request $request, Category $category)
|
2015-02-22 16:19:32 +01:00
|
|
|
{
|
2017-02-05 08:26:54 +01:00
|
|
|
$name = $category->name;
|
2018-01-14 19:36:24 +01:00
|
|
|
$this->repository->destroy($category);
|
2015-02-22 16:19:32 +01:00
|
|
|
|
2018-04-02 15:10:40 +02:00
|
|
|
$request->session()->flash('success', (string)trans('firefly.deleted_category', ['name' => $name]));
|
2018-07-08 12:08:53 +02:00
|
|
|
app('preferences')->mark();
|
2015-02-22 16:19:32 +01:00
|
|
|
|
2017-02-05 08:26:54 +01:00
|
|
|
return redirect($this->getPreviousUri('categories.delete.uri'));
|
2015-02-22 16:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Edit a category.
|
|
|
|
*
|
2017-02-17 20:14:22 +01:00
|
|
|
* @param Request $request
|
2015-02-22 16:19:32 +01:00
|
|
|
* @param Category $category
|
|
|
|
*
|
2018-07-08 12:08:53 +02:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2015-02-22 16:19:32 +01:00
|
|
|
*/
|
2017-02-17 20:14:22 +01:00
|
|
|
public function edit(Request $request, Category $category)
|
2015-02-22 16:19:32 +01:00
|
|
|
{
|
2018-07-15 09:38:49 +02:00
|
|
|
$subTitle = (string)trans('firefly.edit_category', ['name' => $category->name]);
|
2015-02-22 16:19:32 +01:00
|
|
|
|
2015-04-01 09:43:19 +02:00
|
|
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
2017-11-15 12:25:49 +01:00
|
|
|
if (true !== session('categories.edit.fromUpdate')) {
|
2017-02-05 08:26:54 +01:00
|
|
|
$this->rememberPreviousUri('categories.edit.uri');
|
2015-04-01 09:43:19 +02:00
|
|
|
}
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->forget('categories.edit.fromUpdate');
|
2015-04-01 09:43:19 +02:00
|
|
|
|
2015-02-22 16:19:32 +01:00
|
|
|
return view('categories.edit', compact('category', 'subTitle'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Show all categories.
|
|
|
|
*
|
2018-01-14 19:36:24 +01:00
|
|
|
* @param Request $request
|
2015-04-05 19:43:20 +02:00
|
|
|
*
|
2018-01-14 19:36:24 +01:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2015-02-22 16:19:32 +01:00
|
|
|
*/
|
2018-01-14 19:36:24 +01:00
|
|
|
public function index(Request $request)
|
2015-02-22 16:19:32 +01:00
|
|
|
{
|
2018-04-02 15:10:40 +02:00
|
|
|
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
|
2018-07-14 16:08:34 +02:00
|
|
|
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
|
2018-01-14 19:36:24 +01:00
|
|
|
$collection = $this->repository->getCategories();
|
2017-12-28 09:53:21 +01:00
|
|
|
$total = $collection->count();
|
|
|
|
$collection = $collection->slice(($page - 1) * $pageSize, $pageSize);
|
2015-03-04 09:42:47 +01:00
|
|
|
|
2017-12-28 09:53:21 +01:00
|
|
|
$collection->each(
|
2018-01-14 19:36:24 +01:00
|
|
|
function (Category $category) {
|
|
|
|
$category->lastActivity = $this->repository->lastUseDate($category, new Collection);
|
2015-03-04 09:42:47 +01:00
|
|
|
}
|
|
|
|
);
|
2015-02-22 16:19:32 +01:00
|
|
|
|
2017-12-28 09:53:21 +01:00
|
|
|
// paginate categories
|
|
|
|
$categories = new LengthAwarePaginator($collection, $total, $pageSize, $page);
|
|
|
|
$categories->setPath(route('categories.index'));
|
|
|
|
|
2015-02-22 16:19:32 +01:00
|
|
|
return view('categories.index', compact('categories'));
|
|
|
|
}
|
|
|
|
|
2015-03-04 09:42:47 +01:00
|
|
|
|
2015-02-22 16:19:32 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Store new category.
|
|
|
|
*
|
2016-12-27 10:46:11 +01:00
|
|
|
* @param CategoryFormRequest $request
|
|
|
|
* @param CategoryRepositoryInterface $repository
|
2015-02-22 16:19:32 +01:00
|
|
|
*
|
2016-12-27 10:46:11 +01:00
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-22 16:19:32 +01:00
|
|
|
*/
|
2016-12-27 10:46:11 +01:00
|
|
|
public function store(CategoryFormRequest $request, CategoryRepositoryInterface $repository)
|
2015-02-22 16:19:32 +01:00
|
|
|
{
|
2016-10-23 12:10:22 +02:00
|
|
|
$data = $request->getCategoryData();
|
|
|
|
$category = $repository->store($data);
|
2015-02-22 16:19:32 +01:00
|
|
|
|
2018-04-02 15:10:40 +02:00
|
|
|
$request->session()->flash('success', (string)trans('firefly.stored_category', ['name' => $category->name]));
|
2018-07-08 12:08:53 +02:00
|
|
|
app('preferences')->mark();
|
2015-03-29 21:27:51 +02:00
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
$redirect = redirect(route('categories.index'));
|
2018-04-02 15:10:40 +02:00
|
|
|
if (1 === (int)$request->get('create_another')) {
|
2017-03-18 20:53:44 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->put('categories.create.fromStore', true);
|
2015-02-22 16:19:32 +01:00
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
$redirect = redirect(route('categories.create'))->withInput();
|
2017-03-18 20:53:44 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-03-25 16:13:39 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
return $redirect;
|
2015-02-22 16:19:32 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
|
2015-02-22 16:19:32 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Update category.
|
|
|
|
*
|
2016-12-27 10:46:11 +01:00
|
|
|
* @param CategoryFormRequest $request
|
|
|
|
* @param CategoryRepositoryInterface $repository
|
|
|
|
* @param Category $category
|
2015-02-22 16:19:32 +01:00
|
|
|
*
|
2016-12-27 10:46:11 +01:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-22 16:19:32 +01:00
|
|
|
*/
|
2016-12-27 10:46:11 +01:00
|
|
|
public function update(CategoryFormRequest $request, CategoryRepositoryInterface $repository, Category $category)
|
2015-02-22 16:19:32 +01:00
|
|
|
{
|
2016-10-23 12:10:22 +02:00
|
|
|
$data = $request->getCategoryData();
|
|
|
|
$repository->update($category, $data);
|
2015-02-22 16:19:32 +01:00
|
|
|
|
2018-04-02 15:10:40 +02:00
|
|
|
$request->session()->flash('success', (string)trans('firefly.updated_category', ['name' => $category->name]));
|
2018-07-08 12:08:53 +02:00
|
|
|
app('preferences')->mark();
|
|
|
|
|
|
|
|
$redirect = redirect($this->getPreviousUri('categories.edit.uri'));
|
2015-02-22 16:19:32 +01:00
|
|
|
|
2018-04-02 15:10:40 +02:00
|
|
|
if (1 === (int)$request->get('return_to_edit')) {
|
2017-03-18 20:53:44 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2017-02-17 20:14:22 +01:00
|
|
|
$request->session()->put('categories.edit.fromUpdate', true);
|
2015-04-05 19:43:20 +02:00
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
$redirect = redirect(route('categories.edit', [$category->id]));
|
2017-03-18 20:53:44 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-03-26 17:45:03 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
return $redirect;
|
2015-02-22 16:19:32 +01:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:08:53 +02:00
|
|
|
|
2015-02-22 16:19:32 +01:00
|
|
|
}
|