Skip to content

Commit

Permalink
Merge branch 'extension/trait-subcategory' of https://github.com/CH3R…
Browse files Browse the repository at this point in the history
…VB/lorekeeper into extension/develop-feature-subcategories

# Conflicts:
#	app/Http/Controllers/Admin/Data/FeatureController.php
#	app/Http/Controllers/WorldController.php
#	app/Models/Feature/Feature.php
#	app/Services/FeatureService.php
#	resources/views/admin/features/create_edit_feature.blade.php
#	resources/views/admin/features/features.blade.php
#	resources/views/world/_feature_entry.blade.php
#	resources/views/world/features.blade.php
#	resources/views/world/index.blade.php
  • Loading branch information
ScuffedNewt committed Oct 3, 2024
2 parents 8c3192e + c972057 commit a7a0413
Show file tree
Hide file tree
Showing 18 changed files with 715 additions and 5 deletions.
135 changes: 132 additions & 3 deletions app/Http/Controllers/Admin/Data/FeatureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Http\Controllers\Admin\Data;

use App\Http\Controllers\Controller;
use App\Models\Feature\Feature;
use App\Models\Feature\FeatureCategory;
use App\Models\Feature\FeatureSubcategory;
use App\Models\Rarity;
use App\Models\Species\Species;
use App\Models\Species\Subtype;
Expand Down Expand Up @@ -152,7 +152,130 @@ public function postSortFeatureCategory(Request $request, FeatureService $servic
}

/**********************************************************************************************
FEATURE SUBCATEGORIES
**********************************************************************************************/

/**
* Shows the feature subcategory index.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getSubcategoryIndex()
{
return view('admin.features.feature_subcategories', [
'subcategories' => FeatureSubcategory::orderBy('sort', 'DESC')->get()
]);
}

/**
* Shows the create feature subcategory page.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getCreateFeatureSubcategory()
{
return view('admin.features.create_edit_feature_subcategory', [
'subcategory' => new FeatureSubcategory
]);
}

/**
* Shows the edit feature subcategory page.
*
* @param int $id
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getEditFeatureSubcategory($id)
{
$subcategory = FeatureSubcategory::find($id);
if(!$subcategory) abort(404);
return view('admin.features.create_edit_feature_subcategory', [
'subcategory' => $subcategory
]);
}

/**
* Creates or edits a feature subcategory.
*
* @param \Illuminate\Http\Request $request
* @param App\Services\FeatureService $service
* @param int|null $id
* @return \Illuminate\Http\RedirectResponse
*/
public function postCreateEditFeatureSubcategory(Request $request, FeatureService $service, $id = null)
{
$id ? $request->validate(FeatureSubcategory::$updateRules) : $request->validate(FeatureSubcategory::$createRules);
$data = $request->only([
'name', 'description', 'image', 'remove_image'
]);
if($id && $service->updateFeatureSubcategory(FeatureSubcategory::find($id), $data, Auth::user())) {
flash('Subcategory updated successfully.')->success();
}
else if (!$id && $subcategory = $service->createFeatureSubcategory($data, Auth::user())) {
flash('Subcategory created successfully.')->success();
return redirect()->to('admin/data/trait-subcategories/edit/'.$subcategory->id);
}
else {
foreach($service->errors()->getMessages()['error'] as $error) flash($error)->error();
}
return redirect()->back();
}

/**
* Gets the feature subcategory deletion modal.
*
* @param int $id
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getDeleteFeatureSubcategory($id)
{
$subcategory = FeatureSubcategory::find($id);
return view('admin.features._delete_feature_subcategory', [
'subcategory' => $subcategory,
]);
}

/**
* Creates or edits a feature subcategory.
*
* @param \Illuminate\Http\Request $request
* @param App\Services\FeatureService $service
* @param int|null $id
* @return \Illuminate\Http\RedirectResponse
*/
public function postDeleteFeatureSubcategory(Request $request, FeatureService $service, $id)
{
if($id && $service->deleteFeatureSubcategory(FeatureSubcategory::find($id))) {
flash('Subcategory deleted successfully.')->success();
}
else {
foreach($service->errors()->getMessages()['error'] as $error) flash($error)->error();
}
return redirect()->to('admin/data/trait-subcategories');
}

