2019-06-25 12:24:01 -05:00
|
|
|
<?php
|
2019-10-01 23:37:26 -05:00
|
|
|
/**
|
|
|
|
* CreateController.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2019-10-01 23:37:26 -05:00
|
|
|
*
|
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-08-17 05:09:03 -05:00
|
|
|
declare(strict_types=1);
|
2019-06-25 12:24:01 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Category;
|
|
|
|
|
|
|
|
|
2020-03-19 02:58:55 -05:00
|
|
|
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
|
2019-06-25 12:24:01 -05:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Http\Requests\CategoryFormRequest;
|
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
2020-03-17 09:01:00 -05:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
2019-06-25 12:24:01 -05:00
|
|
|
use Illuminate\Http\Request;
|
2020-03-17 09:01:00 -05:00
|
|
|
use Illuminate\Routing\Redirector;
|
|
|
|
use Illuminate\View\View;
|
2019-06-25 12:24:01 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CreateController
|
|
|
|
*/
|
|
|
|
class CreateController extends Controller
|
|
|
|
{
|
2020-10-12 23:35:33 -05:00
|
|
|
private CategoryRepositoryInterface $repository;
|
|
|
|
private AttachmentHelperInterface $attachments;
|
2020-03-19 02:58:55 -05:00
|
|
|
|
2019-06-25 12:24:01 -05:00
|
|
|
/**
|
|
|
|
* CategoryController constructor.
|
2020-03-17 09:01:00 -05:00
|
|
|
*
|
2019-06-25 12:24:01 -05:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2020-03-17 09:01:00 -05:00
|
|
|
app('view')->share('title', (string) trans('firefly.categories'));
|
2020-05-01 16:54:08 -05:00
|
|
|
app('view')->share('mainTitleIcon', 'fa-bookmark');
|
2020-10-12 23:35:33 -05:00
|
|
|
$this->repository = app(CategoryRepositoryInterface::class);
|
2020-03-19 02:58:55 -05:00
|
|
|
$this->attachments = app(AttachmentHelperInterface::class);
|
2019-06-25 12:24:01 -05:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create category.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
2020-03-17 09:01:00 -05:00
|
|
|
* @return Factory|View
|
2019-06-25 12:24:01 -05:00
|
|
|
*/
|
|
|
|
public function create(Request $request)
|
|
|
|
{
|
|
|
|
if (true !== session('categories.create.fromStore')) {
|
|
|
|
$this->rememberPreviousUri('categories.create.uri');
|
|
|
|
}
|
|
|
|
$request->session()->forget('categories.create.fromStore');
|
2020-03-17 09:01:00 -05:00
|
|
|
$subTitle = (string) trans('firefly.create_new_category');
|
2019-06-25 12:24:01 -05:00
|
|
|
|
|
|
|
return view('categories.create', compact('subTitle'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store new category.
|
|
|
|
*
|
|
|
|
* @param CategoryFormRequest $request
|
|
|
|
*
|
2020-03-17 09:01:00 -05:00
|
|
|
* @return $this|RedirectResponse|Redirector
|
2019-06-25 12:24:01 -05:00
|
|
|
*/
|
|
|
|
public function store(CategoryFormRequest $request)
|
|
|
|
{
|
|
|
|
$data = $request->getCategoryData();
|
|
|
|
$category = $this->repository->store($data);
|
|
|
|
|
2020-03-17 09:01:00 -05:00
|
|
|
$request->session()->flash('success', (string) trans('firefly.stored_category', ['name' => $category->name]));
|
2019-06-25 12:24:01 -05:00
|
|
|
app('preferences')->mark();
|
|
|
|
|
2020-03-19 03:01:35 -05:00
|
|
|
// store attachment(s):
|
|
|
|
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
|
2020-05-06 23:44:01 -05:00
|
|
|
if (null !== $files && !auth()->user()->hasRole('demo')) {
|
|
|
|
$this->attachments->saveAttachmentsForModel($category, $files);
|
|
|
|
}
|
|
|
|
if (null !== $files && auth()->user()->hasRole('demo')) {
|
2020-10-12 23:35:33 -05:00
|
|
|
session()->flash('info', (string) trans('firefly.no_att_demo_user'));
|
2020-05-06 23:44:01 -05:00
|
|
|
}
|
2020-03-19 03:01:35 -05:00
|
|
|
|
|
|
|
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
|
|
|
|
$request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
2019-06-25 12:24:01 -05:00
|
|
|
$redirect = redirect(route('categories.index'));
|
2020-03-17 09:01:00 -05:00
|
|
|
if (1 === (int) $request->get('create_another')) {
|
2019-06-25 12:24:01 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
$request->session()->put('categories.create.fromStore', true);
|
|
|
|
|
|
|
|
$redirect = redirect(route('categories.create'))->withInput();
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
|
|
|
|
|
|
|
return $redirect;
|
|
|
|
}
|
2019-08-17 05:09:03 -05:00
|
|
|
}
|