Files
firefly-iii/app/Http/Controllers/Admin/LinkController.php

263 lines
9.2 KiB
PHP
Raw Normal View History

2017-08-20 12:41:31 +02:00
<?php
2022-12-29 19:41:57 +01:00
2017-08-20 12:41:31 +02:00
/**
* LinkController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2017-08-20 12:41:31 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +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.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +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.
2017-10-21 08:40:00 +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/>.
2017-08-20 12:41:31 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Admin;
use FireflyIII\Http\Controllers\Controller;
2017-12-19 19:25:50 +01:00
use FireflyIII\Http\Middleware\IsDemoUser;
use FireflyIII\Http\Requests\LinkTypeFormRequest;
use FireflyIII\Models\LinkType;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
2023-05-29 13:56:55 +02:00
use Illuminate\View\View;
2017-08-20 12:41:31 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class LinkController.
2017-08-20 12:41:31 +02:00
*/
class LinkController extends Controller
{
2021-04-27 06:23:16 +02:00
private LinkTypeRepositoryInterface $repository;
2017-08-20 12:41:31 +02:00
/**
2018-07-21 08:06:24 +02:00
* LinkController constructor.
2017-08-20 12:41:31 +02:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2025-01-18 17:20:21 +01:00
app('view')->share('title', (string) trans('firefly.system_settings'));
2017-12-16 19:46:36 +01:00
app('view')->share('mainTitleIcon', 'fa-hand-spock-o');
$this->repository = app(LinkTypeRepositoryInterface::class);
2017-08-20 12:41:31 +02:00
return $next($request);
}
);
2017-12-19 19:25:50 +01:00
$this->middleware(IsDemoUser::class)->except(['index', 'show']);
2017-08-20 12:41:31 +02:00
}
/**
2018-07-21 08:06:24 +02:00
* Make a new link form.
2018-08-06 19:14:30 +02:00
*
2021-03-21 09:15:40 +01:00
* @return Factory|View
*/
public function create()
{
Log::channel('audit')->info('User visits link index.');
2024-12-22 08:43:12 +01:00
$subTitle = (string) trans('firefly.create_new_link_type');
$subTitleIcon = 'fa-link';
// put previous url in session if not redirect from store (not "create another").
if (true !== session('link-types.create.fromStore')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('link-types.create.url');
}
2025-01-18 17:26:31 +01:00
return view('settings.link.create', compact('subTitle', 'subTitleIcon'));
}
/**
2018-07-21 08:06:24 +02:00
* Delete a link form.
*
2023-12-20 19:35:52 +01:00
* @return Factory|Redirector|RedirectResponse|View
*/
public function delete(Request $request, LinkType $linkType)
{
2025-01-03 14:56:06 +01:00
if (false === $linkType->editable) {
2024-12-22 08:43:12 +01:00
$request->session()->flash('error', (string) trans('firefly.cannot_edit_link_type', ['name' => e($linkType->name)]));
2025-01-18 17:20:21 +01:00
return redirect(route('settings.links.index'));
}
Log::channel('audit')->info(sprintf('User wants to delete link type #%d', $linkType->id));
2024-12-22 08:43:12 +01:00
$subTitle = (string) trans('firefly.delete_link_type', ['name' => $linkType->name]);
$otherTypes = $this->repository->get();
$count = $this->repository->countJournals($linkType);
$moveTo = [];
2024-12-22 08:43:12 +01:00
$moveTo[0] = (string) trans('firefly.do_not_save_connection');
/** @var LinkType $otherType */
foreach ($otherTypes as $otherType) {
if ($otherType->id !== $linkType->id) {
$moveTo[$otherType->id] = sprintf('%s (%s / %s)', $otherType->name, $otherType->inward, $otherType->outward);
}
}
// put previous url in session
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('link-types.delete.url');
2025-01-18 17:26:31 +01:00
return view('settings.link.delete', compact('linkType', 'subTitle', 'moveTo', 'count'));
}
/**
2018-07-21 08:06:24 +02:00
* Actually destroy the link.
*
2023-12-20 19:35:52 +01:00
* @return Redirector|RedirectResponse
*/
public function destroy(Request $request, LinkType $linkType)
{
Log::channel('audit')->info(sprintf('User destroyed link type #%d', $linkType->id));
$name = $linkType->name;
2024-12-22 08:43:12 +01:00
$moveTo = $this->repository->find((int) $request->get('move_link_type_before_delete'));
$this->repository->destroy($linkType, $moveTo);
2024-12-22 08:43:12 +01:00
$request->session()->flash('success', (string) trans('firefly.deleted_link_type', ['name' => $name]));
2018-07-08 12:08:53 +02:00
app('preferences')->mark();
2022-04-12 18:19:30 +02:00
return redirect($this->getPreviousUrl('link-types.delete.url'));
}
/**
2018-07-21 08:06:24 +02:00
* Edit a link form.
*
2023-12-20 19:35:52 +01:00
* @return Factory|Redirector|RedirectResponse|View
2017-08-20 12:41:31 +02:00
*/
public function edit(Request $request, LinkType $linkType)
{
2025-01-03 14:56:06 +01:00
if (false === $linkType->editable) {
2024-12-22 08:43:12 +01:00
$request->session()->flash('error', (string) trans('firefly.cannot_edit_link_type', ['name' => e($linkType->name)]));
2025-01-18 17:20:21 +01:00
return redirect(route('settings.links.index'));
}
2024-12-22 08:43:12 +01:00
$subTitle = (string) trans('firefly.edit_link_type', ['name' => $linkType->name]);
$subTitleIcon = 'fa-link';
Log::channel('audit')->info(sprintf('User wants to edit link type #%d', $linkType->id));
// put previous url in session if not redirect from store (not "return_to_edit").
if (true !== session('link-types.edit.fromUpdate')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('link-types.edit.url');
}
$request->session()->forget('link-types.edit.fromUpdate');
2025-01-18 17:26:31 +01:00
return view('settings.link.edit', compact('subTitle', 'subTitleIcon', 'linkType'));
}
/**
2018-07-21 08:06:24 +02:00
* Show index of all links.
*
2021-03-21 09:15:40 +01:00
* @return Factory|View
*/
public function index()
2017-08-20 12:41:31 +02:00
{
2024-12-22 08:43:12 +01:00
$subTitle = (string) trans('firefly.journal_link_configuration');
2017-08-20 12:41:31 +02:00
$subTitleIcon = 'fa-link';
$linkTypes = $this->repository->get();
Log::channel('audit')->info('User on index of link types in admin.');
$linkTypes->each(
2023-12-21 05:07:26 +01:00
function (LinkType $linkType): void {
$linkType->journalCount = $this->repository->countJournals($linkType);
}
);
2025-01-18 17:26:31 +01:00
return view('settings.link.index', compact('subTitle', 'subTitleIcon', 'linkTypes'));
}
2017-08-23 21:21:42 +02:00
/**
2018-07-21 08:06:24 +02:00
* Show a single link.
*
2021-03-21 09:15:40 +01:00
* @return Factory|View
2017-08-23 21:21:42 +02:00
*/
public function show(LinkType $linkType)
{
2024-12-22 08:43:12 +01:00
$subTitle = (string) trans('firefly.overview_for_link', ['name' => $linkType->name]);
2017-08-23 21:21:42 +02:00
$subTitleIcon = 'fa-link';
$links = $this->repository->getJournalLinks($linkType);
2017-08-23 21:21:42 +02:00
Log::channel('audit')->info(sprintf('User viewing link type #%d', $linkType->id));
2025-01-18 17:26:31 +01:00
return view('settings.link.show', compact('subTitle', 'subTitleIcon', 'linkType', 'links'));
2017-08-23 21:21:42 +02:00
}
/**
2018-07-21 08:06:24 +02:00
* Store the new link.
*
2023-12-20 19:35:52 +01:00
* @return $this|Redirector|RedirectResponse
*/
public function store(LinkTypeFormRequest $request)
{
$data = [
2022-05-02 19:35:35 +02:00
'name' => $request->convertString('name'),
'inward' => $request->convertString('inward'),
'outward' => $request->convertString('outward'),
];
$linkType = $this->repository->store($data);
Log::channel('audit')->info('User stored new link type.', $linkType->toArray());
2024-12-22 08:43:12 +01:00
$request->session()->flash('success', (string) trans('firefly.stored_new_link_type', ['name' => $linkType->name]));
2022-04-12 18:19:30 +02:00
$redirect = redirect($this->getPreviousUrl('link-types.create.url'));
2024-12-22 08:43:12 +01:00
if (1 === (int) $request->get('create_another')) {
// set value so create routine will not overwrite URL:
$request->session()->put('link-types.create.fromStore', true);
2025-01-18 17:20:21 +01:00
$redirect = redirect(route('settings.links.create'))->withInput();
}
// redirect to previous URL.
2018-07-08 12:08:53 +02:00
return $redirect;
}
2017-12-17 14:30:53 +01:00
/**
2018-07-21 08:06:24 +02:00
* Update an existing link.
*
2023-12-20 19:35:52 +01:00
* @return $this|Redirector|RedirectResponse
2017-12-17 14:30:53 +01:00
*/
public function update(LinkTypeFormRequest $request, LinkType $linkType)
{
2025-01-03 14:56:06 +01:00
if (false === $linkType->editable) {
2024-12-22 08:43:12 +01:00
$request->session()->flash('error', (string) trans('firefly.cannot_edit_link_type', ['name' => e($linkType->name)]));
2025-01-18 17:20:21 +01:00
return redirect(route('settings.links.index'));
}
$data = [
2022-05-02 19:35:35 +02:00
'name' => $request->convertString('name'),
'inward' => $request->convertString('inward'),
'outward' => $request->convertString('outward'),
];
$this->repository->update($linkType, $data);
Log::channel('audit')->info(sprintf('User update link type #%d.', $linkType->id), $data);
2024-12-22 08:43:12 +01:00
$request->session()->flash('success', (string) trans('firefly.updated_link_type', ['name' => $linkType->name]));
2018-07-08 12:08:53 +02:00
app('preferences')->mark();
2022-04-12 18:19:30 +02:00
$redirect = redirect($this->getPreviousUrl('link-types.edit.url'));
2024-12-22 08:43:12 +01:00
if (1 === (int) $request->get('return_to_edit')) {
// set value so edit routine will not overwrite URL:
$request->session()->put('link-types.edit.fromUpdate', true);
2025-01-18 17:26:31 +01:00
$redirect = redirect(route('settings.links.edit', [$linkType->id]))->withInput(['return_to_edit' => 1]);
}
2017-08-20 12:41:31 +02:00
// redirect to previous URL.
2018-07-08 12:08:53 +02:00
return $redirect;
2017-08-20 12:41:31 +02:00
}
2017-08-31 06:47:18 +02:00
}