| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * 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 20:43:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | declare(strict_types = 1); | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Middleware; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use Closure; | 
					
						
							| 
									
										
										
										
											2016-05-02 20:49:19 +02:00
										 |  |  | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  | use Illuminate\Contracts\Auth\Guard; | 
					
						
							| 
									
										
										
										
											2016-01-28 21:33:45 +01:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2016-01-09 07:46:11 +01:00
										 |  |  | use Illuminate\Support\Facades\Auth; | 
					
						
							| 
									
										
										
										
											2015-03-02 13:19:13 +01:00
										 |  |  | use Navigation; | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  | use Preferences; | 
					
						
							|  |  |  | use Session; | 
					
						
							| 
									
										
										
										
											2015-03-05 21:13:38 +01:00
										 |  |  | use View; | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 07:46:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01: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 | 
					
						
							| 
									
										
										
										
											2016-01-09 08:20:55 +01:00
										 |  |  |      * @param Closure                   $theNext | 
					
						
							| 
									
										
										
										
											2016-01-09 07:46:11 +01:00
										 |  |  |      * @param  string|null              $guard | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							| 
									
										
										
										
											2016-01-09 08:20:55 +01:00
										 |  |  |      * @internal param Closure $next | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-28 21:33:45 +01:00
										 |  |  |     public function handle(Request $request, Closure $theNext, $guard = null) | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-09 07:46:11 +01:00
										 |  |  |         if (!Auth::guard($guard)->guest()) { | 
					
						
							| 
									
										
										
										
											2015-03-02 12:35:14 +01: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 07:44:44 +02:00
										 |  |  |                 $viewRange = Preferences::get('viewRange', '1M')->data; | 
					
						
							| 
									
										
										
										
											2015-03-31 19:21:49 +02:00
										 |  |  |                 $start     = new Carbon; | 
					
						
							| 
									
										
										
										
											2015-05-21 07:44:44 +02:00
										 |  |  |                 $start     = Navigation::updateStartDate($viewRange, $start); | 
					
						
							|  |  |  |                 $end       = Navigation::updateEndDate($viewRange, $start); | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-02 12:35:14 +01:00
										 |  |  |                 Session::put('start', $start); | 
					
						
							|  |  |  |                 Session::put('end', $end); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-03-02 13:19:13 +01:00
										 |  |  |             if (!Session::has('first')) { | 
					
						
							| 
									
										
										
										
											2016-05-02 20:49:19 +02:00
										 |  |  |                 /** @var JournalRepositoryInterface $repository */ | 
					
						
							|  |  |  |                 $repository = app(JournalRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2015-03-31 19:21:49 +02:00
										 |  |  |                 $journal    = $repository->first(); | 
					
						
							| 
									
										
										
										
											2016-04-10 21:31:00 +02:00
										 |  |  |                 if (!is_null($journal->id)) { | 
					
						
							| 
									
										
										
										
											2015-03-02 13:19:13 +01:00
										 |  |  |                     Session::put('first', $journal->date); | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2015-03-31 20:45:06 +02:00
										 |  |  |                     Session::put('first', Carbon::now()->startOfYear()); | 
					
						
							| 
									
										
										
										
											2015-03-02 13:19:13 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-01-17 15:48:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // check "sum of everything".
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-20 07:07:46 +02:00
										 |  |  |             $current = Carbon::now()->formatLocalized('%B %Y'); | 
					
						
							|  |  |  |             $next    = Carbon::now()->endOfMonth()->addDay()->formatLocalized('%B %Y'); | 
					
						
							|  |  |  |             $prev    = Carbon::now()->startOfMonth()->subDay()->formatLocalized('%B %Y'); | 
					
						
							| 
									
										
										
										
											2015-03-05 21:13:38 +01:00
										 |  |  |             View::share('currentMonthName', $current); | 
					
						
							|  |  |  |             View::share('previousMonthName', $prev); | 
					
						
							|  |  |  |             View::share('nextMonthName', $next); | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $theNext($request); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |