Skip to content

Commit

Permalink
Make Events a separate entity in management app (#3024)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Jan 9, 2025
1 parent face8a7 commit f4081ce
Show file tree
Hide file tree
Showing 27 changed files with 1,273 additions and 85 deletions.
1 change: 1 addition & 0 deletions app/Filament/Enums/ImageUploadType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ enum ImageUploadType
{
case News;
case HubBadge;
case GameBadge;
}
37 changes: 1 addition & 36 deletions app/Filament/Resources/AchievementResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public static function infolist(Infolist $infolist): Infolist
Infolists\Components\TextEntry::make('DisplayOrder'),
])->grow(false),
])->from('md'),

Infolists\Components\Section::make('Event Association')
->schema([
Infolists\Components\TextEntry::make('eventData.source_achievement_id')
Expand Down Expand Up @@ -246,42 +247,6 @@ public static function form(Form $form): Form
->disabled(!$user->can('updateField', [$form->model, 'DisplayOrder'])),
]),
])->from('md'),

Forms\Components\Section::make('Event Association')
->relationship('eventData')
->columns(['xl' => 4, 'md' => 2])
->schema([
Forms\Components\Select::make('source_achievement_id')
->label('Source Achievement')
->columnSpan(2)
->searchable()
->getSearchResultsUsing(function (string $search): array {
return Achievement::where('Title', 'like', "%{$search}%")
->orWhere('ID', 'like', "%{$search}%")
->limit(50)
->get()
->mapWithKeys(function ($achievement) {
return [$achievement->id => "[{$achievement->id}] {$achievement->title}"];
})
->toArray();
})
->getOptionLabelUsing(function (int $value): string {
$achievement = Achievement::find($value);

return "[{$achievement->id}] {$achievement->title}";
}),

Forms\Components\DatePicker::make('active_from')
->label('Active From')
->native(false)
->date(),

Forms\Components\DatePicker::make('active_through')
->label('Active Through')
->native(false)
->date(),
])
->hidden(fn ($record) => $record && $record->game->system->id !== System::Events),
]);
}

Expand Down
151 changes: 151 additions & 0 deletions app/Filament/Resources/EventAchievementResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources;

use App\Filament\Extensions\Resources\Resource;
use App\Filament\Resources\EventAchievementResource\Pages;
use App\Models\Achievement;
use App\Models\EventAchievement;
use App\Models\Game;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Infolists;
use Filament\Infolists\Infolist;
use Filament\Pages\Page;

class EventAchievementResource extends Resource
{
protected static ?string $model = EventAchievement::class;

protected static ?string $recordTitleAttribute = 'title';

protected static bool $shouldRegisterNavigation = false;

public static function infolist(Infolist $infolist): Infolist
{
return $infolist
->columns(1)
->schema([
Infolists\Components\Section::make()
->schema([
Infolists\Components\TextEntry::make('source_achievement_id')
->label('Source Achievement')
->formatStateUsing(function (int $state): string {
$achievement = Achievement::find($state);

return "[{$achievement->id}] {$achievement->title}";
}),
Infolists\Components\TextEntry::make('active_from')
->label('Active From')
->date(),
Infolists\Components\TextEntry::make('active_through')
->label('Active Through')
->date(),
])
->columns(['xl' => 4, 'md' => 2]),

Infolists\Components\Section::make('Source Achievement')
->relationship('sourceAchievement')
->columns(['xl' => 2, '2xl' => 3])
->schema([
Infolists\Components\Group::make()
->schema([
Infolists\Components\ImageEntry::make('badge_url')
->label('Badge')
->size(config('media.icon.lg.width')),
Infolists\Components\ImageEntry::make('badge_locked_url')
->label('Badge (locked)')
->size(config('media.icon.lg.width')),
]),

Infolists\Components\Group::make()
->schema([
Infolists\Components\TextEntry::make('Title'),

Infolists\Components\TextEntry::make('Description'),

Infolists\Components\TextEntry::make('game')
->label('Game')
->formatStateUsing(fn (Game $state) => '[' . $state->id . '] ' . $state->title)
->url(fn (EventAchievement $record): string => $record->sourceAchievement->game->getCanonicalUrlAttribute()),

Infolists\Components\TextEntry::make('developer')
->label('Author')
->formatStateUsing(fn (User $state) => $state->display_name),
]),

Infolists\Components\Group::make()
->schema([
Infolists\Components\TextEntry::make('canonical_url')
->label('Canonical URL')
->url(fn (EventAchievement $record): string => $record->sourceAchievement->getCanonicalUrlAttribute()),
Infolists\Components\TextEntry::make('permalink')
->url(fn (EventAchievement $record): string => $record->sourceAchievement->getPermalinkAttribute()),
]),
])
->hidden(fn ($record) => !$record->sourceAchievement),
]);
}

public static function form(Form $form): Form
{
return $form
->columns(1)
->schema([
Forms\Components\Section::make()
->columns(['xl' => 4, 'md' => 2])
->schema([
Forms\Components\Select::make('source_achievement_id')
->label('Source Achievement')
->columnSpan(2)
->searchable()
->getSearchResultsUsing(function (string $search): array {
return Achievement::where('Title', 'like', "%{$search}%")
->orWhere('ID', 'like', "%{$search}%")
->limit(50)
->get()
->mapWithKeys(function ($achievement) {
return [$achievement->id => "[{$achievement->id}] {$achievement->title}"];
})
->toArray();
})
->getOptionLabelUsing(function (int $value): string {
$achievement = Achievement::find($value);

return "[{$achievement->id}] {$achievement->title}";
}),

Forms\Components\DatePicker::make('active_from')
->label('Active From')
->native(false)
->date(),

Forms\Components\DatePicker::make('active_through')
->label('Active Through')
->native(false)
->date(),
]),
]);
}

public static function getRecordSubNavigation(Page $page): array
{
return $page->generateNavigationItems([
Pages\Details::class,
Pages\AuditLog::class,
]);
}

public static function getPages(): array
{
return [
'index' => Pages\Index::route('/'),
'view' => Pages\Details::route('/{record}'),
'edit' => Pages\Edit::route('/{record}/edit'),
'audit-log' => Pages\AuditLog::route('/{record}/audit-log'),
];
}
}
37 changes: 37 additions & 0 deletions app/Filament/Resources/EventAchievementResource/Pages/AuditLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Filament\Resources\EventAchievementResource\Pages;

