Skip to content

Commit

Permalink
feat(currencies): add "is visible" toggle to currencies (#1187)
Browse files Browse the repository at this point in the history
* feat(currency): is_visible function

* refactor: fix PHP styling

* add database migration

* refactor: fix PHP styling

* admin and world page edits

* copy paste error fix

* ...when did this change back

---------

Co-authored-by: SpeedyD <[email protected]>
  • Loading branch information
SpeedyD and SpeedyD authored Jan 10, 2025
1 parent f1b67ec commit 18062a2
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/Data/CurrencyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function postCreateEditCurrency(Request $request, CurrencyService $servic
'name', 'abbreviation', 'description', 'currency_category_id',
'is_displayed', 'allow_user_to_user', 'allow_user_to_character', 'allow_character_to_user',
'icon', 'image', 'remove_icon', 'remove_image',
'conversion_id', 'rate',
'conversion_id', 'rate', 'is_visible',
]);
if ($id && $service->updateCurrency(Currency::find($id), $data, Auth::user())) {
flash('Currency updated successfully.')->success();
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/WorldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function getCurrencies(Request $request) {
}

return view('world.currencies', [
'currencies' => $query->orderBy('name')->orderBy('id')->paginate(20)->appends($request->query()),
'currencies' => $query->visible(Auth::user() ?? null)->orderBy('name')->orderBy('id')->paginate(20)->appends($request->query()),
'categories' => ['withoutOption' => 'Without Category'] + CurrencyCategory::visible(Auth::user() ?? null)->orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
]);
}
Expand Down
18 changes: 17 additions & 1 deletion app/Models/Currency/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Currency extends Model {
'is_user_owned', 'is_character_owned', 'currency_category_id',
'name', 'abbreviation', 'description', 'parsed_description', 'sort_user', 'sort_character',
'is_displayed', 'allow_user_to_user', 'allow_user_to_character', 'allow_character_to_user',
'has_icon', 'has_image', 'hash',
'has_icon', 'has_image', 'hash', 'is_visible',
];

/**
Expand Down Expand Up @@ -117,6 +117,22 @@ public function scopeSortOldest($query) {
return $query->orderBy('id');
}

/**
* Scope a query to show only visible currencies.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param mixed|null $user
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeVisible($query, $user = null) {
if ($user && $user->hasPower('edit_data')) {
return $query;
}

return $query->where('is_visible', 1);
}

/**********************************************************************************************
RELATIONSHIPS
Expand Down
2 changes: 1 addition & 1 deletion app/Models/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public function getCurrencies($showAll = false) {

$owned = UserCurrency::where('user_id', $this->id)->pluck('quantity', 'currency_id')->toArray();

$currencies = Currency::where('is_user_owned', 1);
$currencies = Currency::visible()->where('is_user_owned', 1);
if ($showAll) {
$currencies->where(function ($query) use ($owned) {
$query->where('is_displayed', 1)->orWhereIn('id', array_keys($owned));
Expand Down
4 changes: 4 additions & 0 deletions app/Services/CurrencyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ private function populateData($data, $currency = null) {
$data['allow_character_to_user'] = 0;
}

if (!isset($data['is_visible'])) {
$data['is_visible'] = 0;
}

$data['sort_user'] = $data['sort_character'] = 0;

// Process the checkbox fields
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddIsVisibleToCurrencies extends Migration {
/**
* Run the migrations.
*/
public function up() {
Schema::table('currencies', function (Blueprint $table) {
$table->boolean('is_visible')->default(1);
});
}

/**
* Reverse the migrations.
*/
public function down() {
Schema::table('currencies', function (Blueprint $table) {
$table->dropColumn('is_visible');
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@
</div>
@endif

<div class="form-group">
{!! Form::checkbox('is_visible', 1, $currency->id ? $currency->is_visible : 1, ['class' => 'form-check-input', 'data-toggle' => 'toggle']) !!}
{!! Form::label('is_visible', 'Is Visible', ['class' => 'form-check-label ml-3']) !!} {!! add_help('If turned off, the currency will not be visible in the currencies list or available for selection in search. Permissioned staff will still be able to see them, however.') !!}
</div>

<div class="text-right">
{!! Form::submit($currency->id ? 'Edit' : 'Create', ['class' => 'btn btn-primary']) !!}
</div>
Expand Down
6 changes: 5 additions & 1 deletion resources/views/admin/currencies/currencies.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
<div class="logs-table-row">
<div class="row flex-wrap">
<div class="col-12 col-md-3">
<div class="logs-table-cell">{{ $currency->name }} @if ($currency->abbreviation)
<div class="logs-table-cell">
@if (!$currency->is_visible)
<i class="fas fa-eye-slash mr-1"></i>
@endif
{{ $currency->name }} @if ($currency->abbreviation)
({{ $currency->abbreviation }})
@endif
</div>
Expand Down
6 changes: 5 additions & 1 deletion resources/views/world/_currency_entry.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
@endif
<div class="{{ $currency->has_image ? 'col-md-9' : 'col-12' }}">
<x-admin-edit title="Currency" :object="$currency" />
<h3>{{ $currency->name }} @if ($currency->abbreviation)
<h3>
@if (!$currency->is_visible)
<i class="fas fa-eye-slash mr-1"></i>
@endif
{{ $currency->name }} @if ($currency->abbreviation)
({{ $currency->abbreviation }})
@endif
</h3>
Expand Down

0 comments on commit 18062a2

Please sign in to comment.