ProjectResource.php 9.2KB

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