ProfilePartResource.php 3.8KB

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