ProjectResource.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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("district")->label("區域"),
  72. TextInput::make("address")->label("地址"),
  73. Textarea::make("floor_plan")->label("樓層規劃"),
  74. Textarea::make("building_structure")->label("建築結構"),
  75. Textarea::make("design_unit")->label("設計單位")
  76. ])
  77. ->locales(["zh_TW", "en"])
  78. ->actions([
  79. app(DeepLService::class)->createTranslationAction("summaries", ["summaries","address",
  80. "floor_plan","building_structure","design_unit"])
  81. ])->columnSpanFull()->id("summaries"),
  82. Radio::make("badge_type")->label("")->options([1 => "永續目標", 2 => "取得標章"])->default(1)->inline(),
  83. Repeater::make("badgesTarget")->label("永續目標")->schema([
  84. Hidden::make("award_type")->default(1),
  85. Select::make('badge_id')
  86. ->options(function () {
  87. return Badge::all()->mapWithKeys(function ($badge) {
  88. return [
  89. $badge->id => '<div class="flex items-center gap-2">
  90. <img src="' . Storage::url($badge->img_url) . '" class="w-6 h-6 rounded-full">
  91. <span>' . $badge->title . '</span>
  92. </div>'
  93. ];
  94. });
  95. })
  96. ->allowHtml()
  97. ->preload()
  98. ->searchable()
  99. ->label('')
  100. ->required(),
  101. ])->reorderable(false),
  102. Repeater::make("badgesAward")->label("取得標章")->schema([
  103. Hidden::make("award_type")->default(2),
  104. Select::make('badge_id')
  105. ->options(function () {
  106. return Badge::all()->mapWithKeys(function ($badge) {
  107. return [
  108. $badge->id => '<div class="flex items-center gap-2">
  109. <img src="' . Storage::url($badge->img_url) . '" class="w-6 h-6 rounded-full">
  110. <span>' . $badge->title . '</span>
  111. </div>'
  112. ];
  113. });
  114. })
  115. ->allowHtml()
  116. ->preload()
  117. ->searchable()
  118. ->label('')
  119. ->required(),
  120. DatePicker::make('award_date')
  121. ->label('選擇年月')
  122. ->format('Y-m')
  123. ->displayFormat('Y年m月')
  124. ->native(false)
  125. ->closeOnDateSelection()
  126. ])->reorderable(false),
  127. ]),
  128. Tab::make("開發歷程")->schema([
  129. Repeater::make("histories")->label("")->schema([
  130. TextInput::make('histories_item_key')
  131. ->default(fn () => Str::random())
  132. ->hidden()
  133. ->afterStateHydrated(function (TextInput $component, $state) {
  134. if (empty($state)) {
  135. $component->state(Str::random());
  136. }
  137. }),
  138. DatePicker::make("operate_date")->label("歷程日期")->columnSpan(1),
  139. Translate::make()->schema(fn (string $locale) => [
  140. TextInput::make("title")->label("標題")->columnSpanFull()
  141. ])
  142. ->locales(["zh_TW", "en"])
  143. ->actions([
  144. app(DeepLService::class)->createTranslationAction("histories", ["title"])
  145. ])->columnSpanFull()
  146. ->id(fn ($get) => "histories_" . $get('histories_item_key')),
  147. ])
  148. ->relationship("histories", function ($query,Get $get, $livewire) {
  149. return $query->orderby("operate_date","desc");
  150. })
  151. ->collapsible()
  152. ->cloneable()
  153. ]),
  154. Tab::make("空間資訊")->schema([
  155. Group::make()->schema([
  156. Translate::make()->schema(fn (string $locale) => [
  157. TextInput::make("contact_unit")->label("物管單位"),
  158. TextInput::make("contact_phone")->label("物管電話"),
  159. TextInput::make("inversment_phone")->label("招商電話"),
  160. ])
  161. ->locales(["zh_TW", "en"])
  162. ->actions([
  163. app(DeepLService::class)->createTranslationAction("contact", ["contact_unit", "contact_phone", "inversment_phone"])
  164. ])->columnSpanFull()->columns(3)
  165. ->id("contact"),
  166. TextInput::make("offical_link")->label("官網連結"),
  167. ])->columnSpanFull(),
  168. Repeater::make("spaceInfos")->label("")->schema([
  169. Translate::make()->schema(fn (string $locale) => [
  170. TextInput::make("title")->label("標題")->columnSpanFull(),
  171. Textarea::make("content")->label("內文")->columnSpanFull()
  172. ])
  173. ->locales(["zh_TW", "en"])
  174. ->actions([
  175. app(DeepLService::class)->createTranslationAction("spaceInfos", ["title", "content"])
  176. ])->columnSpanFull()
  177. ->id(fn ($get) => "spaceInfos_" . $get('item_key')),
  178. ])
  179. ->relationship("spaceInfos")
  180. ->label("")
  181. ->collapsible()
  182. ->reorderableWithButtons()
  183. ->orderColumn('order')
  184. ->cloneable(),
  185. ])
  186. ])->columnSpanFull(),
  187. ]);
  188. }
  189. public static function table(Table $table): Table
  190. {
  191. return $table
  192. ->columns([
  193. //
  194. TextColumn::make("name")->label("項目名稱")->alignCenter(),
  195. ImageColumn::make("first_list_img_url")->label("列表圖")->alignCenter(),
  196. TextColumn::make("region.name")->label("地址")->alignCenter()
  197. ->formatStateUsing(fn ($record) => $record->region->getTranslation("name", "zh_TW") . ' | ' . $record->getTranslation("address", "zh_TW")),
  198. ])
  199. ->filters([
  200. //
  201. ])
  202. ->actions([
  203. Tables\Actions\EditAction::make(),
  204. ])
  205. ->bulkActions([
  206. Tables\Actions\BulkActionGroup::make([
  207. Tables\Actions\DeleteBulkAction::make(),
  208. ]),
  209. ]);
  210. }
  211. public static function getRelations(): array
  212. {
  213. return [
  214. //
  215. ];
  216. }
  217. public static function getPages(): array
  218. {
  219. return [
  220. 'index' => Pages\ListProjects::route('/'),
  221. 'create' => Pages\CreateProject::route('/create'),
  222. 'edit' => Pages\EditProject::route('/{record}/edit'),
  223. ];
  224. }
  225. }