EsgResource.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\EsgResource\Pages;
  4. use App\Models\Esg;
  5. use Filament\Forms\Components\FileUpload;
  6. use Filament\Forms\Components\Grid;
  7. use Filament\Forms\Components\Group;
  8. use Filament\Forms\Components\Placeholder;
  9. use Filament\Forms\Components\Radio;
  10. use Filament\Forms\Components\Repeater;
  11. use Filament\Forms\Components\RichEditor;
  12. use Filament\Forms\Components\Section;
  13. use Filament\Forms\Components\Select;
  14. use Filament\Forms\Components\Tabs;
  15. use Filament\Forms\Components\Tabs\Tab;
  16. use Filament\Forms\Components\Textarea;
  17. use Filament\Forms\Components\TextInput;
  18. use Filament\Forms\Components\Toggle;
  19. use Filament\Forms\Form;
  20. use Filament\Forms\Get;
  21. use Filament\Resources\Resource;
  22. use Filament\Tables;
  23. use Filament\Tables\Columns\TextColumn;
  24. use Filament\Tables\Table;
  25. use Illuminate\Database\Eloquent\Builder;
  26. use Illuminate\Database\Eloquent\SoftDeletingScope;
  27. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  28. use Str;
  29. class EsgResource extends Resource
  30. {
  31. protected static ?string $model = Esg::class;
  32. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  33. protected static ?string $modelLabel = "ESG 管理";
  34. protected static ?string $navigationGroup = 'ESG 上稿內容管理';
  35. protected static ?string $navigationLabel = "ESG 管理";
  36. public static function form(Form $form): Form
  37. {
  38. return $form
  39. ->schema([
  40. //
  41. Tabs::make("")->tabs([
  42. Tab::make('基本資訊')->schema([
  43. TextInput::make('keyword')->label("關聯字詞")->required(),
  44. Translate::make()->schema(fn (string $locale) => [
  45. TextInput::make('title')
  46. ->label("標題")
  47. ->columnSpan(1)
  48. ->required($locale == "zh_TW")
  49. ->validationMessages([
  50. 'required' => '請填寫標題',
  51. ])
  52. ])->locales(["zh_TW", "en"])
  53. ->columnSpanFull()->columns(3),
  54. ]),
  55. Tab::make('Banner 設定')->schema([
  56. FileUpload::make('banner_pc')->label("Banner (PC)")
  57. ->disk("public")
  58. // ->helperText('建議寬高限制為:1280*720px,檔案大小限制為1M以下')->maxSize('1024')
  59. ->directory("esg")->required()
  60. ->validationMessages([
  61. 'required' => '請上傳圖片',
  62. ]),
  63. FileUpload::make('banner_mobile')->label("Banner (Mobile)")
  64. ->disk("public")
  65. // ->helperText('建議寬高限制為:600*896px,檔案大小限制為1M以下')->maxSize('1024')
  66. ->directory("esg")->required()
  67. ->validationMessages([
  68. 'required' => '請上傳圖片',
  69. ]),
  70. Translate::make()->schema(fn (string $locale) => [
  71. TextInput::make('banner_alt')
  72. ->label("Banner 圖片註釋")
  73. ->required($locale == "zh_TW")
  74. ->validationMessages([
  75. 'required' => '請填寫圖片註釋',
  76. ]),
  77. Textarea::make("description")->rows(5)->columnSpanFull()->label("短文"),
  78. ])->locales(["zh_TW", "en"])
  79. ->columnSpanFull(),
  80. ]),
  81. Tab::make("段落設計")->schema([
  82. //1: 純文字 2:區塊文字 3:表格 4:影片 or 圖片 5:左右圖文
  83. Repeater::make("paragraphs")->schema([
  84. TextInput::make('item_key')
  85. ->default(fn () => Str::random())
  86. ->hidden()
  87. ->afterStateHydrated(function (TextInput $component, $state) {
  88. if (empty($state)) {
  89. $component->state(Str::random());
  90. }
  91. }),
  92. Radio::make("type")->options([
  93. 1 => "純文字",
  94. 2 => "區塊文字",
  95. 3 => "表格",
  96. 4 => "圖片",
  97. 5 => "影片",
  98. ])->label("")->default(1)->Live(),
  99. Group::make()->schema([
  100. Translate::make()->schema(fn (string $locale) => [
  101. RichEditor::make('content.text_content')
  102. ->fileAttachmentsDirectory('attachments')
  103. ->fileAttachmentsVisibility('private')
  104. ->disableToolbarButtons(['attachFiles'])
  105. ->required($locale == "zh_TW")
  106. ->validationMessages([
  107. 'required' => '請填寫文字內容',
  108. ]),
  109. ])
  110. ->locales(["zh_TW", "en"])
  111. ->id(fn ($get) => "para_text_" . $get('item_key')),
  112. ])->visible(fn (Get $get):bool => $get("type") == 1),
  113. Group::make()->schema([
  114. Section::make('區塊文字設定')->schema([
  115. // // 佈局設定
  116. // Radio::make('layout_type')->label('佈局方式')->options([
  117. // 'vertical' => '垂直排列',
  118. // 'horizontal' => '水平排列',
  119. // 'grid' => '網格排列',
  120. // ])->default('vertical')->Live(),
  121. // 文字區塊重複器
  122. Repeater::make("content.text_blocks")->schema([
  123. TextInput::make('block_item_key')
  124. ->default(fn () => Str::random())
  125. ->hidden()
  126. ->afterStateHydrated(function (TextInput $component, $state) {
  127. if (empty($state)) {
  128. $component->state(Str::random());
  129. }
  130. }),
  131. // 多語系內容
  132. Translate::make()->schema(fn (string $locale) => [
  133. // TextInput::make('block_title')
  134. // ->label("區塊標題")
  135. // ->maxLength(200)
  136. // ->visible(fn (Get $get) => in_array($get('block_type'), ['title', 'subtitle', 'highlight'])),
  137. RichEditor::make('block_content')
  138. ->disableToolbarButtons(['attachFiles'])
  139. ->required($locale == "zh_TW")
  140. ->validationMessages([
  141. 'required' => '請填寫文字內容',
  142. ]),
  143. ])
  144. ->locales(["zh_TW", "en"])
  145. ->id(fn ($get) => "text_block_" . $get('block_item_key')),
  146. ])
  147. ->label("")
  148. ->addActionLabel('增加區塊')
  149. ->orderColumn('order')
  150. ->reorderableWithButtons()
  151. ->cloneable()
  152. ->minItems(1)
  153. ->maxItems(20)
  154. ->defaultItems(1),
  155. ]),
  156. ])->visible(fn (Get $get):bool => $get('type') == 2),
  157. Group::make()->schema([
  158. Section::make('表格設定')->schema([
  159. Radio::make('content.is_card')
  160. ->label('手機板卡片顯示')
  161. ->options([
  162. 1 => '是', // 改為數字鍵值
  163. 0 => '否'
  164. ])
  165. ->default(2)
  166. ->inline(),
  167. // 表格基本資訊
  168. // Translate::make()->schema(fn (string $locale) => [
  169. // TextInput::make('content.table_title')
  170. // ->label("表格標題")
  171. // ->maxLength(200),
  172. // Textarea::make('content.table_description')
  173. // ->label("表格說明")
  174. // ->rows(3)
  175. // ->maxLength(500),
  176. // ])->locales(["zh_TW", "en"])
  177. // ->id(fn ($get) => "table_info_" . $get('item_key')),
  178. // 表格資料
  179. Placeholder::make('table_builder_info')
  180. ->content('請先設定表格欄數,然後填入表格資料'),
  181. Grid::make(3)->schema([
  182. Radio::make('content.column_count')
  183. ->label('欄數')
  184. ->options([
  185. 2 => '2欄', // 改為數字鍵值
  186. 3 => '3欄',
  187. 4 => '4欄'
  188. ])
  189. ->default(2)
  190. ->inline()
  191. ->live()
  192. ->reactive(), // ✅ 添加 reactive()
  193. // Radio::make('content.is_card')
  194. // ->label('是否卡片呈現')
  195. // ->options([
  196. // 1 => '是', // 改為數字鍵值
  197. // 0 => '否',
  198. // ])
  199. // ->default(1)
  200. // ->inline()
  201. // ->required()
  202. ]),
  203. Translate::make()->schema(fn (string $locale) => [
  204. Grid::make(2) // 改用 Grid 來支援動態 columns
  205. ->schema([
  206. TextInput::make('content.head1')->label('標題第1欄')->required()
  207. ->validationMessages([
  208. 'required' => '請填寫',
  209. ]),
  210. Radio::make('content.head_align1')
  211. ->label('對齊')
  212. ->options([
  213. 1 => '置左', // 改為數字鍵值
  214. 2 => '置中',
  215. 3 => '置右'
  216. ])
  217. ->default(1),
  218. TextInput::make('content.head2')->label('標題第2欄')->required()
  219. ->validationMessages([
  220. 'required' => '請填寫',
  221. ]),
  222. Radio::make('content.head_align2')
  223. ->label('對齊')
  224. ->options([
  225. 1 => '置左', // 改為數字鍵值
  226. 2 => '置中',
  227. 3 => '置右'
  228. ])
  229. ->default(1),
  230. TextInput::make('content.head3')->label('標題第3欄')
  231. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  232. Radio::make('content.head_align3')
  233. ->label('對齊')
  234. ->options([
  235. 1 => '置左', // 改為數字鍵值
  236. 2 => '置中',
  237. 3 => '置右'
  238. ])
  239. ->default(1)
  240. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  241. TextInput::make('content.head4')->label('標題第4欄')
  242. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  243. Radio::make('content.head_align4')
  244. ->label('對齊')
  245. ->options([
  246. 1 => '置左', // 改為數字鍵值
  247. 2 => '置中',
  248. 3 => '置右'
  249. ])
  250. ->default(1)
  251. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  252. ])
  253. ->reactive() // 加入響應式
  254. ])->locales(["zh_TW", "en"]),
  255. Repeater::make('content.simple_table_rows')
  256. ->label('表格資料')
  257. ->schema([
  258. TextInput::make('simple_table_rows_key')
  259. ->default(fn () => Str::random())
  260. ->hidden()
  261. ->afterStateHydrated(function (TextInput $component, $state) {
  262. if (empty($state)) {
  263. $component->state(Str::random());
  264. }
  265. }),
  266. Translate::make()->schema(fn (string $locale) => [
  267. Grid::make(1) // 改用 Grid 來支援動態 columns
  268. ->schema([
  269. Radio::make('align1')
  270. ->label('對齊')
  271. ->options([
  272. 1 => '置左', // 改為數字鍵值
  273. 2 => '置中',
  274. 3 => '置右'
  275. ])
  276. ->default(1),
  277. RichEditor::make('col1')
  278. ->label('第1欄')
  279. ->disableToolbarButtons(['attachFiles'])
  280. ->required($locale == "zh_TW")
  281. ->validationMessages([
  282. 'required' => '請填寫',
  283. ]),
  284. // TextInput::make('col2')->label('第2欄')->required(),
  285. Radio::make('align2')
  286. ->label('對齊')
  287. ->options([
  288. 1 => '置左', // 改為數字鍵值
  289. 2 => '置中',
  290. 3 => '置右'
  291. ])
  292. ->default(1),
  293. RichEditor::make('col2')
  294. ->label('第2欄')
  295. ->disableToolbarButtons(['attachFiles'])
  296. ->required($locale == "zh_TW")
  297. ->validationMessages([
  298. 'required' => '請填寫',
  299. ]),
  300. Radio::make('align3')
  301. ->label('對齊')
  302. ->options([
  303. 1 => '置左', // 改為數字鍵值
  304. 2 => '置中',
  305. 3 => '置右'
  306. ])
  307. ->default(1)
  308. ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 3),
  309. RichEditor::make('col3')
  310. ->label('第3欄')
  311. ->disableToolbarButtons(['attachFiles'])
  312. ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 3)
  313. ->required($locale == "zh_TW" && fn (Get $get)=> intval($get("../../../content.column_count")) >= 3)
  314. ->validationMessages([
  315. 'required' => '請填寫',
  316. ]),
  317. Radio::make('align4')
  318. ->label('對齊')
  319. ->options([
  320. 1 => '置左', // 改為數字鍵值
  321. 2 => '置中',
  322. 3 => '置右'
  323. ])
  324. ->default(1)
  325. ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 4),
  326. RichEditor::make('col4')
  327. ->label('第4欄')
  328. ->disableToolbarButtons(['attachFiles'])
  329. ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 4)
  330. ->required($locale == "zh_TW" && fn (Get $get)=> intval($get("../../../content.column_count")) >= 4)
  331. ->validationMessages([
  332. 'required' => '請填寫',
  333. ]),
  334. ])
  335. ->reactive() // 加入響應式
  336. ])->locales(["zh_TW", "en"]),
  337. ])
  338. ->id(fn ($get) => "simple_table_" . $get('simple_table_key'))
  339. ->addActionLabel('新增列')
  340. ->reorderableWithButtons()
  341. ->minItems(1)
  342. ]),
  343. ])->visible(fn (Get $get):bool => $get('type') == 3),
  344. Group::make()->schema([
  345. Section::make('')->schema([
  346. Repeater::make("content.multiple_images")->schema([
  347. TextInput::make('para_img_item_key')
  348. ->default(fn () => Str::random())
  349. ->hidden()
  350. ->afterStateHydrated(function (TextInput $component, $state) {
  351. if (empty($state)) {
  352. $component->state(Str::random());
  353. }
  354. }),
  355. Translate::make()->schema(fn (string $locale) => [
  356. TextInput::make('image_alt')->label("圖片註文")
  357. ])->locales(["zh_TW", "en"])
  358. ->id(fn ($get) => "para_img_mul_" . $get('para_img_item_key')),
  359. FileUpload::make('image_url')->label("")->disk("public")
  360. // ->helperText('建議寬高限制為:1080*675px,檔案大小限制為1M以下')->maxSize('1024')
  361. ->directory("esg/paragraphPhoto")
  362. ->maxFiles(10)
  363. ->required()
  364. ->validationMessages([
  365. 'required' => '請上傳圖片',
  366. ]),
  367. ])
  368. ->addActionLabel('新增')
  369. ->label("")
  370. ->orderColumn('order')
  371. ])
  372. ])->visible(fn (Get $get):bool => $get("type") == 4),
  373. Group::make()->schema([
  374. Section::make("")->schema([
  375. FileUpload::make('content.video_img')->label("影片底圖")
  376. ->disk("public")
  377. ->directory("esg/paragraphVideo")
  378. ->helperText('建議寬高限制為:2000*720px,出血寬度720px,主要圖像範圍為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
  379. TextInput::make('content.link')->label("網址")->required()
  380. ->validationMessages([
  381. 'required' => '請附上連結',
  382. ]),
  383. Translate::make()->schema(fn (string $locale) => [
  384. TextInput::make('content.video_alt')->label("影片註文"),
  385. ])->locales(["zh_TW", "en"])
  386. ]),
  387. ])->visible(fn (Get $get):bool => $get("type") == 5),
  388. ])
  389. ->relationship("paragraphs")
  390. ->addActionLabel('新增段落')
  391. ->collapsible()
  392. ->reorderableWithButtons()
  393. ->orderColumn('order')
  394. ->cloneable()
  395. ])->columnSpanFull(),
  396. ])
  397. ->columnSpanFull()
  398. ]);
  399. }
  400. public static function table(Table $table): Table
  401. {
  402. return $table
  403. ->columns([
  404. //
  405. TextColumn::make("title")->label("標題"),
  406. ])
  407. ->filters([
  408. //
  409. ])
  410. ->actions([
  411. Tables\Actions\EditAction::make(),
  412. ])
  413. ->bulkActions([
  414. Tables\Actions\BulkActionGroup::make([
  415. Tables\Actions\DeleteBulkAction::make(),
  416. ]),
  417. ]);
  418. }
  419. public static function getRelations(): array
  420. {
  421. return [
  422. //
  423. ];
  424. }
  425. public static function getPages(): array
  426. {
  427. return [
  428. 'index' => Pages\ListEsg::route('/'),
  429. 'create' => Pages\CreateEsg::route('/create'),
  430. 'edit' => Pages\EditEsg::route('/{record}/edit'),
  431. ];
  432. }
  433. }