EsgResource.php 23KB

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