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

137 lines
4.5 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 Preferences;
use Session;
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
{
2017-08-23 21:21:42 +02:00
/**
*
*/
public function __construct()
{
parent::__construct();
// some useful repositories:
$this->middleware(
function ($request, $next) {
2017-12-16 19:46:36 +01:00
app('view')->share('title', trans('firefly.transactions'));
app('view')->share('mainTitleIcon', 'fa-repeat');
2017-08-23 21:21:42 +02:00
return $next($request);
}
);
}
/**
* @param TransactionJournalLink $link
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function delete(TransactionJournalLink $link)
{
$subTitleIcon = 'fa-link';
$subTitle = trans('breadcrumbs.delete_journal_link');
$this->rememberPreviousUri('journal_links.delete.uri');
return view('transactions.links.delete', compact('link', 'subTitle', 'subTitleIcon'));
}
/**
* @param LinkTypeRepositoryInterface $repository
* @param TransactionJournalLink $link
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function destroy(LinkTypeRepositoryInterface $repository, TransactionJournalLink $link)
{
$repository->destroyLink($link);
Session::flash('success', strval(trans('firefly.deleted_link')));
Preferences::mark();
return redirect(strval(session('journal_links.delete.uri')));
}
/**
* @param JournalLinkRequest $request
* @param LinkTypeRepositoryInterface $repository
* @param JournalRepositoryInterface $journalRepository
* @param TransactionJournal $journal
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function store(
2017-11-15 10:52:29 +01:00
JournalLinkRequest $request,
LinkTypeRepositoryInterface $repository,
JournalRepositoryInterface $journalRepository,
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();
2017-11-15 12:25:49 +01:00
if (0 === $linkInfo['transaction_journal_id']) {
2017-09-09 18:44:05 +02:00
Session::flash('error', trans('firefly.invalid_link_selection'));
return redirect(route('transactions.show', [$journal->id]));
}
2017-08-25 06:58:28 +02:00
$other = $journalRepository->find($linkInfo['transaction_journal_id']);
$alreadyLinked = $repository->findLink($journal, $other);
if ($alreadyLinked) {
Session::flash('error', 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));
2017-12-23 17:42:07 +01:00
$repository->storeLink($linkInfo, $other, $journal);
Session::flash('success', 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
/**
* @param LinkTypeRepositoryInterface $repository
* @param TransactionJournalLink $link
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2017-11-25 08:54:52 +01:00
public function switchLink(LinkTypeRepositoryInterface $repository, TransactionJournalLink $link)
2017-08-24 19:42:23 +02:00
{
$repository->switchLink($link);
return redirect(URL::previous());
}
2017-08-31 06:47:18 +02:00
}