2019-08-05 12:45:20 -05:00
|
|
|
<?php
|
2019-10-01 23:37:26 -05:00
|
|
|
/**
|
|
|
|
* InterestingMessage.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2019-10-01 23:37:26 -05:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-08-17 05:09:03 -05:00
|
|
|
declare(strict_types=1);
|
2019-08-05 12:45:20 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Middleware;
|
|
|
|
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use FireflyIII\Models\TransactionGroup;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use Illuminate\Http\Request;
|
2019-08-11 11:58:30 -05:00
|
|
|
use Preferences;
|
2019-08-05 12:45:20 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class InterestingMessage
|
|
|
|
*/
|
|
|
|
class InterestingMessage
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Flashes the user an interesting message if the URL parameters warrant it.
|
|
|
|
*
|
2020-03-17 09:02:57 -05:00
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
2019-08-05 12:45:20 -05:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
|
|
|
if ($this->testing()) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->groupMessage($request)) {
|
2019-08-11 11:58:30 -05:00
|
|
|
Preferences::mark();
|
2019-08-05 12:45:20 -05:00
|
|
|
$this->handleGroupMessage($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function testing(): bool
|
|
|
|
{
|
|
|
|
// ignore middleware in test environment.
|
|
|
|
return 'testing' === config('app.env') || !auth()->check();
|
|
|
|
}
|
|
|
|
|
2019-08-05 12:45:20 -05:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function groupMessage(Request $request): bool
|
|
|
|
{
|
|
|
|
// get parameters from request.
|
|
|
|
$transactionGroupId = $request->get('transaction_group_id');
|
|
|
|
$message = $request->get('message');
|
|
|
|
|
|
|
|
return null !== $transactionGroupId && null !== $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
private function handleGroupMessage(Request $request): void
|
|
|
|
{
|
|
|
|
|
|
|
|
// get parameters from request.
|
|
|
|
$transactionGroupId = $request->get('transaction_group_id');
|
|
|
|
$message = $request->get('message');
|
|
|
|
|
|
|
|
// send message about newly created transaction group.
|
|
|
|
/** @var TransactionGroup $group */
|
2021-03-21 03:15:40 -05:00
|
|
|
$group = auth()->user()->transactionGroups()->with(['transactionJournals', 'transactionJournals.transactionType'])->find((int)$transactionGroupId);
|
2019-08-05 12:45:20 -05:00
|
|
|
|
|
|
|
if (null === $group) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = $group->transactionJournals->count();
|
|
|
|
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
$journal = $group->transactionJournals->first();
|
|
|
|
if (null === $journal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$title = $count > 1 ? $group->title : $journal->description;
|
|
|
|
if ('created' === $message) {
|
|
|
|
session()->flash('success_uri', route('transactions.show', [$transactionGroupId]));
|
2021-03-21 03:15:40 -05:00
|
|
|
session()->flash('success', (string)trans('firefly.stored_journal', ['description' => $title]));
|
2019-08-05 12:45:20 -05:00
|
|
|
}
|
|
|
|
if ('updated' === $message) {
|
|
|
|
$type = strtolower($journal->transactionType->type);
|
|
|
|
session()->flash('success_uri', route('transactions.show', [$transactionGroupId]));
|
2021-03-21 03:15:40 -05:00
|
|
|
session()->flash('success', (string)trans(sprintf('firefly.updated_%s', $type), ['description' => $title]));
|
2019-08-05 12:45:20 -05:00
|
|
|
}
|
2021-03-21 14:38:44 -05:00
|
|
|
if('no_change' === $message) {
|
|
|
|
$type = strtolower($journal->transactionType->type);
|
|
|
|
session()->flash('warning_uri', route('transactions.show', [$transactionGroupId]));
|
|
|
|
session()->flash('warning', (string)trans(sprintf('firefly.no_changes_%s', $type), ['description' => $title]));
|
|
|
|
}
|
2019-08-05 12:45:20 -05:00
|
|
|
}
|
2019-08-17 05:09:03 -05:00
|
|
|
}
|