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

143 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
* LinkController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05: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 07:41:58 -06: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 14:21:42 -05: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 14:21:42 -05:00
use URL;
2017-08-23 14:21:42 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class LinkController.
2017-08-23 14:21:42 -05:00
*/
class LinkController extends Controller
{
/** @var JournalRepositoryInterface Journals and transactions overview */
2018-01-17 03:40:44 -06:00
private $journalRepository;
/** @var LinkTypeRepositoryInterface */
private $repository;
2017-08-23 14:21:42 -05:00
/**
*
*/
public function __construct()
{
parent::__construct();
// some useful repositories:
$this->middleware(
function ($request, $next) {
2018-07-15 02:38:49 -05:00
app('view')->share('title', (string)trans('firefly.transactions'));
2017-12-16 12:46:36 -06:00
app('view')->share('mainTitleIcon', 'fa-repeat');
2017-08-23 14:21:42 -05:00
2018-01-17 03:40:44 -06:00
$this->journalRepository = app(JournalRepositoryInterface::class);
$this->repository = app(LinkTypeRepositoryInterface::class);
2017-08-23 14:21:42 -05:00
return $next($request);
}
);
}
/**
* @param TransactionJournalLink $link
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function delete(TransactionJournalLink $link)
{
$subTitleIcon = 'fa-link';
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('breadcrumbs.delete_journal_link');
2017-08-23 14:21:42 -05:00
$this->rememberPreviousUri('journal_links.delete.uri');
return view('transactions.links.delete', compact('link', 'subTitle', 'subTitleIcon'));
}
/**
2018-02-09 12:11:55 -06:00
* @param TransactionJournalLink $link
2017-08-23 14:21:42 -05:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2018-01-17 03:40:44 -06:00
public function destroy(TransactionJournalLink $link)
2017-08-23 14:21:42 -05:00
{
2018-01-17 03:40:44 -06:00
$this->repository->destroyLink($link);
2017-08-23 14:21:42 -05:00
2018-04-22 10:10:11 -05:00
session()->flash('success', (string)trans('firefly.deleted_link'));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2017-08-23 14:21:42 -05:00
2018-04-02 08:10:40 -05:00
return redirect((string)session('journal_links.delete.uri'));
2017-08-23 14:21:42 -05:00
}
/**
2018-02-09 12:11:55 -06:00
* @param JournalLinkRequest $request
* @param TransactionJournal $journal
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2018-01-17 03:40:44 -06:00
public function store(JournalLinkRequest $request, TransactionJournal $journal)
{
2017-12-23 10:42:07 -06:00
Log::debug('We are here (store)');
2017-09-14 10:40:02 -05:00
$linkInfo = $request->getLinkInfo();
2018-07-20 07:34:56 -05:00
$other = $this->journalRepository->findNull($linkInfo['transaction_journal_id']);
2018-07-09 12:24:08 -05:00
if (null === $other) {
2018-07-15 02:38:49 -05:00
session()->flash('error', (string)trans('firefly.invalid_link_selection'));
2018-07-09 12:24:08 -05:00
return redirect(route('transactions.show', [$journal->id]));
}
2018-01-17 03:40:44 -06:00
$alreadyLinked = $this->repository->findLink($journal, $other);
2018-03-26 12:19:11 -05:00
2018-04-02 08:10:40 -05:00
if ($other->id === $journal->id) {
2018-07-15 02:38:49 -05:00
session()->flash('error', (string)trans('firefly.journals_link_to_self'));
2018-03-26 12:19:11 -05:00
return redirect(route('transactions.show', [$journal->id]));
}
2017-08-24 23:58:28 -05:00
if ($alreadyLinked) {
2018-07-15 02:38:49 -05:00
session()->flash('error', (string)trans('firefly.journals_error_linked'));
2017-09-08 23:41:45 -05:00
return redirect(route('transactions.show', [$journal->id]));
}
2017-08-24 23:58:28 -05:00
Log::debug(sprintf('Journal is %d, opposing is %d', $journal->id, $other->id));
2018-01-17 03:40:44 -06:00
$this->repository->storeLink($linkInfo, $other, $journal);
2018-07-15 02:38:49 -05:00
session()->flash('success', (string)trans('firefly.journals_linked'));
2017-09-08 23:41:45 -05:00
return redirect(route('transactions.show', [$journal->id]));
}
2017-09-08 23:41:45 -05:00
/**
2018-02-09 12:11:55 -06:00
* @param TransactionJournalLink $link
2017-09-08 23:41:45 -05:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2018-01-17 03:40:44 -06:00
public function switchLink(TransactionJournalLink $link)
2017-08-24 12:42:23 -05:00
{
2018-01-17 03:40:44 -06:00
$this->repository->switchLink($link);
2017-08-24 12:42:23 -05:00
return redirect(URL::previous());
}
2017-08-30 23:47:18 -05:00
}