| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Range.php | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * This file is part of Firefly III. | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Firefly III is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with Firefly III.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01: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; | 
					
						
							| 
									
										
										
										
											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-11-26 13:02:44 +01:00
										 |  |  |      * @param Closure                   $next | 
					
						
							| 
									
										
										
										
											2016-01-09 07:46:11 +01:00
										 |  |  |      * @param  string|null              $guard | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-11-26 13:02:44 +01:00
										 |  |  |     public function handle(Request $request, Closure $next, $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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function configureList() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $pref = Preferences::get('list-length', config('firefly.list_length', 10))->data; | 
					
						
							|  |  |  |         View::share('listLength', $pref); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  |     private function configureView() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-09-18 19:57:21 +02:00
										 |  |  |         $pref = Preferences::get('language', config('firefly.default_language', 'en_US')); | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  |         $lang = $pref->data; | 
					
						
							|  |  |  |         App::setLocale($lang); | 
					
						
							|  |  |  |         Carbon::setLocale(substr($lang, 0, 2)); | 
					
						
							|  |  |  |         $locale = explode(',', trans('config.locale')); | 
					
						
							|  |  |  |         $locale = array_map('trim', $locale); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         setlocale(LC_TIME, $locale); | 
					
						
							| 
									
										
										
										
											2017-09-14 16:12:07 +02:00
										 |  |  |         $moneyResult = setlocale(LC_MONETARY, $locale); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // send error to view if could not set money format
 | 
					
						
							| 
									
										
										
										
											2017-09-14 17:40:02 +02:00
										 |  |  |         if ($moneyResult === false) { | 
					
						
							| 
									
										
										
										
											2017-09-14 16:12:07 +02:00
										 |  |  |             View::share('invalidMonetaryLocale', true); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // save some formats:
 | 
					
						
							|  |  |  |         $monthAndDayFormat = (string)trans('config.month_and_day'); | 
					
						
							|  |  |  |         $dateTimeFormat    = (string)trans('config.date_time'); | 
					
						
							| 
									
										
										
										
											2017-10-05 11:49:06 +02:00
										 |  |  |         $defaultCurrency   = app('amount')->getDefaultCurrency(); | 
					
						
							| 
									
										
										
										
											2017-01-08 16:55:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  |         View::share('monthAndDayFormat', $monthAndDayFormat); | 
					
						
							|  |  |  |         View::share('dateTimeFormat', $dateTimeFormat); | 
					
						
							| 
									
										
										
										
											2016-12-30 11:51:58 +01:00
										 |  |  |         View::share('defaultCurrency', $defaultCurrency); | 
					
						
							| 
									
										
										
										
											2016-06-11 06:33:51 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function setRange() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // ignore preference. set the range to be the current month:
 | 
					
						
							|  |  |  |         if (!Session::has('start') && !Session::has('end')) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $viewRange = Preferences::get('viewRange', '1M')->data; | 
					
						
							|  |  |  |             $start     = new Carbon; | 
					
						
							|  |  |  |             $start     = Navigation::updateStartDate($viewRange, $start); | 
					
						
							|  |  |  |             $end       = Navigation::updateEndDate($viewRange, $start); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Session::put('start', $start); | 
					
						
							|  |  |  |             Session::put('end', $end); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!Session::has('first')) { | 
					
						
							|  |  |  |             /** @var JournalRepositoryInterface $repository */ | 
					
						
							|  |  |  |             $repository = app(JournalRepositoryInterface::class); | 
					
						
							|  |  |  |             $journal    = $repository->first(); | 
					
						
							|  |  |  |             $first      = Carbon::now()->startOfYear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!is_null($journal->id)) { | 
					
						
							|  |  |  |                 $first = $journal->date; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             Session::put('first', $first); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |