EsgResource.php 25KB

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