Skip to content

Commit

Permalink
Implemented requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed Jul 15, 2024
1 parent b14299a commit 8c0fce7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 42 deletions.
22 changes: 8 additions & 14 deletions app/Http/Controllers/Families/MemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,25 @@ 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)
;

$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()) {
Expand Down
19 changes: 7 additions & 12 deletions app/Http/Controllers/Races/MemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 0 additions & 6 deletions app/Models/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
},
Expand Down
15 changes: 13 additions & 2 deletions app/Models/Family.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Renderers/Layouts/Family/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions app/Renderers/Layouts/Race/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down

0 comments on commit 8c0fce7

Please sign in to comment.