Skip to content

Commit

Permalink
fix(currencies): return all user currencies as flat array by default (#…
Browse files Browse the repository at this point in the history
…1190)

* fix: flat array by default & extension compatibility

* refactor: fix PHP styling

---------

Co-authored-by: ScuffedNewt <[email protected]>
  • Loading branch information
ScuffedNewt and ScuffedNewt authored Jan 12, 2025
1 parent 5ca8939 commit dc12faa
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
13 changes: 8 additions & 5 deletions app/Models/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion resources/views/home/bank.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</h1>

<h3>Currencies</h3>
@foreach (Auth::user()->getCurrencies(true) as $category => $currencies)
@foreach (Auth::user()->getCurrencies(true, true) as $category => $currencies)
<div class="card mb-2">
@if ($currencies->first()->category)
<div class="card-header">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/user/_profile_content.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<div class="card-body text-center">
<h5 class="card-title">Bank</h5>
<div class="profile-assets-content">
@foreach ($user->getCurrencies(false, Auth::user() ?? null) as $currency)
@foreach ($user->getCurrencies(false, false, Auth::user() ?? null) as $currency)
<div>{!! $currency->display($currency->quantity) !!}</div>
@endforeach
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/user/bank.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</h1>

<h3>Currencies</h3>
@foreach ($user->getCurrencies(true, Auth::user() ?? null) as $category => $currencies)
@foreach ($user->getCurrencies(true, true, Auth::user() ?? null) as $category => $currencies)
<div class="card mb-2">
@if ($currencies->first()->category)
<div class="card-header">
Expand Down

0 comments on commit dc12faa

Please sign in to comment.