. */ declare(strict_types=1); namespace FireflyIII\Http\Controllers\Transaction; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\TransactionGroup; use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface; use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Http\RedirectResponse; use Illuminate\Routing\Redirector; use Log; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * Class DeleteController */ class DeleteController extends Controller { private TransactionGroupRepositoryInterface $repository; /** * IndexController constructor. * * @codeCoverageIgnore */ public function __construct() { parent::__construct(); // translations: $this->middleware( function ($request, $next) { app('view')->share('title', (string) trans('firefly.transactions')); app('view')->share('mainTitleIcon', 'fa-exchange'); $this->repository = app(TransactionGroupRepositoryInterface::class); return $next($request); } ); } /** * Shows the form that allows a user to delete a transaction journal. * * @param TransactionGroup $group * * @return Factory|View|Redirector|RedirectResponse */ public function delete(TransactionGroup $group) { if (!$this->isEditableGroup($group)) { return $this->redirectGroupToAccount($group); } 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); $subTitle = (string) trans('firefly.delete_' . $objectType, ['description' => $group->title ?? $journal->description]); $previous = app('steam')->getSafePreviousUrl(route('index')); // put previous url in session Log::debug('Will try to remember previous URL'); $this->rememberPreviousUrl('transactions.delete.url'); return view('transactions.delete', compact('group', 'journal', 'subTitle', 'objectType', 'previous')); } /** * Actually destroys the journal. * * @param TransactionGroup $group * * @return RedirectResponse */ public function destroy(TransactionGroup $group): RedirectResponse { if (!$this->isEditableGroup($group)) { return $this->redirectGroupToAccount($group); } $journal = $group->transactionJournals->first(); if (null === $journal) { throw new NotFoundHttpException; } $objectType = strtolower($journal->transaction_type_type ?? $journal->transactionType->type); session()->flash('success', (string) trans('firefly.deleted_' . strtolower($objectType), ['description' => $group->title ?? $journal->description])); $this->repository->destroy($group); app('preferences')->mark(); return redirect($this->getPreviousUrl('transactions.delete.url')); } }