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

216 lines
8.7 KiB
PHP
Raw Normal View History

2017-01-10 11:25:03 -06:00
<?php
/**
* JavascriptController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
* 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 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-01-10 11:25:03 -06:00
*/
declare(strict_types=1);
2017-01-10 11:25:03 -06:00
namespace FireflyIII\Http\Controllers;
2017-08-10 13:41:33 -05:00
use Carbon\Carbon;
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;
2018-06-01 15:04:52 -05:00
use Illuminate\Http\Response;
2017-08-10 13:41:33 -05:00
use Log;
2017-01-10 11:25:03 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class JavascriptController.
2017-01-10 11:25:03 -06:00
*/
class JavascriptController extends Controller
{
2017-03-03 11:19:25 -06:00
/**
2018-07-22 01:10:16 -05:00
* Show info about accounts.
*
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
*/
2018-07-08 05:28:42 -05:00
public function accounts(AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository): Response
2017-03-03 11:19:25 -06:00
{
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$preference = app('preferences')->get('currencyPreference', config('firefly.default_currency', 'EUR'));
2018-07-09 12:24:08 -05:00
/** @noinspection NullPointerExceptionInspection */
$default = $currencyRepository->findByCodeNull($preference->data);
2017-03-03 11:19:25 -06:00
2017-11-15 05:25:49 -06:00
$data = ['accounts' => []];
2017-03-03 11:19:25 -06:00
/** @var Account $account */
foreach ($accounts as $account) {
2018-07-09 12:24:08 -05:00
$accountId = $account->id;
$currency = (int)$repository->getMetaValue($account, 'currency_id');
/** @noinspection NullPointerExceptionInspection */
2017-11-15 05:25:49 -06:00
$currency = 0 === $currency ? $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');
}
/**
2018-07-22 01:10:16 -05:00
* Get info about currencies.
*
* @param CurrencyRepositoryInterface $repository
*
2018-06-01 15:04:52 -05:00
* @return Response
*/
2018-06-01 15:04:52 -05:00
public function currencies(CurrencyRepositoryInterface $repository): Response
{
$currencies = $repository->get();
2017-11-15 05:25:49 -06:00
$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
/**
2018-07-22 01:10:16 -05:00
* Show some common variables to be used in scripts.
*
2017-12-17 07:30:53 -06:00
* @param Request $request
* @param AccountRepositoryInterface $repository
* @param CurrencyRepositoryInterface $currencyRepository
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
*/
2018-07-08 05:28:42 -05:00
public function variables(Request $request, AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository): Response
2017-01-10 11:25:03 -06:00
{
2018-04-02 08:10:40 -05:00
$account = $repository->findNull((int)$request->get('account'));
$currencyId = 0;
if (null !== $account) {
2018-04-02 08:10:40 -05:00
$currencyId = (int)$repository->getMetaValue($account, 'currency_id');
}
/** @var TransactionCurrency $currency */
$currency = $currencyRepository->findNull($currencyId);
2018-06-01 15:04:52 -05:00
if (null === $currency) {
/** @var TransactionCurrency $currency */
$currency = app('amount')->getDefaultCurrency();
}
2017-01-10 11:35:00 -06:00
$localeconv = localeconv();
2017-10-05 04:49:06 -05:00
$accounting = app('amount')->getJsConfig($localeconv);
2017-01-10 11:35:00 -06:00
$localeconv = localeconv();
$localeconv['frac_digits'] = $currency->decimal_places;
$pref = app('preferences')->get('language', config('firefly.default_language', 'en_US'));
2018-07-09 12:24:08 -05:00
/** @noinspection NullPointerExceptionInspection */
$lang = $pref->data;
$dateRange = $this->getDateRangeConfig();
2017-08-10 13:41:33 -05:00
$data = [
'currencyCode' => $currency->code,
'currencySymbol' => $currency->symbol,
2017-08-10 13:41:33 -05:00
'accounting' => $accounting,
'localeconv' => $localeconv,
'language' => $lang,
'dateRangeTitle' => $dateRange['title'],
'dateRangeConfig' => $dateRange['configuration'],
2017-01-10 11:35:00 -06:00
];
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');
}
2017-08-10 13:41:33 -05:00
/**
2018-07-22 01:10:16 -05:00
* Get config for date range.
*
2017-08-10 13:41:33 -05:00
* @return array
2018-07-20 07:34:56 -05:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2017-08-10 13:41:33 -05:00
*/
private function getDateRangeConfig(): array
{
$viewRange = app('preferences')->get('viewRange', '1M')->data;
2017-08-10 13:41:33 -05:00
$start = session('start');
$end = session('end');
$first = session('first');
$title = sprintf('%s - %s', $start->formatLocalized($this->monthAndDayFormat), $end->formatLocalized($this->monthAndDayFormat));
2017-12-22 11:32:43 -06:00
$isCustom = true === session('is_custom_range', false);
2017-12-15 05:59:21 -06:00
$today = new Carbon;
2017-08-10 13:41:33 -05:00
$ranges = [
// first range is the current range:
$title => [$start, $end],
];
Log::debug(sprintf('viewRange is %s', $viewRange));
Log::debug(sprintf('isCustom is %s', var_export($isCustom, true)));
2017-08-10 13:41:33 -05:00
// when current range is a custom range, add the current period as the next range.
if ($isCustom) {
Log::debug('Custom is true.');
2017-12-15 05:59:21 -06:00
$index = app('navigation')->periodShow($start, $viewRange);
$customPeriodStart = app('navigation')->startOfPeriod($start, $viewRange);
$customPeriodEnd = app('navigation')->endOfPeriod($customPeriodStart, $viewRange);
2017-08-10 13:41:33 -05:00
$ranges[$index] = [$customPeriodStart, $customPeriodEnd];
}
// then add previous range and next range
2017-12-15 05:59:21 -06:00
$previousDate = app('navigation')->subtractPeriod($start, $viewRange);
$index = app('navigation')->periodShow($previousDate, $viewRange);
$previousStart = app('navigation')->startOfPeriod($previousDate, $viewRange);
$previousEnd = app('navigation')->endOfPeriod($previousStart, $viewRange);
2017-08-10 13:41:33 -05:00
$ranges[$index] = [$previousStart, $previousEnd];
2017-12-15 05:59:21 -06:00
$nextDate = app('navigation')->addPeriod($start, $viewRange, 0);
$index = app('navigation')->periodShow($nextDate, $viewRange);
$nextStart = app('navigation')->startOfPeriod($nextDate, $viewRange);
$nextEnd = app('navigation')->endOfPeriod($nextStart, $viewRange);
2017-08-10 13:41:33 -05:00
$ranges[$index] = [$nextStart, $nextEnd];
2017-12-15 05:59:21 -06:00
// today:
2018-07-09 12:24:08 -05:00
/** @var Carbon $todayStart */
2017-12-15 05:59:21 -06:00
$todayStart = app('navigation')->startOfPeriod($today, $viewRange);
2018-07-09 12:24:08 -05:00
/** @var Carbon $todayEnd */
2018-07-13 08:50:42 -05:00
$todayEnd = app('navigation')->endOfPeriod($todayStart, $viewRange);
2017-12-15 05:59:21 -06:00
if ($todayStart->ne($start) || $todayEnd->ne($end)) {
2018-04-02 08:10:40 -05:00
$ranges[ucfirst((string)trans('firefly.today'))] = [$todayStart, $todayEnd];
2017-12-15 05:59:21 -06:00
}
2017-08-10 13:41:33 -05:00
// everything
2018-04-02 08:10:40 -05:00
$index = (string)trans('firefly.everything');
2017-08-10 13:41:33 -05:00
$ranges[$index] = [$first, new Carbon];
$return = [
'title' => $title,
'configuration' => [
2018-04-02 08:10:40 -05:00
'apply' => (string)trans('firefly.apply'),
'cancel' => (string)trans('firefly.cancel'),
'from' => (string)trans('firefly.from'),
'to' => (string)trans('firefly.to'),
'customRange' => (string)trans('firefly.customRange'),
2017-08-10 13:41:33 -05:00
'start' => $start->format('Y-m-d'),
'end' => $end->format('Y-m-d'),
'ranges' => $ranges,
],
];
return $return;
}
}