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

167 lines
6.1 KiB
PHP
Raw Normal View History

2017-01-10 18:25:03 +01:00
<?php
2022-12-29 19:41:57 +01:00
2017-01-10 18:25:03 +01:00
/**
* JavascriptController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2017-01-10 18:25:03 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +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
*
* 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/>.
2017-01-10 18:25:03 +01:00
*/
declare(strict_types=1);
2017-01-10 18:25:03 +01:00
namespace FireflyIII\Http\Controllers;
2020-06-17 07:06:45 +02:00
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
2017-03-03 18:19:25 +01:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency;
2017-03-03 18:19:25 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2023-10-28 06:58:33 +02:00
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
2017-02-16 21:01:22 +01:00
use Illuminate\Http\Request;
2018-06-01 22:04:52 +02:00
use Illuminate\Http\Response;
2022-12-29 19:41:57 +01:00
use JsonException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2017-01-10 18:25:03 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class JavascriptController.
2017-01-10 18:25:03 +01:00
*/
class JavascriptController extends Controller
{
use GetConfigurationData;
2017-03-03 18:19:25 +01:00
/**
2018-07-22 08:10:16 +02:00
* Show info about accounts.
*
2023-12-10 06:51:59 +01:00
* @param AccountRepositoryInterface $repository
2017-03-03 18:19:25 +01:00
*
* @return Response
2022-12-29 19:41:57 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2017-03-03 18:19:25 +01:00
*/
2023-11-29 06:30:35 +01:00
public function accounts(AccountRepositoryInterface $repository): Response
2017-03-03 18:19:25 +01:00
{
$accounts = $repository->getAccountsByType(
[AccountType::DEFAULT, AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD]
2018-10-28 19:17:33 +01:00
);
$default = app('amount')->getDefaultCurrency();
$data = ['accounts' => []];
2017-03-03 18:19:25 +01:00
/** @var Account $account */
foreach ($accounts as $account) {
2022-12-29 19:41:57 +01:00
$accountId = $account->id;
$currency = (int)$repository->getMetaValue($account, 'currency_id');
2017-11-15 12:25:49 +01:00
$currency = 0 === $currency ? $default->id : $currency;
$entry = ['preferredCurrency' => $currency, 'name' => $account->name];
2017-03-03 18:19:25 +01:00
$data['accounts'][$accountId] = $entry;
}
return response()
2022-01-29 14:28:16 +01:00
->view('javascript.accounts', $data)
2017-03-03 18:19:25 +01:00
->header('Content-Type', 'text/javascript');
}
/**
2018-07-22 08:10:16 +02:00
* Get info about currencies.
*
2023-06-21 12:34:58 +02:00
* @param CurrencyRepositoryInterface $repository
*
2018-06-01 22:04:52 +02:00
* @return Response
*/
2018-06-01 22:04:52 +02:00
public function currencies(CurrencyRepositoryInterface $repository): Response
{
$currencies = $repository->get();
2017-11-15 12:25:49 +01:00
$data = ['currencies' => []];
/** @var TransactionCurrency $currency */
foreach ($currencies as $currency) {
2017-06-05 11:12:50 +02:00
$currencyId = $currency->id;
$entry = ['name' => $currency->name, 'code' => $currency->code, 'symbol' => $currency->symbol];
$data['currencies'][$currencyId] = $entry;
}
return response()
2022-01-29 14:28:16 +01:00
->view('javascript.currencies', $data)
->header('Content-Type', 'text/javascript');
}
2017-01-10 18:25:03 +01:00
/**
2018-07-22 08:10:16 +02:00
* Show some common variables to be used in scripts.
*
2023-12-10 06:51:59 +01:00
* @param Request $request
* @param AccountRepositoryInterface $repository
2017-01-10 18:25:03 +01:00
*
* @return Response
* @throws FireflyException
2022-12-29 19:41:57 +01:00
* @throws JsonException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2017-01-10 18:25:03 +01:00
*/
2023-11-29 06:30:35 +01:00
public function variables(Request $request, AccountRepositoryInterface $repository): Response
2017-01-10 18:25:03 +01:00
{
2022-12-29 19:41:57 +01:00
$account = $repository->find((int)$request->get('account'));
2020-07-27 07:06:44 +02:00
$currency = app('amount')->getDefaultCurrency();
if (null !== $account) {
2020-07-27 07:06:44 +02:00
$currency = $repository->getAccountCurrency($account) ?? $currency;
}
2020-07-27 20:06:18 +02:00
$locale = app('steam')->getLocale();
2020-07-27 07:08:29 +02:00
$accounting = app('amount')->getJsConfig();
$accounting['frac_digits'] = $currency->decimal_places;
$pref = app('preferences')->get('language', config('firefly.default_language', 'en_US'));
2022-12-29 19:41:57 +01:00
$lang = $pref->data;
$dateRange = $this->getDateRangeConfig();
$uid = substr(hash('sha256', sprintf('%s-%s-%s', (string)config('app.key'), auth()->user()->id, auth()->user()->email)), 0, 12);
2017-08-10 20:41:33 +02:00
$data = [
2020-07-27 20:06:18 +02:00
'currencyCode' => $currency->code,
'currencySymbol' => $currency->symbol,
'accountingLocaleInfo' => $accounting,
'language' => $lang,
'dateRangeTitle' => $dateRange['title'],
'locale' => $locale,
'dateRangeConfig' => $dateRange['configuration'],
'uid' => $uid,
2017-01-10 18:35:00 +01:00
];
2017-02-16 21:01:22 +01:00
$request->session()->keep(['two-factor-secret']);
2017-01-10 18:25:03 +01:00
2017-01-10 18:35:00 +01:00
return response()
2022-01-29 14:28:16 +01:00
->view('javascript.variables', $data)
2017-01-10 18:35:00 +01:00
->header('Content-Type', 'text/javascript');
}
2017-08-10 20:41:33 +02:00
/**
* Bit of a hack but OK.
*
* @return Response
*/
2023-11-29 06:30:35 +01:00
public function variablesV2(): Response
{
/** @var Carbon $start */
2023-02-11 07:36:45 +01:00
$start = clone session('start', today(config('app.timezone'))->startOfMonth());
/** @var Carbon $end */
2023-02-11 07:36:45 +01:00
$end = clone session('end', today(config('app.timezone'))->endOfMonth());
$data = [
'start' => $start->format('Y-m-d'),
'end' => $end->format('Y-m-d'),
];
return response()
->view('v2.javascript.variables', $data)
->header('Content-Type', 'text/javascript');
}
}