Files
firefly-iii/app/Support/Http/Controllers/UserNavigation.php

153 lines
5.6 KiB
PHP
Raw Normal View History

<?php
/**
* UserNavigation.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program 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 Affero General Public License for more details.
*
* 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\Support\Http\Controllers;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
2019-08-17 08:29:35 +02:00
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
2019-08-17 08:29:35 +02:00
use FireflyIII\Models\TransactionType;
2021-03-21 09:15:40 +01:00
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
/**
* Trait UserNavigation
*/
trait UserNavigation
{
2021-03-21 09:15:40 +01:00
/**
* Functionality:.
*
2023-06-21 12:34:58 +02:00
* - If the $identifier contains the word "delete" then a remembered url with the text "/show/" in it will not be
* returned but instead the index (/) will be returned.
* - If the remembered url contains "jscript/" the remembered url will not be returned but instead the index (/)
* will be returned.
2021-03-21 09:15:40 +01:00
*/
2022-04-12 18:19:30 +02:00
final protected function getPreviousUrl(string $identifier): string
2021-03-21 09:15:40 +01:00
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Trying to retrieve URL stored under "%s"', $identifier));
2024-12-22 08:43:12 +01:00
$url = (string) session($identifier);
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('The URL is %s', $url));
2021-03-21 09:15:40 +01:00
2021-10-03 06:05:47 +02:00
return app('steam')->getSafeUrl($url, route('index'));
2021-03-21 09:15:40 +01:00
}
/**
2019-08-17 08:29:35 +02:00
* Will return false if you cant edit this account type.
*/
2021-04-06 17:00:16 +02:00
final protected function isEditableAccount(Account $account): bool
{
2019-08-17 08:29:35 +02:00
$editable = [AccountType::EXPENSE, AccountType::REVENUE, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
$type = $account->accountType->type;
2019-08-01 06:22:07 +02:00
2019-08-17 08:29:35 +02:00
return in_array($type, $editable, true);
}
2021-04-06 17:00:16 +02:00
final protected function isEditableGroup(TransactionGroup $group): bool
2019-08-17 08:29:35 +02:00
{
2023-12-20 19:35:52 +01:00
/** @var null|TransactionJournal $journal */
2024-12-22 20:32:58 +01:00
$journal = $group->transactionJournals()->first();
2019-08-17 08:29:35 +02:00
if (null === $journal) {
return false;
}
2019-08-17 08:29:35 +02:00
$type = $journal->transactionType->type;
2019-11-13 06:57:17 +01:00
$editable = [TransactionType::WITHDRAWAL, TransactionType::TRANSFER, TransactionType::DEPOSIT, TransactionType::RECONCILIATION];
2019-08-17 08:29:35 +02:00
return in_array($type, $editable, true);
}
/**
2023-12-20 19:35:52 +01:00
* @return Redirector|RedirectResponse
*/
2021-04-06 17:00:16 +02:00
final protected function redirectAccountToAccount(Account $account)
{
2019-08-17 08:29:35 +02:00
$type = $account->accountType->type;
if (AccountType::RECONCILIATION === $type || AccountType::INITIAL_BALANCE === $type || AccountType::LIABILITY_CREDIT === $type) {
2019-08-17 08:29:35 +02:00
// reconciliation must be stored somewhere in this account's transactions.
2023-12-20 19:35:52 +01:00
/** @var null|Transaction $transaction */
2019-08-17 08:29:35 +02:00
$transaction = $account->transactions()->first();
if (null === $transaction) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('Account #%d has no transactions. Dont know where it belongs.', $account->id));
2019-08-17 08:29:35 +02:00
session()->flash('error', trans('firefly.cant_find_redirect_account'));
return redirect(route('index'));
}
2024-12-22 20:32:58 +01:00
$journal = $transaction->transactionJournal;
2023-12-20 19:35:52 +01:00
/** @var null|Transaction $other */
2024-12-22 20:32:58 +01:00
$other = $journal->transactions()->where('id', '!=', $transaction->id)->first();
2019-08-17 08:29:35 +02:00
if (null === $other) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('Account #%d has no valid journals. Dont know where it belongs.', $account->id));
2019-08-17 08:29:35 +02:00
session()->flash('error', trans('firefly.cant_find_redirect_account'));
return redirect(route('index'));
}
return redirect(route('accounts.show', [$other->account_id]));
}
2019-08-17 08:29:35 +02:00
return redirect(route('index'));
}
/**
2023-12-20 19:35:52 +01:00
* @return Redirector|RedirectResponse
2019-08-17 08:29:35 +02:00
*/
2021-04-06 17:00:16 +02:00
final protected function redirectGroupToAccount(TransactionGroup $group)
2019-08-17 08:29:35 +02:00
{
2023-12-20 19:35:52 +01:00
/** @var null|TransactionJournal $journal */
2024-12-22 20:32:58 +01:00
$journal = $group->transactionJournals()->first();
2021-03-21 09:15:40 +01:00
if (null === $journal) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('No journals in group #%d', $group->id));
2021-03-21 09:15:40 +01:00
return redirect(route('index'));
}
// prefer redirect to everything but expense and revenue:
$transactions = $journal->transactions;
$ignore = [AccountType::REVENUE, AccountType::EXPENSE, AccountType::RECONCILIATION, AccountType::INITIAL_BALANCE];
2023-12-20 19:35:52 +01:00
2021-03-21 09:15:40 +01:00
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$type = $transaction->account->accountType->type;
if (!in_array($type, $ignore, true)) {
return redirect(route('accounts.edit', [$transaction->account_id]));
}
}
2021-03-21 09:15:40 +01:00
return redirect(route('index'));
}
2018-08-10 17:05:37 +02:00
2022-04-12 18:19:30 +02:00
final protected function rememberPreviousUrl(string $identifier): ?string
2018-08-10 17:05:37 +02:00
{
2021-10-03 06:05:47 +02:00
$return = app('steam')->getSafePreviousUrl();
session()->put($identifier, $return);
2021-03-21 09:15:40 +01:00
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('rememberPreviousUrl: %s: "%s"', $identifier, $return));
2021-10-02 12:50:21 +02:00
return $return;
2018-08-10 17:05:37 +02:00
}
2018-12-31 07:48:23 +01:00
}