Expand code.

This commit is contained in:
James Cole 2016-09-16 10:50:19 +02:00
parent 2f8263f53a
commit 63fb435002
2 changed files with 76 additions and 50 deletions

View File

@ -48,36 +48,11 @@ class Controller extends BaseController
View::share('hideBills', false); View::share('hideBills', false);
View::share('hideTags', false); View::share('hideTags', false);
if (Auth::check()) { // save some formats:
$pref = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US')); $this->monthFormat = (string)trans('config.month');
$lang = $pref->data; $this->monthAndDayFormat = (string)trans('config.month_and_day');
$this->dateTimeFormat = (string)trans('config.date_time');
App::setLocale($lang);
Carbon::setLocale(substr($lang, 0, 2));
$locale = explode(',', trans('config.locale'));
$locale = array_map('trim', $locale);
setlocale(LC_TIME, $locale);
setlocale(LC_MONETARY, $locale);
// save some formats:
$this->monthFormat = (string)trans('config.month');
$this->monthAndDayFormat = (string)trans('config.month_and_day');
$this->dateTimeFormat = (string)trans('config.date_time');
// change localeconv to a new array:
$numberFormatter = numfmt_create($lang, NumberFormatter::CURRENCY);
$localeconv = [
'mon_decimal_point' => $numberFormatter->getSymbol($numberFormatter->getAttribute(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL)),
'mon_thousands_sep' => $numberFormatter->getSymbol($numberFormatter->getAttribute(NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL)),
'frac_digits' => $numberFormatter->getAttribute(NumberFormatter::MAX_FRACTION_DIGITS),
];
View::share('monthFormat', $this->monthFormat);
View::share('monthAndDayFormat', $this->monthAndDayFormat);
View::share('dateTimeFormat', $this->dateTimeFormat);
View::share('language', $lang);
View::share('localeconv', $localeconv);
}
} }
/** /**

View File

@ -11,6 +11,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Middleware; namespace FireflyIII\Http\Middleware;
use App;
use Carbon\Carbon; use Carbon\Carbon;
use Closure; use Closure;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
@ -19,6 +20,7 @@ use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Navigation; use Navigation;
use NumberFormatter;
use Preferences; use Preferences;
use Session; use Session;
use View; use View;
@ -62,36 +64,56 @@ class Range
public function handle(Request $request, Closure $theNext, $guard = null) public function handle(Request $request, Closure $theNext, $guard = null)
{ {
if (!Auth::guard($guard)->guest()) { if (!Auth::guard($guard)->guest()) {
// ignore preference. set the range to be the current month:
if (!Session::has('start') && !Session::has('end')) {
$viewRange = Preferences::get('viewRange', '1M')->data; // set start, end and finish:
$start = new Carbon; $this->setRange();
$start = Navigation::updateStartDate($viewRange, $start);
$end = Navigation::updateEndDate($viewRange, $start);
Session::put('start', $start); // get variables for date range:
Session::put('end', $end); $this->datePicker();
}
if (!Session::has('first')) {
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$journal = $repository->first();
$first = Carbon::now()->startOfYear();
if (!is_null($journal->id)) {
$first = $journal->date;
}
Session::put('first', $first);
}
// set view variables.
$this->configureView();
} }
$this->datePicker();
return $theNext($request); return $theNext($request);
} }
private function configureView()
{
$pref = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US'));
$lang = $pref->data;
App::setLocale($lang);
Carbon::setLocale(substr($lang, 0, 2));
$locale = explode(',', trans('config.locale'));
$locale = array_map('trim', $locale);
setlocale(LC_TIME, $locale);
setlocale(LC_MONETARY, $locale);
// save some formats:
$monthFormat = (string)trans('config.month');
$monthAndDayFormat = (string)trans('config.month_and_day');
$dateTimeFormat = (string)trans('config.date_time');
// change localeconv to a new array:
$numberFormatter = numfmt_create($lang, NumberFormatter::CURRENCY);
$localeconv = [
'mon_decimal_point' => $numberFormatter->getSymbol($numberFormatter->getAttribute(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL)),
'mon_thousands_sep' => $numberFormatter->getSymbol($numberFormatter->getAttribute(NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL)),
'frac_digits' => $numberFormatter->getAttribute(NumberFormatter::MAX_FRACTION_DIGITS),
];
View::share('monthFormat', $monthFormat);
View::share('monthAndDayFormat', $monthAndDayFormat);
View::share('dateTimeFormat', $dateTimeFormat);
View::share('language', $lang);
View::share('localeconv', $localeconv);
}
/**
* @throws FireflyException
*/
private function datePicker() private function datePicker()
{ {
$viewRange = Preferences::get('viewRange', '1M')->data; $viewRange = Preferences::get('viewRange', '1M')->data;
@ -141,4 +163,33 @@ class Range
View::share('dpRanges', $ranges); View::share('dpRanges', $ranges);
} }
/**
*
*/
private function setRange()
{
// ignore preference. set the range to be the current month:
if (!Session::has('start') && !Session::has('end')) {
$viewRange = Preferences::get('viewRange', '1M')->data;
$start = new Carbon;
$start = Navigation::updateStartDate($viewRange, $start);
$end = Navigation::updateEndDate($viewRange, $start);
Session::put('start', $start);
Session::put('end', $end);
}
if (!Session::has('first')) {
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$journal = $repository->first();
$first = Carbon::now()->startOfYear();
if (!is_null($journal->id)) {
$first = $journal->date;
}
Session::put('first', $first);
}
}
} }