/**
* Sorts feature subcategories.
*
* @param \Illuminate\Http\Request $request
* @param App\Services\FeatureService $service
* @return \Illuminate\Http\RedirectResponse
*/
public function postSortFeatureSubcategory(Request $request, FeatureService $service)
{
if($service->sortFeatureSubcategory($request->get('sort'))) {
flash('Subcategory order updated successfully.')->success();
}
else {
foreach($service->errors()->getMessages()['error'] as $error) flash($error)->error();
}
return redirect()->back();
}

/**********************************************************************************************
FEATURES
**********************************************************************************************/
Expand All @@ -164,7 +287,7 @@ public function postSortFeatureCategory(Request $request, FeatureService $servic
*/
public function getFeatureIndex(Request $request) {
$query = Feature::query();
$data = $request->only(['rarity_id', 'feature_category_id', 'species_id', 'subtype_id', 'name', 'sort', 'visibility']);
$data = $request->only(['rarity_id', 'feature_category_id', 'species_id', 'subtype_id', 'name', 'sort', 'visibility', 'feature_subcategory_id']);
if (isset($data['rarity_id']) && $data['rarity_id'] != 'none') {
$query->where('rarity_id', $data['rarity_id']);
}
Expand All @@ -175,6 +298,9 @@ public function getFeatureIndex(Request $request) {
$query->where('feature_category_id', $data['feature_category_id']);
}
}
if (isset($data['feature_subcategory_id']) && $data['feature_subcategory_id'] != 'none') {
$query->where('feature_subcategory_id', $data['feature_subcategory_id']);
}
if (isset($data['species_id']) && $data['species_id'] != 'none') {
if ($data['species_id'] == 'withoutOption') {
$query->whereNull('species_id');
Expand Down Expand Up @@ -240,6 +366,7 @@ public function getFeatureIndex(Request $request) {
'specieses' => ['none' => 'Any Species'] + ['withoutOption' => 'Without Species'] + Species::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'subtypes' => ['none' => 'Any Subtype'] + ['withoutOption' => 'Without Subtype'] + Subtype::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'categories' => ['none' => 'Any Category'] + ['withoutOption' => 'Without Category'] + FeatureCategory::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'subcategories' => ['none' => 'Any Subcategory'] + FeatureSubcategory::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray()
]);
}

Expand All @@ -255,6 +382,7 @@ public function getCreateFeature() {
'specieses' => ['none' => 'No restriction'] + Species::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'subtypes' => ['none' => 'No subtype'] + Subtype::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'categories' => ['none' => 'No category'] + FeatureCategory::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'subcategories' => ['none' => 'No subcategory'] + FeatureSubcategory::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray()
]);
}

Expand All @@ -277,6 +405,7 @@ public function getEditFeature($id) {
'specieses' => ['none' => 'No restriction'] + Species::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'subtypes' => ['none' => 'No subtype'] + Subtype::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'categories' => ['none' => 'No category'] + FeatureCategory::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'subcategories' => ['none' => 'No subcategory'] + FeatureSubcategory::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray()
]);
}

Expand All @@ -291,7 +420,7 @@ public function getEditFeature($id) {
public function postCreateEditFeature(Request $request, FeatureService $service, $id = null) {
$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',
'name', 'species_id', 'subtype_id', 'rarity_id', 'feature_category_id', 'description', 'image', 'remove_image', 'is_visible', 'feature_subcategory_id',
]);
if ($id && $service->updateFeature(Feature::find($id), $data, Auth::user())) {
flash('Trait updated successfully.')->success();
Expand Down
24 changes: 23 additions & 1 deletion app/Http/Controllers/WorldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Currency\Currency;
use App\Models\Feature\Feature;
use App\Models\Feature\FeatureCategory;
use App\Models\Feature\FeatureSubcategory;
use App\Models\Item\Item;
use App\Models\Item\ItemCategory;
use App\Models\Rarity;
Expand All @@ -16,7 +17,6 @@
use App\Models\User\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class WorldController extends Controller {
/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -141,6 +141,22 @@ public function getFeatureCategories(Request $request) {
]);
}

