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

89 lines
2.4 KiB
PHP
Raw Normal View History

2015-02-06 13:43:19 -06:00
<?php
namespace FireflyIII\Http\Middleware;
2015-03-31 12:21:49 -05:00
use App;
2015-02-06 13:43:19 -06:00
use Carbon\Carbon;
use Closure;
use Illuminate\Contracts\Auth\Guard;
2015-03-29 00:43:20 -05:00
use Illuminate\Http\Request;
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
/**
* 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
2015-05-17 03:30:18 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2015-02-06 13:43:19 -06:00
*
* @return mixed
*/
2015-03-29 00:43:20 -05:00
public function handle(Request $request, Closure $theNext)
2015-02-06 13:43:19 -06:00
{
if ($this->auth->check()) {
2015-02-06 13:43:19 -06:00
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')) {
/** @var \FireflyIII\Models\Preference $viewRange */
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')) {
2015-03-31 12:21:49 -05:00
/** @var \FireflyIII\Repositories\Journal\JournalRepositoryInterface $repository */
$repository = App::make('FireflyIII\Repositories\Journal\JournalRepositoryInterface');
$journal = $repository->first();
2015-03-02 06:19:13 -06:00
if ($journal) {
Session::put('first', $journal->date);
} else {
2015-03-31 13:45:06 -05:00
Session::put('first', Carbon::now()->startOfYear());
2015-03-02 06:19:13 -06:00
}
}
$current = Carbon::now()->formatLocalized('%B %Y');
$next = Carbon::now()->endOfMonth()->addDay()->formatLocalized('%B %Y');
$prev = Carbon::now()->startOfMonth()->subDay()->formatLocalized('%B %Y');
View::share('currentMonthName', $current);
View::share('previousMonthName', $prev);
View::share('nextMonthName', $next);
2015-02-06 13:43:19 -06:00
}
return $theNext($request);
}
2015-03-29 01:14:32 -05:00
}