ProfilePartResource.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\ProfilePartResource\Pages;
  4. use App\Models\ProfilePart;
  5. use App\Service\DeepLService;
  6. use Filament\Forms\Components\FileUpload;
  7. use Filament\Forms\Components\Group;
  8. use Filament\Forms\Components\Section;
  9. use Filament\Forms\Components\Textarea;
  10. use Filament\Forms\Components\TextInput;
  11. use Filament\Forms\Form;
  12. use Filament\Resources\Resource;
  13. use Filament\Tables;
  14. use Filament\Tables\Columns\ImageColumn;
  15. use Filament\Tables\Columns\TextColumn;
  16. use Filament\Tables\Table;
  17. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  18. class ProfilePartResource extends Resource
  19. {
  20. protected static ?string $model = ProfilePart::class;
  21. protected static ?string $navigationIcon = 'heroicon-o-document';
  22. protected static ?string $navigationLabel = '宏匯簡介管理';
  23. protected static ?string $navigationGroup = '關於宏匯';
  24. protected static ?string $modelLabel = '宏匯簡介管理';
  25. protected static ?int $navigationSort = 2;
  26. // 預設排序
  27. protected static function booted()
  28. {
  29. static::addGlobalScope('ordered', function ($query) {
  30. $query->orderBy('order')->orderBy('id');
  31. });
  32. }
  33. public static function form(Form $form): Form
  34. {
  35. return $form
  36. ->schema([
  37. Section::make('')->schema([
  38. Translate::make()->schema(fn (string $locale) => [
  39. Textarea::make('title')->label('標題')->columnSpanFull(),
  40. Textarea::make('content')->label('內文')->columnSpanFull(),
  41. ])
  42. ->locales(['zh_TW', 'en'])
  43. ->actions([
  44. app(DeepLService::class)->createTranslationAction('Main', ['title', 'content']),
  45. ])
  46. ->id('Main')->columnSpanFull(),
  47. Group::make()->schema([
  48. FileUpload::make('img_url')->label('圖片')
  49. ->image()
  50. ->optimize('webp')
  51. ->directory('profile-parts')
  52. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor()
  53. ->columnSpanFull(),
  54. Translate::make()->schema(fn (string $locale) => [
  55. TextInput::make('img_alt')->label('圖片註釋')->columnSpanFull(),
  56. ])
  57. ->locales(['zh_TW', 'en'])
  58. ->actions([
  59. app(DeepLService::class)->createTranslationAction('Img', ['img_alt']),
  60. ])
  61. ->id('Img')
  62. ->columnSpanFull(),
  63. ])->columnSpanFull(),
  64. ])->columns(3),
  65. ]);
  66. }
  67. public static function table(Table $table): Table
  68. {
  69. return $table
  70. ->columns([
  71. TextColumn::make('title')->label('標題'),
  72. ImageColumn::make('img_url')->alignCenter(),
  73. ])
  74. ->filters([
  75. //
  76. ])
  77. ->actions([
  78. Tables\Actions\EditAction::make(),
  79. ])
  80. ->bulkActions([
  81. Tables\Actions\BulkActionGroup::make([
  82. Tables\Actions\DeleteBulkAction::make(),
  83. ]),
  84. ])
  85. ->reorderable('order')
  86. ->defaultSort('order');
  87. }
  88. public static function getRelations(): array
  89. {
  90. return [
  91. //
  92. ];
  93. }
  94. public static function getPages(): array
  95. {
  96. return [
  97. 'index' => Pages\ListProfileParts::route('/'),
  98. 'create' => Pages\CreateProfilePart::route('/create'),
  99. 'edit' => Pages\EditProfilePart::route('/{record}/edit'),
  100. ];
  101. }
  102. }