| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Range.php | 
					
						
							| 
									
										
										
										
											2020-01-31 07:32:04 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02: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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2025-10-05 12:59:43 +02:00
										 |  |  | use Closure; | 
					
						
							| 
									
										
										
										
											2016-05-02 20:49:19 +02:00
										 |  |  | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2024-12-30 10:51:34 +01:00
										 |  |  | use FireflyIII\Support\Facades\Amount; | 
					
						
							| 
									
										
										
										
											2025-10-05 12:59:43 +02:00
										 |  |  | use FireflyIII\Support\Facades\Steam; | 
					
						
							| 
									
										
										
										
											2020-04-19 06:11:49 +02:00
										 |  |  | use FireflyIII\Support\Http\Controllers\RequestInformation; | 
					
						
							| 
									
										
										
										
											2016-01-28 21:33:45 +01:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2025-10-05 12:59:43 +02:00
										 |  |  | use Illuminate\Support\Facades\App; | 
					
						
							| 
									
										
										
										
											2024-07-10 11:55:02 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class SessionFilter. | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class Range | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-19 06:11:49 +02:00
										 |  |  |     use RequestInformation; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Handle an incoming request. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-05-27 16:53:48 +02:00
										 |  |  |     public function handle(Request $request, Closure $next) | 
					
						
							| 
									
										
										
										
											2015-02-06 20:43:19 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-11-05 09:40:45 +01:00
										 |  |  |         if (null !== $request->user()) { | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Set the range for the current view. | 
					
						
							| 
									
										
										
										
											2016-10-25 18:53:54 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function setRange(): void | 
					
						
							| 
									
										
										
										
											2016-10-25 18:53:54 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // ignore preference. set the range to be the current month:
 | 
					
						
							|  |  |  |         if (!app('session')->has('start') && !app('session')->has('end')) { | 
					
						
							| 
									
										
										
										
											2024-07-10 11:55:02 +02:00
										 |  |  |             Log::debug('setRange: Session has no start or end.'); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $viewRange = app('preferences')->get('viewRange', '1M')->data; | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |             if (is_array($viewRange)) { | 
					
						
							| 
									
										
										
										
											2023-11-28 04:45:07 +01:00
										 |  |  |                 $viewRange = '1M'; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $today     = today(config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $start     = app('navigation')->updateStartDate((string) $viewRange, $today); | 
					
						
							|  |  |  |             $end       = app('navigation')->updateEndDate((string) $viewRange, $start); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             app('session')->put('start', $start); | 
					
						
							|  |  |  |             app('session')->put('end', $end); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!app('session')->has('first')) { | 
					
						
							| 
									
										
										
										
											2024-07-10 11:55:02 +02:00
										 |  |  |             Log::debug('setRange: Session has no "first".'); | 
					
						
							| 
									
										
										
										
											2024-07-15 06:18:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             /** @var JournalRepositoryInterface $repository */ | 
					
						
							|  |  |  |             $repository = app(JournalRepositoryInterface::class); | 
					
						
							|  |  |  |             $journal    = $repository->firstNull(); | 
					
						
							|  |  |  |             $first      = today(config('app.timezone'))->startOfYear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (null !== $journal) { | 
					
						
							|  |  |  |                 $first = $journal->date ?? $first; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             app('session')->put('first', $first); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-10-25 18:53:54 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-26 17:33:53 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-22 08:10:16 +02:00
										 |  |  |      * Configure the user's view. | 
					
						
							| 
									
										
										
										
											2017-12-26 17:33:53 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-08 12:28:42 +02:00
										 |  |  |     private function configureView(): void | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-04-19 06:11:49 +02:00
										 |  |  |         // get locale preference:
 | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |         $language          = Steam::getLanguage(); | 
					
						
							|  |  |  |         $locale            = Steam::getLocale(); | 
					
						
							| 
									
										
										
										
											2025-05-24 16:39:20 +02:00
										 |  |  |         App::setLocale($language); | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |         Carbon::setLocale(substr($locale, 0, 2)); | 
					
						
							| 
									
										
										
										
											2020-04-19 06:11:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-07 07:31:00 +02:00
										 |  |  |         $localeArray       = Steam::getLocaleArray($locale); | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-19 06:51:40 +02:00
										 |  |  |         setlocale(LC_TIME, $localeArray); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $moneyResult       = setlocale(LC_MONETARY, $localeArray); | 
					
						
							| 
									
										
										
										
											2017-09-14 16:12:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-30 10:51:34 +01:00
										 |  |  |         // send error to view, if it could not set money format
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (false === $moneyResult) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error('Could not set locale. The following array doesnt work: ', $localeArray); | 
					
						
							| 
									
										
										
										
											2021-06-08 19:19:56 +02:00
										 |  |  |             app('view')->share('invalidMonetaryLocale', true); | 
					
						
							| 
									
										
										
										
											2017-09-14 16:12:07 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  |         // save some formats:
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $monthAndDayFormat = (string) trans('config.month_and_day_js', [], $locale); | 
					
						
							|  |  |  |         $dateTimeFormat    = (string) trans('config.date_time_js', [], $locale); | 
					
						
							| 
									
										
										
										
											2025-08-01 13:48:32 +02:00
										 |  |  |         $primaryCurrency   = Amount::getPrimaryCurrency(); | 
					
						
							| 
									
										
										
										
											2017-01-08 16:55:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 06:40:48 +02:00
										 |  |  |         // also format for moment JS:
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $madMomentJS       = (string) trans('config.month_and_day_moment_js', [], $locale); | 
					
						
							| 
									
										
										
										
											2019-08-23 06:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         app('view')->share('madMomentJS', $madMomentJS); | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |         app('view')->share('monthAndDayFormat', $monthAndDayFormat); | 
					
						
							|  |  |  |         app('view')->share('dateTimeFormat', $dateTimeFormat); | 
					
						
							| 
									
										
										
										
											2025-08-01 13:48:32 +02:00
										 |  |  |         app('view')->share('primaryCurrency', $primaryCurrency); | 
					
						
							| 
									
										
										
										
											2016-06-11 06:33:51 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Configure the list length. | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function configureList(): void | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $pref = app('preferences')->get('list-length', config('firefly.list_length', 10))->data; | 
					
						
							|  |  |  |         app('view')->share('listLength', $pref); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // share security message:
 | 
					
						
							|  |  |  |         if ( | 
					
						
							|  |  |  |             app('fireflyconfig')->has('upgrade_security_message') | 
					
						
							|  |  |  |             && app('fireflyconfig')->has('upgrade_security_level') | 
					
						
							|  |  |  |         ) { | 
					
						
							|  |  |  |             app('view')->share('upgrade_security_message', app('fireflyconfig')->get('upgrade_security_message')->data); | 
					
						
							|  |  |  |             app('view')->share('upgrade_security_level', app('fireflyconfig')->get('upgrade_security_level')->data); | 
					
						
							| 
									
										
										
										
											2021-06-08 19:19:56 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-16 10:50:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |