Files
firefly-iii/app/Http/Controllers/Transaction/DeleteController.php

121 lines
4.0 KiB
PHP
Raw Normal View History

2019-08-01 06:22:07 +02:00
<?php
/**
* DeleteController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* 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 12:09:03 +02:00
declare(strict_types=1);
2019-08-01 06:22:07 +02:00
namespace FireflyIII\Http\Controllers\Transaction;
2021-03-28 11:46:23 +02:00
2019-08-01 06:22:07 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
2021-09-18 10:20:19 +02:00
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
2019-08-01 06:22:07 +02:00
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
2019-08-01 06:22:07 +02:00
use Log;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class DeleteController
*/
class DeleteController extends Controller
{
2021-04-07 07:53:05 +02:00
private TransactionGroupRepositoryInterface $repository;
2019-08-01 06:22:07 +02:00
/**
* IndexController constructor.
*
2019-08-01 06:22:07 +02:00
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
2022-03-29 14:58:06 +02:00
app('view')->share('title', (string) trans('firefly.transactions'));
2020-05-01 06:24:24 +02:00
app('view')->share('mainTitleIcon', 'fa-exchange');
2019-08-01 06:22:07 +02:00
$this->repository = app(TransactionGroupRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Shows the form that allows a user to delete a transaction journal.
*
* @param TransactionGroup $group
*
2021-09-18 10:20:19 +02:00
* @return Factory|View|Redirector|RedirectResponse
2019-08-01 06:22:07 +02:00
*/
public function delete(TransactionGroup $group)
{
2019-08-17 08:29:35 +02:00
if (!$this->isEditableGroup($group)) {
return $this->redirectGroupToAccount($group);
2019-08-17 08:29:35 +02:00
}
2019-08-01 06:22:07 +02:00
Log::debug(sprintf('Start of delete view for group #%d', $group->id));
$journal = $group->transactionJournals->first();
if (null === $journal) {
throw new NotFoundHttpException;
}
$objectType = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
2022-03-29 14:58:06 +02:00
$subTitle = (string) trans('firefly.delete_' . $objectType, ['description' => $group->title ?? $journal->description]);
2021-10-03 06:05:47 +02:00
$previous = app('steam')->getSafePreviousUrl(route('index'));
2019-08-01 06:22:07 +02:00
// put previous url in session
2022-04-12 18:19:30 +02:00
Log::debug('Will try to remember previous URL');
$this->rememberPreviousUrl('transactions.delete.url');
2019-08-01 06:22:07 +02:00
return view('transactions.delete', compact('group', 'journal', 'subTitle', 'objectType', 'previous'));
2019-08-01 06:22:07 +02:00
}
/**
* Actually destroys the journal.
*
* @param TransactionGroup $group
*
* @return RedirectResponse
*/
public function destroy(TransactionGroup $group): RedirectResponse
{
2019-08-17 08:29:35 +02:00
if (!$this->isEditableGroup($group)) {
return $this->redirectGroupToAccount($group);
2019-08-17 08:29:35 +02:00
}
2019-08-01 06:22:07 +02:00
$journal = $group->transactionJournals->first();
if (null === $journal) {
throw new NotFoundHttpException;
}
$objectType = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
2022-03-29 14:58:06 +02:00
session()->flash('success', (string) trans('firefly.deleted_' . strtolower($objectType), ['description' => $group->title ?? $journal->description]));
2019-08-01 06:22:07 +02:00
$this->repository->destroy($group);
app('preferences')->mark();
2022-04-12 18:19:30 +02:00
return redirect($this->getPreviousUrl('transactions.delete.url'));
2019-08-01 06:22:07 +02:00
}
2019-08-17 12:09:03 +02:00
}