Skip to content

Commit

Permalink
feat: added entry id field on tracks resource (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Jun 30, 2024
1 parent 9a22e2a commit f68e87a
Show file tree
Hide file tree
Showing 20 changed files with 446 additions and 77 deletions.
16 changes: 10 additions & 6 deletions app/Actions/Models/Wiki/Anime/BackfillAnimeSynonymsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public function handle(): ActionResult
}

foreach ($titles as $type => $text) {
if ($type === 'romaji' && $text === $this->getModel()->getName()) continue;
if (
$text === null
|| empty($text)
|| ($type === 'romaji' && $text === $this->getModel()->getName())
) continue;

Log::info("Creating {$text}");

Expand All @@ -83,9 +87,9 @@ public function handle(): ActionResult
* Get the enum related to the array map.
*
* @param string $title
* @return AnimeSynonymType|null
* @return AnimeSynonymType
*/
protected static function getAnilistSynonymsMap($title): ?AnimeSynonymType
protected static function getAnilistSynonymsMap($title): AnimeSynonymType
{
return match ($title) {
'english' => AnimeSynonymType::ENGLISH,
Expand Down Expand Up @@ -141,9 +145,9 @@ protected function getTitles(): ?array
])
->throw()
->json();

$titles = Arr::get($response, 'data.Media.title');

return $titles;
}

Expand All @@ -169,4 +173,4 @@ protected function relation(): HasMany
{
return $this->getModel()->animesynonyms();
}
}
}
2 changes: 1 addition & 1 deletion app/Filament/Resources/Discord/DiscordThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static function form(Form $form): Form
->formatStateUsing(fn ($state) => strval($state))
->required()
->rules(['required'])
->live(true)
->live()
->afterStateUpdated(fn (Set $set, string $state) => $set(DiscordThreadModel::ATTRIBUTE_NAME, Arr::get((new DiscordThreadAction())->get($state), 'thread.name'))),

TextInput::make(DiscordThreadModel::ATTRIBUTE_NAME)
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Resources/List/Playlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public static function table(Table $table): Table
->label(__('filament.resources.singularLabel.user'))
->toggleable()
->placeholder('-')
->urlToRelated(UserResource::class, PlaylistModel::RELATION_USER),
->urlToRelated(UserResource::class, PlaylistModel::RELATION_USER, true),

TextColumn::make(PlaylistModel::ATTRIBUTE_ID)
->label(__('filament.fields.base.id'))
Expand Down
74 changes: 64 additions & 10 deletions app/Filament/Resources/List/Playlist/Track.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
use App\Filament\Resources\List\Playlist\Track\Pages\EditTrack;
use App\Filament\Resources\List\Playlist\Track\Pages\ListTracks;
use App\Filament\Resources\List\Playlist\Track\Pages\ViewTrack;
use App\Filament\Resources\Wiki\Anime\Theme\Entry;
use App\Filament\Resources\Wiki\Video as VideoResource;
use App\Models\List\Playlist as PlaylistModel;
use App\Models\List\Playlist\PlaylistTrack as TrackModel;
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
use App\Models\Wiki\Video as VideoModel;
use App\Pivots\Wiki\AnimeThemeEntryVideo;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Infolist;
use Filament\Resources\RelationManagers\RelationGroup;
use Filament\Tables\Table;
use Illuminate\Validation\Rule;

