ProjectResource.php 11KB

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