ProjectResource.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace App\Filament\Resources;
  3. use AbdulmajeedJamaan\FilamentTranslatableTabs\TranslatableTabs;
  4. use App\Filament\Resources\ProjectResource\Pages;
  5. use App\Filament\Resources\ProjectResource\RelationManagers;
  6. use App\Models\Project;
  7. use App\Models\Region;
  8. use Filament\Forms;
  9. use Filament\Forms\Components\DatePicker;
  10. use Filament\Forms\Components\FileUpload;
  11. use Filament\Forms\Components\Group;
  12. use Filament\Forms\Components\Hidden;
  13. use Filament\Forms\Components\Radio;
  14. use Filament\Forms\Components\Repeater;
  15. use Filament\Forms\Components\Section;
  16. use Filament\Forms\Components\Select;
  17. use Filament\Forms\Components\Tabs;
  18. use Filament\Forms\Components\Tabs\Tab;
  19. use Filament\Forms\Components\Textarea;
  20. use Filament\Forms\Components\TextInput;
  21. use Filament\Forms\Form;
  22. use Filament\Forms\Get;
  23. use Filament\Resources\Resource;
  24. use Filament\Tables;
  25. use Filament\Tables\Columns\ImageColumn;
  26. use Filament\Tables\Columns\TextColumn;
  27. use Filament\Tables\Table;
  28. use Illuminate\Database\Eloquent\Builder;
  29. use Illuminate\Database\Eloquent\SoftDeletingScope;
  30. use Illuminate\Support\Facades\Storage;
  31. use Illuminate\Support\Str;
  32. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  33. use App\Service\DeepLService;
  34. class ProjectResource extends Resource
  35. {
  36. protected static ?string $model = Project::class;
  37. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  38. protected static ?string $navigationGroup = '專案項目管理';
  39. protected static ?string $navigationLabel = "專案項目管理";
  40. protected static ?string $modelLabel = "專案項目管理";
  41. protected static ?int $navigationSort = 5;
  42. public static function form(Form $form): Form
  43. {
  44. return $form
  45. ->schema([
  46. Section::make("")->schema([
  47. Translate::make()->schema(fn (string $locale) => [
  48. TextInput::make("name")->label("項目名稱"),
  49. TextInput::make("sub_name")->label("項目子名稱"),
  50. ])
  51. ->locales(["zh_TW", "en"])
  52. ->actions([
  53. app(DeepLService::class)->createTranslationAction("Main", ["name", "sub_name"])
  54. ])->columnSpanFull()->id("main"),
  55. Select::make('tags')
  56. ->multiple()
  57. ->relationship('tags', 'name')
  58. ->preload()
  59. ->label('標籤'),
  60. FileUpload::make("img_url")->label("圖片")->directory("project")->multiple()->maxFiles(5),
  61. TextInput::make('order')->label("排序")->default("0")
  62. ]),
  63. Tabs::make()->schema([
  64. Tab::make("專案概要")->schema([
  65. Select::make("region_id")->label("地區")->options(function (){
  66. return Region::where("visible",true)->pluck("name", "id");
  67. }),
  68. Translate::make()->schema(fn (string $locale) => [
  69. Textarea::make("summaries")->label("簡述"),
  70. TextInput::make("address")->label("地址"),
  71. Textarea::make("floor_plan")->label("樓層規劃"),
  72. Textarea::make("building_structure")->label("建築結構"),
  73. Textarea::make("design_unit")->label("設計團隊")
  74. ])
  75. ->locales(["zh_TW", "en"])
  76. ->actions([
  77. app(DeepLService::class)->createTranslationAction("summaries", ["summaries","address",
  78. "floor_plan","building_structure","design_unit"])
  79. ])->columnSpanFull()->id("summaries"),
  80. Radio::make("badge_type")->label("標章呈現方式")->options([1 => "永續目標", 2 => "取得標章"])->default(1)->inline(),
  81. Repeater::make("badgesTarget")->label("永續目標")->schema([
  82. Hidden::make("award_type")->default(1),
  83. Select::make('badge_id')
  84. ->relationship('badges', 'title')
  85. ->getOptionLabelFromRecordUsing(function ($record) {
  86. $imageHtml = $record->img_url
  87. ? '<img src="' . Storage::url($record->img_url) . '" class="w-6 h-6 rounded-full mr-2 inline-block" />'
  88. : '<div class="w-6 h-6 bg-gray-200 rounded-full mr-2 inline-block"></div>';
  89. return new \Illuminate\Support\HtmlString($imageHtml . $record->title);
  90. })
  91. ->allowHtml()
  92. ->preload()
  93. ->searchable()
  94. ->label('')
  95. ->required(),
  96. ])->reorderable(false)
  97. // ✅ 加上這個!
  98. ->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
  99. $data['award_type'] = 1; // 確保有值
  100. $data['award_date'] = null; // 確保有值
  101. return $data;
  102. }),
  103. Repeater::make("badgesAward")->label("取得標章")->schema([
  104. Hidden::make("award_type")->default(2),
  105. Select::make('badge_id')
  106. ->getOptionLabelFromRecordUsing(function ($record) {
  107. $imageHtml = $record->img_url
  108. ? '<img src="' . Storage::url($record->img_url) . '" class="w-6 h-6 rounded-full mr-2 inline-block" />'
  109. : '<div class="w-6 h-6 bg-gray-200 rounded-full mr-2 inline-block"></div>';
  110. return new \Illuminate\Support\HtmlString($imageHtml . $record->title);
  111. })
  112. ->allowHtml()
  113. ->preload()
  114. ->searchable()
  115. ->label('')
  116. ->required(),
  117. DatePicker::make('award_date')
  118. ->label('選擇年月')
  119. ->format('Y-m')
  120. ->displayFormat('Y年m月')
  121. ->native(false)
  122. ->closeOnDateSelection()
  123. ])->reorderable(false)
  124. // ✅ 加上這個!
  125. ->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
  126. $data['award_type'] = 2; // 確保有值
  127. return $data;
  128. }),
  129. ]),
  130. Tab::make("開發歷程")->schema([
  131. Repeater::make("histories")->label("")->schema([
  132. TextInput::make('histories_item_key')
  133. ->default(fn () => Str::random())
  134. ->hidden()
  135. ->afterStateHydrated(function (TextInput $component, $state) {
  136. if (empty($state)) {
  137. $component->state(Str::random());
  138. }
  139. }),
  140. DatePicker::make("operate_date")->label("歷程日期")->columnSpan(1),
  141. Translate::make()->schema(fn (string $locale) => [
  142. TextInput::make("title")->label("標題")->columnSpanFull()
  143. ])
  144. ->locales(["zh_TW", "en"])
  145. ->actions([
  146. app(DeepLService::class)->createTranslationAction("histories", ["title"])
  147. ])->columnSpanFull()
  148. ->id(fn ($get) => "histories_" . $get('histories_item_key')),
  149. ])
  150. ->relationship("histories", function ($query,Get $get, $livewire) {
  151. return $query->orderby("operate_date","desc");
  152. })
  153. ->collapsible()
  154. ->cloneable()
  155. ]),
  156. Tab::make("空間資訊")->schema([
  157. Group::make()->schema([
  158. Translate::make()->schema(fn (string $locale) => [
  159. TextInput::make("contact_unit")->label("物管單位"),
  160. TextInput::make("contact_phone")->label("物管電話"),
  161. TextInput::make("inversment_phone")->label("招商電話"),
  162. ])
  163. ->locales(["zh_TW", "en"])
  164. ->actions([
  165. app(DeepLService::class)->createTranslationAction("contact", ["contact_unit", "contact_phone", "inversment_phone"])
  166. ])->columnSpanFull()->columns(3)
  167. ->id("contact"),
  168. ])->columnSpanFull(),
  169. Repeater::make("spaceInfos")->label("")->schema([
  170. Translate::make()->schema(fn (string $locale) => [
  171. TextInput::make("title")->label("標題")->columnSpanFull(),
  172. Textarea::make("content")->label("內文")->columnSpanFull()
  173. ])
  174. ->locales(["zh_TW", "en"])
  175. ->actions([
  176. app(DeepLService::class)->createTranslationAction("spaceInfos", ["title", "content"])
  177. ])->columnSpanFull()
  178. ->id(fn ($get) => "spaceInfos_" . $get('item_key')),
  179. ])
  180. ->relationship("spaceInfos")
  181. ->label("")
  182. ->collapsible()
  183. ->reorderableWithButtons()
  184. ->orderColumn('order')
  185. ->cloneable(),
  186. ])
  187. ])->columnSpanFull(),
  188. ]);
  189. }
  190. public static function table(Table $table): Table
  191. {
  192. return $table
  193. ->columns([
  194. //
  195. TextColumn::make("name")->label("項目名稱")->alignCenter(),
  196. ImageColumn::make("first_list_img_url")->label("列表圖")->alignCenter(),
  197. TextColumn::make("region.name")->label("地址")->alignCenter()
  198. ->formatStateUsing(fn ($record) => $record->region->getTranslation("name", "zh_TW") . ' | ' . $record->getTranslation("address", "zh_TW")),
  199. ])
  200. ->filters([
  201. //
  202. ])
  203. ->actions([
  204. Tables\Actions\EditAction::make(),
  205. ])
  206. ->bulkActions([
  207. Tables\Actions\BulkActionGroup::make([
  208. Tables\Actions\DeleteBulkAction::make(),
  209. ]),
  210. ]);
  211. }
  212. public static function getRelations(): array
  213. {
  214. return [
  215. //
  216. ];
  217. }
  218. public static function getPages(): array
  219. {
  220. return [
  221. 'index' => Pages\ListProjects::route('/'),
  222. 'create' => Pages\CreateProject::route('/create'),
  223. 'edit' => Pages\EditProject::route('/{record}/edit'),
  224. ];
  225. }
  226. }