2016-05-20 08:57:45 +02:00
|
|
|
<?php
|
2016-05-20 12:27:31 +02:00
|
|
|
/**
|
|
|
|
* PreferencesController.php
|
2019-10-02 06:37:26 +02:00
|
|
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
2016-05-20 12:27:31 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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/>.
|
2016-05-20 12:27:31 +02:00
|
|
|
*/
|
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
|
|
|
|
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;
|
2017-12-29 09:05:35 +01:00
|
|
|
use Illuminate\Http\Request;
|
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) {
|
2018-07-15 09:38:49 +02: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.
|
|
|
|
*
|
2016-10-10 07:49:39 +02:00
|
|
|
* @param AccountRepositoryInterface $repository
|
2015-05-03 12:58:55 +02:00
|
|
|
*
|
2018-07-08 12:08:53 +02:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\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
|
|
|
{
|
2018-07-13 15:50:42 +02:00
|
|
|
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
2019-01-26 12:10:53 +01:00
|
|
|
$accountIds = $accounts->pluck('id')->toArray();
|
2018-07-14 16:08:34 +02:00
|
|
|
$viewRangePref = app('preferences')->get('viewRange', '1M');
|
2018-07-09 19:24:08 +02:00
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
2017-12-22 18:32:43 +01:00
|
|
|
$viewRange = $viewRangePref->data;
|
2019-01-26 12:10:53 +01:00
|
|
|
$frontPageAccounts = app('preferences')->get('frontPageAccounts', $accountIds);
|
2018-07-14 16:08:34 +02:00
|
|
|
$language = app('preferences')->get('language', config('firefly.default_language', 'en_US'))->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;
|
2018-07-14 16:08:34 +02:00
|
|
|
$tjOptionalFields = app('preferences')->get('transaction_journal_optional_fields', [])->data;
|
2015-12-24 08:35:08 +01:00
|
|
|
|
2019-01-26 12:10:53 +01:00
|
|
|
// an important fallback is that the frontPageAccount array gets refilled automatically
|
|
|
|
// when it turns up empty.
|
2019-05-30 12:31:19 +02:00
|
|
|
if (0 === count($frontPageAccounts->data)) {
|
2019-01-26 12:10:53 +01:00
|
|
|
$frontPageAccounts = $accountIds;
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:52:21 +01:00
|
|
|
return view(
|
|
|
|
'preferences.index',
|
2016-02-10 06:25:21 +01:00
|
|
|
compact(
|
2017-11-15 10:52:29 +01:00
|
|
|
'language',
|
|
|
|
'accounts',
|
|
|
|
'frontPageAccounts',
|
|
|
|
'tjOptionalFields',
|
|
|
|
'viewRange',
|
|
|
|
'customFiscalYear',
|
2017-12-21 21:18:30 +01:00
|
|
|
'listPageSize',
|
2018-03-09 05:45:22 +01:00
|
|
|
'fiscalYearStart'
|
2016-02-10 06:25:21 +01:00
|
|
|
)
|
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
|
2016-09-04 16:21:51 +02:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\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 = [];
|
2019-06-22 13:09:25 +02:00
|
|
|
if (is_array($request->get('frontPageAccounts')) && count($request->get('frontPageAccounts')) > 0) {
|
2016-09-04 16:21:51 +02:00
|
|
|
foreach ($request->get('frontPageAccounts') as $id) {
|
2018-04-02 15:10:40 +02:00
|
|
|
$frontPageAccounts[] = (int)$id;
|
2015-05-14 12:10:42 +02:00
|
|
|
}
|
2018-07-14 16:08:34 +02:00
|
|
|
app('preferences')->set('frontPageAccounts', $frontPageAccounts);
|
2015-02-25 21:19:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// view range:
|
2018-07-14 16:08:34 +02:00
|
|
|
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
|
|
|
|
2016-01-22 21:09:02 +10:00
|
|
|
// custom fiscal year
|
2018-04-02 15:10:40 +02:00
|
|
|
$customFiscalYear = 1 === (int)$request->get('customFiscalYear');
|
|
|
|
$fiscalYearStart = date('m-d', strtotime((string)$request->get('fiscalYearStart')));
|
2018-07-14 16:08:34 +02:00
|
|
|
app('preferences')->set('customFiscalYear', $customFiscalYear);
|
|
|
|
app('preferences')->set('fiscalYearStart', $fiscalYearStart);
|
2016-01-22 21:09:02 +10:00
|
|
|
|
2016-04-21 08:59:15 +02:00
|
|
|
// save page size:
|
2018-07-14 16:08:34 +02:00
|
|
|
app('preferences')->set('listPageSize', 50);
|
2018-04-02 15:10:40 +02:00
|
|
|
$listPageSize = (int)$request->get('listPageSize');
|
2017-12-21 21:18:30 +01:00
|
|
|
if ($listPageSize > 0 && $listPageSize < 1337) {
|
2018-07-14 16:08:34 +02:00
|
|
|
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');
|
2016-09-04 16:21:51 +02:00
|
|
|
$lang = $request->get('language');
|
2018-04-02 15:10:40 +02:00
|
|
|
if (array_key_exists($lang, config('firefly.languages'))) {
|
2018-07-14 16:08:34 +02:00
|
|
|
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
|
|
|
|
2016-09-04 16:21:51 +02:00
|
|
|
// 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']),
|
2016-09-10 18:36:52 +02:00
|
|
|
'invoice_date' => isset($setOptions['invoice_date']),
|
2016-09-04 16:21:51 +02:00
|
|
|
'internal_reference' => isset($setOptions['internal_reference']),
|
|
|
|
'notes' => isset($setOptions['notes']),
|
|
|
|
'attachments' => isset($setOptions['attachments']),
|
|
|
|
];
|
2018-07-14 16:08:34 +02:00
|
|
|
app('preferences')->set('transaction_journal_optional_fields', $optionalTj);
|
2016-09-04 16:21:51 +02:00
|
|
|
|
2018-04-22 17:10:11 +02: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
|
|
|
|
2016-12-05 21:58:23 +01:00
|
|
|
return redirect(route('preferences.index'));
|
2016-03-03 20:45:27 +01:00
|
|
|
}
|
2015-02-25 21:19:06 +01:00
|
|
|
}
|