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

137 lines
4.4 KiB
PHP
Raw Normal View History

2015-02-06 20:43:19 +01:00
<?php
/**
* Range.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-02-06 20:43:19 +01:00
namespace FireflyIII\Http\Middleware;
2016-09-16 10:50:19 +02:00
use App;
2015-02-06 20:43:19 +01:00
use Carbon\Carbon;
use Closure;
2016-05-02 20:49:19 +02:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2020-04-19 06:11:49 +02:00
use FireflyIII\Support\Http\Controllers\RequestInformation;
use Illuminate\Http\Request;
2020-05-26 06:28:42 +02:00
use Log;
2015-02-06 20:43:19 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class SessionFilter.
2015-02-06 20:43:19 +01:00
*/
class Range
{
2020-04-19 06:11:49 +02:00
use RequestInformation;
2015-02-06 20:43:19 +01:00
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
2015-02-06 20:43:19 +01:00
*
* @return mixed
*/
2018-03-03 17:16:47 +01:00
public function handle(Request $request, Closure $next)
2015-02-06 20:43:19 +01:00
{
2018-03-03 17:16:47 +01:00
if ($request->user()) {
2016-09-16 10:50:19 +02:00
// set start, end and finish:
$this->setRange();
2015-02-06 20:43:19 +01:00
2016-09-16 10:50:19 +02:00
// set view variables.
$this->configureView();
2016-10-25 18:53:54 +02:00
// set more view variables:
$this->configureList();
2018-04-21 20:36:42 +02:00
2015-02-06 20:43:19 +01:00
}
2016-11-26 13:02:44 +01:00
return $next($request);
2015-02-06 20:43:19 +01:00
}
2016-10-25 18:53:54 +02:00
/**
2018-07-22 08:10:16 +02:00
* Configure the list length.
2016-10-25 18:53:54 +02:00
*/
2018-07-08 12:28:42 +02:00
private function configureList(): void
2016-10-25 18:53:54 +02:00
{
$pref = app('preferences')->get('list-length', config('firefly.list_length', 10))->data;
app('view')->share('listLength', $pref);
2016-10-25 18:53:54 +02:00
}
2017-12-26 17:33:53 +01:00
/**
2018-07-22 08:10:16 +02:00
* Configure the user's view.
2017-12-26 17:33:53 +01:00
*/
2018-07-08 12:28:42 +02:00
private function configureView(): void
2016-09-16 10:50:19 +02:00
{
2020-04-19 06:11:49 +02:00
// get locale preference:
$language = app('steam')->getLanguage();
$locale = app('steam')->getLocale();
2020-04-19 06:11:49 +02:00
App::setLocale($language);
Carbon::setLocale(substr($locale, 0, 2));
2020-04-19 06:11:49 +02:00
$localeArray = app('steam')->getLocaleArray($locale);
2016-09-16 10:50:19 +02:00
setlocale(LC_TIME, $localeArray);
$moneyResult = setlocale(LC_MONETARY, $localeArray);
2017-09-14 16:12:07 +02:00
// send error to view if could not set money format
2017-11-15 12:25:49 +01:00
if (false === $moneyResult) {
2020-05-26 06:28:42 +02:00
Log::error('Could not set locale. The following array doesnt work: ', $localeArray);
app('view')->share('invalidMonetaryLocale', true); // @codeCoverageIgnore
2017-09-14 16:12:07 +02:00
}
2016-09-16 10:50:19 +02:00
// save some formats:
$monthAndDayFormat = (string) trans('config.month_and_day', [], $locale);
$dateTimeFormat = (string) trans('config.date_time', [], $locale);
2017-10-05 11:49:06 +02:00
$defaultCurrency = app('amount')->getDefaultCurrency();
2019-08-23 06:40:48 +02:00
// also format for moment JS:
$madMomentJS = (string) trans('config.month_and_day_moment_js', [], $locale);
2019-08-23 06:40:48 +02:00
app('view')->share('madMomentJS', $madMomentJS);
app('view')->share('monthAndDayFormat', $monthAndDayFormat);
app('view')->share('dateTimeFormat', $dateTimeFormat);
app('view')->share('defaultCurrency', $defaultCurrency);
2016-06-11 06:33:51 +02:00
}
2016-09-16 10:50:19 +02:00
/**
2018-07-22 08:10:16 +02:00
* Set the range for the current view.
2016-09-16 10:50:19 +02:00
*/
2018-07-08 12:28:42 +02:00
private function setRange(): void
2016-09-16 10:50:19 +02:00
{
// ignore preference. set the range to be the current month:
2018-07-17 22:21:03 +02:00
if (!app('session')->has('start') && !app('session')->has('end')) {
$viewRange = app('preferences')->get('viewRange', '1M')->data;
2020-07-11 08:16:31 +02:00
$today = today(config('app.timezone'));
$start = app('navigation')->updateStartDate($viewRange, $today);
2018-07-17 22:21:03 +02:00
$end = app('navigation')->updateEndDate($viewRange, $start);
2016-09-16 10:50:19 +02:00
2018-07-17 22:21:03 +02:00
app('session')->put('start', $start);
app('session')->put('end', $end);
2016-09-16 10:50:19 +02:00
}
2018-07-17 22:21:03 +02:00
if (!app('session')->has('first')) {
2016-09-16 10:50:19 +02:00
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
2018-04-28 05:42:47 +02:00
$journal = $repository->firstNull();
2020-07-11 08:16:31 +02:00
$first = today(config('app.timezone'))->startOfYear();
2016-09-16 10:50:19 +02:00
2018-04-28 05:42:47 +02:00
if (null !== $journal) {
$first = $journal->date ?? $first;
2016-09-16 10:50:19 +02:00
}
2018-07-17 22:21:03 +02:00
app('session')->put('first', $first);
2016-09-16 10:50:19 +02:00
}
}
2015-03-29 08:14:32 +02:00
}