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

145 lines
4.7 KiB
PHP
Raw Normal View History

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