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

152 lines
5.4 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* PreferencesController.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02: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 14:41:58 +01:00
* along with Firefly III. If not, see <http://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
2016-05-20 11:02:07 +02:00
use FireflyIII\Models\AccountType;
2016-10-10 07:49:39 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-03-24 11:07:38 +01:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2017-12-29 09:05:35 +01:00
use Illuminate\Http\Request;
2015-03-10 17:26:31 +01:00
use Preferences;
use Session;
use View;
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
/**
2016-02-04 07:28:39 +01:00
*
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) {
2017-12-16 19:46:36 +01:00
app('view')->share('title', trans('firefly.preferences'));
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
}
/**
2016-10-10 07:49:39 +02:00
* @param AccountRepositoryInterface $repository
2015-05-03 12:58:55 +02:00
*
2016-05-20 11:02:07 +02:00
* @return 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
{
2017-12-22 18:32:43 +01:00
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$viewRangePref = Preferences::get('viewRange', '1M');
$viewRange = $viewRangePref->data;
$frontPageAccounts = Preferences::get('frontPageAccounts', []);
$language = Preferences::get('language', config('firefly.default_language', 'en_US'))->data;
$listPageSize = Preferences::get('listPageSize', 50)->data;
$customFiscalYear = Preferences::get('customFiscalYear', 0)->data;
$fiscalYearStartStr = Preferences::get('fiscalYearStart', '01-01')->data;
$fiscalYearStart = date('Y') . '-' . $fiscalYearStartStr;
$tjOptionalFields = Preferences::get('transaction_journal_optional_fields', [])->data;
2015-12-24 08:35:08 +01:00
2016-01-27 21:52:21 +01:00
return view(
'preferences.index',
compact(
2017-11-15 10:52:29 +01:00
'language',
'accounts',
'frontPageAccounts',
'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
}
/**
2017-04-09 07:56:46 +02:00
* @param Request $request
* @param UserRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 21:19:06 +01:00
*/
2017-03-24 11:07:38 +01:00
public function postIndex(Request $request, UserRepositoryInterface $repository)
2015-02-25 21:19:06 +01:00
{
// front page accounts
$frontPageAccounts = [];
if (is_array($request->get('frontPageAccounts'))) {
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
}
Preferences::set('frontPageAccounts', $frontPageAccounts);
2015-02-25 21:19:06 +01:00
}
// view range:
Preferences::set('viewRange', $request->get('viewRange'));
2015-02-25 21:19:06 +01:00
// forget session values:
Session::forget('start');
Session::forget('end');
Session::forget('range');
// 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')));
Preferences::set('customFiscalYear', $customFiscalYear);
Preferences::set('fiscalYearStart', $fiscalYearStart);
2016-04-21 08:59:15 +02:00
// save page size:
Preferences::set('listPageSize', 50);
2018-04-02 15:10:40 +02:00
$listPageSize = (int)$request->get('listPageSize');
if ($listPageSize > 0 && $listPageSize < 1337) {
Preferences::set('listPageSize', $listPageSize);
2016-04-21 08:59:15 +02:00
}
2015-05-14 09:59:30 +02:00
// language:
$lang = $request->get('language');
2018-04-02 15:10:40 +02:00
if (array_key_exists($lang, config('firefly.languages'))) {
2015-05-14 09:59:30 +02:00
Preferences::set('language', $lang);
}
// 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']),
];
Preferences::set('transaction_journal_optional_fields', $optionalTj);
2018-04-22 17:10:11 +02:00
session()->flash('success', (string)trans('firefly.saved_preferences'));
2016-03-03 20:45:27 +01:00
Preferences::mark();
return redirect(route('preferences.index'));
2016-03-03 20:45:27 +01:00
}
2015-02-25 21:19:06 +01:00
}