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

224 lines
8.5 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* 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
2020-03-13 15:47:26 +01:00
use FireflyIII\Models\Account;
2016-05-20 11:02:07 +02:00
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;
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;
use JsonException;
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.
*
2019-07-21 17:15:06 +02:00
* @codeCoverageIgnore
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) {
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.
*
2016-10-10 07:49:39 +02:00
* @param AccountRepositoryInterface $repository
2015-05-03 12:58:55 +02:00
*
* @return Factory|View
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
{
2020-03-13 15:47:26 +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
}
if ('' === $role || 'opt_group_' === $role) {
$role = 'opt_group_defaultAsset';
}
$groupedAccounts[trans(sprintf('firefly.%s', $role))][$account->id] = $account->name;
2020-03-13 15:47:26 +01:00
}
ksort($groupedAccounts);
$accountIds = $accounts->pluck('id')->toArray();
$viewRangePref = app('preferences')->get('viewRange', '1M');
2020-03-13 15:47:26 +01:00
2017-12-22 18:32:43 +01:00
$viewRange = $viewRangePref->data;
$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;
$customFiscalYear = app('preferences')->get('customFiscalYear', 0)->data;
$fiscalYearStartStr = app('preferences')->get('fiscalYearStart', '01-01')->data;
2017-12-22 18:32:43 +01:00
$fiscalYearStart = date('Y') . '-' . $fiscalYearStartStr;
$tjOptionalFields = app('preferences')->get('transaction_journal_optional_fields', [])->data;
2015-12-24 08:35:08 +01: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(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 = [];
}
$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.
if (0 === count($frontPageAccounts->data)) {
$frontPageAccounts = $accountIds;
}
2016-01-27 21:52:21 +01:00
return view(
'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',
'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.
*
2018-07-08 07:59:58 +02:00
* @param Request $request
*
* @return RedirectResponse|Redirector
2018-07-20 14:34:56 +02:00
*
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) {
$frontPageAccounts[] = (int) $id;
2015-05-14 12:10:42 +02:00
}
app('preferences')->set('frontPageAccounts', $frontPageAccounts);
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
// custom fiscal year
$customFiscalYear = 1 === (int) $request->get('customFiscalYear');
$fiscalYearStart = date('m-d', strtotime((string) $request->get('fiscalYearStart')));
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);
$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) {
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')) {
/** @var Preference $currentLocale */
$locale = $request->get('locale');
app('preferences')->set('locale', $locale);
}
// optional fields for transactions:
$setOptions = $request->get('tj');
$optionalTj = [
'interest_date' => isset($setOptions['interest_date']),
'book_date' => isset($setOptions['book_date']),
'process_date' => isset($setOptions['process_date']),
'due_date' => isset($setOptions['due_date']),
'payment_date' => isset($setOptions['payment_date']),
'invoice_date' => isset($setOptions['invoice_date']),
'internal_reference' => isset($setOptions['internal_reference']),
'notes' => isset($setOptions['notes']),
'attachments' => isset($setOptions['attachments']),
2020-07-23 19:40:10 +02:00
'external_uri' => isset($setOptions['external_uri']),
];
app('preferences')->set('transaction_journal_optional_fields', $optionalTj);
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
2020-05-24 11:24:28 +02:00
// telemetry: user language preference + default language.
2020-05-24 12:12:06 +02:00
app('telemetry')->feature('config.firefly.default_language', config('firefly.default_language', 'en_US'));
app('telemetry')->feature('user.preferences.language', app('steam')->getLanguage());
app('telemetry')->feature('user.preferences.locale', app('steam')->getLocale());
2020-05-24 11:24:28 +02:00
return redirect(route('preferences.index'));
2016-03-03 20:45:27 +01:00
}
2015-02-25 21:19:06 +01:00
}