Skip to content

Commit

Permalink
feat(filament): added subnames (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored May 16, 2024
1 parent 62b0b51 commit db2c05f
Show file tree
Hide file tree
Showing 122 changed files with 729 additions and 63 deletions.
18 changes: 18 additions & 0 deletions app/Contracts/Models/SubNameable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Contracts\Models;

/**
* Interface SubNameable.
*/
interface SubNameable
{
/**
* Get subname.
*
* @return string
*/
public function getSubName(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ public function getForm(Form $form): Form
->label(__('filament.actions.video.backfill.fields.derive_source.name'))
->options(DeriveSourceVideo::asSelectArray())
->rules(['required', new Enum(DeriveSourceVideo::class)])
->default(DeriveSourceVideo::YES)
->default(DeriveSourceVideo::YES->value)
->helperText(__('filament.actions.video.backfill.fields.derive_source.help')),

Select::make(self::OVERWRITE_AUDIO)
->label(__('filament.actions.video.backfill.fields.overwrite.name'))
->options(OverwriteAudio::asSelectArray())
->rules(['required', new Enum(OverwriteAudio::class)])
->default(OverwriteAudio::NO)
->default(OverwriteAudio::NO->value)
->helperText(__('filament.actions.video.backfill.fields.overwrite.help')),
]);
}
Expand Down
19 changes: 18 additions & 1 deletion app/Filament/Components/Fields/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Filament\Components\Fields;

use App\Models\Auth\User;
use App\Models\BaseModel;
use Filament\Forms\Components\Select as ComponentsSelect;
use Laravel\Scout\Searchable;
Expand All @@ -24,16 +25,32 @@ public function useScout(string $model, ?string $loadRelation = null): static
{
if (in_array(Searchable::class, class_uses_recursive($model))) {
return $this
->allowHtml()
->searchable()
->getSearchResultsUsing(function (string $search) use ($model, $loadRelation) {
return (new $model)::search($search)
->get()
->load($loadRelation ?? [])
->mapWithKeys(fn (BaseModel $model) => [$model->getKey() => $model->getName()])
->mapWithKeys(fn (BaseModel $model) => [$model->getKey() => static::getSearchLabelWithBlade($model)])
->toArray();
});
}

return $this->searchable();
}

