ProjectResource.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\ProjectResource\Pages;
  4. use App\Models\Badge;
  5. use App\Models\Project;
  6. use App\Models\Region;
  7. use App\Service\DeepLService;
  8. use Filament\Forms\Components\DatePicker;
  9. use Filament\Forms\Components\FileUpload;
  10. use Filament\Forms\Components\Group;
  11. use Filament\Forms\Components\Hidden;
  12. use Filament\Forms\Components\Radio;
  13. use Filament\Forms\Components\Repeater;
  14. use Filament\Forms\Components\Section;
  15. use Filament\Forms\Components\Select;
  16. use Filament\Forms\Components\Tabs;
  17. use Filament\Forms\Components\Tabs\Tab;
  18. use Filament\Forms\Components\Textarea;
  19. use Filament\Forms\Components\TextInput;
  20. use Filament\Forms\Form;
  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\Filters\SelectFilter;
  26. use Filament\Tables\Table;
  27. use Illuminate\Database\Eloquent\Builder;
  28. use Illuminate\Support\Facades\Storage;
  29. use Illuminate\Support\Str;
  30. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  31. class ProjectResource extends Resource
  32. {
  33. protected static ?string $model = Project::class;
  34. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  35. protected static ?string $navigationGroup = '專案項目管理';
  36. protected static ?string $navigationLabel = '專案項目管理';
  37. protected static ?string $modelLabel = '專案項目管理';
  38. protected static ?int $navigationSort = 5;
  39. public static function form(Form $form): Form
  40. {
  41. return $form
  42. ->schema([
  43. Section::make('')->schema([
  44. Translate::make()->schema(fn (string $locale) => [
  45. TextInput::make('name')->label('項目名稱'),
  46. TextInput::make('sub_name')->label('項目子名稱'),
  47. ])
  48. ->locales(['zh_TW', 'en'])
  49. ->actions([
  50. app(DeepLService::class)->createTranslationAction('Main', ['name', 'sub_name']),
  51. ])->columnSpanFull()->id('main'),
  52. Select::make('tags')
  53. ->multiple()
  54. ->relationship('tags', 'name')
  55. ->preload()
  56. ->label('標籤'),
  57. FileUpload::make('thumbnail')->label('縮圖')->directory('project')->image()
  58. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor(),
  59. FileUpload::make('img_url')->label('圖片')->directory('project')->reorderable()
  60. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor()->multiple()->maxFiles(20),
  61. FileUpload::make('mobile_img_url')->label('手機版圖片')->directory('project')->reorderable()
  62. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor()->multiple()->maxFiles(20),
  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', 'district',
  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. ])
  102. ->relationship('targetBadges')
  103. ->reorderableWithButtons()
  104. ->orderColumn('sort_order'),
  105. Repeater::make('badgesAward')->label('取得標章')->schema([
  106. Hidden::make('award_type')->default(2),
  107. Select::make('badge_id')
  108. ->options(function () {
  109. return Badge::all()->mapWithKeys(function ($badge) {
  110. return [
  111. $badge->id => '<div class="flex items-center gap-2">
  112. <img src="'.Storage::url($badge->img_url).'" class="w-6 h-6 rounded-full">
  113. <span>'.$badge->title.'</span>
  114. </div>',
  115. ];
  116. });
  117. })
  118. ->allowHtml()
  119. ->preload()
  120. ->searchable()
  121. ->label('')
  122. ->required(),
  123. DatePicker::make('award_date')
  124. ->label('選擇年月')
  125. ->format('Y-m')
  126. ->displayFormat('Y年m月')
  127. ->native(false)
  128. ->closeOnDateSelection(),
  129. ])
  130. ->relationship('awardBadges')
  131. ->reorderableWithButtons()
  132. ->orderColumn('sort_order'),
  133. ]),
  134. Tab::make('開發歷程')->schema([
  135. Repeater::make('histories')->label('')->schema([
  136. TextInput::make('histories_item_key')
  137. ->default(fn () => Str::random())
  138. ->hidden()
  139. ->afterStateHydrated(function (TextInput $component, $state) {
  140. if (empty($state)) {
  141. $component->state(Str::random());
  142. }
  143. }),
  144. DatePicker::make('operate_date')->label('歷程日期')->columnSpan(1),
  145. Translate::make()->schema(fn (string $locale) => [
  146. TextInput::make('title')->label('標題')->columnSpanFull(),
  147. ])
  148. ->locales(['zh_TW', 'en'])
  149. ->actions([
  150. app(DeepLService::class)->createTranslationAction('histories', ['title']),
  151. ])->columnSpanFull()
  152. ->id(fn ($get) => 'histories_'.$get('histories_item_key')),
  153. ])
  154. ->relationship('histories')
  155. ->reorderableWithButtons()
  156. ->orderColumn('order')
  157. ->collapsible()
  158. ->cloneable(),
  159. ]),
  160. Tab::make('空間資訊')->schema([
  161. Group::make()->schema([
  162. Translate::make()->schema(fn (string $locale) => [
  163. // TextInput::make('contact_unit')->label('物管單位'),
  164. TextInput::make('contact_phone')->label('物管電話'),
  165. TextInput::make('inversment_phone')->label('招商電話'),
  166. ])
  167. ->locales(['zh_TW', 'en'])
  168. ->actions([
  169. app(DeepLService::class)->createTranslationAction('contact', ['contact_unit', 'contact_phone', 'inversment_phone']),
  170. ])->columnSpanFull()->columns(3)
  171. ->id('contact'),
  172. TextInput::make('offical_link')->label('官網連結'),
  173. ])->columnSpanFull(),
  174. Repeater::make('spaceInfos')->label('')->schema([
  175. TextInput::make('spaceInfos_item_key')
  176. ->default(fn () => Str::random())
  177. ->hidden()
  178. ->afterStateHydrated(function (TextInput $component, $state) {
  179. if (empty($state)) {
  180. $component->state(Str::random());
  181. }
  182. }),
  183. Translate::make()->schema(fn (string $locale) => [
  184. TextInput::make('title')->label('標題')->columnSpanFull(),
  185. Textarea::make('content')->label('內文')->columnSpanFull(),
  186. ])
  187. ->locales(['zh_TW', 'en'])
  188. ->actions([
  189. app(DeepLService::class)->createTranslationAction('spaceInfos', ['title', 'content']),
  190. ])->columnSpanFull()
  191. ->id(fn ($get) => 'spaceInfos_'.$get('spaceInfos_item_key')),
  192. ])
  193. ->relationship('spaceInfos')
  194. ->label('')
  195. ->collapsible()
  196. ->reorderableWithButtons()
  197. ->orderColumn('order')
  198. ->cloneable(),
  199. ]),
  200. ])->columnSpanFull(),
  201. ]);
  202. }
  203. public static function table(Table $table): Table
  204. {
  205. return $table
  206. ->columns([
  207. //
  208. TextColumn::make('name')->label('項目名稱')->alignCenter(),
  209. ImageColumn::make('thumbnail_url')->label('縮圖')->alignCenter(),
  210. ImageColumn::make('first_list_img_url')->label('列表圖')->alignCenter(),
  211. TextColumn::make('region.name')->label('地址')->alignCenter()
  212. ->formatStateUsing(fn ($record) => $record->region->getTranslation('name', 'zh_TW').' | '.$record->getTranslation('address', 'zh_TW')),
  213. ])
  214. ->filters([
  215. SelectFilter::make('visible')->label('上/下架')
  216. ->options([
  217. 0 => '下架',
  218. 1 => '上架',
  219. ])
  220. ->query(
  221. fn (array $data, Builder $query): Builder => $query->when(
  222. $data['value'],
  223. fn (Builder $query, $value): Builder => $query->where('visible', $data['value'])
  224. )
  225. ),
  226. ])
  227. ->actions([
  228. Tables\Actions\EditAction::make(),
  229. Tables\Actions\DeleteAction::make(),
  230. \Filament\Tables\Actions\Action::make('audit')
  231. ->label(fn ($record) => match ($record->visible) {
  232. 0 => '上架',
  233. 1 => '下架',
  234. })
  235. ->color(fn ($record) => match ($record->visible) {
  236. 0 => 'warning',
  237. 1 => 'gray',
  238. })
  239. ->icon(fn ($record) => match ($record->visible) {
  240. 0 => 'heroicon-m-chevron-double-up',
  241. 1 => 'heroicon-m-chevron-double-down',
  242. })
  243. ->action(function ($record): void {
  244. $record->visible = ! $record->visible;
  245. $record->save();
  246. })
  247. ->outlined()
  248. ->requiresConfirmation(),
  249. ])
  250. ->bulkActions([
  251. Tables\Actions\BulkActionGroup::make([
  252. Tables\Actions\DeleteBulkAction::make(),
  253. ]),
  254. ])
  255. ->reorderable('order')
  256. ->defaultSort('order');
  257. }
  258. public static function getRelations(): array
  259. {
  260. return [
  261. //
  262. ];
  263. }
  264. public static function getPages(): array
  265. {
  266. return [
  267. 'index' => Pages\ListProjects::route('/'),
  268. 'create' => Pages\CreateProject::route('/create'),
  269. 'edit' => Pages\EditProject::route('/{record}/edit'),
  270. ];
  271. }
  272. }