From 8c0fce7cc6b841127e10d9b44624ed0af2d3de52 Mon Sep 17 00:00:00 2001 From: Spitfire Date: Mon, 15 Jul 2024 13:31:47 -0600 Subject: [PATCH] Implemented requested changes --- .../Controllers/Families/MemberController.php | 22 +++++++------------ .../Controllers/Races/MemberController.php | 19 ++++++---------- app/Models/Character.php | 6 ----- app/Models/Family.php | 15 +++++++++++-- app/Renderers/Layouts/Family/Character.php | 4 ++-- app/Renderers/Layouts/Race/Character.php | 10 ++++----- 6 files changed, 34 insertions(+), 42 deletions(-) diff --git a/app/Http/Controllers/Families/MemberController.php b/app/Http/Controllers/Families/MemberController.php index bf70e8d7f5..1f29cba025 100644 --- a/app/Http/Controllers/Families/MemberController.php +++ b/app/Http/Controllers/Families/MemberController.php @@ -24,12 +24,10 @@ public function index(Campaign $campaign, Family $family) $this->campaign($campaign)->authEntityView($family->entity); $options = ['campaign' => $campaign, 'family' => $family]; - //$filters = []; - $relation = 'allCharacterFamilies'; + $relation = 'allMembers'; if (request()->has('family_id')) { $options['family_id'] = $family->id; - //$filters['family_id'] = $options['family_id']; - $relation = 'pivotMembers'; + $relation = 'members'; } Datagrid::layout(\App\Renderers\Layouts\Family\Character::class) ->route('families.members', $options) @@ -37,18 +35,14 @@ public function index(Campaign $campaign, Family $family) $this->rows = $family ->{$relation}() - //->filter($filters) + ->sort(request()->only(['o', 'k']), ['name' => 'asc']) ->with([ - 'family', 'family.entity', - 'character', 'character.entity', 'character.entity.tags', 'character.entity.tags.entity', 'character.entity.image', - 'character.races', 'character.races.entity', - 'character.location', 'character.location.entity', - 'character.characterFamilies' + 'location', 'location.entity', + 'characterFamilies', + 'entity', 'entity.tags', 'entity.tags.entity', 'entity.image' ]) - ->has('character') - ->has('character.entity') - ->leftJoin('characters as c', 'c.id', 'character_family.character_id') - ->paginate(15); + ->has('entity') + ->paginate(); // Ajax Datagrid if (request()->ajax()) { diff --git a/app/Http/Controllers/Races/MemberController.php b/app/Http/Controllers/Races/MemberController.php index 3741711955..9eca492ae8 100644 --- a/app/Http/Controllers/Races/MemberController.php +++ b/app/Http/Controllers/Races/MemberController.php @@ -24,27 +24,22 @@ public function index(Campaign $campaign, Race $race) $this->campaign($campaign)->authEntityView($race->entity); $options = ['campaign' => $campaign, 'race' => $race]; - $relation = 'allCharacterRaces'; + $relation = 'allCharacters'; if (request()->has('race_id')) { $options['race_id'] = (int) request()->get('race_id'); - $relation = 'characterRaces'; + $relation = 'characters'; } Datagrid::layout(\App\Renderers\Layouts\Race\Character::class) ->route('races.characters', $options); - $this->rows = $race ->{$relation}() - ->select('character_race.*') + ->sort(request()->only(['o', 'k']), ['name' => 'asc']) ->with([ - 'race', 'race.entity', - 'character', 'character.entity', 'character.entity.tags', 'character.entity.tags.entity', 'character.entity.image', - 'character.location', 'character.location.entity', - 'character.characterRaces' + 'location', 'location.entity', + 'characterRaces', + 'entity', 'entity.tags', 'entity.image', 'entity.tags.entity' ]) - ->has('character') - ->has('character.entity') - ->leftJoin('characters as c', 'c.id', 'character_race.character_id') - ->sort(request()->only(['o', 'k']), ['c.name' => 'asc']) + ->has('entity') ->paginate(15); return $this->campaign($campaign)->datagridAjax(); diff --git a/app/Models/Character.php b/app/Models/Character.php index 70a9aaa902..629ede3de0 100644 --- a/app/Models/Character.php +++ b/app/Models/Character.php @@ -158,12 +158,6 @@ public function scopePreparedWith(Builder $query): Builder 'families' => function ($sub) { $sub->select('families.id', 'families.name'); }, - 'characterFamilies' => function ($sub) { - $sub->select('*'); //check this one - }, - 'characterFamilies.family' => function ($sub) { - $sub->select('id', 'name'); - }, 'characterFamilies.family.entity' => function ($sub) { $sub->select('id', 'name', 'entity_id', 'type_id'); }, diff --git a/app/Models/Family.php b/app/Models/Family.php index 324f72cd71..96a1080a57 100644 --- a/app/Models/Family.php +++ b/app/Models/Family.php @@ -205,7 +205,12 @@ public function familyTree() */ public function members(): BelongsToMany { - return $this->belongsToMany('App\Models\Character', 'character_family'); + $query = $this->belongsToMany('App\Models\Character', 'character_family'); + if (auth()->guest() || !auth()->user()->isAdmin()) { + $query->wherePivot('is_private', false); + } + + return $query; } public function pivotMembers(): HasMany @@ -241,13 +246,19 @@ public function allMembers() $familyId[] = $descendant->id; }; - return Character::select('characters.*') + $query = Character::select('characters.*') ->distinct('characters.id') ->leftJoin('character_family as cf', function ($join) { $join->on('cf.character_id', '=', 'characters.id'); }) ->has('entity') ->whereIn('cf.family_id', $familyId); + + if (auth()->guest() || !auth()->user()->isAdmin()) { + $query->where('cf.is_private', false); + } + + return $query; } /** diff --git a/app/Renderers/Layouts/Family/Character.php b/app/Renderers/Layouts/Family/Character.php index ba74cbecd2..1b6f972b8d 100644 --- a/app/Renderers/Layouts/Family/Character.php +++ b/app/Renderers/Layouts/Family/Character.php @@ -19,12 +19,12 @@ public function columns(): array 'render' => Standard::IMAGE ], 'character_id' => [ - 'key' => 'character.name', + 'key' => 'name', 'label' => Module::singular(config('entities.ids.character'), 'entities.character'), 'render' => Standard::CHARACTER, ], 'type' => [ - 'key' => 'character.type', + 'key' => 'type', 'label' => 'crud.fields.type', 'render' => function ($model) { return $model->type; diff --git a/app/Renderers/Layouts/Race/Character.php b/app/Renderers/Layouts/Race/Character.php index 451b5b6b14..4783125ed2 100644 --- a/app/Renderers/Layouts/Race/Character.php +++ b/app/Renderers/Layouts/Race/Character.php @@ -16,23 +16,21 @@ public function columns(): array { $columns = [ 'image' => [ - 'render' => Standard::IMAGE, - 'with' => ['target' => 'character'] + 'render' => Standard::IMAGE ], 'character_id' => [ - 'key' => 'character.name', + 'key' => 'name', 'label' => Module::singular(config('entities.ids.character'), 'entities.character'), 'render' => Standard::CHARACTER, ], 'type' => [ - 'key' => 'character.type', + 'key' => 'type', 'label' => 'crud.fields.type', 'render' => function ($model) { - return $model->character->type; + return $model->type; }, ], 'location' => [ - 'with' => 'character', 'label' => Module::singular(config('entities.ids.location'), 'entities.location'), 'render' => Standard::LOCATION, ],