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

145 lines
4.4 KiB
PHP
Raw Normal View History

2015-02-06 13:43:19 -06:00
<?php
/**
* Range.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
2015-02-06 13:43:19 -06:00
declare(strict_types = 1);
2015-02-06 13:43:19 -06:00
namespace FireflyIII\Http\Middleware;
use Carbon\Carbon;
use Closure;
2016-06-11 00:38:30 -05:00
use FireflyIII\Exceptions\FireflyException;
2016-05-02 13:49:19 -05:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2015-02-06 13:43:19 -06:00
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
2016-01-09 00:46:11 -06:00
use Illuminate\Support\Facades\Auth;
2015-03-02 06:19:13 -06:00
use Navigation;
2015-02-06 13:43:19 -06:00
use Preferences;
use Session;
use View;
2015-02-06 13:43:19 -06:00
2016-01-09 00:46:11 -06:00
2015-02-06 13:43:19 -06:00
/**
* Class SessionFilter
*
* @package FireflyIII\Http\Middleware
*/
class Range
{
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
* @param Guard $auth
*
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param Closure $theNext
2016-01-09 00:46:11 -06:00
* @param string|null $guard
2015-02-06 13:43:19 -06:00
*
* @return mixed
* @internal param Closure $next
2015-02-06 13:43:19 -06:00
*/
public function handle(Request $request, Closure $theNext, $guard = null)
2015-02-06 13:43:19 -06:00
{
2016-01-09 00:46:11 -06:00
if (!Auth::guard($guard)->guest()) {
2015-03-02 05:35:14 -06:00
// ignore preference. set the range to be the current month:
if (!Session::has('start') && !Session::has('end')) {
2015-05-21 00:44:44 -05:00
$viewRange = Preferences::get('viewRange', '1M')->data;
2015-03-31 12:21:49 -05:00
$start = new Carbon;
2015-05-21 00:44:44 -05:00
$start = Navigation::updateStartDate($viewRange, $start);
$end = Navigation::updateEndDate($viewRange, $start);
2015-02-06 13:43:19 -06:00
2015-03-02 05:35:14 -06:00
Session::put('start', $start);
Session::put('end', $end);
}
2015-03-02 06:19:13 -06:00
if (!Session::has('first')) {
2016-05-02 13:49:19 -05:00
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
2015-03-31 12:21:49 -05:00
$journal = $repository->first();
$first = Carbon::now()->startOfYear();
2016-04-10 14:31:00 -05:00
if (!is_null($journal->id)) {
$first = $journal->date;
2015-03-02 06:19:13 -06:00
}
Session::put('first', $first);
2015-03-02 06:19:13 -06:00
}
2016-06-10 23:33:51 -05:00
2015-02-06 13:43:19 -06:00
}
2016-06-10 23:33:51 -05:00
$this->datePicker();
2015-02-06 13:43:19 -06:00
return $theNext($request);
}
2016-06-10 23:33:51 -05:00
private function datePicker()
{
2016-06-11 00:38:30 -05:00
$viewRange = Preferences::get('viewRange', '1M')->data;
$start = Session::get('start');
$end = Session::get('end');
$prevStart = Navigation::subtractPeriod($start, $viewRange);// subtract for previous period
2016-06-23 01:01:15 -05:00
$prevEnd = Navigation::endOfPeriod($prevStart, $viewRange);
$nextStart = Navigation::addPeriod($start, $viewRange, 0); // add for previous period
$nextEnd = Navigation::endOfPeriod($nextStart, $viewRange);
2016-06-11 00:38:30 -05:00
$ranges = [];
$ranges['current'] = [$start->format('Y-m-d'), $end->format('Y-m-d')];
$ranges['previous'] = [$prevStart->format('Y-m-d'), $prevEnd->format('Y-m-d')];
$ranges['next'] = [$nextStart->format('Y-m-d'), $nextEnd->format('Y-m-d')];
switch ($viewRange) {
case '1D':
$format = (string)trans('config.month_and_day');
break;
case '3M':
$format = (string)trans('config.quarter_in_year');
break;
case '6M':
$format = (string)trans('config.half_year');
break;
case '1Y':
$format = (string)trans('config.year');
break;
case '1M':
$format = (string)trans('config.month');
break;
default:
throw new FireflyException('The date picker does not yet support "' . $viewRange . '".');
case '1W':
$format = (string)trans('config.week_in_year');
break;
}
$current = $start->formatLocalized($format);
$next = $nextStart->formatLocalized($format);
$prev = $prevStart->formatLocalized($format);
View::share('dpStart', $start->format('Y-m-d'));
View::share('dpEnd', $end->format('Y-m-d'));
View::share('dpCurrent', $current);
View::share('dpPrevious', $prev);
View::share('dpNext', $next);
View::share('dpRanges', $ranges);
2016-06-10 23:33:51 -05:00
}
2015-03-29 01:14:32 -05:00
}