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

268 lines
10 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
2022-12-29 12:41:57 -06:00
/**
* PreferencesController.php
2020-01-31 00:32:04 -06: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 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05: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 01:40:00 -05: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 05:07:38 -05:00
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
2015-02-25 14:19:06 -06:00
use FireflyIII\Exceptions\FireflyException;
2020-03-13 09:47:26 -05:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2019-08-08 10:08:36 -05:00
use FireflyIII\Models\Preference;
2016-10-10 00:49:39 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
2017-12-29 02:05:35 -06:00
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;
use JsonException;
2021-04-07 00:28:43 -05:00
use Log;
2022-12-29 12:41:57 -06:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2015-03-10 11:26:31 -05:00
2015-02-25 14:19:06 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class PreferencesController.
2015-02-25 14:19:06 -06:00
*/
2015-03-10 11:26:31 -05:00
class PreferencesController extends Controller
{
2015-02-25 14:19:06 -06:00
/**
2018-07-22 01:10:16 -05:00
* PreferencesController constructor.
*
2019-07-21 10:15:06 -05:00
* @codeCoverageIgnore
2015-02-25 14:19:06 -06:00
*/
public function __construct()
{
2015-04-28 08:26:30 -05:00
parent::__construct();
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
2022-12-29 12:41:57 -06:00
app('view')->share('title', (string)trans('firefly.preferences'));
2017-12-16 12:46:36 -06:00
app('view')->share('mainTitleIcon', 'fa-gear');
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2015-02-25 14:19:06 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Show overview of preferences.
*
2022-12-29 12:41:57 -06:00
* @param AccountRepositoryInterface $repository
2015-05-03 05:58:55 -05:00
*
* @return Factory|View
* @throws FireflyException
2022-12-29 12:41:57 -06:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2015-02-25 14:19:06 -06:00
*/
2016-10-10 00:49:39 -05:00
public function index(AccountRepositoryInterface $repository)
2015-02-25 14:19:06 -06:00
{
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]);
2020-06-30 13:43:53 -05:00
$isDocker = env('IS_DOCKER', false);
2020-07-23 12:40:10 -05:00
2020-03-13 09:47:26 -05: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 09:47:26 -05:00
}
2023-01-02 23:48:53 -06:00
if ('opt_group_' === $role) {
2020-03-13 09:47:26 -05:00
$role = 'opt_group_defaultAsset';
}
$groupedAccounts[trans(sprintf('firefly.%s', $role))][$account->id] = $account->name;
2020-03-13 09:47:26 -05:00
}
ksort($groupedAccounts);
$accountIds = $accounts->pluck('id')->toArray();
2023-02-11 00:37:05 -06:00
$viewRangePref = app('navigation')->getViewRange(false);
2020-03-13 09:47:26 -05:00
2017-12-22 11:32:43 -06:00
$viewRange = $viewRangePref->data;
$frontPageAccounts = app('preferences')->get('frontPageAccounts', $accountIds);
$language = app('steam')->getLanguage();
2020-03-16 00:54:18 -05:00
$languages = config('firefly.languages');
$locale = app('preferences')->get('locale', config('firefly.default_locale', 'equal'))->data;
$listPageSize = app('preferences')->get('listPageSize', 50)->data;
2022-09-24 05:14:27 -05:00
$slackUrl = app('preferences')->get('slack_webhook_url', '')->data;
$customFiscalYear = app('preferences')->get('customFiscalYear', 0)->data;
$fiscalYearStartStr = app('preferences')->get('fiscalYearStart', '01-01')->data;
2022-12-29 12:41:57 -06:00
$fiscalYearStart = date('Y').'-'.$fiscalYearStartStr;
$tjOptionalFields = app('preferences')->get('transaction_journal_optional_fields', [])->data;
2015-12-24 01:35:08 -06:00
2022-09-18 09:49:15 -05: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 00:54:18 -05:00
ksort($languages);
2020-03-16 00:53:10 -05: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 12:41:57 -06: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-03 23:11:05 -05:00
if (0 === count($frontPageAccounts->data)) {
$frontPageAccounts = $accountIds;
}
return view(
2016-01-27 14:52:21 -06:00
'preferences.index',
compact(
2017-11-15 03:52:29 -06:00
'language',
2020-03-13 09:47:26 -05:00
'groupedAccounts',
2020-06-30 13:43:53 -05:00
'isDocker',
2017-11-15 03:52:29 -06:00
'frontPageAccounts',
2020-03-16 00:54:18 -05:00
'languages',
2022-09-18 09:49:15 -05:00
'notifications',
2022-09-24 05:14:27 -05:00
'slackUrl',
'locales',
'locale',
2017-11-15 03:52:29 -06:00
'tjOptionalFields',
'viewRange',
'customFiscalYear',
'listPageSize',
2018-03-08 22:45:22 -06:00
'fiscalYearStart'
)
2016-01-27 14:52:21 -06:00
);
2015-02-25 14:19:06 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Store new preferences.
*
2022-12-29 12:41:57 -06:00
* @param Request $request
*
* @return RedirectResponse|Redirector
* @throws FireflyException
2022-12-29 12:41:57 -06:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2015-02-25 14:19:06 -06:00
*/
2018-07-08 00:59:58 -05:00
public function postIndex(Request $request)
2015-02-25 14:19:06 -06: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 12:41:57 -06:00
$frontPageAccounts[] = (int)$id;
2015-05-14 05:10:42 -05:00
}
app('preferences')->set('frontPageAccounts', $frontPageAccounts);
2015-02-25 14:19:06 -06:00
}
2022-09-18 09:49:15 -05:00
// extract notifications:
$all = $request->all();
2022-09-24 05:14:27 -05:00
foreach (config('firefly.available_notifications') as $option) {
2022-09-18 09:49:15 -05:00
$key = sprintf('notification_%s', $option);
2022-09-24 05:14:27 -05:00
if (array_key_exists($key, $all)) {
2022-09-18 09:49:15 -05:00
app('preferences')->set($key, true);
}
2022-09-24 05:14:27 -05:00
if (!array_key_exists($key, $all)) {
2022-09-18 09:49:15 -05:00
app('preferences')->set($key, false);
}
}
2015-02-25 14:19:06 -06:00
// view range:
app('preferences')->set('viewRange', $request->get('viewRange'));
2015-02-25 14:19:06 -06:00
// forget session values:
2018-04-22 10:12:22 -05:00
session()->forget('start');
session()->forget('end');
session()->forget('range');
2015-02-25 14:19:06 -06:00
2022-09-24 05:14:27 -05:00
// slack URL:
2022-12-29 12:41:57 -06:00
$url = (string)$request->get('slackUrl');
2022-10-30 08:24:19 -05:00
if (str_starts_with($url, 'https://hooks.slack.com/services/')) {
2022-09-24 05:14:27 -05:00
app('preferences')->set('slack_webhook_url', $url);
}
2022-10-30 08:24:19 -05:00
if ('' === $url) {
2022-09-24 05:14:27 -05:00
app('preferences')->delete('slack_webhook_url');
}
// custom fiscal year
2022-12-29 12:41:57 -06:00
$customFiscalYear = 1 === (int)$request->get('customFiscalYear');
$string = strtotime((string)$request->get('fiscalYearStart'));
if (false !== $string) {
$fiscalYearStart = date('m-d', $string);
2021-07-08 13:31:12 -05:00
app('preferences')->set('customFiscalYear', $customFiscalYear);
app('preferences')->set('fiscalYearStart', $fiscalYearStart);
}
2016-04-21 01:59:15 -05:00
// save page size:
app('preferences')->set('listPageSize', 50);
2022-12-29 12:41:57 -06:00
$listPageSize = (int)$request->get('listPageSize');
if ($listPageSize > 0 && $listPageSize < 1337) {
app('preferences')->set('listPageSize', $listPageSize);
2016-04-21 01:59:15 -05:00
}
2015-05-14 02:59:30 -05:00
// language:
2019-08-08 10:08:36 -05:00
/** @var Preference $currentLang */
$currentLang = app('preferences')->get('language', 'en_US');
2020-03-13 09:47:26 -05:00
$lang = $request->get('language');
2018-04-02 08:10:40 -05:00
if (array_key_exists($lang, config('firefly.languages'))) {
app('preferences')->set('language', $lang);
2015-05-14 02:59:30 -05:00
}
2019-08-08 10:08:36 -05:00
if ($currentLang->data !== $lang) {
2021-03-30 23:08:02 -05:00
// this string is untranslated on purpose.
2019-08-08 10:08:36 -05:00
session()->flash('info', 'All translations are supplied by volunteers. There might be errors and mistakes. I appreciate your feedback.');
}
2015-05-14 02:59:30 -05:00
// same for locale:
2020-05-18 14:19:45 -05:00
if (!auth()->user()->hasRole('demo')) {
2021-04-26 23:23:16 -05:00
/** @var Preference $locale */
2020-05-18 14:19:45 -05:00
$locale = $request->get('locale');
app('preferences')->set('locale', $locale);
}
// optional fields for transactions:
2021-04-07 07:17:03 -05:00
$setOptions = $request->get('tj') ?? [];
$optionalTj = [
2021-04-07 00:28:43 -05: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 00:50:33 -06:00
'external_url' => array_key_exists('external_url', $setOptions),
2021-04-07 00:28:43 -05:00
'location' => array_key_exists('location', $setOptions),
'links' => array_key_exists('links', $setOptions),
];
app('preferences')->set('transaction_journal_optional_fields', $optionalTj);
2022-12-29 12:41:57 -06:00
session()->flash('success', (string)trans('firefly.saved_preferences'));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2016-03-03 13:45:27 -06:00
return redirect(route('preferences.index'));
2016-03-03 13:45:27 -06:00
}
2015-02-25 14:19:06 -06:00
}