ProfilePartResource.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Filament\Resources;
  3. use AbdulmajeedJamaan\FilamentTranslatableTabs\TranslatableTabs;
  4. use App\Filament\Resources\ProfilePartResource\Pages;
  5. use App\Filament\Resources\ProfilePartResource\RelationManagers;
  6. use App\Models\ProfilePart;
  7. use Filament\Forms;
  8. use Filament\Forms\Components\FileUpload;
  9. use Filament\Forms\Components\Group;
  10. use Filament\Forms\Components\Section;
  11. use Filament\Forms\Components\Textarea;
  12. use Filament\Forms\Components\TextInput;
  13. use Filament\Forms\Form;
  14. use Filament\Resources\Resource;
  15. use Filament\Tables;
  16. use Filament\Tables\Columns\ImageColumn;
  17. use Filament\Tables\Columns\TextColumn;
  18. use Filament\Tables\Table;
  19. use Illuminate\Database\Eloquent\Builder;
  20. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  21. use App\Service\DeepLService;
  22. class ProfilePartResource extends Resource
  23. {
  24. protected static ?string $model = ProfilePart::class;
  25. protected static ?string $navigationIcon = 'heroicon-o-document';
  26. protected static ?string $navigationLabel = "宏匯簡介管理";
  27. protected static ?string $navigationGroup = '關於宏匯';
  28. protected static ?string $modelLabel = "宏匯簡介管理";
  29. protected static ?int $navigationSort = 2;
  30. // 預設排序
  31. protected static function booted()
  32. {
  33. static::addGlobalScope('ordered', function ($query) {
  34. $query->orderBy('order')->orderBy('id');
  35. });
  36. }
  37. public static function form(Form $form): Form
  38. {
  39. return $form
  40. ->schema([
  41. Section::make("")->schema([
  42. Translate::make()->schema(fn (string $locale) => [
  43. TextInput::make("title")->label("標題")->columnSpanFull(),
  44. Textarea::make("content")->label("內文")->columnSpanFull()
  45. ])
  46. ->locales(["zh_TW", "en"])
  47. ->actions([
  48. app(DeepLService::class)->createTranslationAction("Main", ["title", "content"])
  49. ])
  50. ->id("Main")->columnSpanFull(),
  51. Group::make()->schema([
  52. FileUpload::make("img_url")->label("圖片")
  53. ->directory("profile-parts")
  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. ])->reorderable('order');
  86. }
  87. public static function getRelations(): array
  88. {
  89. return [
  90. //
  91. ];
  92. }
  93. public static function getPages(): array
  94. {
  95. return [
  96. 'index' => Pages\ListProfileParts::route('/'),
  97. 'create' => Pages\CreateProfilePart::route('/create'),
  98. 'edit' => Pages\EditProfilePart::route('/{record}/edit'),
  99. ];
  100. }
  101. }