firefly-iii/app/Http/Controllers/CategoryController.php

209 lines
6.8 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* CategoryController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05: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 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2017-04-08 02:00:37 -05:00
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
2015-02-22 09:19:32 -06:00
use FireflyIII\Http\Requests\CategoryFormRequest;
use FireflyIII\Models\Category;
2016-12-27 03:46:11 -06:00
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2016-12-28 06:02:56 -06:00
use Illuminate\Http\Request;
2017-12-28 02:53:21 -06:00
use Illuminate\Pagination\LengthAwarePaginator;
2015-09-25 09:00:14 -05:00
use Illuminate\Support\Collection;
2015-02-22 09:19:32 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class CategoryController.
2015-02-22 09:19:32 -06:00
*/
class CategoryController extends Controller
{
2018-01-14 12:36:24 -06:00
/** @var CategoryRepositoryInterface */
private $repository;
2015-02-24 15:53:38 -06:00
/**
2018-07-08 05:08:53 -05:00
* CategoryController constructor.
2015-02-24 15:53:38 -06:00
*/
2015-02-22 09:19:32 -06:00
public function __construct()
{
2015-04-28 08:26:30 -05:00
parent::__construct();
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
2018-07-15 02:38:49 -05:00
app('view')->share('title', (string)trans('firefly.categories'));
2017-12-16 12:46:36 -06:00
app('view')->share('mainTitleIcon', 'fa-bar-chart');
2018-07-14 15:48:22 -05:00
$this->repository = app(CategoryRepositoryInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2015-02-22 09:19:32 -06:00
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-22 09:19:32 -06:00
*/
2017-02-17 13:14:22 -06:00
public function create(Request $request)
2015-02-22 09:19:32 -06:00
{
2017-11-15 05:25:49 -06:00
if (true !== session('categories.create.fromStore')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('categories.create.uri');
2015-04-01 02:43:19 -05:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('categories.create.fromStore');
2018-07-15 02:38:49 -05:00
$subTitle =(string)trans('firefly.create_new_category');
2015-04-01 02:43:19 -05:00
2015-05-05 03:23:01 -05:00
return view('categories.create', compact('subTitle'));
2015-02-22 09:19:32 -06:00
}
/**
* @param Category $category
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-22 09:19:32 -06:00
*/
2017-12-29 02:05:35 -06:00
public function delete(Category $category)
2015-02-22 09:19:32 -06:00
{
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('firefly.delete_category', ['name' => $category->name]);
2015-02-22 09:19:32 -06:00
2015-04-01 02:43:19 -05:00
// put previous url in session
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('categories.delete.uri');
2015-04-01 02:43:19 -05:00
2015-02-22 09:19:32 -06:00
return view('categories.delete', compact('category', 'subTitle'));
}
/**
2018-01-14 12:36:24 -06:00
* @param Request $request
* @param Category $category
2015-02-22 09:19:32 -06:00
*
2016-12-27 03:46:11 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-22 09:19:32 -06:00
*/
2018-01-14 12:36:24 -06:00
public function destroy(Request $request, Category $category)
2015-02-22 09:19:32 -06:00
{
2017-02-05 01:26:54 -06:00
$name = $category->name;
2018-01-14 12:36:24 -06:00
$this->repository->destroy($category);
2015-02-22 09:19:32 -06:00
2018-04-02 08:10:40 -05:00
$request->session()->flash('success', (string)trans('firefly.deleted_category', ['name' => $name]));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2015-02-22 09:19:32 -06:00
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('categories.delete.uri'));
2015-02-22 09:19:32 -06:00
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
2015-02-22 09:19:32 -06:00
* @param Category $category
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-22 09:19:32 -06:00
*/
2017-02-17 13:14:22 -06:00
public function edit(Request $request, Category $category)
2015-02-22 09:19:32 -06:00
{
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('firefly.edit_category', ['name' => $category->name]);
2015-02-22 09:19:32 -06:00
2015-04-01 02:43:19 -05:00
// put previous url in session if not redirect from store (not "return_to_edit").
2017-11-15 05:25:49 -06:00
if (true !== session('categories.edit.fromUpdate')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('categories.edit.uri');
2015-04-01 02:43:19 -05:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('categories.edit.fromUpdate');
2015-04-01 02:43:19 -05:00
2015-02-22 09:19:32 -06:00
return view('categories.edit', compact('category', 'subTitle'));
}
/**
2018-01-14 12:36:24 -06:00
* @param Request $request
*
2018-01-14 12:36:24 -06:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-22 09:19:32 -06:00
*/
2018-01-14 12:36:24 -06:00
public function index(Request $request)
2015-02-22 09:19:32 -06:00
{
2018-04-02 08:10:40 -05:00
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
2018-01-14 12:36:24 -06:00
$collection = $this->repository->getCategories();
2017-12-28 02:53:21 -06:00
$total = $collection->count();
$collection = $collection->slice(($page - 1) * $pageSize, $pageSize);
2015-03-04 02:42:47 -06:00
2017-12-28 02:53:21 -06:00
$collection->each(
2018-01-14 12:36:24 -06:00
function (Category $category) {
$category->lastActivity = $this->repository->lastUseDate($category, new Collection);
2015-03-04 02:42:47 -06:00
}
);
2015-02-22 09:19:32 -06:00
2017-12-28 02:53:21 -06:00
// paginate categories
$categories = new LengthAwarePaginator($collection, $total, $pageSize, $page);
$categories->setPath(route('categories.index'));
2015-02-22 09:19:32 -06:00
return view('categories.index', compact('categories'));
}
2015-03-04 02:42:47 -06:00
2015-02-22 09:19:32 -06:00
/**
2016-12-27 03:46:11 -06:00
* @param CategoryFormRequest $request
* @param CategoryRepositoryInterface $repository
2015-02-22 09:19:32 -06:00
*
2016-12-27 03:46:11 -06:00
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-22 09:19:32 -06:00
*/
2016-12-27 03:46:11 -06:00
public function store(CategoryFormRequest $request, CategoryRepositoryInterface $repository)
2015-02-22 09:19:32 -06:00
{
2016-10-23 05:10:22 -05:00
$data = $request->getCategoryData();
$category = $repository->store($data);
2015-02-22 09:19:32 -06:00
2018-04-02 08:10:40 -05:00
$request->session()->flash('success', (string)trans('firefly.stored_category', ['name' => $category->name]));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2015-03-29 14:27:51 -05:00
2018-07-08 05:08:53 -05:00
$redirect = redirect(route('categories.index'));
2018-04-02 08:10:40 -05:00
if (1 === (int)$request->get('create_another')) {
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreStart
2017-02-17 13:14:22 -06:00
$request->session()->put('categories.create.fromStore', true);
2015-02-22 09:19:32 -06:00
2018-07-08 05:08:53 -05:00
$redirect = redirect(route('categories.create'))->withInput();
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreEnd
2015-03-25 10:13:39 -05:00
}
2018-07-08 05:08:53 -05:00
return $redirect;
2015-02-22 09:19:32 -06:00
}
2018-07-08 05:08:53 -05:00
2015-02-22 09:19:32 -06:00
/**
2016-12-27 03:46:11 -06:00
* @param CategoryFormRequest $request
* @param CategoryRepositoryInterface $repository
* @param Category $category
2015-02-22 09:19:32 -06:00
*
2016-12-27 03:46:11 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-22 09:19:32 -06:00
*/
2016-12-27 03:46:11 -06:00
public function update(CategoryFormRequest $request, CategoryRepositoryInterface $repository, Category $category)
2015-02-22 09:19:32 -06:00
{
2016-10-23 05:10:22 -05:00
$data = $request->getCategoryData();
$repository->update($category, $data);
2015-02-22 09:19:32 -06:00
2018-04-02 08:10:40 -05:00
$request->session()->flash('success', (string)trans('firefly.updated_category', ['name' => $category->name]));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
$redirect = redirect($this->getPreviousUri('categories.edit.uri'));
2015-02-22 09:19:32 -06:00
2018-04-02 08:10:40 -05:00
if (1 === (int)$request->get('return_to_edit')) {
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreStart
2017-02-17 13:14:22 -06:00
$request->session()->put('categories.edit.fromUpdate', true);
2018-07-08 05:08:53 -05:00
$redirect = redirect(route('categories.edit', [$category->id]));
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreEnd
}
2018-07-08 05:08:53 -05:00
return $redirect;
2015-02-22 09:19:32 -06:00
}
2018-07-08 05:08:53 -05:00
2015-02-22 09:19:32 -06:00
}