Skip to content

Commit

Permalink
feat: extension
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Oct 14, 2024
1 parent 1a9b789 commit 96f3499
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 5 deletions.
3 changes: 3 additions & 0 deletions app/Http/Controllers/Admin/Data/FeatureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public function getFeatureIndex(Request $request) {
public function getCreateFeature() {
return view('admin.features.create_edit_feature', [
'feature' => new Feature,
'features' => Feature::orderBy('name', 'ASC')->pluck('name', 'id')->toArray(),
'rarities' => ['none' => 'Select a Rarity'] + Rarity::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'specieses' => ['none' => 'No restriction'] + Species::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'subtypes' => ['none' => 'No subtype'] + Subtype::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
Expand All @@ -273,6 +274,7 @@ public function getEditFeature($id) {

return view('admin.features.create_edit_feature', [
'feature' => $feature,
'features' => Feature::where('id', '!=', $id)->orderBy('name', 'ASC')->pluck('name', 'id')->toArray(),
'rarities' => ['none' => 'Select a Rarity'] + Rarity::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'specieses' => ['none' => 'No restriction'] + Species::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'subtypes' => ['none' => 'No subtype'] + Subtype::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
Expand All @@ -292,6 +294,7 @@ public function postCreateEditFeature(Request $request, FeatureService $service,
$id ? $request->validate(Feature::$updateRules) : $request->validate(Feature::$createRules);
$data = $request->only([
'name', 'species_id', 'subtype_id', 'rarity_id', 'feature_category_id', 'description', 'image', 'remove_image', 'is_visible',
'parent_id',
]);
if ($id && $service->updateFeature(Feature::find($id), $data, Auth::user())) {
flash('Trait 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 @@ -147,7 +147,7 @@ public function getFeatureCategories(Request $request) {
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getFeatures(Request $request) {
$query = Feature::visible(Auth::user() ?? null)->with('category')->with('rarity')->with('species');
$query = Feature::visible(Auth::user() ?? null)->whereNull('parent_id')->with('category')->with('rarity')->with('species');
$data = $request->only(['rarity_id', 'feature_category_id', 'species_id', 'subtype_id', 'name', 'sort']);
if (isset($data['rarity_id']) && $data['rarity_id'] != 'none') {
$query->where('rarity_id', $data['rarity_id']);
Expand Down
15 changes: 15 additions & 0 deletions app/Models/Feature/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Feature extends Model {
*/
protected $fillable = [
'feature_category_id', 'species_id', 'subtype_id', 'rarity_id', 'name', 'has_image', 'description', 'parsed_description', 'is_visible', 'hash',
'parent_id',
];

/**
Expand Down Expand Up @@ -88,6 +89,20 @@ public function category() {
return $this->belongsTo(FeatureCategory::class, 'feature_category_id');
}

/**
* Get the parent feature.
*/
public function parent() {
return $this->belongsTo(Feature::class, 'parent_id');
}

/**
* Get the child features.
*/
public function children() {
return $this->hasMany(Feature::class, 'parent_id');
}

/**********************************************************************************************
SCOPES
Expand Down
3 changes: 3 additions & 0 deletions app/Services/FeatureService.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ public function updateFeature($feature, $data, $user) {
if (isset($data['subtype_id']) && $data['subtype_id'] == 'none') {
$data['subtype_id'] = null;
}
if (isset($data['parent_id']) && $feature->children->count() > 0) {
throw new \Exception('This feature has children. Please remove them before changing the parent feature.');
}

// More specific validation
if (Feature::where('name', $data['name'])->where('id', '!=', $feature->id)->exists()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
//
Schema::table('features', function (Blueprint $table) {
$table->unsignedInteger('parent_id')->nullable()->default(null);
$table->foreign('parent_id')->references('id')->on('features')->onDelete('set null');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
Schema::table('features', function (Blueprint $table) {
$table->dropForeign(['parent_id']);
$table->dropColumn('parent_id');
});
}
};
18 changes: 14 additions & 4 deletions resources/views/admin/features/create_edit_feature.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@
</div>

<div class="row">
<div class="col-md-4 form-group">
<div class="col-md-6 form-group">
{!! Form::label('Trait Category (Optional)') !!}
{!! Form::select('feature_category_id', $categories, $feature->feature_category_id, ['class' => 'form-control']) !!}
</div>
<div class="col-md-4 form-group">
<div class="col-md-6 form-group">
<div class="alert alert-info">Traits that are variants of other traits don't appear individually on the encyclopedia.</div>
{!! Form::label('Parent Trait (Optional)') !!} {!! add_help('If this trait is a variant of another trait, select the parent trait here.') !!}
{!! Form::select('parent_id', $features, $feature->parent_id, ['class' => 'form-control', 'placeholder' => 'Select a Parent Trait']) !!}
</div>
<div class="col-md-6 form-group">
{!! Form::label('Species Restriction (Optional)') !!}
{!! Form::select('species_id', $specieses, $feature->species_id, ['class' => 'form-control', 'id' => 'species']) !!}
</div>
<div class="col-md-4 form-group" id="subtypes">
<div class="col-md-6 form-group" id="subtypes">
{!! Form::label('Subtype (Optional)') !!} {!! add_help('This is cosmetic and does not limit choice of traits in selections.') !!}
{!! Form::select('subtype_id', $subtypes, $feature->subtype_id, ['class' => 'form-control', 'id' => 'subtype']) !!}
</div>
Expand All @@ -74,7 +79,12 @@
<h3>Preview</h3>
<div class="card mb-3">
<div class="card-body">
@include('world._feature_entry', ['feature' => $feature])
@if ($feature->parent_id)
<div class="alert alert-info">This item is a variant of another item. It will not appear on the encyclopedia.</div>
@include('world._feature_entry', ['feature' => $feature->parent])
@else
@include('world._feature_entry', ['feature' => $feature])
@endif
</div>
</div>
@endif
Expand Down
20 changes: 20 additions & 0 deletions resources/views/world/_feature_entry.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,25 @@
<div class="world-entry-text parsed-text">
{!! $feature->parsed_description !!}
</div>
@if ($feature->children->count())
<h3>Trait Variants</h3>
<div class="row">
@php
// Sort children collection in PHP based on the rarity's sort attribute
$sortedChildren = $feature->children()->with('rarity')->get()->sortBy(function ($child, $key) {
return $child->rarity ? $child->rarity->sort : null;
});
@endphp
@foreach($sortedChildren as $i)
<div class="col-md-12">
<div class="card">
<div class="card-body">
@include('world._feature_variants_entry', ['variant' => $i])
</div>
</div>
</div>
@endforeach
</div>
@endif
</div>
</div>
37 changes: 37 additions & 0 deletions resources/views/world/_feature_variants_entry.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<div class="row world-entry">
@if ($variant->has_image)
<div class="col-md-3 world-entry-image">
<a href="{{ $variant->imageUrl }}" data-lightbox="entry" data-title="{{ $variant->name }}">
<img src="{{ $variant->imageUrl }}" class="world-entry-image" alt="{{ $variant->name }}" />
</a>
</div>
@endif
<div class="{{ $variant->has_image ? 'col-md-9' : 'col-12' }}">
<x-admin-edit title="Trait" :object="$variant" />
<h3>
@if (!$variant->is_visible)
<i class="fas fa-eye-slash mr-1"></i>
@endif
{!! $variant->displayName !!}
<a href="{{ $variant->searchUrl }}" class="world-entry-search text-muted">
<i class="fas fa-search"></i>
</a>
</h3>
@if ($variant->feature_category_id)
<div>
<strong>Category:</strong> {!! $variant->category->displayName !!}
</div>
@endif
@if ($variant->species_id)
<div>
<strong>Species:</strong> {!! $variant->species->displayName !!}
@if ($variant->subtype_id)
({!! $variant->subtype->displayName !!} subtype)
@endif
</div>
@endif
<div class="world-entry-text parsed-text">
{!! $variant->parsed_description !!}
</div>
</div>
</div>

0 comments on commit 96f3499

Please sign in to comment.