ProjectResource.php 14KB

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