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

89 lines
2.3 KiB
PHP
Raw Normal View History

2015-02-06 13:43:19 -06:00
<?php
namespace FireflyIII\Http\Middleware;
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
*
* @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-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 */
$viewRange = Preferences::get('viewRange', '1M');
$start = Session::has('start') ? Session::get('start') : new Carbon;
$start = Navigation::updateStartDate($viewRange->data, $start);
$end = Navigation::updateEndDate($viewRange->data, $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')) {
$journal = $this->auth->user()->transactionjournals()->orderBy('date', 'ASC')->first(['transaction_journals.*']);
if ($journal) {
Session::put('first', $journal->date);
} else {
Session::put('first', Carbon::now());
}
}
2015-02-06 13:43:19 -06:00
// set current / next / prev month.
$current = Carbon::now()->format('F Y');
$next = Carbon::now()->endOfMonth()->addDay()->format('F Y');
$prev = Carbon::now()->startOfMonth()->subDay()->format('F 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
}