schema([ // Tabs::make()->schema([ Tab::make("基本資訊")->schema([ Select::make("news_category_id")->label("分類")->options(function (){ return NewsCategory::pluck("name", "id"); })->required() ->columnSpan(1), DatePicker::make("post_date")->label("發佈日") ->native(false) ->closeOnDateSelection() ->columnSpan(1), FileUpload::make("news_img")->label("圖片") ->directory("news")->maxSize(122880) ->columnSpan(2), TranslatableTabs::make('') ->schema([ TextInput::make("title") ->label("標題") ->columnSpan(1), TextInput::make("written_by") ->label("撰文者") ->columnSpan(1), Textarea::make("description") ->label("簡述") ->columnSpan(2) ])->columns(2)->columnSpan(2), ])->columns(2), Tab::make("SEO")->schema([ TranslatableTabs::make('') ->schema([ TextInput::make("meta_title")->label("SEO 標題"), TextInput::make("meta_keyword")->label("SEO 關鍵字"), Textarea::make("meta_description")->label("SEO 簡述") ])->columnSpanFull(), FileUpload::make('meta_img')->label("放大預覽圖") ->directory("news/seo") ->columnSpan(1), ]), Tab::make("內文編輯")->schema([ Repeater::make("paragraphs")->schema([ TextInput::make('item_key') ->default(fn () => Str::random()) ->hidden() ->afterStateHydrated(function (TextInput $component, $state) { if (empty($state)) { $component->state(Str::random()); } }), Radio::make("paragraph_type")->options([ 1 => "圖片", 2 => "文字", // 3 => "影音" ])->label("")->inline()->default(1)->Live(), Group::make()->schema([ Section::make('')->schema([ Repeater::make("multiple_images")->schema([ TextInput::make('para_img_item_key') ->default(fn () => Str::random()) ->hidden() ->afterStateHydrated(function (TextInput $component, $state) { if (empty($state)) { $component->state(Str::random()); } }), TextInput::make("img_alt")->label("圖片註釋")->translatableTabs()->columnSpan(1), FileUpload::make('img_url')->label("") ->directory("news/paragraphs") ->maxFiles(10), ]) ->relationship("photos") ->label("") ->orderColumn('order') ]) ])->visible(fn (Get $get):bool => $get("paragraph_type") == 1), Group::make()->schema([ FilamentLexicalEditor::make('text_content_tw') ->label('繁體中文內容') ->id(fn ($get) => "text_content_tw_" . $get('item_key') . "_" . uniqid()) ->enabledToolbars($editor_toolbar) ->columnSpanFull(), FilamentLexicalEditor::make('text_content_en') ->label('English Content') ->id(fn ($get) => "text_content_en_" . $get('item_key') . "_" . uniqid()) ->enabledToolbars($editor_toolbar) ->columnSpanFull(), ])->visible(fn (Get $get):bool => $get("paragraph_type") == 2), ]) ->mutateRelationshipDataBeforeFillUsing(function (array $data): array { if ($data['paragraph_type'] == 2 && !empty($data['text_content'])) { $content = is_string($data['text_content']) ? json_decode($data['text_content'], true) : $data['text_content']; if (is_array($content)) { $data['text_content_tw'] = $content['zh_TW'] ?? ''; $data['text_content_en'] = $content['en'] ?? ''; } } return $data; }) ->mutateRelationshipDataBeforeSaveUsing(function (array $data): array{ if ($data['paragraph_type'] == 2) { $data["text_content"] = ["zh_TW" => $data["text_content_tw"], "en" => $data["text_content_en"]]; // 移除臨時的分離欄位,只保留合併的 JSON 欄位 unset($data['text_content_tw']); unset($data['text_content_en']); } return $data; }) ->relationship("paragraphs") ->label("段落") ->collapsible() ->reorderableWithButtons() ->orderColumn('order') ->cloneable() ]) ])->columnSpanFull(), ]); } public static function table(Table $table): Table { return $table ->columns([ // TextColumn::make("newsCategory.name")->label("分類")->alignCenter(), TextColumn::make("title")->label("標題")->alignCenter(), TextColumn::make("written_by")->label("發佈者")->alignCenter(), TextColumn::make("post_date")->label("發佈時間")->dateTime('Y/m/d')->alignCenter(), ImageColumn::make("news_img_url")->label("列表圖")->alignCenter(), TextColumn::make("list_audit_state")->label("狀態")->badge() ->color(fn (string $state): string => match ($state) { '暫存' => 'warning', '已發佈' => 'success', }), IconColumn::make("on_top")->label("置頂") ->color(fn (string $state): string => match ($state) { 1 => 'success', default => '' }) ->icon(fn (string $state): string => match ($state) { 1 => 'heroicon-o-check-circle', default => '' }) ->action(function ($record): void { $record->on_top = !$record->on_top; $record->save(); }), TextColumn::make("created_at")->label("建立時間")->dateTime('Y/m/d H:i:s')->alignCenter(), TextColumn::make("updated_at")->label("更新時間")->dateTime()->alignCenter(), ]) ->filters([ SelectFilter::make('news_category_id')->label("分類") ->options(NewsCategory::orderBy("order")->pluck("name", "id")) ->attribute('news_category_id'), SelectFilter::make('post_date')->label("年份") ->options(News::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('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): void { $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\ListNews::route('/'), 'create' => Pages\CreateNews::route('/create'), 'edit' => Pages\EditNews::route('/{record}/edit'), ]; } }