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

112 lines
3.9 KiB
PHP
Raw Normal View History

2017-01-10 11:25:03 -06:00
<?php
/**
* JavascriptController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
2017-01-10 11:25:03 -06:00
namespace FireflyIII\Http\Controllers;
use Amount;
use FireflyIII\Exceptions\FireflyException;
2017-03-03 11:19:25 -06:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency;
2017-03-03 11:19:25 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-02-16 14:01:22 -06:00
use Illuminate\Http\Request;
2017-01-10 11:25:03 -06:00
use Navigation;
use Preferences;
use Session;
/**
* Class JavascriptController
*
* @package FireflyIII\Http\Controllers
*/
class JavascriptController extends Controller
{
2017-03-03 11:19:25 -06:00
/**
2017-04-09 00:56:46 -05:00
* @param AccountRepositoryInterface $repository
* @param CurrencyRepositoryInterface $currencyRepository
2017-03-03 11:19:25 -06:00
*
2017-06-05 04:12:50 -05:00
* @return \Illuminate\Http\Response
2017-03-03 11:19:25 -06:00
*/
public function accounts(AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository)
{
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$preference = Preferences::get('currencyPreference', config('firefly.default_currency', 'EUR'));
$default = $currencyRepository->findByCode($preference->data);
$data = ['accounts' => [],];
/** @var Account $account */
foreach ($accounts as $account) {
$accountId = $account->id;
$currency = intval($account->getMeta('currency_id'));
$currency = $currency === 0 ? $default->id : $currency;
$entry = ['preferredCurrency' => $currency, 'name' => $account->name];
2017-03-03 11:19:25 -06:00
$data['accounts'][$accountId] = $entry;
}
return response()
->view('javascript.accounts', $data, 200)
->header('Content-Type', 'text/javascript');
}
/**
* @param CurrencyRepositoryInterface $repository
*
2017-06-05 04:12:50 -05:00
* @return \Illuminate\Http\Response
*/
public function currencies(CurrencyRepositoryInterface $repository)
{
$currencies = $repository->get();
$data = ['currencies' => [],];
/** @var TransactionCurrency $currency */
foreach ($currencies as $currency) {
2017-06-05 04:12:50 -05:00
$currencyId = $currency->id;
$entry = ['name' => $currency->name, 'code' => $currency->code, 'symbol' => $currency->symbol];
$data['currencies'][$currencyId] = $entry;
}
return response()
->view('javascript.currencies', $data, 200)
->header('Content-Type', 'text/javascript');
}
2017-01-10 11:25:03 -06:00
/**
2017-02-24 22:57:01 -06:00
* @param Request $request
2017-01-10 11:25:03 -06:00
*
2017-02-25 10:39:50 -06:00
* @return \Illuminate\Http\Response
2017-01-10 11:25:03 -06:00
*/
2017-02-16 14:01:22 -06:00
public function variables(Request $request)
2017-01-10 11:25:03 -06:00
{
2017-01-10 11:35:00 -06:00
$localeconv = localeconv();
$accounting = Amount::getJsConfig($localeconv);
$localeconv = localeconv();
$defaultCurrency = Amount::getDefaultCurrency();
$localeconv['frac_digits'] = $defaultCurrency->decimal_places;
$pref = Preferences::get('language', config('firefly.default_language', 'en_US'));
$lang = $pref->data;
$data = [
'currencyCode' => Amount::getCurrencyCode(),
'currencySymbol' => Amount::getCurrencySymbol(),
'accounting' => $accounting,
'localeconv' => $localeconv,
'language' => $lang,
];
2017-02-16 14:01:22 -06:00
$request->session()->keep(['two-factor-secret']);
2017-01-10 11:25:03 -06:00
2017-01-10 11:35:00 -06:00
return response()
->view('javascript.variables', $data, 200)
->header('Content-Type', 'text/javascript');
}
}