123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- <?php
-
- namespace App\Filament\Resources;
-
- use App\Filament\Resources\EsgResource\Pages;
- use App\Models\Esg;
- use Filament\Forms\Components\FileUpload;
- use Filament\Forms\Components\Grid;
- use Filament\Forms\Components\Group;
- use Filament\Forms\Components\Placeholder;
- use Filament\Forms\Components\Radio;
- use Filament\Forms\Components\Repeater;
- use Filament\Forms\Components\RichEditor;
- use Filament\Forms\Components\Section;
- use Filament\Forms\Components\Select;
- use Filament\Forms\Components\Tabs;
- use Filament\Forms\Components\Tabs\Tab;
- use Filament\Forms\Components\Textarea;
- use Filament\Forms\Components\TextInput;
- use Filament\Forms\Components\Toggle;
- use Filament\Forms\Form;
- use Filament\Forms\Get;
- use Filament\Resources\Resource;
- use Filament\Tables;
- use Filament\Tables\Columns\TextColumn;
- use Filament\Tables\Table;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\SoftDeletingScope;
- use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
- use Str;
-
- class EsgResource extends Resource
- {
- protected static ?string $model = Esg::class;
-
- protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
- protected static ?string $modelLabel = "ESG 管理";
- protected static ?string $navigationGroup = 'ESG 上稿內容管理';
- protected static ?string $navigationLabel = "ESG 管理";
-
- public static function form(Form $form): Form
- {
- return $form
- ->schema([
- //
- Tabs::make("")->tabs([
- Tab::make('基本資訊')->schema([
- TextInput::make('keyword')->label("關聯字詞"),
- Translate::make()->schema(fn (string $locale) => [
- TextInput::make('title')
- ->label("標題")
- ->columnSpan(1),
- ])->locales(["zh_TW", "en"])
- ->columnSpanFull()->columns(3),
- ]),
- Tab::make('Banner 設定')->schema([
- FileUpload::make('banner_pc')->label("Banner (PC)")
- ->disk("public")
- // ->helperText('建議寬高限制為:1280*720px,檔案大小限制為1M以下')->maxSize('1024')
- ->directory("esg"),
- FileUpload::make('banner_mobile')->label("Banner (Mobile)")
- ->disk("public")
- // ->helperText('建議寬高限制為:600*896px,檔案大小限制為1M以下')->maxSize('1024')
- ->directory("esg"),
- Translate::make()->schema(fn (string $locale) => [
- TextInput::make('banner_alt')
- ->label("Banner 圖片註釋"),
- Textarea::make("description")->rows(5)->columnSpanFull()->label("短文"),
- ])->locales(["zh_TW", "en"])
- ->columnSpanFull(),
- ]),
- Tab::make("段落設計")->schema([
- //1: 純文字 2:區塊文字 3:表格 4:影片 or 圖片 5:左右圖文
- Repeater::make("paragraphs")->schema([
- TextInput::make('item_key')
- ->default(fn () => Str::random())
- ->hidden()
- ->afterStateHydrated(function (TextInput $component, $state) {
- if (empty($state)) {
- $component->state(Str::random());
- }
- }),
- Radio::make("type")->options([
- 1 => "純文字",
- 2 => "區塊文字",
- 3 => "表格",
- 4 => "圖片",
- ])->label("")->default(1)->Live(),
- Group::make()->schema([
- Translate::make()->schema(fn (string $locale) => [
- RichEditor::make('content.text_content')
- ->fileAttachmentsDirectory('attachments')
- ->fileAttachmentsVisibility('private')
- ->disableToolbarButtons(['attachFiles']),
- ])
- ->locales(["zh_TW", "en"])
- ->id(fn ($get) => "para_text_" . $get('item_key')),
- ])->visible(fn (Get $get):bool => $get("type") == 1),
- Group::make()->schema([
- Section::make('區塊文字設定')->schema([
- // // 佈局設定
- // Radio::make('layout_type')->label('佈局方式')->options([
- // 'vertical' => '垂直排列',
- // 'horizontal' => '水平排列',
- // 'grid' => '網格排列',
- // ])->default('vertical')->Live(),
- // 文字區塊重複器
- Repeater::make("content.text_blocks")->schema([
- TextInput::make('block_item_key')
- ->default(fn () => Str::random())
- ->hidden()
- ->afterStateHydrated(function (TextInput $component, $state) {
- if (empty($state)) {
- $component->state(Str::random());
- }
- }),
- // 多語系內容
- Translate::make()->schema(fn (string $locale) => [
- // TextInput::make('block_title')
- // ->label("區塊標題")
- // ->maxLength(200)
- // ->visible(fn (Get $get) => in_array($get('block_type'), ['title', 'subtitle', 'highlight'])),
- RichEditor::make('block_content')
- ->disableToolbarButtons(['attachFiles'])
- ->required(),
- ])
- ->locales(["zh_TW", "en"])
- ->id(fn ($get) => "text_block_" . $get('block_item_key')),
-
- ])
- ->label("")
- ->addActionLabel('增加區塊')
- ->orderColumn('order')
- ->reorderableWithButtons()
- ->cloneable()
- ->minItems(1)
- ->maxItems(20)
- ->defaultItems(1),
- ]),
- ])->visible(fn (Get $get):bool => $get('type') == 2),
- Group::make()->schema([
- Section::make('表格設定')->schema([
- Radio::make('content.is_card')
- ->label('手機板卡片顯示')
- ->options([
- 1 => '是', // 改為數字鍵值
- 0 => '否'
- ])
- ->default(2)
- ->inline(),
- // 表格基本資訊
- // Translate::make()->schema(fn (string $locale) => [
- // TextInput::make('content.table_title')
- // ->label("表格標題")
- // ->maxLength(200),
-
- // Textarea::make('content.table_description')
- // ->label("表格說明")
- // ->rows(3)
- // ->maxLength(500),
- // ])->locales(["zh_TW", "en"])
- // ->id(fn ($get) => "table_info_" . $get('item_key')),
- // 表格資料
- Placeholder::make('table_builder_info')
- ->content('請先設定表格欄數,然後填入表格資料'),
- Grid::make(3)->schema([
- Radio::make('content.column_count')
- ->label('欄數')
- ->options([
- 2 => '2欄', // 改為數字鍵值
- 3 => '3欄',
- 4 => '4欄'
- ])
- ->default(2)
- ->inline()
- ->live()
- ->reactive(), // ✅ 添加 reactive()
- // Radio::make('content.is_card')
- // ->label('是否卡片呈現')
- // ->options([
- // 1 => '是', // 改為數字鍵值
- // 0 => '否',
- // ])
- // ->default(1)
- // ->inline()
- // ->required()
- ]),
- Translate::make()->schema(fn (string $locale) => [
- Grid::make(2) // 改用 Grid 來支援動態 columns
- ->schema([
- TextInput::make('content.head1')->label('標題第1欄')->required(),
- Radio::make('content.head_align1')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1),
- TextInput::make('content.head2')->label('標題第2欄')->required(),
- Radio::make('content.head_align2')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1),
- TextInput::make('content.head3')->label('標題第3欄')
- ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
- Radio::make('content.head_align3')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1)
- ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
- TextInput::make('content.head4')->label('標題第4欄')
- ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
- Radio::make('content.head_align4')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1)
- ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
- ])
- ->reactive() // 加入響應式
- ])->locales(["zh_TW", "en"]),
- Repeater::make('content.simple_table_rows')
- ->label('表格資料')
- ->schema([
- TextInput::make('simple_table_rows_key')
- ->default(fn () => Str::random())
- ->hidden()
- ->afterStateHydrated(function (TextInput $component, $state) {
- if (empty($state)) {
- $component->state(Str::random());
- }
- }),
- Translate::make()->schema(fn (string $locale) => [
- Grid::make(1) // 改用 Grid 來支援動態 columns
- ->schema([
- Radio::make('align1')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1),
- RichEditor::make('col1')
- ->label('第1欄')
- ->disableToolbarButtons(['attachFiles']),
- // TextInput::make('col2')->label('第2欄')->required(),
- Radio::make('align2')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1),
- RichEditor::make('col2')
- ->label('第2欄')
- ->disableToolbarButtons(['attachFiles']),
- Radio::make('align3')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1)
- ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 3),
- RichEditor::make('col3')
- ->label('第3欄')
- ->disableToolbarButtons(['attachFiles'])
- ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 3),
- Radio::make('align4')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1)
- ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 4),
- RichEditor::make('col4')
- ->label('第4欄')
- ->disableToolbarButtons(['attachFiles'])
- ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 4),
- ])
- ->reactive() // 加入響應式
- ])->locales(["zh_TW", "en"]),
- ])
- ->id(fn ($get) => "simple_table_" . $get('simple_table_key'))
- ->addActionLabel('新增列')
- ->reorderableWithButtons()
- ->minItems(1)
- ]),
- ])->visible(fn (Get $get):bool => $get('type') == 3),
- Group::make()->schema([
- Section::make('')->schema([
- Repeater::make("content.multiple_images")->schema([
- TextInput::make('para_img_item_key')
- ->default(fn () => Str::random())
- ->hidden()
- ->afterStateHydrated(function (TextInput $component, $state) {
- if (empty($state)) {
- $component->state(Str::random());
- }
- }),
- Translate::make()->schema(fn (string $locale) => [
- TextInput::make('image_alt')->label("圖片註文"),
- ])->locales(["zh_TW", "en"])
- ->id(fn ($get) => "para_img_mul_" . $get('para_img_item_key')),
- FileUpload::make('image_url')->label("")->disk("public")
- // ->helperText('建議寬高限制為:1080*675px,檔案大小限制為1M以下')->maxSize('1024')
- ->directory("esg/paragraphPhoto")
- ->maxFiles(10),
- ])
- ->addActionLabel('新增')
- ->label("")
- ->orderColumn('order')
- ])
- ])->visible(fn (Get $get):bool => $get("type") == 4),
- ])
- ->relationship("paragraphs")
- ->addActionLabel('新增段落')
- ->collapsible()
- ->reorderableWithButtons()
- ->orderColumn('order')
- ->cloneable()
- ])->columnSpanFull(),
- ])
- ->columnSpanFull()
- ]);
- }
-
- public static function table(Table $table): Table
- {
- return $table
- ->columns([
- //
- TextColumn::make("title")->label("標題"),
- ])
- ->filters([
- //
- ])
- ->actions([
- Tables\Actions\EditAction::make(),
- ])
- ->bulkActions([
- Tables\Actions\BulkActionGroup::make([
- Tables\Actions\DeleteBulkAction::make(),
- ]),
- ]);
- }
-
- public static function getRelations(): array
- {
- return [
- //
- ];
- }
-
- public static function getPages(): array
- {
- return [
- 'index' => Pages\ListEsg::route('/'),
- 'create' => Pages\CreateEsg::route('/create'),
- 'edit' => Pages\EditEsg::route('/{record}/edit'),
- ];
- }
- }
|