| 
									
										
										
										
											2023-10-22 07:17:53 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2023-10-24 18:32:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-28 17:17:09 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * IndexController.php | 
					
						
							|  |  |  |  * Copyright (c) 2023 james@firefly-iii.org | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program 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 Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-22 07:17:53 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Controllers\TransactionCurrency; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Http\Controllers\Controller; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:35:13 +01:00
										 |  |  | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2025-05-04 17:41:26 +02:00
										 |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2023-10-22 07:17:53 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							|  |  |  | use Illuminate\Contracts\View\Factory; | 
					
						
							|  |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | use Illuminate\Pagination\LengthAwarePaginator; | 
					
						
							|  |  |  | use Illuminate\View\View; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class IndexController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     protected CurrencyRepositoryInterface $repository; | 
					
						
							|  |  |  |     protected UserRepositoryInterface     $userRepository; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * CurrencyController constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 app('view')->share('title', (string) trans('firefly.currencies')); | 
					
						
							| 
									
										
										
										
											2023-10-22 07:17:53 +02:00
										 |  |  |                 app('view')->share('mainTitleIcon', 'fa-usd'); | 
					
						
							|  |  |  |                 $this->repository     = app(CurrencyRepositoryInterface::class); | 
					
						
							|  |  |  |                 $this->userRepository = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show overview of currencies. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Factory|View | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function index(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var User $user */ | 
					
						
							| 
									
										
										
										
											2023-10-28 06:58:33 +02:00
										 |  |  |         $user       = auth()->user(); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $page       = 0 === (int) $request->get('page') ? 1 : (int) $request->get('page'); | 
					
						
							|  |  |  |         $pageSize   = (int) app('preferences')->get('listPageSize', 50)->data; | 
					
						
							| 
									
										
										
										
											2023-10-28 06:58:33 +02:00
										 |  |  |         $collection = $this->repository->getAll(); | 
					
						
							| 
									
										
										
										
											2023-10-22 07:17:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-21 07:12:11 +01:00
										 |  |  |         // order so default and enabled are on top:
 | 
					
						
							| 
									
										
										
										
											2023-10-22 07:17:53 +02:00
										 |  |  |         $collection = $collection->sortBy( | 
					
						
							| 
									
										
										
										
											2023-11-04 14:18:49 +01:00
										 |  |  |             static function (TransactionCurrency $currency) { | 
					
						
							| 
									
										
										
										
											2025-01-19 11:46:08 +01:00
										 |  |  |                 $native  = true === $currency->userGroupNative ? 0 : 1; | 
					
						
							| 
									
										
										
										
											2023-12-16 16:06:23 +01:00
										 |  |  |                 $enabled = true === $currency->userGroupEnabled ? 0 : 1; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-19 11:40:07 +01:00
										 |  |  |                 return sprintf('%s-%s-%s', $native, $enabled, $currency->code); | 
					
						
							| 
									
										
										
										
											2023-10-22 07:17:53 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2024-12-21 07:12:11 +01:00
										 |  |  |         $total      = $collection->count(); | 
					
						
							|  |  |  |         $collection = $collection->slice(($page - 1) * $pageSize, $pageSize); | 
					
						
							| 
									
										
										
										
											2023-10-22 07:17:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $currencies = new LengthAwarePaginator($collection, $total, $pageSize, $page); | 
					
						
							|  |  |  |         $currencies->setPath(route('currencies.index')); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $isOwner    = true; | 
					
						
							| 
									
										
										
										
											2023-10-22 07:17:53 +02:00
										 |  |  |         if (!$this->userRepository->hasRole($user, 'owner')) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $request->session()->flash('info', (string) trans('firefly.ask_site_owner', ['owner' => config('firefly.site_owner')])); | 
					
						
							| 
									
										
										
										
											2023-10-22 07:17:53 +02:00
										 |  |  |             $isOwner = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-24 18:32:24 +02:00
										 |  |  |         return view('currencies.index', compact('currencies', 'isOwner')); | 
					
						
							| 
									
										
										
										
											2023-10-22 07:17:53 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |