Skip to content

Commit

Permalink
Fixed races character datagrid
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed Jul 8, 2024
1 parent fcba7d6 commit 94e1d53
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
7 changes: 2 additions & 5 deletions app/Http/Controllers/Races/MemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ public function index(Campaign $campaign, Race $race)
$this->campaign($campaign)->authEntityView($race->entity);

$options = ['campaign' => $campaign, 'race' => $race];
$filters = [];
$relation = 'characterRaces';
$relation = 'allCharacterRaces';
if (request()->has('race_id')) {
$options['race_id'] = (int) request()->get('race_id');
$filters['race_id'] = $options['race_id'];
$relation = 'characters';
$relation = 'characterRaces';
}
Datagrid::layout(\App\Renderers\Layouts\Race\Character::class)
->route('races.characters', $options);
Expand All @@ -45,7 +43,6 @@ public function index(Campaign $campaign, Race $race)
->has('character.entity')
->leftJoin('characters as c', 'c.id', 'character_race.character_id')
->sort(request()->only(['o', 'k']), ['c.name' => 'asc'])
->filter($filters)
->paginate(15);

return $this->campaign($campaign)->datagridAjax();
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ public function races(): BelongsToMany
->with('entity');
}

public function publicRaces(): BelongsToMany
{
return $this->races()->where('character_race.is_private', false);
}

public function organisationMemberships(): HasMany
{
return $this->hasMany('App\Models\OrganisationMember', 'character_id', 'id');
Expand Down
17 changes: 16 additions & 1 deletion app/Models/CharacterRace.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\Concerns\Privatable;
use App\Models\Concerns\SortableTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
Expand All @@ -32,6 +33,11 @@ class CharacterRace extends Model
'is_private',
];

protected array $sortable = [
'character.name',
'character.type',
];

public function character(): BelongsTo
{
return $this->belongsTo(Character::class);
Expand All @@ -44,6 +50,15 @@ public function race(): BelongsTo

public function getCharacterRacesAttribute()
{
return $this->character->races;
if (auth()->check() && auth()->user()->isAdmin()) {
return $this->character()->with('races')->first()->races;
}

return $this->character->publicRaces()->get();
}

public function getCharacterLocationAttribute()
{
return $this->character->location;
}
}
14 changes: 13 additions & 1 deletion app/Models/Race.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function characterRaces(): HasMany
}

/**
* Get all characters in the location and descendants
* Get all characters in the race and descendants
*/
public function allCharacters()
{
Expand All @@ -206,6 +206,18 @@ public function allCharacters()
->whereIn('cr.race_id', $raceIds);
}

/**
* Get all characters in the race and descendants
*/
public function allCharacterRaces()
{
$raceIds = [$this->id];
foreach ($this->descendants as $descendant) {
$raceIds[] = $descendant->id;
};
return CharacterRace::groupBy('character_id')->distinct('character_id')->whereIn('race_id', $raceIds)->with('character');
}

/**
* Get the entity_type id from the entity_types table
*/
Expand Down
6 changes: 3 additions & 3 deletions app/Renderers/Layouts/Race/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ public function columns(): array
'with' => ['target' => 'character']
],
'character_id' => [
'key' => 'name',
'key' => 'character.name',
'label' => Module::singular(config('entities.ids.character'), 'entities.character'),
'render' => Standard::CHARACTER,
],
'type' => [
'key' => 'type',
'key' => 'character.type',
'label' => 'crud.fields.type',
'render' => function ($model) {
return $model->character->type;
},
],
'location' => [
'key' => 'location.name',
'with' => 'character',
'label' => Module::singular(config('entities.ids.location'), 'entities.location'),
'render' => Standard::LOCATION,
],
Expand Down

0 comments on commit 94e1d53

Please sign in to comment.