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

139 lines
4.8 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* NewUserController.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/>.
*/
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
2015-06-01 11:13:54 -05:00
use FireflyIII\Http\Requests\NewUserFormRequest;
2020-04-14 10:23:58 -05:00
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-11-03 00:58:39 -05:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2018-08-10 10:05:37 -05:00
use FireflyIII\Support\Http\Controllers\CreateStuff;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
2015-06-01 11:13:54 -05:00
use View;
/**
2017-11-15 05:25:49 -06:00
* Class NewUserController.
2015-06-01 11:13:54 -05:00
*/
class NewUserController extends Controller
{
2018-08-10 10:05:37 -05:00
use CreateStuff;
/** @var AccountRepositoryInterface The account repository */
2018-04-15 01:52:58 -05:00
private $repository;
/**
* NewUserController constructor.
*/
2016-01-08 11:29:47 -06:00
public function __construct()
{
2016-01-08 13:40:48 -06:00
parent::__construct();
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
2018-04-15 01:52:58 -05:00
$this->repository = app(AccountRepositoryInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2016-01-08 11:29:47 -06:00
}
2015-06-01 11:13:54 -05:00
/**
2018-07-22 01:10:16 -05:00
* Form the user gets when he has no data in the system.
*
* @return RedirectResponse|Redirector|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-06-01 11:13:54 -05:00
*/
2018-04-15 01:52:58 -05:00
public function index()
2015-06-01 11:13:54 -05:00
{
app('view')->share('title', (string) trans('firefly.welcome'));
2017-12-16 12:46:36 -06:00
app('view')->share('mainTitleIcon', 'fa-fire');
2015-06-01 11:13:54 -05:00
2016-04-26 14:40:15 -05:00
$types = config('firefly.accountTypesByIdentifier.asset');
2018-04-15 01:52:58 -05:00
$count = $this->repository->count($types);
$languages = [];
2015-06-01 11:13:54 -05:00
if ($count > 0) {
2015-07-06 09:27:21 -05:00
return redirect(route('index'));
2015-06-01 11:13:54 -05:00
}
2018-04-15 01:52:58 -05:00
return view('new-user.index', compact('languages'));
2015-06-01 11:13:54 -05:00
}
/**
2018-07-22 01:10:16 -05:00
* Store his new settings.
*
2017-11-17 22:46:19 -06:00
* @param NewUserFormRequest $request
* @param CurrencyRepositoryInterface $currencyRepository
*
* @return RedirectResponse|Redirector
2015-06-01 11:13:54 -05:00
*/
2018-04-15 01:52:58 -05:00
public function submit(NewUserFormRequest $request, CurrencyRepositoryInterface $currencyRepository)
2015-06-01 11:13:54 -05:00
{
2018-04-15 01:52:58 -05:00
$language = $request->string('language');
if (!array_key_exists($language, config('firefly.languages'))) {
$language = 'en_US';
2015-06-01 11:13:54 -05:00
2018-04-15 01:52:58 -05:00
}
2015-06-01 11:13:54 -05:00
2018-04-15 01:52:58 -05:00
// set language preference:
app('preferences')->set('language', $language);
2018-04-15 01:52:58 -05:00
// Store currency preference from input:
$currency = $currencyRepository->findNull((int) $request->input('amount_currency_id_bank_balance'));
2017-11-03 00:58:39 -05:00
2018-04-15 01:52:58 -05:00
// if is null, set to EUR:
if (null === $currency) {
$currency = $currencyRepository->findByCodeNull('EUR');
2017-11-03 00:58:39 -05:00
}
$currencyRepository->enable($currency);
2017-11-03 00:58:39 -05:00
2018-07-20 07:34:56 -05:00
$this->createAssetAccount($request, $currency); // create normal asset account
$this->createSavingsAccount($request, $currency, $language); // create savings account
$this->createCashWalletAccount($currency, $language); // create cash wallet account
2018-04-15 01:52:58 -05:00
// store currency preference:
app('preferences')->set('currencyPreference', $currency->code);
2020-04-14 10:23:58 -05:00
// store frontpage preferences:
$accounts = $this->repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray();
app('preferences')->set('frontPageAccounts', $accounts);
// mark.
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2018-04-15 01:52:58 -05:00
2018-02-10 01:21:35 -06:00
// set default optional fields:
2018-07-20 07:34:56 -05:00
$visibleFields = ['interest_date' => true, 'book_date' => false, 'process_date' => false, 'due_date' => false, 'payment_date' => false,
'invoice_date' => false, 'internal_reference' => false, 'notes' => true, 'attachments' => true,];
app('preferences')->set('transaction_journal_optional_fields', $visibleFields);
2018-02-10 01:21:35 -06:00
2020-05-24 04:24:28 -05:00
// telemetry: user language preference + default language.
2020-05-24 05:12:06 -05: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 04:24:28 -05:00
session()->flash('success', (string) trans('firefly.stored_new_accounts_new_user'));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2015-06-01 11:13:54 -05:00
2015-07-06 09:27:21 -05:00
return redirect(route('index'));
2015-06-01 11:13:54 -05:00
}
2015-06-05 06:39:24 -05:00
}