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

201 lines
8.1 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;
2017-08-10 13:41:33 -05:00
use Log;
2017-01-10 11:25:03 -06:00
use Preferences;
/**
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
/**
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);
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) {
$accountId = $account->id;
$currency = intval($account->getMeta('currency_id'));
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');
}
/**
* @param CurrencyRepositoryInterface $repository
*
2017-06-05 04:12:50 -05:00
* @return \Illuminate\Http\Response
*/
public function currencies(CurrencyRepositoryInterface $repository)
{
$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
/**
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
*/
public function variables(Request $request, AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository)
2017-01-10 11:25:03 -06:00
{
$account = $repository->find(intval($request->get('account')));
$currencyId = 0;
if (null !== $account) {
$currencyId = intval($account->getMeta('currency_id'));
}
/** @var TransactionCurrency $currency */
2017-11-18 09:30:45 -06:00
$currency = $currencyRepository->find($currencyId);
if (0 === $currencyId) {
$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;
2017-01-10 11:35:00 -06:00
$pref = Preferences::get('language', config('firefly.default_language', 'en_US'));
$lang = $pref->data;
2017-08-10 13:41:33 -05:00
$dateRange = $this->getDateRangeConfig();
$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
/**
* @return array
*/
private function getDateRangeConfig(): array
{
$viewRange = Preferences::get('viewRange', '1M')->data;
$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:
$todayStart = app('navigation')->startOfPeriod($today, $viewRange);
$todayEnd = app('navigation')->endOfPeriod($todayStart, $viewRange);
if ($todayStart->ne($start) || $todayEnd->ne($end)) {
$ranges[ucfirst(strval(trans('firefly.today')))] = [$todayStart, $todayEnd];
}
2017-08-10 13:41:33 -05:00
// everything
$index = strval(trans('firefly.everything'));
$ranges[$index] = [$first, new Carbon];
$return = [
'title' => $title,
'configuration' => [
'apply' => strval(trans('firefly.apply')),
'cancel' => strval(trans('firefly.cancel')),
'from' => strval(trans('firefly.from')),
'to' => strval(trans('firefly.to')),
'customRange' => strval(trans('firefly.customRange')),
'start' => $start->format('Y-m-d'),
'end' => $end->format('Y-m-d'),
'ranges' => $ranges,
],
];
return $return;
}
}