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

148 lines
5.0 KiB
PHP
Raw Normal View History

2015-02-06 20:43:19 +01:00
<?php
2022-12-29 19:42:26 +01:00
/**
* 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;
use Carbon\Carbon;
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;
2024-07-10 11:55:02 +02:00
use Illuminate\Support\Facades\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;
2021-03-21 09:15:40 +01:00
2015-02-06 20:43:19 +01:00
/**
* Handle an incoming request.
*
* @return mixed
*/
2023-12-20 19:35:52 +01:00
public function handle(Request $request, \Closure $next)
2015-02-06 20:43:19 +01:00
{
2023-11-05 09:40:45 +01:00
if (null !== $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();
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
/**
2023-06-21 12:34:58 +02:00
* Set the range for the current view.
2016-10-25 18:53:54 +02:00
*/
2023-06-21 12:34:58 +02:00
private function setRange(): void
2016-10-25 18:53:54 +02:00
{
2023-06-21 12:34:58 +02:00
// ignore preference. set the range to be the current month:
if (!app('session')->has('start') && !app('session')->has('end')) {
2024-07-10 11:55:02 +02:00
Log::debug('setRange: Session has no start or end.');
2023-06-21 12:34:58 +02:00
$viewRange = app('preferences')->get('viewRange', '1M')->data;
2023-12-10 06:51:59 +01:00
if (is_array($viewRange)) {
2023-11-28 04:45:07 +01:00
$viewRange = '1M';
}
$today = today(config('app.timezone'));
$start = app('navigation')->updateStartDate((string)$viewRange, $today);
$end = app('navigation')->updateEndDate((string)$viewRange, $start);
2021-03-21 09:15:40 +01:00
2023-06-21 12:34:58 +02:00
app('session')->put('start', $start);
app('session')->put('end', $end);
}
if (!app('session')->has('first')) {
2024-07-10 11:55:02 +02:00
Log::debug('setRange: Session has no "first".');
2023-06-21 12:34:58 +02:00
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$journal = $repository->firstNull();
$first = today(config('app.timezone'))->startOfYear();
if (null !== $journal) {
$first = $journal->date ?? $first;
}
app('session')->put('first', $first);
2021-03-21 09:15:40 +01:00
}
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();
2023-12-20 19:35:52 +01: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
2023-02-11 07:37:05 +01:00
// send error to view, if could not set money format
2017-11-15 12:25:49 +01:00
if (false === $moneyResult) {
2023-10-29 06:32:00 +01:00
app('log')->error('Could not set locale. The following array doesnt work: ', $localeArray);
app('view')->share('invalidMonetaryLocale', true);
2017-09-14 16:12:07 +02:00
}
2016-09-16 10:50:19 +02:00
// save some formats:
2022-12-29 19:42:26 +01:00
$monthAndDayFormat = (string)trans('config.month_and_day_js', [], $locale);
$dateTimeFormat = (string)trans('config.date_time_js', [], $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
/**
2023-06-21 12:34:58 +02:00
* Configure the list length.
2016-09-16 10:50:19 +02:00
*/
2023-06-21 12:34:58 +02:00
private function configureList(): void
2016-09-16 10:50:19 +02:00
{
2023-06-21 12:34:58 +02:00
$pref = app('preferences')->get('list-length', config('firefly.list_length', 10))->data;
app('view')->share('listLength', $pref);
2023-05-29 13:56:55 +02:00
2023-06-21 12:34:58 +02:00
// share security message:
if (
app('fireflyconfig')->has('upgrade_security_message')
&& app('fireflyconfig')->has('upgrade_security_level')
) {
app('view')->share('upgrade_security_message', app('fireflyconfig')->get('upgrade_security_message')->data);
app('view')->share('upgrade_security_level', app('fireflyconfig')->get('upgrade_security_level')->data);
}
2016-09-16 10:50:19 +02:00
}
2015-03-29 08:14:32 +02:00
}