firefly-iii/app/Http/Middleware/Range.php

130 lines
3.8 KiB
PHP
Raw Normal View History

2015-02-06 13:43:19 -06:00
<?php
/**
* Range.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* 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:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-02-06 13:43:19 -06:00
namespace FireflyIII\Http\Middleware;
2016-09-16 03:50:19 -05:00
use App;
2015-02-06 13:43:19 -06:00
use Carbon\Carbon;
use Closure;
2016-05-02 13:49:19 -05:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Http\Request;
2015-02-06 13:43:19 -06:00
use Preferences;
use Session;
use View;
2015-02-06 13:43:19 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class SessionFilter.
2015-02-06 13:43:19 -06:00
*/
class Range
{
/**
* Handle an incoming request.
*
2017-11-15 05:25:49 -06:00
* @param \Illuminate\Http\Request $request
* @param Closure $next
2015-02-06 13:43:19 -06:00
*
* @return mixed
*/
2018-03-03 10:16:47 -06:00
public function handle(Request $request, Closure $next)
2015-02-06 13:43:19 -06:00
{
2018-03-03 10:16:47 -06:00
if ($request->user()) {
2016-09-16 03:50:19 -05:00
// set start, end and finish:
$this->setRange();
2015-02-06 13:43:19 -06:00
2016-09-16 03:50:19 -05:00
// set view variables.
$this->configureView();
2016-10-25 11:53:54 -05:00
// set more view variables:
$this->configureList();
2015-02-06 13:43:19 -06:00
}
2016-11-26 06:02:44 -06:00
return $next($request);
2015-02-06 13:43:19 -06:00
}
2016-10-25 11:53:54 -05:00
/**
*
*/
private function configureList()
{
$pref = Preferences::get('list-length', config('firefly.list_length', 10))->data;
View::share('listLength', $pref);
}
2017-12-26 10:33:53 -06:00
/**
*
*/
2016-09-16 03:50:19 -05:00
private function configureView()
{
2016-09-18 12:57:21 -05:00
$pref = Preferences::get('language', config('firefly.default_language', 'en_US'));
2016-09-16 03:50:19 -05:00
$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);
2017-09-14 09:12:07 -05:00
$moneyResult = setlocale(LC_MONETARY, $locale);
// send error to view if could not set money format
2017-11-15 05:25:49 -06:00
if (false === $moneyResult) {
2017-12-26 10:33:53 -06:00
View::share('invalidMonetaryLocale', true); // @codeCoverageIgnore
2017-09-14 09:12:07 -05:00
}
2016-09-16 03:50:19 -05:00
// save some formats:
$monthAndDayFormat = (string)trans('config.month_and_day');
$dateTimeFormat = (string)trans('config.date_time');
2017-10-05 04:49:06 -05:00
$defaultCurrency = app('amount')->getDefaultCurrency();
2016-09-16 03:50:19 -05:00
View::share('monthAndDayFormat', $monthAndDayFormat);
View::share('dateTimeFormat', $dateTimeFormat);
View::share('defaultCurrency', $defaultCurrency);
2016-06-10 23:33:51 -05:00
}
2016-09-16 03:50:19 -05:00
/**
*
*/
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 = app('navigation')->updateStartDate($viewRange, $start);
$end = app('navigation')->updateEndDate($viewRange, $start);
2016-09-16 03:50:19 -05:00
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();
2017-11-15 05:25:49 -06:00
if (null !== $journal->id) {
2016-09-16 03:50:19 -05:00
$first = $journal->date;
}
Session::put('first', $first);
}
}
2015-03-29 01:14:32 -05:00
}