EsgMainPage.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Models\EsgMain;
  4. use Filament\Forms\Components\Actions\Action;
  5. use Filament\Forms\Components\FileUpload;
  6. use Filament\Forms\Components\Tabs;
  7. use Filament\Forms\Components\Tabs\Tab;
  8. use Filament\Forms\Components\Textarea;
  9. use Filament\Forms\Components\TextInput;
  10. use Filament\Forms\Concerns\InteractsWithForms;
  11. use Filament\Forms\Form;
  12. use Filament\Notifications\Notification;
  13. use Filament\Pages\Page;
  14. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  15. class EsgMainPage extends Page
  16. {
  17. use InteractsWithForms;
  18. protected static ?string $navigationIcon = 'heroicon-o-cog-6-tooth';
  19. protected static ?string $navigationLabel = 'ESG 主要設定';
  20. protected static ?string $title = 'ESG 主要設定';
  21. protected static string $view = 'filament.pages.esg-main';
  22. protected static ?string $navigationGroup = 'ESG 上稿內容管理';
  23. protected static ?int $navigationSort = 99;
  24. public ?array $data = [];
  25. protected ?EsgMain $esgMain = null;
  26. public function mount(): void
  27. {
  28. $this->esgMain = EsgMain::getMain();
  29. $esgMainArray = $this->esgMain->safeToArray();
  30. \Log::info("輸出", $esgMainArray);
  31. $this->form->fill($esgMainArray);
  32. }
  33. public function form(Form $form): Form
  34. {
  35. return $form
  36. ->schema([
  37. Tabs::make('設定')->tabs([
  38. Tab::make('基本資訊')->schema([
  39. Translate::make()->schema(fn (string $locale) => [
  40. TextInput::make('title')
  41. ->label("標題"),
  42. Textarea::make('description')
  43. ])->locales(["zh_TW", "en"])
  44. ->columnSpanFull(),
  45. FileUpload::make('banner_pc')->label("Banner (PC)")
  46. ->disk("public")
  47. ->directory("esg"),
  48. FileUpload::make('banner_mobile')->label("Banner (Mobile)")
  49. ->disk("public")
  50. ->directory("esg"),
  51. ]),
  52. Tab::make('SEO 設定')->schema([
  53. Translate::make()->schema(fn (string $locale) => [
  54. TextInput::make('meta_title'),
  55. TextInput::make('meta_keyword'),
  56. Textarea::make('meta_description'),
  57. ])->locales(["zh_TW", "en"])
  58. ->columnSpanFull(),
  59. FileUpload::make('og_img')
  60. ->disk("public")
  61. ->directory("esg"),
  62. ]),
  63. ])
  64. ])
  65. ->statePath('data');
  66. }
  67. public function save(): void
  68. {
  69. try {
  70. $data = $this->form->getState();
  71. $settings = EsgMain::getSettings();
  72. $settings->update($data);
  73. Notification::make()
  74. ->title('設定已儲存')
  75. ->success()
  76. ->send();
  77. } catch (\Exception $e) {
  78. Notification::make()
  79. ->title('儲存失敗')
  80. ->body($e->getMessage())
  81. ->danger()
  82. ->send();
  83. }
  84. }
  85. protected function getFormActions(): array
  86. {
  87. return [
  88. Action::make('save')
  89. ->label('儲存設定')
  90. ->action('save')
  91. ->color('primary'),
  92. ];
  93. }
  94. }