Files
firefly-iii/app/Http/Controllers/PreferencesController.php

285 lines
11 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
2022-12-29 19:41:57 +01:00
/**
* PreferencesController.php
2020-01-31 07:32:04 +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.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* 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.
2017-10-21 08:40:00 +02:00
*
* 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/>.
*/
2017-03-24 11:07:38 +01:00
declare(strict_types=1);
2016-05-20 08:57:45 +02:00
namespace FireflyIII\Http\Controllers;
2015-02-25 21:19:06 +01:00
use FireflyIII\Exceptions\FireflyException;
2020-03-13 15:47:26 +01:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2019-08-08 17:08:36 +02:00
use FireflyIII\Models\Preference;
2016-10-10 07:49:39 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2023-08-15 13:52:30 +02:00
use FireflyIII\Support\Notifications\UrlValidator;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
2017-12-29 09:05:35 +01:00
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
2023-04-08 06:55:38 +02:00
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
use JsonException;
2022-12-29 19:41:57 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2015-03-10 17:26:31 +01:00
2015-02-25 21:19:06 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class PreferencesController.
2015-02-25 21:19:06 +01:00
*/
2015-03-10 17:26:31 +01:00
class PreferencesController extends Controller
{
2015-02-25 21:19:06 +01:00
/**
2018-07-22 08:10:16 +02:00
* PreferencesController constructor.
*
2015-02-25 21:19:06 +01:00
*/
public function __construct()
{
2015-04-28 15:26:30 +02:00
parent::__construct();
2016-10-29 07:44:46 +02:00
$this->middleware(
function ($request, $next) {
2022-12-29 19:41:57 +01:00
app('view')->share('title', (string)trans('firefly.preferences'));
2017-12-16 19:46:36 +01:00
app('view')->share('mainTitleIcon', 'fa-gear');
2016-10-29 07:44:46 +02:00
return $next($request);
}
);
2015-02-25 21:19:06 +01:00
}
/**
2018-07-22 08:10:16 +02:00
* Show overview of preferences.
*
2023-06-21 12:34:58 +02:00
* @param AccountRepositoryInterface $repository
2015-05-03 12:58:55 +02:00
*
* @return Factory|View
* @throws FireflyException
2022-12-29 19:41:57 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2015-02-25 21:19:06 +01:00
*/
2016-10-10 07:49:39 +02:00
public function index(AccountRepositoryInterface $repository)
2015-02-25 21:19:06 +01:00
{
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]);
2020-06-30 20:43:53 +02:00
$isDocker = env('IS_DOCKER', false);
2020-07-23 19:40:10 +02:00
2020-03-13 15:47:26 +01:00
// group accounts
$groupedAccounts = [];
/** @var Account $account */
foreach ($accounts as $account) {
$type = $account->accountType->type;
$role = sprintf('opt_group_%s', $repository->getMetaValue($account, 'account_role'));
if (in_array($type, [AccountType::MORTGAGE, AccountType::DEBT, AccountType::LOAN], true)) {
$role = sprintf('opt_group_l_%s', $type);
2020-03-13 15:47:26 +01:00
}
2023-01-03 06:48:53 +01:00
if ('opt_group_' === $role) {
2020-03-13 15:47:26 +01:00
$role = 'opt_group_defaultAsset';
}
$groupedAccounts[trans(sprintf('firefly.%s', $role))][$account->id] = $account->name;
2020-03-13 15:47:26 +01:00
}
ksort($groupedAccounts);
2023-02-22 18:14:14 +01:00
$accountIds = $accounts->pluck('id')->toArray();
$viewRange = app('navigation')->getViewRange(false);
$frontPageAccounts = app('preferences')->get('frontPageAccounts', $accountIds);
$language = app('steam')->getLanguage();
2020-03-16 06:54:18 +01:00
$languages = config('firefly.languages');
$locale = app('preferences')->get('locale', config('firefly.default_locale', 'equal'))->data;
$listPageSize = app('preferences')->get('listPageSize', 50)->data;
2023-04-08 06:55:38 +02:00
$darkMode = app('preferences')->get('darkMode', 'browser')->data;
2022-09-24 12:14:27 +02:00
$slackUrl = app('preferences')->get('slack_webhook_url', '')->data;
$customFiscalYear = app('preferences')->get('customFiscalYear', 0)->data;
$fiscalYearStartStr = app('preferences')->get('fiscalYearStart', '01-01')->data;
2023-06-21 12:34:58 +02:00
$fiscalYearStart = date('Y') . '-' . $fiscalYearStartStr;
$tjOptionalFields = app('preferences')->get('transaction_journal_optional_fields', [])->data;
2023-04-08 06:55:38 +02:00
$availableDarkModes = config('firefly.available_dark_modes');
2015-12-24 08:35:08 +01:00
2022-09-18 16:49:15 +02:00
// notification preferences (single value for each):
$notifications = [];
foreach (config('firefly.available_notifications') as $notification) {
$notifications[$notification] = app('preferences')->get(sprintf('notification_%s', $notification), true)->data;
}
2020-03-16 06:54:18 +01:00
ksort($languages);
2020-03-16 06:53:10 +01:00
// list of locales also has "equal" which makes it equal to whatever the language is.
try {
$locales = json_decode(file_get_contents(resource_path(sprintf('lang/%s/locales.json', $language))), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
Log::error($e->getMessage());
$locales = [];
}
2022-12-29 19:41:57 +01:00
$locales = ['equal' => (string)trans('firefly.equal_to_language')] + $locales;
// an important fallback is that the frontPageAccount array gets refilled automatically
// when it turns up empty.
2022-11-04 05:11:05 +01:00
if (0 === count($frontPageAccounts->data)) {
$frontPageAccounts = $accountIds;
}
// for the demo user, the slackUrl is automatically emptied.
// this isn't really secure but it means that the demo site has a semi-secret
// slackUrl.
if (auth()->user()->hasRole('demo')) {
$slackUrl = '';
}
return view(
2016-01-27 21:52:21 +01:00
'preferences.index',
compact(
2017-11-15 10:52:29 +01:00
'language',
2020-03-13 15:47:26 +01:00
'groupedAccounts',
2020-06-30 20:43:53 +02:00
'isDocker',
2017-11-15 10:52:29 +01:00
'frontPageAccounts',
2020-03-16 06:54:18 +01:00
'languages',
2023-04-08 06:55:38 +02:00
'darkMode',
'availableDarkModes',
2022-09-18 16:49:15 +02:00
'notifications',
2022-09-24 12:14:27 +02:00
'slackUrl',
'locales',
'locale',
2017-11-15 10:52:29 +01:00
'tjOptionalFields',
'viewRange',
'customFiscalYear',
'listPageSize',
2018-03-09 05:45:22 +01:00
'fiscalYearStart'
)
2016-01-27 21:52:21 +01:00
);
2015-02-25 21:19:06 +01:00
}
/**
2018-07-22 08:10:16 +02:00
* Store new preferences.
*
2023-06-21 12:34:58 +02:00
* @param Request $request
*
* @return RedirectResponse|Redirector
* @throws FireflyException
2022-12-29 19:41:57 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2015-02-25 21:19:06 +01:00
*/
2018-07-08 07:59:58 +02:00
public function postIndex(Request $request)
2015-02-25 21:19:06 +01:00
{
// front page accounts
$frontPageAccounts = [];
if (is_array($request->get('frontPageAccounts')) && count($request->get('frontPageAccounts')) > 0) {
foreach ($request->get('frontPageAccounts') as $id) {
2022-12-29 19:41:57 +01:00
$frontPageAccounts[] = (int)$id;
2015-05-14 12:10:42 +02:00
}
app('preferences')->set('frontPageAccounts', $frontPageAccounts);
2015-02-25 21:19:06 +01:00
}
2022-09-18 16:49:15 +02:00
// extract notifications:
$all = $request->all();
2022-09-24 12:14:27 +02:00
foreach (config('firefly.available_notifications') as $option) {
2022-09-18 16:49:15 +02:00
$key = sprintf('notification_%s', $option);
2022-09-24 12:14:27 +02:00
if (array_key_exists($key, $all)) {
2022-09-18 16:49:15 +02:00
app('preferences')->set($key, true);
}
2022-09-24 12:14:27 +02:00
if (!array_key_exists($key, $all)) {
2022-09-18 16:49:15 +02:00
app('preferences')->set($key, false);
}
}
2015-02-25 21:19:06 +01:00
// view range:
app('preferences')->set('viewRange', $request->get('viewRange'));
2015-02-25 21:19:06 +01:00
// forget session values:
2018-04-22 17:12:22 +02:00
session()->forget('start');
session()->forget('end');
session()->forget('range');
2015-02-25 21:19:06 +01:00
2022-09-24 12:14:27 +02:00
// slack URL:
if (!auth()->user()->hasRole('demo')) {
$url = (string)$request->get('slackUrl');
2023-08-15 13:52:30 +02:00
if (UrlValidator::isValidWebhookURL($url)) {
app('preferences')->set('slack_webhook_url', $url);
}
if ('' === $url) {
app('preferences')->delete('slack_webhook_url');
}
2022-09-24 12:14:27 +02:00
}
// custom fiscal year
2022-12-29 19:41:57 +01:00
$customFiscalYear = 1 === (int)$request->get('customFiscalYear');
$string = strtotime((string)$request->get('fiscalYearStart'));
if (false !== $string) {
$fiscalYearStart = date('m-d', $string);
2021-07-08 20:31:12 +02:00
app('preferences')->set('customFiscalYear', $customFiscalYear);
app('preferences')->set('fiscalYearStart', $fiscalYearStart);
}
2016-04-21 08:59:15 +02:00
// save page size:
app('preferences')->set('listPageSize', 50);
2022-12-29 19:41:57 +01:00
$listPageSize = (int)$request->get('listPageSize');
if ($listPageSize > 0 && $listPageSize < 1337) {
app('preferences')->set('listPageSize', $listPageSize);
2016-04-21 08:59:15 +02:00
}
2015-05-14 09:59:30 +02:00
// language:
2019-08-08 17:08:36 +02:00
/** @var Preference $currentLang */
$currentLang = app('preferences')->get('language', 'en_US');
2020-03-13 15:47:26 +01:00
$lang = $request->get('language');
2018-04-02 15:10:40 +02:00
if (array_key_exists($lang, config('firefly.languages'))) {
app('preferences')->set('language', $lang);
2015-05-14 09:59:30 +02:00
}
2019-08-08 17:08:36 +02:00
if ($currentLang->data !== $lang) {
2021-03-31 06:08:02 +02:00
// this string is untranslated on purpose.
2019-08-08 17:08:36 +02:00
session()->flash('info', 'All translations are supplied by volunteers. There might be errors and mistakes. I appreciate your feedback.');
}
2015-05-14 09:59:30 +02:00
// same for locale:
2020-05-18 21:19:45 +02:00
if (!auth()->user()->hasRole('demo')) {
2021-04-27 06:23:16 +02:00
/** @var Preference $locale */
2020-05-18 21:19:45 +02:00
$locale = $request->get('locale');
app('preferences')->set('locale', $locale);
}
// optional fields for transactions:
2021-04-07 14:17:03 +02:00
$setOptions = $request->get('tj') ?? [];
$optionalTj = [
2021-04-07 07:28:43 +02:00
'interest_date' => array_key_exists('interest_date', $setOptions),
'book_date' => array_key_exists('book_date', $setOptions),
'process_date' => array_key_exists('process_date', $setOptions),
'due_date' => array_key_exists('due_date', $setOptions),
'payment_date' => array_key_exists('payment_date', $setOptions),
'invoice_date' => array_key_exists('invoice_date', $setOptions),
'internal_reference' => array_key_exists('internal_reference', $setOptions),
'notes' => array_key_exists('notes', $setOptions),
'attachments' => array_key_exists('attachments', $setOptions),
2022-01-24 07:50:33 +01:00
'external_url' => array_key_exists('external_url', $setOptions),
2021-04-07 07:28:43 +02:00
'location' => array_key_exists('location', $setOptions),
'links' => array_key_exists('links', $setOptions),
];
app('preferences')->set('transaction_journal_optional_fields', $optionalTj);
2023-04-08 06:55:38 +02:00
// dark mode
$darkMode = $request->get('darkMode') ?? 'browser';
2023-05-29 13:56:55 +02:00
if (in_array($darkMode, config('firefly.available_dark_modes'), true)) {
2023-04-08 06:55:38 +02:00
app('preferences')->set('darkMode', $darkMode);
}
2022-12-29 19:41:57 +01:00
session()->flash('success', (string)trans('firefly.saved_preferences'));
2018-07-08 12:08:53 +02:00
app('preferences')->mark();
2016-03-03 20:45:27 +01:00
return redirect(route('preferences.index'));
2016-03-03 20:45:27 +01:00
}
2015-02-25 21:19:06 +01:00
}