ProjectResource.php 11KB

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