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

317 lines
13 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;
use Illuminate\View\View;
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(
2023-11-04 14:18:49 +01:00
static function ($request, $next) {
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.
*
* @return Factory|View
2023-12-20 19:35:52 +01:00
*
* @throws FireflyException
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
{
2024-12-14 05:45:54 +01:00
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]);
$isDocker = env('IS_DOCKER', false);
$groupedAccounts = [];
2023-12-20 19:35:52 +01:00
2020-03-13 15:47:26 +01:00
/** @var Account $account */
foreach ($accounts as $account) {
2024-12-14 05:45:54 +01:00
$type = $account->accountType->type;
$role = sprintf('opt_group_%s', $repository->getMetaValue($account, 'account_role'));
2020-03-13 15:47:26 +01:00
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[(string) trans(sprintf('firefly.%s', $role))][$account->id] = $account->name;
2020-03-13 15:47:26 +01:00
}
ksort($groupedAccounts);
2023-11-28 04:45:07 +01:00
/** @var array<int, int> $accountIds */
$accountIds = $accounts->pluck('id')->toArray();
$viewRange = app('navigation')->getViewRange(false);
2024-04-01 20:26:02 +02:00
$frontpageAccountsPref = app('preferences')->get('frontpageAccounts', $accountIds);
$frontpageAccounts = $frontpageAccountsPref->data;
if (!is_array($frontpageAccounts)) {
$frontpageAccounts = $accountIds;
2023-11-28 04:45:07 +01:00
}
2024-12-14 05:45:54 +01:00
$language = app('steam')->getLanguage();
$languages = config('firefly.languages');
$locale = app('preferences')->get('locale', config('firefly.default_locale', 'equal'))->data;
$listPageSize = app('preferences')->get('listPageSize', 50)->data;
$darkMode = app('preferences')->get('darkMode', 'browser')->data;
$customFiscalYear = app('preferences')->get('customFiscalYear', 0)->data;
$fiscalYearStartStr = app('preferences')->get('fiscalYearStart', '01-01')->data;
2023-11-28 04:45:07 +01:00
if (is_array($fiscalYearStartStr)) {
$fiscalYearStartStr = '01-01';
}
2024-12-14 05:45:54 +01:00
$fiscalYearStart = sprintf('%s-%s', date('Y'), (string) $fiscalYearStartStr);
$tjOptionalFields = app('preferences')->get('transaction_journal_optional_fields', [])->data;
$availableDarkModes = config('firefly.available_dark_modes');
// notifications settings
2024-12-14 05:45:54 +01:00
$slackUrl = app('preferences')->getEncrypted('slack_webhook_url', '')->data;
$pushoverAppToken = app('preferences')->getEncrypted('pushover_app_token', '')->data;
$pushoverUserToken = app('preferences')->getEncrypted('pushover_user_token', '')->data;
$ntfyServer = app('preferences')->getEncrypted('ntfy_server', 'https://ntfy.sh')->data;
$ntfyTopic = app('preferences')->getEncrypted('ntfy_topic', '')->data;
$ntfyAuth = app('preferences')->get('ntfy_auth', false)->data;
$ntfyUser = app('preferences')->getEncrypted('ntfy_user', '')->data;
$ntfyPass = app('preferences')->getEncrypted('ntfy_pass', '')->data;
$channels = config('notifications.channels');
$forcedAvailability = [];
2024-12-14 05:45:54 +01:00
// notification preferences
$notifications = [];
2024-12-12 07:09:17 +01:00
foreach (config('notifications.notifications.user') as $key => $info) {
if ($info['enabled']) {
$notifications[$key]
= [
2024-12-14 05:45:54 +01:00
'enabled' => app('preferences')->get(sprintf('notification_%s', $key), true)->data,
'configurable' => $info['configurable'],
];
2024-12-12 07:09:17 +01:00
}
2022-09-18 16:49:15 +02:00
}
2024-12-14 05:45:54 +01:00
// loop all channels to see if they are available.
foreach ($channels as $channel => $info) {
$forcedAvailability[$channel] = true;
}
$forcedAvailability['ntfy'] = '' !== $ntfyTopic;
$forcedAvailability['pushover'] = '' !== $pushoverAppToken && '' !== $pushoverUserToken;
2022-09-18 16:49:15 +02:00
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((string) file_get_contents(resource_path(sprintf('locales/%s/locales.json', $language))), true, 512, JSON_THROW_ON_ERROR);
2023-12-20 19:35:52 +01:00
} catch (\JsonException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error($e->getMessage());
$locales = [];
}
2024-12-14 05:45:54 +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.
2024-04-01 20:26:02 +02:00
if (0 === count($frontpageAccounts)) {
$frontpageAccounts = $accountIds;
}
// for the demo user, the notification settings are automatically emptied.
// this isn't really secure, but it means that the demo site has semi-secret notification settings.
if (auth()->user()->hasRole('demo')) {
2024-12-14 05:45:54 +01:00
$slackUrl = '';
$pushoverAppToken = '';
$pushoverUserToken = '';
$ntfyServer = '';
$ntfyTopic = '';
$ntfyAuth = false;
$ntfyUser = '';
$ntfyPass = '';
}
2024-12-14 05:45:54 +01:00
return view('preferences.index', compact(
'language',
'pushoverAppToken',
'pushoverUserToken',
'ntfyServer',
'ntfyTopic',
'ntfyAuth',
'ntfyUser',
'forcedAvailability',
'ntfyPass',
'groupedAccounts',
'isDocker',
'frontpageAccounts',
'languages',
'darkMode',
'availableDarkModes',
'notifications',
'slackUrl',
'locales',
'locale',
'tjOptionalFields',
'viewRange',
'customFiscalYear',
'listPageSize',
'fiscalYearStart'
));
2015-02-25 21:19:06 +01:00
}
/**
2018-07-22 08:10:16 +02:00
* Store new preferences.
*
2023-12-20 19:35:52 +01:00
* @return Redirector|RedirectResponse
*
* @throws FireflyException
2023-12-22 17:28:42 +01:00
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2023-12-22 20:12:38 +01:00
* @SuppressWarnings(PHPMD.NPathComplexity)
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
2024-04-01 20:26:02 +02:00
$frontpageAccounts = [];
if (is_array($request->get('frontpageAccounts')) && count($request->get('frontpageAccounts')) > 0) {
foreach ($request->get('frontpageAccounts') as $id) {
$frontpageAccounts[] = (int) $id;
2015-05-14 12:10:42 +02:00
}
2024-04-01 20:26:02 +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:
2024-12-14 05:45:54 +01:00
$all = $request->all();
exit('fix the reference to the available notifications.');
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
2024-12-14 05:45:54 +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);
2024-12-14 05:45:54 +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 */
2024-12-14 05:45:54 +01:00
$currentLang = app('preferences')->get('language', 'en_US');
$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')) {
$locale = (string) $request->get('locale');
$locale = '' === $locale ? null : $locale;
2020-05-18 21:19:45 +02:00
app('preferences')->set('locale', $locale);
}
// optional fields for transactions:
2024-12-14 05:45:54 +01: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
2024-12-14 05:45:54 +01:00
$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);
}
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
}