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

155 lines
5.6 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* PreferencesController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://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
2016-05-20 04:02:07 -05:00
use FireflyIII\Models\AccountType;
2016-10-10 00:49:39 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-12-29 02:05:35 -06:00
use Illuminate\Http\Request;
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.
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) {
2018-07-15 02:38:49 -05: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.
*
2016-10-10 00:49:39 -05:00
* @param AccountRepositoryInterface $repository
2015-05-03 05:58:55 -05:00
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
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
{
2018-07-13 08:50:42 -05:00
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$viewRangePref = app('preferences')->get('viewRange', '1M');
2018-07-09 12:24:08 -05:00
/** @noinspection NullPointerExceptionInspection */
2017-12-22 11:32:43 -06:00
$viewRange = $viewRangePref->data;
$frontPageAccounts = app('preferences')->get('frontPageAccounts', []);
$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 11:32:43 -06:00
$fiscalYearStart = date('Y') . '-' . $fiscalYearStartStr;
$tjOptionalFields = app('preferences')->get('transaction_journal_optional_fields', [])->data;
2015-12-24 01:35:08 -06:00
2016-01-27 14:52:21 -06:00
return view(
'preferences.index',
compact(
2017-11-15 03:52:29 -06:00
'language',
'accounts',
'frontPageAccounts',
'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.
*
2018-07-08 00:59:58 -05:00
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2018-07-20 07:34:56 -05:00
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
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 = [];
2018-04-27 23:23:13 -05:00
if (\is_array($request->get('frontPageAccounts'))) {
foreach ($request->get('frontPageAccounts') as $id) {
2018-04-02 08:10:40 -05: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
}
// 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
// custom fiscal year
2018-04-02 08:10:40 -05:00
$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 01:59:15 -05:00
// save page size:
app('preferences')->set('listPageSize', 50);
2018-04-02 08:10:40 -05: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:
$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
}
// 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']),
];
app('preferences')->set('transaction_journal_optional_fields', $optionalTj);
2018-04-22 10:10:11 -05: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
}