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

169 lines
5.2 KiB
PHP
Raw Normal View History

<?php
/**
* LinkController.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).
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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Transaction;
2017-08-23 21:21:42 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\JournalLinkRequest;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalLink;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
2019-07-20 06:47:34 +02:00
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
2019-07-20 06:47:34 +02:00
use Illuminate\View\View;
use Log;
2017-08-23 21:21:42 +02:00
use URL;
2017-08-23 21:21:42 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class LinkController.
2017-08-23 21:21:42 +02:00
*/
class LinkController extends Controller
{
/** @var JournalRepositoryInterface Journals and transactions overview */
2018-01-17 10:40:44 +01:00
private $journalRepository;
2018-07-22 08:10:16 +02:00
/** @var LinkTypeRepositoryInterface Link repository. */
2018-01-17 10:40:44 +01:00
private $repository;
2017-08-23 21:21:42 +02:00
/**
2018-07-22 08:10:16 +02:00
* LinkController constructor.
*
2019-07-20 06:47:34 +02:00
* @codeCoverageIgnore
2017-08-23 21:21:42 +02:00
*/
public function __construct()
{
parent::__construct();
// some useful repositories:
$this->middleware(
function ($request, $next) {
app('view')->share('title', (string) trans('firefly.transactions'));
2020-05-01 06:24:24 +02:00
app('view')->share('mainTitleIcon', 'fa-exchange');
2017-08-23 21:21:42 +02:00
2018-01-17 10:40:44 +01:00
$this->journalRepository = app(JournalRepositoryInterface::class);
$this->repository = app(LinkTypeRepositoryInterface::class);
2017-08-23 21:21:42 +02:00
return $next($request);
}
);
}
/**
2018-07-22 08:10:16 +02:00
* Delete a link.
*
2017-08-23 21:21:42 +02:00
* @param TransactionJournalLink $link
*
2019-07-20 06:47:34 +02:00
* @return Factory|View
2017-08-23 21:21:42 +02:00
*/
public function delete(TransactionJournalLink $link)
{
$subTitleIcon = 'fa-link';
$subTitle = (string) trans('breadcrumbs.delete_journal_link');
2017-08-23 21:21:42 +02:00
$this->rememberPreviousUri('journal_links.delete.uri');
return view('transactions.links.delete', compact('link', 'subTitle', 'subTitleIcon'));
}
/**
2018-07-22 08:10:16 +02:00
* Actually destroy it.
*
2018-02-09 19:11:55 +01:00
* @param TransactionJournalLink $link
2017-08-23 21:21:42 +02:00
*
* @return RedirectResponse|Redirector
2017-08-23 21:21:42 +02:00
*/
2018-01-17 10:40:44 +01:00
public function destroy(TransactionJournalLink $link)
2017-08-23 21:21:42 +02:00
{
2018-01-17 10:40:44 +01:00
$this->repository->destroyLink($link);
2017-08-23 21:21:42 +02:00
session()->flash('success', (string) trans('firefly.deleted_link'));
2018-07-08 12:08:53 +02:00
app('preferences')->mark();
2017-08-23 21:21:42 +02:00
return redirect((string) session('journal_links.delete.uri'));
}
/**
* @param TransactionJournal $journal
*
* @return Factory|View
*/
public function modal(TransactionJournal $journal)
{
$linkTypes = $this->repository->get();
return view('transactions.links.modal', compact('journal', 'linkTypes'));
2017-08-23 21:21:42 +02:00
}
/**
2018-07-22 08:10:16 +02:00
* Store a new link.
*
2018-02-09 19:11:55 +01:00
* @param JournalLinkRequest $request
* @param TransactionJournal $journal
*
* @return RedirectResponse|Redirector
*/
2018-01-17 10:40:44 +01:00
public function store(JournalLinkRequest $request, TransactionJournal $journal)
{
2019-07-20 06:47:34 +02:00
$linkInfo = $request->getLinkInfo();
2018-01-17 10:40:44 +01:00
2017-12-23 17:42:07 +01:00
Log::debug('We are here (store)');
$other = $this->journalRepository->findNull($linkInfo['transaction_journal_id']);
2018-07-09 19:24:08 +02:00
if (null === $other) {
session()->flash('error', (string) trans('firefly.invalid_link_selection'));
2018-07-09 19:24:08 +02:00
2019-10-05 04:53:58 +02:00
return redirect(route('transactions.show', [$journal->transaction_group_id]));
2018-07-09 19:24:08 +02:00
}
2018-01-17 10:40:44 +01:00
$alreadyLinked = $this->repository->findLink($journal, $other);
2018-03-26 19:19:11 +02:00
2018-04-02 15:10:40 +02:00
if ($other->id === $journal->id) {
session()->flash('error', (string) trans('firefly.journals_link_to_self'));
2018-03-26 19:19:11 +02:00
2019-10-05 04:53:58 +02:00
return redirect(route('transactions.show', [$journal->transaction_group_id]));
2018-03-26 19:19:11 +02:00
}
2017-08-25 06:58:28 +02:00
if ($alreadyLinked) {
session()->flash('error', (string) trans('firefly.journals_error_linked'));
2019-10-05 04:53:58 +02:00
return redirect(route('transactions.show', [$journal->transaction_group_id]));
}
2017-08-25 06:58:28 +02:00
Log::debug(sprintf('Journal is %d, opposing is %d', $journal->id, $other->id));
2018-01-17 10:40:44 +01:00
$this->repository->storeLink($linkInfo, $other, $journal);
session()->flash('success', (string) trans('firefly.journals_linked'));
2019-08-06 19:23:18 +02:00
return redirect(route('transactions.show', [$journal->transaction_group_id]));
}
2017-09-09 06:41:45 +02:00
/**
2018-07-22 08:10:16 +02:00
* Switch link from A <> B to B <> A.
2018-08-06 19:14:30 +02:00
*
2018-02-09 19:11:55 +01:00
* @param TransactionJournalLink $link
2017-09-09 06:41:45 +02:00
*
* @return RedirectResponse|Redirector
2017-09-09 06:41:45 +02:00
*/
2018-01-17 10:40:44 +01:00
public function switchLink(TransactionJournalLink $link)
2017-08-24 19:42:23 +02:00
{
2018-01-17 10:40:44 +01:00
$this->repository->switchLink($link);
2017-08-24 19:42:23 +02:00
return redirect(URL::previous());
}
2017-08-31 06:47:18 +02:00
}