From dc12faacba3a37b5f68f00a0f07742364875b819 Mon Sep 17 00:00:00 2001 From: Newt Date: Sun, 12 Jan 2025 13:35:31 +0000 Subject: [PATCH] fix(currencies): return all user currencies as flat array by default (#1190) * fix: flat array by default & extension compatibility * refactor: fix PHP styling --------- Co-authored-by: ScuffedNewt --- app/Models/User/User.php | 13 ++++++++----- resources/views/home/bank.blade.php | 2 +- resources/views/user/_profile_content.blade.php | 2 +- resources/views/user/bank.blade.php | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/Models/User/User.php b/app/Models/User/User.php index 3f32d8ef6..b617f43ba 100644 --- a/app/Models/User/User.php +++ b/app/Models/User/User.php @@ -486,10 +486,11 @@ public function canEditRank($rank) { * * @param bool $showAll * @param mixed|null $user + * @param mixed $showCategories * * @return \Illuminate\Support\Collection */ - public function getCurrencies($showAll = false, $user = null) { + public function getCurrencies($showAll = false, $showCategories = false, $user = null) { // Get a list of currencies that need to be displayed // On profile: only ones marked is_displayed // In bank: ones marked is_displayed + the ones the user has @@ -507,10 +508,12 @@ public function getCurrencies($showAll = false, $user = null) { $query->where('is_displayed', 1)->orWhereIn('id', array_keys($owned)); }); - $categories = CurrencyCategory::orderBy('sort', 'DESC')->get(); + if ($showCategories) { + $categories = CurrencyCategory::visible()->orderBy('sort', 'DESC')->get(); - if ($categories->count()) { - $currencies->orderByRaw('FIELD(currency_category_id,'.implode(',', $categories->pluck('id')->toArray()).')'); + if ($categories->count()) { + $currencies->orderByRaw('FIELD(currency_category_id,'.implode(',', $categories->pluck('id')->toArray()).')'); + } } } else { $currencies = $currencies->where('is_displayed', 1); @@ -522,7 +525,7 @@ public function getCurrencies($showAll = false, $user = null) { $currency->quantity = $owned[$currency->id] ?? 0; } - if ($showAll) { + if ($showAll && $showCategories) { $currencies = $currencies->groupBy(function ($currency) use ($categories) { if (!$currency->category) { return 'Miscellaneous'; diff --git a/resources/views/home/bank.blade.php b/resources/views/home/bank.blade.php index 99d404997..6060d313a 100644 --- a/resources/views/home/bank.blade.php +++ b/resources/views/home/bank.blade.php @@ -12,7 +12,7 @@

Currencies

- @foreach (Auth::user()->getCurrencies(true) as $category => $currencies) + @foreach (Auth::user()->getCurrencies(true, true) as $category => $currencies)
@if ($currencies->first()->category)
diff --git a/resources/views/user/_profile_content.blade.php b/resources/views/user/_profile_content.blade.php index df96be27c..e32221bfa 100644 --- a/resources/views/user/_profile_content.blade.php +++ b/resources/views/user/_profile_content.blade.php @@ -83,7 +83,7 @@
Bank
- @foreach ($user->getCurrencies(false, Auth::user() ?? null) as $currency) + @foreach ($user->getCurrencies(false, false, Auth::user() ?? null) as $currency)
{!! $currency->display($currency->quantity) !!}
@endforeach
diff --git a/resources/views/user/bank.blade.php b/resources/views/user/bank.blade.php index 64e09d4e8..5593b5cf5 100644 --- a/resources/views/user/bank.blade.php +++ b/resources/views/user/bank.blade.php @@ -12,7 +12,7 @@

Currencies

- @foreach ($user->getCurrencies(true, Auth::user() ?? null) as $category => $currencies) + @foreach ($user->getCurrencies(true, true, Auth::user() ?? null) as $category => $currencies)
@if ($currencies->first()->category)