ProjectResource.php 14KB

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