| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- <?php
-
- namespace App\Filament\Resources;
-
- use App\Filament\Resources\EsgResource\Pages;
- use App\Models\Esg;
- use Filament\Forms\Components\Fieldset;
- 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 Malzariey\FilamentLexicalEditor\Enums\ToolbarItem;
- use Malzariey\FilamentLexicalEditor\FilamentLexicalEditor;
- 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
- {
- $editor_toolbar= [
- ToolbarItem::UNDO, ToolbarItem::REDO, ToolbarItem::NORMAL,
- ToolbarItem::H2, ToolbarItem::H3, ToolbarItem::H4, ToolbarItem::H5,
- ToolbarItem::BULLET, ToolbarItem::NUMBERED, ToolbarItem::FONT_SIZE,
- ToolbarItem::BOLD, ToolbarItem::ITALIC, ToolbarItem::UNDERLINE,
- ToolbarItem::LINK, ToolbarItem::TEXT_COLOR, ToolbarItem::BACKGROUND_COLOR,
- ToolbarItem::SUBSCRIPT, ToolbarItem::LOWERCASE, ToolbarItem::DIVIDER,
- ToolbarItem::UPPERCASE, ToolbarItem::CLEAR, ToolbarItem::HR
- ];
- return $form
- ->schema([
- //
- Tabs::make("")->tabs([
- Tab::make('基本資訊')->schema([
- TextInput::make('keyword')->label("關聯字詞")->required()
- ->validationMessages([
- 'required' => '請填寫關聯字詞',
- ]),
- Translate::make()->schema(fn (string $locale) => [
- TextInput::make('title')
- ->label("標題")
- ->columnSpan(1)
- ->required($locale == "zh_TW")
- ->validationMessages([
- 'required' => '請填寫標題',
- ])
- ])->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")->required()
- ->validationMessages([
- 'required' => '請上傳圖片',
- ]),
- FileUpload::make('banner_mobile')->label("Banner (Mobile)")
- ->disk("public")
- // ->helperText('建議寬高限制為:600*896px,檔案大小限制為1M以下')->maxSize('1024')
- ->directory("esg")->required()
- ->validationMessages([
- 'required' => '請上傳圖片',
- ]),
- Translate::make()->schema(fn (string $locale) => [
- TextInput::make('banner_alt')
- ->label("Banner 圖片註釋")
- ->required($locale == "zh_TW")
- ->validationMessages([
- 'required' => '請填寫圖片註釋',
- ]),
- Textarea::make("description")->rows(5)->columnSpanFull()->label("短文"),
- ])->locales(["zh_TW", "en"])
- ->columnSpanFull(),
- ]),
- Tab::make("段落設計")->schema([
- //1: 純文字 2:區塊文字 3:表格 4:影片 or 圖片 5:左右圖文
- Repeater::make("paragraphs")->label("")->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 => "圖片",
- 5 => "影片",
- ])->label("")->default(1)->Live(),
- Group::make()->schema([
- Section::make('純文字設定')->schema([
- FilamentLexicalEditor::make('content.text_content_tw')
- ->label('中文')
- ->id(fn ($get) => "text_content_tw_" . $get('item_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->required()
- ->columnSpanFull(),
- FilamentLexicalEditor::make('content.text_content_en')
- ->label('English')
- ->id(fn ($get) => "text_content_en_" . $get('item_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->columnSpanFull(),
- ]),
- ])->visible(fn (Get $get):bool => $get("type") == 1),
- Group::make()->schema([
- Section::make('區塊文字設定')->schema([
- // 文字區塊重複器
- 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());
- }
- }),
- FilamentLexicalEditor::make('block_content_tw')
- ->label('繁體中文內容')
- ->id(fn ($get) => "block_content_tw_" . $get('block_item_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->required()
- ->live(onBlur: true)
- ->columnSpanFull(),
- FilamentLexicalEditor::make('block_content_en')
- ->label('English Content')
- ->id(fn ($get) => "block_content_en_" . $get('block_item_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->columnSpanFull(),
- ])
- ->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('0')
- ->inline(),
- // 表格資料
- Placeholder::make('table_builder_info')->label("")->content('請先設定表格欄數,然後填入表格資料'),
- Grid::make(3)->schema([
- Radio::make('content.column_count')
- ->label('欄數')
- ->options([
- 2 => '2欄', // 改為數字鍵值
- 3 => '3欄',
- 4 => '4欄'
- ])
- ->default(2)
- ->inline()
- ->live()
- ->reactive(), // ✅ 添加 reactive()
- ]),
- Translate::make()->schema(fn (string $locale) => [
- Grid::make(2) // 改用 Grid 來支援動態 columns
- ->schema([
- TextInput::make('content.head1')->label('標題第1欄')->required()
- ->validationMessages([
- 'required' => '請填寫',
- ]),
- Radio::make('content.head_align1')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1),
- TextInput::make('content.head2')->label('標題第2欄')->required()
- ->validationMessages([
- '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());
- }
- }),
- Grid::make(1) // 改用 Grid 來支援動態 columns
- ->schema([
- Section::make("第一欄")->schema([
- Fieldset::make('中文')->schema([
- Radio::make('align1_tw')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1)->inline(),
- FilamentLexicalEditor::make('col1_tw')
- ->id(fn ($get) => "col1_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->required()
- ->columnSpanFull(),
- ]),
- Fieldset::make('英文')->schema([
- Radio::make('align1_en')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1)->inline(),
- FilamentLexicalEditor::make('col1_en')
- ->id(fn ($get) => "col1_en_" . $get('simple_table_rows_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->columnSpanFull(),
- ]),
- ]),
- Section::make("第二欄")->schema([
- Fieldset::make('中文')->schema([
- Radio::make('align2_tw')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1)->inline(),
- FilamentLexicalEditor::make('col2_tw')
- ->id(fn ($get) => "col2_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->required()
- ->columnSpanFull(),
- ]),
- Fieldset::make('英文')->schema([
- Radio::make('align2_en')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1)->inline(),
- FilamentLexicalEditor::make('col2_en')
- ->id(fn ($get) => "col2_en_" . $get('simple_table_rows_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->columnSpanFull(),
- ]),
- ]),
- Section::make("第三欄")->schema([
- Fieldset::make('中文')->schema([
- Radio::make('align3_tw')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1),
- FilamentLexicalEditor::make('col3_tw')
- ->id(fn ($get) => "col3_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->required()
- ->columnSpanFull(),
- ]),
- Fieldset::make('英文')->schema([
- Radio::make('align3_en')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1)->inline(),
- FilamentLexicalEditor::make('col3_en')
- ->id(fn ($get) => "col3_en_" . $get('simple_table_rows_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->columnSpanFull(),
- ]),
- ])->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 3),
- Section::make("第四欄")->schema([
- Fieldset::make('中文')->schema([
- Radio::make('align4_tw')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1)->inline(),
- FilamentLexicalEditor::make('col4_tw')
- ->id(fn ($get) => "col4_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->required()
- ->columnSpanFull(),
- ]),
- Fieldset::make('英文')->schema([
- Radio::make('align4_en')
- ->label('對齊')
- ->options([
- 1 => '置左', // 改為數字鍵值
- 2 => '置中',
- 3 => '置右'
- ])
- ->default(1)->inline(),
- FilamentLexicalEditor::make('col4_en')
- ->id(fn ($get) => "col4_en_" . $get('simple_table_rows_key') . "_" . uniqid())
- ->enabledToolbars($editor_toolbar)
- ->columnSpanFull(),
- ]),
- ])->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 4),
- ])
- ->reactive() // 加入響應式
- ])
- ->id(fn ($get) => "simple_table_" . $get('simple_table_rows_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)
- ->required()
- ->validationMessages([
- 'required' => '請上傳圖片',
- ]),
- ])
- ->addActionLabel('新增')
- ->label("")
- ->orderColumn('order')
- ])
- ])->visible(fn (Get $get):bool => $get("type") == 4),
- Group::make()->schema([
- Section::make("")->schema([
- FileUpload::make('content.video_img')->label("影片底圖")
- ->disk("public")
- ->directory("esg/paragraphVideo"),
- TextInput::make('content.link')->label("網址")->required()
- ->validationMessages([
- 'required' => '請附上連結',
- ]),
- Translate::make()->schema(fn (string $locale) => [
- TextInput::make('content.video_alt')->label("影片註文"),
- ])->locales(["zh_TW", "en"])
- ]),
- ])->visible(fn (Get $get):bool => $get("type") == 5),
- ])
- ->relationship("paragraphs")
- ->addActionLabel('新增段落')
- ->collapsible()
- ->reorderableWithButtons()
- ->orderColumn('order')
- ->cloneable()
- ->mutateRelationshipDataBeforeFillUsing(function (array $data): array {
- switch($data["type"]){
- case "1":
- if (!empty($data["content"]['text_content'])) {
- $content = is_string($data["content"]['text_content'])
- ? json_decode($data["content"]['text_content'], true)
- : $data["content"]['text_content'];
-
- if (is_array($content)) {
- $data["content"]['text_content_tw'] = $content['zh_TW'] ?? '';
- $data["content"]['text_content_en'] = $content['en'] ?? '';
- }
- }
- break;
- case "2":
- if (!empty($data["content"]['text_blocks']) && count($data["content"]['text_blocks']) > 0) {
- foreach ($data["content"]['text_blocks'] as $block_index => $block){
- $content = is_string($block['block_content'])
- ? json_decode($block['block_content'], true)
- : $block['block_content'];
-
- if (is_array($content)) {
- $data["content"]['text_blocks'][$block_index]["block_content_tw"] = $content['zh_TW'] ?? '';
- $data["content"]['text_blocks'][$block_index]["block_content_en"] = $content['en'] ?? '';
- }
- }
- }
- break;
- case "3":
- if (!empty($data["content"]['simple_table_rows']) && count($data["content"]['simple_table_rows']) > 0) {
- foreach ($data["content"]['simple_table_rows'] as $row_key => $row) {
- $flatRow = [];
-
- foreach ($row as $key => $value) {
- if (is_array($value) && isset($value['zh_TW'], $value['en'])) {
- // 多语言字段,分离为两个字段
- $flatRow[$key . '_tw'] = $value['zh_TW'];
- $flatRow[$key . '_en'] = $value['en'];
- } else {
- // 非多语言字段,保持原样
- $flatRow[$key] = $value;
- }
- }
-
- $data["content"]['simple_table_rows'][$row_key] = $flatRow;
- }
- }
- break;
- }
- return $data;
- })
- ->mutateRelationshipDataBeforeSaveUsing(function (array $data): array {
- switch($data["type"]){
- case "1":
- $data["content"]["text_content"] = ["zh_TW" => $data["content"]["text_content_tw"] ?? "" , "en" => $data["content"]["text_content_en"] ?? ""];
- // 移除臨時的分離欄位,只保留合併的 JSON 欄位
- unset($data["content"]["text_content_tw"]);
- unset($data["content"]["text_content_en"]);
-
- // 清理 HTML 標籤(如果需要)
- foreach ($data['content']['text_content'] as $locale => $content) {
- $data['content']['text_content'][$locale] = stripslashes($content);
- }
- break;
- case "2":
- // 確保有 text_blocks
- if (!isset($data['content']['text_blocks'])) {
- $data['content']['text_blocks'] = [];
- }
-
- // 處理每個文字區塊
- foreach ($data['content']['text_blocks'] as $index => &$block) {
- // 確保區塊有必要的欄位
- $block['block_item_key'] = $block['block_item_key'] ?? Str::random();
- $block['order'] = $block['order'] ?? ($index + 1);
-
- $block["block_content"] = ["zh_TW" => $block["block_content_tw"] ?? "" , "en" => $block["block_content_en"] ?? ""];
- // 移除臨時的分離欄位,只保留合併的 JSON 欄位
- unset($block["block_content_tw"]);
- unset($block["block_content_en"]);
- // 清理區塊內容
- if (isset($block['block_content'])) {
- foreach ($block['block_content'] as $locale => $content) {
- $block['block_content'][$locale] = stripslashes($content);
- }
- }
- }
- break;
- case "3":
- // 確保欄數設定
- $columnCount = $data['content']['column_count'] ?? 2;
-
- // 確保表頭存在
- if (!isset($data['content']['headers'])) {
- $data['content']['headers'] = ['zh_TW' => [], 'en' => []];
- }
-
- // 確保表格資料存在
- if (!isset($data['content']['simple_table_rows'])) {
- $data['content']['simple_table_rows'] = [];
- }
-
- // 處理表格資料,確保欄數一致
- foreach ($data['content']['simple_table_rows'] as $rowIndex => &$row) {
- // 確保每行都有正確的欄數
- for ($i = 1; $i <= $columnCount; $i++) {
- $row["col{$i}"] = ["zh_TW" => $row["col{$i}_tw"] ?? "" , "en" => $row["col{$i}_en"] ?? ""];
- foreach ($row["col{$i}"] as $locale => $content) {
- $row["col{$i}"][$locale] = $content;
- }
- unset($row["col{$i}_tw"]);
- unset($row["col{$i}_en"]);
- $row["align{$i}"] = ["zh_TW" => $row["align{$i}_tw"] ?? "" , "en" => $row["align{$i}_en"] ?? ""];
- foreach ($row["align{$i}"] as $locale => $content) {
- $row["align{$i}"][$locale] = $content;
- }
- unset($row["align{$i}_tw"]);
- unset($row["align{$i}_en"]);
- }
-
- // 移除多餘的欄位
- for ($i = $columnCount + 1; $i <= 4; $i++) {
- unset($row["col{$i}_tw"]);
- unset($row["col{$i}_en"]);
- unset($row["align{$i}_tw"]);
- unset($row["align{$i}_en"]);
- }
- }
- break;
- }
- return $data;
- })
- ->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
- switch($data["type"]){
- case "1":
- $data["content"]["text_content"] = ["zh_TW" => $data["content"]["text_content_tw"] ?? "" , "en" => $data["content"]["text_content_en"] ?? ""];
- // 移除臨時的分離欄位,只保留合併的 JSON 欄位
- unset($data["content"]["text_content_tw"]);
- unset($data["content"]["text_content_en"]);
-
- // 清理 HTML 標籤(如果需要)
- foreach ($data['content']['text_content'] as $locale => $content) {
- $data['content']['text_content'][$locale] = stripslashes($content);
- }
- break;
- case "2":
- // 確保有 text_blocks
- if (!isset($data['content']['text_blocks'])) {
- $data['content']['text_blocks'] = [];
- }
-
- // 處理每個文字區塊
- foreach ($data['content']['text_blocks'] as $index => &$block) {
- // 確保區塊有必要的欄位
- $block['block_item_key'] = $block['block_item_key'] ?? Str::random();
- $block['order'] = $block['order'] ?? ($index + 1);
-
- $block["block_content"] = ["zh_TW" => $block["block_content_tw"] ?? "" , "en" => $block["block_content_en"] ?? ""];
- // 移除臨時的分離欄位,只保留合併的 JSON 欄位
- unset($block["block_content_tw"]);
- unset($block["block_content_en"]);
- // 清理區塊內容
- if (isset($block['block_content'])) {
- foreach ($block['block_content'] as $locale => $content) {
- $block['block_content'][$locale] = stripslashes($content);
- }
- }
- }
- break;
- case "3":
- // 確保欄數設定
- $columnCount = $data['content']['column_count'] ?? 2;
-
- // 確保表頭存在
- if (!isset($data['content']['headers'])) {
- $data['content']['headers'] = ['zh_TW' => [], 'en' => []];
- }
-
- // 確保表格資料存在
- if (!isset($data['content']['simple_table_rows'])) {
- $data['content']['simple_table_rows'] = [];
- }
-
- // 處理表格資料,確保欄數一致
- foreach ($data['content']['simple_table_rows'] as $rowIndex => &$row) {
- // 確保每行都有正確的欄數
- for ($i = 1; $i <= $columnCount; $i++) {
- $row["col{$i}"] = ["zh_TW" => $row["col{$i}_tw"] ?? "" , "en" => $row["col{$i}_en"] ?? ""];
- foreach ($row["col{$i}"] as $locale => $content) {
- $row["col{$i}"][$locale] = $content;
- }
- unset($row["col{$i}_tw"]);
- unset($row["col{$i}_en"]);
- $row["align{$i}"] = ["zh_TW" => $row["align{$i}_tw"] ?? "" , "en" => $row["align{$i}_en"] ?? ""];
- foreach ($row["align{$i}"] as $locale => $content) {
- $row["align{$i}"][$locale] = $content;
- }
- unset($row["align{$i}_tw"]);
- unset($row["align{$i}_en"]);
- }
-
- // 移除多餘的欄位
- for ($i = $columnCount + 1; $i <= 4; $i++) {
- unset($row["col{$i}_tw"]);
- unset($row["col{$i}_en"]);
- unset($row["align{$i}_tw"]);
- unset($row["align{$i}_en"]);
- }
- }
- break;
- }
- \Log::info($data);
- return $data;
- })
- ])->columnSpanFull(),
- ])
- ->columnSpanFull()
- ]);
- }
-
- public static function table(Table $table): Table
- {
- return $table
- ->columns([
- //
- TextColumn::make("title")->label("標題"),
- ])
- ->filters([
- //
- ])
- ->actions([
- Tables\Actions\EditAction::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'),
- ];
- }
- }
|