/**
* Use the blade to make the results.
*
* @param BaseModel|User $model
* @return string
*/
public static function getSearchLabelWithBlade(BaseModel|User $model): string
{
return view('filament.components.select')
->with('name', $model->getName())
->with('subname', $model->getSubName())
->with('image', $model instanceof User ? $model->getFilamentAvatarUrl() : null)
->render();
}
}
4 changes: 2 additions & 2 deletions app/Filament/Components/Filters/NumberFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ public function applyToBaseQuery(Builder $query, array $data = []): Builder
return $query
->when(
Arr::get($data, $this->attribute.'_'.'from'),
fn (Builder $query, $date): Builder => $query->where($this->attribute, ComparisonOperator::GTE->value, $date),
fn (Builder $query, $value): Builder => $query->where($this->attribute, ComparisonOperator::GTE->value, $value),
)
->when(
Arr::get($data, $this->attribute.'_'.'to'),
fn (Builder $query, $date): Builder => $query->where($this->attribute, ComparisonOperator::LTE->value, $date),
fn (Builder $query, $value): Builder => $query->where($this->attribute, ComparisonOperator::LTE->value, $value),
);
}
}
2 changes: 1 addition & 1 deletion app/Filament/Components/Infolist/TextEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TextEntry extends ComponentsTextEntry
/**
* Used for entry relationships.
*
* @param class-string<BaseResource> $resourceRelated
* @param class-string<BaseResource>|string $resourceRelated
* @param string $relation
* @return static
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use App\Enums\Actions\Models\Wiki\Video\DeriveSourceVideo;
use App\Enums\Actions\Models\Wiki\Video\OverwriteAudio;
use App\Filament\Components\Fields\Select;
use App\Models\BaseModel;
use App\Models\Wiki\Video;
use Exception;
use Filament\Forms\Form;
Expand Down Expand Up @@ -92,14 +91,14 @@ public function getForm(Form $form): Form
->label(__('filament.actions.video.backfill.fields.derive_source.name'))
->options(DeriveSourceVideo::asSelectArray())
->rules(['required', new Enum(DeriveSourceVideo::class)])
->default(DeriveSourceVideo::YES)
->default(DeriveSourceVideo::YES->value)
->helperText(__('filament.actions.video.backfill.fields.derive_source.help')),

Select::make(self::OVERWRITE_AUDIO)
->label(__('filament.actions.video.backfill.fields.overwrite.name'))
->options(OverwriteAudio::asSelectArray())
->rules(['required', new Enum(OverwriteAudio::class)])
->default(OverwriteAudio::NO)
->default(OverwriteAudio::NO->value)
->helperText(__('filament.actions.video.backfill.fields.overwrite.help')),
]);
}
Expand Down
1 change: 1 addition & 0 deletions app/Filament/Resources/Admin/Announcement.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public static function table(Table $table): Table
])
->defaultSort(AnnouncementModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->actions(static::getActions())
->bulkActions(static::getBulkActions());
}
Expand Down
1 change: 1 addition & 0 deletions app/Filament/Resources/Admin/Dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public static function table(Table $table): Table
])
->defaultSort(DumpModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->actions(static::getActions())
->bulkActions(static::getBulkActions())
->headerActions(static::getHeaderActions());
Expand Down
1 change: 1 addition & 0 deletions app/Filament/Resources/Admin/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public static function table(Table $table): Table
])
->defaultSort(FeatureModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->actions(static::getActions())
->bulkActions(static::getBulkActions());
}
Expand Down
14 changes: 12 additions & 2 deletions app/Filament/Resources/Admin/FeaturedTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Filament\Resources\Admin;

use App\Enums\Http\Api\Filter\AllowedDateFormat;
use App\Enums\Http\Api\Filter\ComparisonOperator;
use App\Filament\Components\Columns\TextColumn;
use App\Filament\Components\Fields\Select;
use App\Filament\Components\Infolist\TextEntry;
Expand Down Expand Up @@ -202,7 +203,15 @@ public static function form(Form $form): Form
Select::make(FeaturedThemeModel::ATTRIBUTE_USER)
->label(__('filament.resources.singularLabel.user'))
->relationship(FeaturedThemeModel::RELATION_USER, User::ATTRIBUTE_NAME)
->searchable(),
->allowHtml()
->searchable()
->getSearchResultsUsing(function (string $search) {
return User::query()
->where(User::ATTRIBUTE_NAME, ComparisonOperator::LIKE->value, "%$search%")
->get()
->mapWithKeys(fn (User $model) => [$model->getKey() => Select::getSearchLabelWithBlade($model)])
->toArray();
}),
])
->columns(1);
}
Expand Down Expand Up @@ -245,7 +254,7 @@ public static function table(Table $table): Table
->label(__('filament.resources.singularLabel.anime_theme_entry'))
->toggleable()
->placeholder('-')
->formatStateUsing(fn (string $state) => EntryModel::find(intval($state))->load(EntryModel::RELATION_ANIME)->getName())
->formatStateUsing(fn (string $state) => EntryModel::find(intval($state))->load(EntryModel::RELATION_ANIME_SHALLOW)->getName())
->urlToRelated(EntryResource::class, FeaturedThemeModel::RELATION_ENTRY),

TextColumn::make(FeaturedThemeModel::RELATION_USER.'.'.User::ATTRIBUTE_NAME)
Expand All @@ -256,6 +265,7 @@ public static function table(Table $table): Table
])
->defaultSort(FeaturedThemeModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->actions(static::getActions())
->bulkActions(static::getBulkActions());
}
Expand Down
1 change: 1 addition & 0 deletions app/Filament/Resources/Auth/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public static function table(Table $table): Table
])
->defaultSort(PermissionModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->actions(static::getActions())
->bulkActions(static::getBulkActions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function table(Table $table): Table
->columns(RoleResource::table($table)->getColumns())
->defaultSort(Role::TABLE.'.'.Role::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->headerActions(static::getHeaderActions())
->actions(static::getActions())
->bulkActions(static::getBulkActions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function table(Table $table): Table
->columns(UserResource::table($table)->getColumns())
->defaultSort(User::TABLE.'.'.User::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->headerActions(static::getHeaderActions())
->actions(static::getActions())
->bulkActions(static::getBulkActions());
Expand Down
1 change: 1 addition & 0 deletions app/Filament/Resources/Auth/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public static function table(Table $table): Table
])
->defaultSort(RoleModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->actions(static::getActions())
->bulkActions(static::getBulkActions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function table(Table $table): Table
->columns(PermissionResource::table($table)->getColumns())
->defaultSort(Permission::TABLE.'.'.Permission::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->headerActions(static::getHeaderActions())
->actions(static::getActions())
->bulkActions(static::getBulkActions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function table(Table $table): Table
->columns(UserResource::table($table)->getColumns())
->defaultSort(User::TABLE.'.'.User::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->headerActions(static::getHeaderActions())
->actions(static::getActions())
->bulkActions(static::getBulkActions());
Expand Down
1 change: 1 addition & 0 deletions app/Filament/Resources/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public static function table(Table $table): Table
])
->defaultSort(UserModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->actions(static::getActions())
->bulkActions(static::getBulkActions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function table(Table $table): Table
->columns(PermissionResource::table($table)->getColumns())
->defaultSort(Permission::TABLE.'.'.Permission::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->headerActions(static::getHeaderActions())
->actions(static::getActions())
->bulkActions(static::getBulkActions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function table(Table $table): Table
->columns(PlaylistResource::table($table)->getColumns())
->defaultSort(Playlist::TABLE.'.'.Playlist::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->headerActions(static::getHeaderActions())
->actions(static::getActions())
->bulkActions(static::getBulkActions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function table(Table $table): Table
->columns(RoleResource::table($table)->getColumns())
->defaultSort(Role::TABLE.'.'.Role::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->headerActions(static::getHeaderActions())
->actions(static::getActions())
->bulkActions(static::getBulkActions());
Expand Down
14 changes: 13 additions & 1 deletion app/Filament/Resources/BaseRelationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Filament\Resources;

use App\Filament\Components\Fields\Select;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables\Actions\AttachAction;
use Filament\Tables\Actions\BulkActionGroup;
Expand All @@ -17,6 +18,7 @@
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
* Class BaseRelationManager.
Expand Down Expand Up @@ -111,7 +113,17 @@ public static function getHeaderActions(): array
{
return [
CreateAction::make(),
AttachAction::make(),

AttachAction::make()
->hidden(fn (BaseRelationManager $livewire) => !($livewire->getRelationship() instanceof BelongsToMany))
->recordSelect(function (BaseRelationManager $livewire) {
/** @var string */
$model = $livewire->getTable()->getModel();
$title = $livewire->getTable()->getRecordTitle(new $model);
return Select::make($title)
->label($title)
->useScout($model);
}),
];
}
}
1 change: 1 addition & 0 deletions app/Filament/Resources/Document/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public static function table(Table $table): Table
])
->defaultSort(PageModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->actions(static::getActions())
->bulkActions(static::getBulkActions());
}
Expand Down
1 change: 1 addition & 0 deletions app/Filament/Resources/List/Playlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public static function table(Table $table): Table
->searchable()
->defaultSort(PlaylistModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->actions(static::getActions())
->bulkActions(static::getBulkActions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function table(Table $table): Table
->columns(ImageResource::table($table)->getColumns())
->defaultSort(Image::TABLE.'.'.Image::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->headerActions(static::getHeaderActions())
->actions(static::getActions())
->bulkActions(static::getBulkActions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function table(Table $table): Table
->columns(TrackResource::table($table)->getColumns())
->defaultSort(PlaylistTrack::TABLE.'.'.PlaylistTrack::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->headerActions(static::getHeaderActions())
->actions(static::getActions())
->bulkActions(static::getBulkActions());
Expand Down
17 changes: 13 additions & 4 deletions app/Filament/Resources/List/Playlist/Track.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public static function table(Table $table): Table
])
->defaultSort(TrackModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->actions(static::getActions())
->bulkActions(static::getBulkActions());
}
Expand All @@ -207,16 +208,24 @@ public static function infolist(Infolist $infolist): Infolist
->label(__('filament.resources.singularLabel.playlist'))
->urlToRelated(PlaylistResource::class, TrackModel::RELATION_PLAYLIST),

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

TextEntry::make(TrackModel::ATTRIBUTE_HASHID)
->label(__('filament.fields.playlist_track.hashid.name'))
->placeholder('-'),

TextEntry::make(TrackModel::ATTRIBUTE_ID)
->label(__('filament.fields.base.id')),

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)
->label(__('filament.fields.playlist_track.previous.name'))
->urlToRelated(Track::class, TrackModel::RELATION_PREVIOUS),

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

Expand Down
1 change: 1 addition & 0 deletions app/Filament/Resources/Wiki/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public static function table(Table $table): Table
->searchable()
->defaultSort(AnimeModel::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->actions(static::getActions())
->bulkActions(static::getBulkActions());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function table(Table $table): Table
->columns(ImageResource::table($table)->getColumns())
->defaultSort(Image::TABLE.'.'.Image::ATTRIBUTE_ID, 'desc')
->filters(static::getFilters())
->filtersFormMaxHeight('400px')
->headerActions(static::getHeaderActions())
->actions(static::getActions())
->bulkActions(static::getBulkActions());
Expand Down
Loading

0 comments on commit db2c05f

Please sign in to comment.