/**
* Shows the trait subcategories page.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getFeatureSubcategories(Request $request)
{
$query = FeatureSubcategory::query();
$name = $request->get('name');
if($name) $query->where('name', 'LIKE', '%'.$name.'%');
return view('world.feature_subcategories', [
'subcategories' => $query->orderBy('sort', 'DESC')->paginate(20)->appends($request->query()),
]);
}

/**
* Shows the traits page.
*
Expand All @@ -159,6 +175,9 @@ public function getFeatures(Request $request) {
$query->where('feature_category_id', $data['feature_category_id']);
}
}
if(isset($data['feature_subcategory_id']) && $data['feature_subcategory_id'] != 'none') {
$query->where('feature_subcategory_id', $data['feature_subcategory_id']);
}
if (isset($data['species_id']) && $data['species_id'] != 'none') {
if ($data['species_id'] == 'withoutOption') {
$query->whereNull('species_id');
Expand Down Expand Up @@ -217,6 +236,7 @@ public function getFeatures(Request $request) {
'specieses' => ['none' => 'Any Species'] + ['withoutOption' => 'Without Species'] + Species::visible(Auth::user() ?? null)->orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'subtypes' => ['none' => 'Any Subtype'] + ['withoutOption' => 'Without Subtype'] + Subtype::visible(Auth::user() ?? null)->orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'categories' => ['none' => 'Any Category'] + ['withoutOption' => 'Without Category'] + FeatureCategory::visible(Auth::user() ?? null)->orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(),
'subcategories' => ['none' => 'Any Subcategory'] + FeatureSubcategory::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray()
]);
}

Expand All @@ -229,6 +249,7 @@ public function getFeatures(Request $request) {
*/
public function getSpeciesFeatures($id) {
$categories = FeatureCategory::orderBy('sort', 'DESC')->get();
$subcategories = FeatureSubcategory::orderBy('sort', 'DESC')->get();
$rarities = Rarity::orderBy('sort', 'ASC')->get();
$species = Species::visible(Auth::user() ?? null)->where('id', $id)->first();
if (!$species) {
Expand All @@ -253,6 +274,7 @@ public function getSpeciesFeatures($id) {
return view('world.species_features', [
'species' => $species,
'categories' => $categories->keyBy('id'),
'subcategories' => $subcategories->keyBy('id'),
'rarities' => $rarities->keyBy('id'),
'features' => $features,
]);
Expand Down
24 changes: 24 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',
'feature_subcategory_id',
];

/**
Expand All @@ -31,6 +32,7 @@ class Feature extends Model {
*/
public static $createRules = [
'feature_category_id' => 'nullable',
'feature_subcategory_id' => 'nullable',
'species_id' => 'nullable',
'subtype_id' => 'nullable',
'rarity_id' => 'required|exists:rarities,id',
Expand All @@ -46,6 +48,7 @@ class Feature extends Model {
*/
public static $updateRules = [
'feature_category_id' => 'nullable',
'feature_subcategory_id' => 'nullable',
'species_id' => 'nullable',
'subtype_id' => 'nullable',
'rarity_id' => 'required|exists:rarities,id',
Expand Down Expand Up @@ -88,6 +91,14 @@ public function category() {
return $this->belongsTo(FeatureCategory::class, 'feature_category_id');
}

/**
* Get the category the feature belongs to.
*/
public function subcategory()
{
return $this->belongsTo('App\Models\Feature\FeatureSubcategory', 'feature_subcategory_id');
}

/**********************************************************************************************
SCOPES
Expand Down Expand Up @@ -199,6 +210,19 @@ public function scopeVisible($query, $user = null) {
return $query->where('is_visible', 1);
}

/**
* Scope a query to sort features in subcategory order.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param bool $reverse
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeSortSubcategory($query)
{
$ids = FeatureSubcategory::orderBy('sort', 'DESC')->pluck('id')->toArray();
return count($ids) ? $query->orderByRaw(DB::raw('FIELD(feature_subcategory_id, '.implode(',', $ids).')')) : $query;
}

/**********************************************************************************************
ACCESSORS
Expand Down
Loading

0 comments on commit a7a0413

Please sign in to comment.