ProjectResource.php 15KB

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