| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
-
- namespace App\Filament\Resources;
-
- use App\Filament\Resources\NewsResource\Pages;
- use App\Models\News;
- use App\Models\NewsCategory;
- 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\Repeater;
- use Filament\Forms\Components\RichEditor;
- use Filament\Forms\Components\Section;
- use Filament\Forms\Components\Select;
- use Filament\Forms\Components\Tabs;
- use Filament\Forms\Components\Tabs\Tab;
- use Filament\Forms\Components\Textarea;
- use Filament\Forms\Components\TextInput;
- use Filament\Forms\Form;
- use Filament\Forms\Get;
- use Filament\Resources\Resource;
- use Filament\Tables;
- use Filament\Tables\Columns\IconColumn;
- 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\Str;
- use Malzariey\FilamentLexicalEditor\Enums\ToolbarItem;
- use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
-
- class NewsResource extends Resource
- {
- protected static ?string $model = News::class;
-
- protected static ?string $navigationIcon = 'heroicon-o-newspaper';
-
- protected static ?string $navigationLabel = '最新消息管理';
-
- protected static ?string $navigationGroup = '最新消息';
-
- protected static ?string $modelLabel = '最新消息管理';
-
- protected static ?int $navigationSort = 3;
-
- public static function form(Form $form): Form
- {
- $editor_toolbar = [
- ToolbarItem::UNDO, ToolbarItem::REDO, ToolbarItem::NORMAL,
- ToolbarItem::H2, ToolbarItem::H3, ToolbarItem::H4, ToolbarItem::H5,
- ToolbarItem::BULLET, ToolbarItem::NUMBERED, ToolbarItem::FONT_SIZE,
- ToolbarItem::BOLD, ToolbarItem::ITALIC, ToolbarItem::UNDERLINE,
- ToolbarItem::LINK, ToolbarItem::TEXT_COLOR, ToolbarItem::BACKGROUND_COLOR,
- ToolbarItem::SUBSCRIPT, ToolbarItem::LOWERCASE, ToolbarItem::DIVIDER,
- ToolbarItem::UPPERCASE, ToolbarItem::CLEAR, ToolbarItem::HR,
- ];
-
- return $form
- ->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),
- Translate::make()->schema(fn(string $locale) => [
- TextInput::make('title')->required($locale == 'zh_TW')->label('標題')->columnSpan(1),
- TextInput::make('written_by')->required($locale == 'zh_TW')->label('撰文者')->columnSpan(1),
- Textarea::make('description')->required($locale == 'zh_TW')->label('簡述')->columnSpan(2),
- ])
- ->locales(['zh_TW', 'en'])
- ->actions([
- app(DeepLService::class)->createTranslationAction('Main', ['title', 'written_by', 'description']),
- ])->columns(2)
- ->columnSpan(2)
- ->id('Main-content'),
- FileUpload::make('news_img')->label('圖片')
- ->directory('news')
- ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor()
- ->columnSpan(2),
- Translate::make()->schema(fn(string $locale) => [
- TextInput::make('news_img_alt')->label('圖片註釋'),
- ])
- ->locales(['zh_TW', 'en'])
- ->actions([
- app(DeepLService::class)->createTranslationAction('NewsImageAlt', ['news_img_alt']),
- ])->columnSpan(2)
- ->id('Main-alt'),
- ])->columns(2),
- Tab::make('SEO')->schema([
- Translate::make()->schema(fn(string $locale) => [
- TextInput::make('meta_title')->label('SEO 標題'),
- TextInput::make('meta_keyword')->label('SEO 關鍵字'),
- Textarea::make('meta_description')->label('SEO 簡述'),
- ])
- ->locales(['zh_TW', 'en'])
- ->actions([
- app(DeepLService::class)->createTranslationAction('Seo', ['meta_title', 'meta_keyword', 'meta_description']),
- ])->columnSpanFull(),
- FileUpload::make('meta_img')->label('放大預覽圖')
- ->directory('news/seo')
- ->columnSpan(1)
- ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor()
- ->id('Seo'),
- ]),
- 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([
- FileUpload::make('img_url')->label('')->directory('news/paragraphs')
- ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor(),
- Translate::make()->schema(fn(string $locale) => [
- TextInput::make('img_alt')->label('圖片註釋'),
- ])
- ->locales(['zh_TW', 'en'])
- ->actions([
- app(DeepLService::class)->createTranslationAction('ParagraphImgAlt', ['img_alt']),
- ])->columnSpan(1)
- ->id(fn($get) => 'para_img_' . $get('item_key')),
- ]),
- ])->visible(fn(Get $get): bool => $get('paragraph_type') == 1),
- Group::make()->schema([
- Translate::make()->schema(fn(string $locale) => [
- RichEditor::make('text_content')
- ->toolbarButtons([
- 'blockquote',
- 'bold',
- 'bulletList',
- 'h2',
- 'h3',
- 'italic',
- 'link',
- 'orderedList',
- 'redo',
- 'strike',
- 'underline',
- 'undo',
- ])
- ->disableToolbarButtons([
- 'blockquote',
- 'strike',
- 'attachFiles',
- ])
- ->fileAttachmentsDisk('s3')
- ->fileAttachmentsDirectory('attachments')
- ->fileAttachmentsVisibility('private'),
- ])
- ->locales(['zh_TW', 'en'])
- ->actions([
- app(DeepLService::class)->createTranslationAction('ParagraphText', ['text_content']),
- ])->columnSpan(1)
- ->id(fn($get) => 'para_text_' . $get('item_key')),
- ])->visible(fn(Get $get): bool => $get('paragraph_type') == 2),
- ])
- ->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')->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(),
- ]),
- ])
- ->reorderable('order')
- ->defaultSort('order');
- }
-
- 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'),
- ];
- }
- }
|