ProjectResource.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. Repeater::make("badgesAward")->label("取得標章")->schema([
  98. Hidden::make("award_type")->default(2),
  99. Select::make('badge_id')
  100. ->relationship('badges', 'title')
  101. ->getOptionLabelFromRecordUsing(function ($record) {
  102. $imageHtml = $record->img_url
  103. ? '<img src="' . Storage::url($record->img_url) . '" class="w-6 h-6 rounded-full mr-2 inline-block" />'
  104. : '<div class="w-6 h-6 bg-gray-200 rounded-full mr-2 inline-block"></div>';
  105. return new \Illuminate\Support\HtmlString($imageHtml . $record->title);
  106. })
  107. ->allowHtml()
  108. ->preload()
  109. ->searchable()
  110. ->label('')
  111. ->required(),
  112. DatePicker::make('award_date')
  113. ->label('選擇年月')
  114. ->format('Y-m')
  115. ->displayFormat('Y年m月')
  116. ->native(false)
  117. ->closeOnDateSelection()
  118. ])->reorderable(false),
  119. ]),
  120. Tab::make("開發歷程")->schema([
  121. Repeater::make("histories")->label("")->schema([
  122. TextInput::make('histories_item_key')
  123. ->default(fn () => Str::random())
  124. ->hidden()
  125. ->afterStateHydrated(function (TextInput $component, $state) {
  126. if (empty($state)) {
  127. $component->state(Str::random());
  128. }
  129. }),
  130. DatePicker::make("operate_date")->label("歷程日期")->columnSpan(1),
  131. Translate::make()->schema(fn (string $locale) => [
  132. TextInput::make("title")->label("標題")->columnSpanFull()
  133. ])
  134. ->locales(["zh_TW", "en"])
  135. ->actions([
  136. app(DeepLService::class)->createTranslationAction("histories", ["title"])
  137. ])->columnSpanFull()
  138. ->id(fn ($get) => "histories_" . $get('histories_item_key')),
  139. ])
  140. ->relationship("histories", function ($query,Get $get, $livewire) {
  141. return $query->orderby("operate_date","desc");
  142. })
  143. ->collapsible()
  144. ->cloneable()
  145. ]),
  146. Tab::make("空間資訊")->schema([
  147. Group::make()->schema([
  148. Translate::make()->schema(fn (string $locale) => [
  149. TextInput::make("contact_unit")->label("物管單位"),
  150. TextInput::make("contact_phone")->label("物管電話"),
  151. TextInput::make("inversment_phone")->label("招商電話"),
  152. ])
  153. ->locales(["zh_TW", "en"])
  154. ->actions([
  155. app(DeepLService::class)->createTranslationAction("contact", ["contact_unit", "contact_phone", "inversment_phone"])
  156. ])->columnSpanFull()->columns(3)
  157. ->id("contact"),
  158. ])->columnSpanFull(),
  159. Repeater::make("spaceInfos")->label("")->schema([
  160. Translate::make()->schema(fn (string $locale) => [
  161. TextInput::make("title")->label("標題")->columnSpanFull(),
  162. Textarea::make("content")->label("內文")->columnSpanFull()
  163. ])
  164. ->locales(["zh_TW", "en"])
  165. ->actions([
  166. app(DeepLService::class)->createTranslationAction("spaceInfos", ["title", "content"])
  167. ])->columnSpanFull()
  168. ->id(fn ($get) => "spaceInfos_" . $get('item_key')),
  169. ])
  170. ->relationship("spaceInfos")
  171. ->label("")
  172. ->collapsible()
  173. ->reorderableWithButtons()
  174. ->orderColumn('order')
  175. ->cloneable(),
  176. ])
  177. ])->columnSpanFull(),
  178. ]);
  179. }
  180. public static function table(Table $table): Table
  181. {
  182. return $table
  183. ->columns([
  184. //
  185. TextColumn::make("name")->label("項目名稱")->alignCenter(),
  186. ImageColumn::make("first_list_img_url")->label("列表圖")->alignCenter(),
  187. TextColumn::make("region.name")->label("地址")->alignCenter()
  188. ->formatStateUsing(fn ($record) => $record->region->getTranslation("name", "zh_TW") . ' | ' . $record->getTranslation("address", "zh_TW")),
  189. ])
  190. ->filters([
  191. //
  192. ])
  193. ->actions([
  194. Tables\Actions\EditAction::make(),
  195. ])
  196. ->bulkActions([
  197. Tables\Actions\BulkActionGroup::make([
  198. Tables\Actions\DeleteBulkAction::make(),
  199. ]),
  200. ]);
  201. }
  202. public static function getRelations(): array
  203. {
  204. return [
  205. //
  206. ];
  207. }
  208. public static function getPages(): array
  209. {
  210. return [
  211. 'index' => Pages\ListProjects::route('/'),
  212. 'create' => Pages\CreateProject::route('/create'),
  213. 'edit' => Pages\EditProject::route('/{record}/edit'),
  214. ];
  215. }
  216. }