| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
-
- namespace App\Filament\Resources;
-
- use App\Filament\Resources\AlbumResource\Pages;
- use App\Models\Album;
- use App\Models\AlbumCategory;
- use App\Service\DeepLService;
- use Filament\Forms\Components\DatePicker;
- use Filament\Forms\Components\FileUpload;
- use Filament\Forms\Components\Group;
- use Filament\Forms\Components\Radio;
- use Filament\Forms\Components\Section;
- use Filament\Forms\Components\Select;
- use Filament\Forms\Components\TextInput;
- use Filament\Forms\Components\Toggle;
- use Filament\Forms\Form;
- use Filament\Forms\Get;
- use Filament\Resources\Resource;
- use Filament\Tables;
- use Filament\Tables\Columns\ImageColumn;
- use Filament\Tables\Columns\TextColumn;
- use Filament\Tables\Filters\SelectFilter;
- use Filament\Tables\Table;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\DB;
- use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
-
- class AlbumResource extends Resource
- {
- protected static ?string $model = Album::class;
-
- protected static ?string $modelLabel = '影音管理';
-
- protected static ?string $navigationIcon = 'heroicon-o-photo';
-
- protected static ?string $navigationGroup = '最新消息';
-
- protected static ?string $navigationLabel = '影音管理';
-
- public static function form(Form $form): Form
- {
- return $form
- ->schema([
- Section::make('新增 影音')->schema([
- Group::make()->schema([
- Select::make('album_category_id')
- ->options(AlbumCategory::orderBy('order')->get()->pluck('name', 'id'))
- ->label('影音分類')
- ->required()
- ->columnSpan(1)
- ->native(false)
- ->Live(),
- ])->columnSpanFull()->columns(2),
- Group::make()->schema([
- DatePicker::make('post_date')
- ->label('發布日期')
- ->closeOnDateSelection(),
- ])->columnSpanFull()->columns(2),
- FileUpload::make('news_banner')->label('列表大圖')
- ->directory('album/img')
- ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
- ->helperText('建議寬高限制為:2000*720px,出血寬度720px,主要圖像範圍為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
- FileUpload::make('news_img_pc')->label('列表圖(desktop)')
- ->directory('album/img')
- ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
- ->helperText('建議寬高限制為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
- FileUpload::make('news_img_mobile')->label('列表圖(mobile)')
- ->directory('album/img')
- ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
- ->helperText('建議寬高限制為:600x896px,檔案大小限制為1M以下')->maxSize('1024'),
- Translate::make()->schema(fn (string $locale) => [
- TextInput::make('title')
- ->label('標題')
- ->columnSpan(1),
- ])
- ->locales(['zh_TW', 'en', 'jp'])
- ->actions([
- app(DeepLService::class)->createTranslationAction('Main', ['title']),
- ])
- ->id('main')->columnSpanFull()->columns(3),
-
- Section::make('')->schema([
- Radio::make('upload_type')->label('')->options([
- 1 => '網址',
- 2 => '檔案',
- ])->columnSpanFull()->default(1)->Live(),
- Group::make()->schema([
- TextInput::make('link_video')->label('網址')->nullable(),
- ])->visible(fn (Get $get): bool => $get('upload_type') == 1)->columnSpanFull(),
- Group::make()->schema([
- FileUpload::make('link_upload')->label('')->directory('album/video')
- ->acceptedFileTypes(['video/mp4', 'video/mpeg', 'video/quicktime', 'video/x-msvideo', 'video/x-ms-wmv'])
- ->helperText('建議影片寬高限制為:1920*1080px,出血寬度720px,大小限制為:100M以下')
- ->maxSize(102400)->nullable(),
- ])->visible(fn (Get $get): bool => $get('upload_type') == 2)->columnSpanFull(),
- ])->columnSpanFull(),
- Toggle::make('on_top')->inline()->label('置頂輪播')->columnSpanFull(),
- Toggle::make('homepage_top')->inline()->label('在首頁輪播')->columnSpanFull(),
- TextInput::make('order')->label('排序')->integer()->default(0),
- ])->columns(3),
- ]);
- }
-
- public static function table(Table $table): Table
- {
- return $table
- ->columns([
- //
- TextColumn::make('albumCategory.name')->label('分類')->alignCenter(),
- TextColumn::make('title')->label('標題')->alignCenter(),
- TextColumn::make('post_date')->date()->alignCenter(),
- ImageColumn::make('news_img_pc')->alignCenter(),
- TextColumn::make('list_audit_state')->label('狀態')->badge()
- ->color(fn (string $state): string => match ($state) {
- '暫存' => 'warning',
- '已發佈' => 'success',
- }),
- TextColumn::make('created_at')->label('建立時間')->dateTime()->alignCenter(),
- TextColumn::make('updated_at')->label('更新時間')->dateTime()->alignCenter(),
- ])
- ->filters([
- SelectFilter::make('post_date')->label('年份')
- ->options(Album::select(DB::raw("DATE_FORMAT(post_date, '%Y') as year"))->whereNotNull('post_date')->distinct()->pluck('year', 'year')->toArray())
- ->query(
- fn (array $data, Builder $query): Builder => $query->when(
- $data['value'],
- fn (Builder $query, $value): Builder => $query->where('post_date', 'like', $data['value'].'%')
- )
- ),
- SelectFilter::make('album_category_id')->label('分類')
- ->relationship('albumCategory', 'name')
- ->getOptionLabelFromRecordUsing(fn ($record, $livewire) => $record->getTranslation('name', 'zh_TW')),
- SelectFilter::make('visible')->label('狀態')
- ->options([
- 0 => '暫存',
- 1 => '已發佈',
- ])
- ->query(
- fn (array $data, Builder $query): Builder => $query->when(
- $data['value'],
- fn (Builder $query, $value): Builder => $query->where('visible', $data['value'])
- )
- ),
- ])
- ->actions([
- Tables\Actions\EditAction::make(),
- Tables\Actions\DeleteAction::make(),
- \Filament\Tables\Actions\Action::make('audit')
- ->label(fn ($record) => match ($record->visible) {
- 0 => '發佈',
- 1 => '下架',
- })
- ->color(fn ($record) => match ($record->visible) {
- 0 => 'warning',
- 1 => 'gray',
- })
- ->icon(fn ($record) => match ($record->visible) {
- 0 => 'heroicon-m-chevron-double-up',
- 1 => 'heroicon-m-chevron-double-down',
- })
- ->action(function ($record) {
- $record->visible = ! $record->visible;
- $record->save();
- })
- ->outlined()
- ->requiresConfirmation(),
- ])
- ->bulkActions([
- Tables\Actions\BulkActionGroup::make([
- Tables\Actions\DeleteBulkAction::make(),
- ]),
- ])
- ->defaultSort('order', 'desc')
- ->defaultSort('created_at', 'desc');
- }
-
- public static function getRelations(): array
- {
- return [
- //
- ];
- }
-
- public static function getPages(): array
- {
- return [
- 'index' => Pages\ListAlbums::route('/'),
- 'create' => Pages\CreateAlbum::route('/create'),
- 'edit' => Pages\EditAlbum::route('/{record}/edit'),
- 'view' => Pages\ViewAlbum::route('/{record}/view'),
- ];
- }
- }
|