/**
* Class Track.
Expand Down Expand Up @@ -126,13 +131,56 @@ public static function form(Form $form): Form
->label(__('filament.resources.singularLabel.playlist'))
->relationship(TrackModel::RELATION_PLAYLIST, PlaylistModel::ATTRIBUTE_NAME)
->searchable()
->required()
->rules(['required'])
->hiddenOn([TrackPlaylistRelationManager::class])
->createOptionForm(PlaylistResource::form($form)->getComponents()),

Select::make(TrackModel::ATTRIBUTE_ENTRY)
->label(__('filament.resources.singularLabel.anime_theme_entry'))
->relationship(TrackModel::RELATION_ENTRY, AnimeThemeEntry::ATTRIBUTE_ID)
->live(true)
->useScout(AnimeThemeEntry::class, AnimeThemeEntry::RELATION_ANIME_SHALLOW)
->rules([
fn (Get $get) => function () use ($get) {
return [
Rule::when(
!empty($get(TrackModel::RELATION_ENTRY)) && !empty($get(TrackModel::RELATION_VIDEO)),
[
Rule::exists(AnimeThemeEntryVideo::class, AnimeThemeEntryVideo::ATTRIBUTE_ENTRY)
->where(AnimeThemeEntryVideo::ATTRIBUTE_VIDEO, $get(TrackModel::RELATION_VIDEO)),
]
)
];
}
]),

Select::make(TrackModel::ATTRIBUTE_VIDEO)
->label(__('filament.resources.singularLabel.video'))
->relationship(TrackModel::RELATION_VIDEO, VideoModel::ATTRIBUTE_FILENAME)
->searchable(),

->rules([
fn (Get $get) => function () use ($get) {
return [
Rule::when(
!empty($get(TrackModel::RELATION_ENTRY)) && !empty($get(TrackModel::RELATION_VIDEO)),
[
Rule::exists(AnimeThemeEntryVideo::class, AnimeThemeEntryVideo::ATTRIBUTE_VIDEO)
->where(AnimeThemeEntryVideo::ATTRIBUTE_ENTRY, $get(TrackModel::RELATION_ENTRY)),
]
)
];
}
])
->options(function (Get $get) {
return VideoModel::query()
->whereHas(VideoModel::RELATION_ANIMETHEMEENTRIES, function ($query) use ($get) {
$query->where(AnimeThemeEntry::TABLE . '.' . AnimeThemeEntry::ATTRIBUTE_ID, $get(TrackModel::ATTRIBUTE_ENTRY));
})
->get()
->mapWithKeys(fn (VideoModel $video) => [$video->getKey() => $video->getName()])
->toArray();
}),

TextInput::make(TrackModel::ATTRIBUTE_HASHID)
->label(__('filament.fields.playlist_track.hashid.name'))
->helperText(__('filament.fields.playlist_track.hashid.help'))
Expand Down Expand Up @@ -165,13 +213,18 @@ public static function table(Table $table): Table
{
return parent::table($table)
->columns([
TextColumn::make(TrackModel::RELATION_PLAYLIST.'.'.PlaylistModel::ATTRIBUTE_NAME)
TextColumn::make(TrackModel::RELATION_PLAYLIST . '.' . PlaylistModel::ATTRIBUTE_NAME)
->label(__('filament.resources.singularLabel.playlist'))
->toggleable()
->hiddenOn(TrackPlaylistRelationManager::class)
->urlToRelated(PlaylistResource::class, TrackModel::RELATION_PLAYLIST),

TextColumn::make(TrackModel::RELATION_VIDEO.'.'.VideoModel::ATTRIBUTE_FILENAME)
TextColumn::make(TrackModel::RELATION_ENTRY . '.' . AnimeThemeEntry::ATTRIBUTE_ID)
->label(__('filament.resources.singularLabel.anime_theme_entry'))
->toggleable()
->urlToRelated(Entry::class, TrackModel::RELATION_ENTRY, true),

TextColumn::make(TrackModel::RELATION_VIDEO . '.' . VideoModel::ATTRIBUTE_FILENAME)
->label(__('filament.resources.singularLabel.video'))
->toggleable()
->urlToRelated(VideoResource::class, TrackModel::RELATION_VIDEO),
Expand Down Expand Up @@ -202,7 +255,7 @@ public static function infolist(Infolist $infolist): Infolist
->schema([
Section::make(static::getRecordTitle($infolist->getRecord()))
->schema([
TextEntry::make(TrackModel::RELATION_PLAYLIST.'.'.PlaylistModel::ATTRIBUTE_NAME)
TextEntry::make(TrackModel::RELATION_PLAYLIST . '.' . PlaylistModel::ATTRIBUTE_NAME)
->label(__('filament.resources.singularLabel.playlist'))
->urlToRelated(PlaylistResource::class, TrackModel::RELATION_PLAYLIST),

Expand All @@ -213,15 +266,15 @@ public static function infolist(Infolist $infolist): Infolist
TextEntry::make(TrackModel::ATTRIBUTE_ID)
->label(__('filament.fields.base.id')),

TextEntry::make(TrackModel::RELATION_VIDEO.'.'.VideoModel::ATTRIBUTE_FILENAME)
TextEntry::make(TrackModel::RELATION_VIDEO . '.' . VideoModel::ATTRIBUTE_FILENAME)
->label(__('filament.resources.singularLabel.video'))
->urlToRelated(VideoResource::class, TrackModel::RELATION_VIDEO),

TextEntry::make(TrackModel::RELATION_PREVIOUS.'.'.TrackModel::RELATION_VIDEO.'.'.VideoModel::ATTRIBUTE_FILENAME)
TextEntry::make(TrackModel::RELATION_PREVIOUS . '.' . TrackModel::RELATION_VIDEO . '.' . VideoModel::ATTRIBUTE_FILENAME)
->label(__('filament.fields.playlist_track.previous.name'))
->urlToRelated(Track::class, TrackModel::RELATION_PREVIOUS),
TextEntry::make(TrackModel::RELATION_NEXT.'.'.TrackModel::RELATION_VIDEO.'.'.VideoModel::ATTRIBUTE_FILENAME)

TextEntry::make(TrackModel::RELATION_NEXT . '.' . TrackModel::RELATION_VIDEO . '.' . VideoModel::ATTRIBUTE_FILENAME)
->label(__('filament.fields.playlist_track.next.name'))
->urlToRelated(Track::class, TrackModel::RELATION_NEXT),
])
Expand All @@ -243,7 +296,8 @@ public static function infolist(Infolist $infolist): Infolist
public static function getRelations(): array
{
return [
RelationGroup::make(static::getLabel(),
RelationGroup::make(
static::getLabel(),
array_merge(
[],
parent::getBaseRelations(),
Expand Down
18 changes: 10 additions & 8 deletions app/Filament/Resources/Wiki/Anime/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,24 @@ public static function form(Form $form): Form
->label(__('filament.resources.singularLabel.anime'))
->relationship(ThemeModel::RELATION_ANIME, AnimeModel::ATTRIBUTE_NAME)
->searchable()
->hiddenOn(ThemeRelationManager::class),
->hiddenOn(ThemeRelationManager::class)
->required()
->rules(['required']),

Select::make(ThemeModel::ATTRIBUTE_TYPE)
->label(__('filament.fields.anime_theme.type.name'))
->helperText(__('filament.fields.anime_theme.type.help'))
->options(ThemeType::asSelectArray())
->required()
->live(true)
->live()
->afterStateUpdated(fn (Set $set, Get $get) => Theme::setThemeSlug($set, $get))
->rules(['required', new Enum(ThemeType::class)]),

TextInput::make(ThemeModel::ATTRIBUTE_SEQUENCE)
->label(__('filament.fields.anime_theme.sequence.name'))
->helperText(__('filament.fields.anime_theme.sequence.help'))
->numeric()
->live(true)
->live()
->afterStateUpdated(fn (Set $set, Get $get) => Theme::setThemeSlug($set, $get))
->rules(['nullable', 'integer']),

Expand All @@ -207,7 +209,7 @@ public static function form(Form $form): Form
->label(__('filament.resources.singularLabel.group'))
->relationship(ThemeModel::RELATION_GROUP, Group::ATTRIBUTE_NAME)
->searchable()
->live(true)
->live()
->afterStateUpdated(fn (Set $set, Get $get) => Theme::setThemeSlug($set, $get))
->createOptionForm(GroupResource::form($form)->getComponents()),
]),
Expand All @@ -218,7 +220,7 @@ public static function form(Form $form): Form
Select::make(ThemeModel::ATTRIBUTE_SONG)
->label(__('filament.resources.singularLabel.song'))
->relationship(ThemeModel::RELATION_SONG, Song::ATTRIBUTE_TITLE)
->live(true)
->live()
->useScout(Song::class)
->createOptionForm(SongResource::form($form)->getComponents())
->afterStateUpdated(function (Set $set, $state) {
Expand All @@ -237,7 +239,7 @@ public static function form(Form $form): Form
->label(__('filament.resources.label.artists'))
->addActionLabel(__('filament.buttons.add').' '.__('filament.resources.singularLabel.artist'))
->hidden(fn (Get $get) => $get(ThemeModel::ATTRIBUTE_SONG) === null)
->live(true)
->live()
->key('song.artists')
->collapsible()
->defaultItems(0)
Expand Down Expand Up @@ -351,10 +353,10 @@ public static function table(Table $table): Table
TextColumn::make(ThemeModel::RELATION_SONG . '.' . Song::ATTRIBUTE_TITLE)
->label(__('filament.resources.singularLabel.song'))
->toggleable()
->placeholder('-')
->hiddenOn(ThemeSongRelationManager::class)
->urlToRelated(SongResource::class, ThemeModel::RELATION_SONG, limit: 30)
->tooltip(fn (TextColumn $column) => $column->getState()),
->placeholder('-')
->tooltip(fn (TextColumn $column) => is_array($column->getState()) ? null : $column->getState()),
])
->searchable();
}
Expand Down
4 changes: 4 additions & 0 deletions app/Filament/Resources/Wiki/Anime/Theme/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ public static function form(Form $form): Form
->label(__('filament.resources.singularLabel.anime'))
->relationship(EntryModel::RELATION_ANIME_SHALLOW, AnimeModel::ATTRIBUTE_NAME)
->searchable()
->required()
->rules(['required'])
->visibleOn([CreateEntry::class, EditEntry::class])
->allowHtml()
->getOptionLabelUsing(fn ($state) => Select::getSearchLabelWithBlade(AnimeModel::find($state)))
Expand All @@ -171,6 +173,8 @@ public static function form(Form $form): Form
->label(__('filament.resources.singularLabel.anime_theme'))
->relationship(EntryModel::RELATION_THEME, ThemeModel::ATTRIBUTE_ID)
->searchable()
->required()
->rules(['required'])
->visibleOn([CreateEntry::class, EditEntry::class])
->allowHtml()
->getOptionLabelUsing(fn ($state) => Select::getSearchLabelWithBlade(ThemeModel::find($state)))
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Resources/Wiki/ExternalResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static function form(Form $form): Form
->label(__('filament.fields.external_resource.link.name'))
->helperText(__('filament.fields.external_resource.link.help'))
->required()
->live(true)
->live()
->afterStateUpdated(function (Set $set, ?string $state) {
if ($state !== null) {
$set(ExternalResourceModel::ATTRIBUTE_SITE, ResourceSite::valueOf($state) ?? ResourceSite::OFFICIAL_SITE);
Expand Down
Loading

0 comments on commit f68e87a

Please sign in to comment.