ProjectResource.php 15KB

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