ProfilePartResource.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. ->maxImageWidth(1920)
  52. ->directory('profile-parts')
  53. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor()
  54. ->columnSpanFull(),
  55. Translate::make()->schema(fn (string $locale) => [
  56. TextInput::make('img_alt')->label('圖片註釋')->columnSpanFull(),
  57. ])
  58. ->locales(['zh_TW', 'en'])
  59. ->actions([
  60. app(DeepLService::class)->createTranslationAction('Img', ['img_alt']),
  61. ])
  62. ->id('Img')
  63. ->columnSpanFull(),
  64. ])->columnSpanFull(),
  65. ])->columns(3),
  66. ]);
  67. }
  68. public static function table(Table $table): Table
  69. {
  70. return $table
  71. ->columns([
  72. TextColumn::make('title')->label('標題'),
  73. ImageColumn::make('img_url')->alignCenter(),
  74. ])
  75. ->filters([
  76. //
  77. ])
  78. ->actions([
  79. Tables\Actions\EditAction::make(),
  80. ])
  81. ->bulkActions([
  82. Tables\Actions\BulkActionGroup::make([
  83. Tables\Actions\DeleteBulkAction::make(),
  84. ]),
  85. ])
  86. ->reorderable('order')
  87. ->defaultSort('order');
  88. }
  89. public static function getRelations(): array
  90. {
  91. return [
  92. //
  93. ];
  94. }
  95. public static function getPages(): array
  96. {
  97. return [
  98. 'index' => Pages\ListProfileParts::route('/'),
  99. 'create' => Pages\CreateProfilePart::route('/create'),
  100. 'edit' => Pages\EditProfilePart::route('/{record}/edit'),
  101. ];
  102. }
  103. }