use App\Filament\Pages\ResourceAuditLog;
use App\Filament\Resources\EventAchievementResource;
use App\Models\EventAchievement;
use Illuminate\Support\Collection;

class AuditLog extends ResourceAuditLog
{
protected static string $resource = EventAchievementResource::class;

protected function createFieldLabelMap(): Collection
{
$fieldLabelMap = parent::createFieldLabelMap();

$fieldLabelMap['active_until'] = 'Active Until';

return $fieldLabelMap;
}

public function getBreadcrumbs(): array
{
/** @var EventAchievement $eventAchievement */
$eventAchievement = $this->record;
$game = $eventAchievement->achievement->game;
$event = $game->event;

return [
route('filament.admin.resources.events.index') => 'Events',
route('filament.admin.resources.events.view', $event) => $game->title,
route('filament.admin.resources.event-achievements.view', $eventAchievement) => $eventAchievement->achievement->title,
'Audit Log',
];
}
}
37 changes: 37 additions & 0 deletions app/Filament/Resources/EventAchievementResource/Pages/Details.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\EventAchievementResource\Pages;

use App\Filament\Resources\EventAchievementResource;
use App\Models\EventAchievement;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;

class Details extends ViewRecord
{
protected static string $resource = EventAchievementResource::class;

public function getBreadcrumbs(): array
{
/** @var EventAchievement $eventAchievement */
$eventAchievement = $this->record;
$game = $eventAchievement->achievement->game;
$event = $game->event;

return [
route('filament.admin.resources.events.index') => 'Events',
route('filament.admin.resources.events.view', $event) => $game->title,
route('filament.admin.resources.event-achievements.view', $eventAchievement) => $eventAchievement->achievement->title,
'View',
];
}

protected function getHeaderActions(): array
{
return [
Actions\EditAction::make(),
];
}
}
32 changes: 32 additions & 0 deletions app/Filament/Resources/EventAchievementResource/Pages/Edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\EventAchievementResource\Pages;

use App\Filament\Concerns\HasFieldLevelAuthorization;
use App\Filament\Resources\EventAchievementResource;
use App\Models\EventAchievement;
use Filament\Resources\Pages\EditRecord;

class Edit extends EditRecord
{
use HasFieldLevelAuthorization;

protected static string $resource = EventAchievementResource::class;

public function getBreadcrumbs(): array
{
/** @var EventAchievement $eventAchievement */
$eventAchievement = $this->record;
$game = $eventAchievement->achievement->game;
$event = $game->event;

return [
route('filament.admin.resources.events.index') => 'Events',
route('filament.admin.resources.events.view', $event) => $game->title,
route('filament.admin.resources.event-achievements.view', $eventAchievement) => $eventAchievement->achievement->title,
'Edit',
];
}
}
19 changes: 19 additions & 0 deletions app/Filament/Resources/EventAchievementResource/Pages/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\EventAchievementResource\Pages;

use App\Filament\Resources\EventAchievementResource;
use Filament\Resources\Pages\ListRecords;

class Index extends ListRecords
{
protected static string $resource = EventAchievementResource::class;

protected function getHeaderActions(): array
{
return [
];
}
}
Loading

0 comments on commit f4081ce

Please sign in to comment.