EsgResource.php 24KB

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