2015-02-06 20:43:19 +01:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* Range.php
|
2020-01-31 07:32:04 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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/>.
|
2016-05-20 12:41:23 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
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;
|
2016-01-28 21:33:45 +01:00
|
|
|
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;
|
2021-03-21 09:15:40 +01:00
|
|
|
|
2015-02-06 20:43:19 +01:00
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2020-03-17 15:02:57 +01:00
|
|
|
* @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
|
|
|
/**
|
2021-03-21 09:15:40 +01:00
|
|
|
* Set the range for the current view.
|
2016-10-25 18:53:54 +02:00
|
|
|
*/
|
2021-03-21 09:15:40 +01:00
|
|
|
private function setRange(): void
|
2016-10-25 18:53:54 +02:00
|
|
|
{
|
2021-03-21 09:15:40 +01:00
|
|
|
// ignore preference. set the range to be the current month:
|
|
|
|
if (!app('session')->has('start') && !app('session')->has('end')) {
|
|
|
|
$viewRange = app('preferences')->get('viewRange', '1M')->data;
|
|
|
|
$today = today(config('app.timezone'));
|
|
|
|
$start = app('navigation')->updateStartDate($viewRange, $today);
|
|
|
|
$end = app('navigation')->updateEndDate($viewRange, $start);
|
|
|
|
|
|
|
|
app('session')->put('start', $start);
|
|
|
|
app('session')->put('end', $end);
|
|
|
|
}
|
|
|
|
if (!app('session')->has('first')) {
|
|
|
|
/** @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);
|
|
|
|
}
|
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();
|
2020-04-19 06:51:40 +02:00
|
|
|
$locale = app('steam')->getLocale();
|
2020-04-19 06:11:49 +02:00
|
|
|
App::setLocale($language);
|
2020-04-19 06:51:40 +02:00
|
|
|
Carbon::setLocale(substr($locale, 0, 2));
|
2020-04-19 06:11:49 +02:00
|
|
|
|
2020-04-19 06:51:40 +02:00
|
|
|
$localeArray = app('steam')->getLocaleArray($locale);
|
2016-09-16 10:50:19 +02:00
|
|
|
|
2020-04-19 06:51:40 +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);
|
2018-07-14 16:08:34 +02:00
|
|
|
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:
|
2021-03-21 09:15:40 +01:00
|
|
|
$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();
|
2017-01-08 16:55:02 +01:00
|
|
|
|
2019-08-23 06:40:48 +02:00
|
|
|
// also format for moment JS:
|
2021-03-21 09:15:40 +01:00
|
|
|
$madMomentJS = (string)trans('config.month_and_day_moment_js', [], $locale);
|
2019-08-23 06:40:48 +02:00
|
|
|
|
|
|
|
app('view')->share('madMomentJS', $madMomentJS);
|
2018-07-14 16:08:34 +02:00
|
|
|
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
|
|
|
/**
|
2021-03-21 09:15:40 +01:00
|
|
|
* Configure the list length.
|
2016-09-16 10:50:19 +02:00
|
|
|
*/
|
2021-03-21 09:15:40 +01:00
|
|
|
private function configureList(): void
|
2016-09-16 10:50:19 +02:00
|
|
|
{
|
2021-03-21 09:15:40 +01:00
|
|
|
$pref = app('preferences')->get('list-length', config('firefly.list_length', 10))->data;
|
|
|
|
app('view')->share('listLength', $pref);
|
2016-09-16 10:50:19 +02:00
|
|
|
}
|
2015-03-29 08:14:32 +02:00
|
|
|
}
|