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

151 lines
4.8 KiB
PHP
Raw Normal View History

<?php
/**
* LinkController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://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;
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.
2017-08-23 21:21:42 +02:00
*/
public function __construct()
{
parent::__construct();
// some useful repositories:
$this->middleware(
function ($request, $next) {
2018-07-15 09:38:49 +02:00
app('view')->share('title', (string)trans('firefly.transactions'));
2017-12-16 19:46:36 +01:00
app('view')->share('mainTitleIcon', 'fa-repeat');
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
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function delete(TransactionJournalLink $link)
{
$subTitleIcon = 'fa-link';
2018-07-15 09:38:49 +02:00
$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 \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
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
2018-04-22 17:10:11 +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
2018-04-02 15:10:40 +02:00
return redirect((string)session('journal_links.delete.uri'));
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 \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2018-01-17 10:40:44 +01:00
public function store(JournalLinkRequest $request, TransactionJournal $journal)
{
2017-12-23 17:42:07 +01:00
Log::debug('We are here (store)');
2017-09-14 17:40:02 +02:00
$linkInfo = $request->getLinkInfo();
2018-07-20 14:34:56 +02:00
$other = $this->journalRepository->findNull($linkInfo['transaction_journal_id']);
2018-07-09 19:24:08 +02:00
if (null === $other) {
2018-07-15 09:38:49 +02:00
session()->flash('error', (string)trans('firefly.invalid_link_selection'));
2018-07-09 19:24:08 +02:00
return redirect(route('transactions.show', [$journal->id]));
}
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) {
2018-07-15 09:38:49 +02:00
session()->flash('error', (string)trans('firefly.journals_link_to_self'));
2018-03-26 19:19:11 +02:00
return redirect(route('transactions.show', [$journal->id]));
}
2017-08-25 06:58:28 +02:00
if ($alreadyLinked) {
2018-07-15 09:38:49 +02:00
session()->flash('error', (string)trans('firefly.journals_error_linked'));
2017-09-09 06:41:45 +02:00
return redirect(route('transactions.show', [$journal->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);
2018-07-15 09:38:49 +02:00
session()->flash('success', (string)trans('firefly.journals_linked'));
2017-09-09 06:41:45 +02:00
return redirect(route('transactions.show', [$journal->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 \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
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
}