ProjectResource.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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\Actions\Action;
  9. use Filament\Forms\Components\DatePicker;
  10. use Filament\Forms\Components\FileUpload;
  11. use Filament\Forms\Components\Group;
  12. use Filament\Forms\Components\Hidden;
  13. use Filament\Forms\Components\Radio;
  14. use Filament\Forms\Components\Repeater;
  15. use Filament\Forms\Components\Section;
  16. use Filament\Forms\Components\Select;
  17. use Filament\Forms\Components\Tabs;
  18. use Filament\Forms\Components\Tabs\Tab;
  19. use Filament\Forms\Components\Textarea;
  20. use Filament\Forms\Components\TextInput;
  21. use Filament\Forms\Form;
  22. use Filament\Forms\Get;
  23. use Filament\Resources\Resource;
  24. use Filament\Tables;
  25. use Filament\Tables\Columns\ImageColumn;
  26. use Filament\Tables\Columns\TextColumn;
  27. use Filament\Tables\Filters\SelectFilter;
  28. use Filament\Tables\Table;
  29. use Illuminate\Database\Eloquent\Builder;
  30. use Illuminate\Support\Facades\Storage;
  31. use Illuminate\Support\Str;
  32. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  33. class ProjectResource extends Resource
  34. {
  35. protected static ?string $model = Project::class;
  36. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  37. protected static ?string $navigationGroup = '專案項目管理';
  38. protected static ?string $navigationLabel = '專案項目管理';
  39. protected static ?string $modelLabel = '專案項目管理';
  40. protected static ?int $navigationSort = 5;
  41. public static function form(Form $form): Form
  42. {
  43. return $form
  44. ->schema([
  45. Section::make('')->schema([
  46. Translate::make()->schema(fn(string $locale) => [
  47. TextInput::make('name')->label('項目名稱'),
  48. TextInput::make('sub_name')->label('項目子名稱'),
  49. ])
  50. ->locales(['zh_TW', 'en'])
  51. ->actions([
  52. app(DeepLService::class)->createTranslationAction('Main', ['name', 'sub_name']),
  53. ])->columnSpanFull()->id('main'),
  54. Select::make('tags')
  55. ->multiple()
  56. ->relationship('tags', 'name')
  57. ->preload()
  58. ->label('標籤'),
  59. FileUpload::make('thumbnail')->label('縮圖')->directory('project')->image()
  60. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor(),
  61. FileUpload::make('img_url')->label('圖片')->directory('project')->reorderable()
  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. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->required()->imageEditor()->multiple()->maxFiles(20),
  65. TextInput::make('order')->label('排序')->default('0'),
  66. ]),
  67. Tabs::make()->schema([
  68. Tab::make('專案概要')->schema([
  69. Select::make('region_id')->label('地區')->options(function () {
  70. return Region::where('visible', true)->pluck('name', 'id');
  71. }),
  72. Translate::make()->schema(fn(string $locale) => [
  73. Textarea::make('summaries')->label('簡述'),
  74. TextInput::make('district')->label('區域'),
  75. TextInput::make('address')->label('地址'),
  76. Textarea::make('floor_plan')->label('樓層規劃'),
  77. Textarea::make('building_structure')->label('建築結構'),
  78. Textarea::make('design_unit')->label('設計單位'),
  79. ])
  80. ->locales(['zh_TW', 'en'])
  81. ->actions([
  82. app(DeepLService::class)->createTranslationAction('summaries', ['summaries', 'address', 'district',
  83. 'floor_plan', 'building_structure', 'design_unit']),
  84. ])->columnSpanFull()->id('summaries'),
  85. Radio::make('badge_type')->label('')->options([1 => '永續目標', 2 => '取得標章'])->default(1)->inline(),
  86. Repeater::make('badgesTarget')->label('永續目標')->schema([
  87. Hidden::make('award_type')->default(1),
  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. ])->reorderable(false),
  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. ])->reorderable(false),
  130. ]),
  131. Tab::make('開發歷程')->schema([
  132. Repeater::make('histories')->label('')->schema([
  133. TextInput::make('histories_item_key')
  134. ->default(fn() => Str::random())
  135. ->hidden()
  136. ->afterStateHydrated(function (TextInput $component, $state) {
  137. if (empty($state)) {
  138. $component->state(Str::random());
  139. }
  140. }),
  141. DatePicker::make('operate_date')->label('歷程日期')->columnSpan(1),
  142. Translate::make()->schema(fn(string $locale) => [
  143. TextInput::make('title')->label('標題')->columnSpanFull(),
  144. ])
  145. ->locales(['zh_TW', 'en'])
  146. ->actions([
  147. app(DeepLService::class)->createTranslationAction('histories', ['title']),
  148. ])->columnSpanFull()
  149. ->id(fn($get) => 'histories_' . $get('histories_item_key')),
  150. ])
  151. ->relationship('histories', function ($query, Get $get, $livewire) {
  152. return $query->orderby('operate_date', 'desc');
  153. })
  154. ->collapsible()
  155. ->cloneable(),
  156. ]),
  157. Tab::make('空間資訊')->schema([
  158. Group::make()->schema([
  159. Translate::make()->schema(fn(string $locale) => [
  160. TextInput::make('contact_unit')->label('物管單位'),
  161. TextInput::make('contact_phone')->label('物管電話'),
  162. TextInput::make('inversment_phone')->label('招商電話'),
  163. ])
  164. ->locales(['zh_TW', 'en'])
  165. ->actions([
  166. app(DeepLService::class)->createTranslationAction('contact', ['contact_unit', 'contact_phone', 'inversment_phone']),
  167. ])->columnSpanFull()->columns(3)
  168. ->id('contact'),
  169. TextInput::make('offical_link')->label('官網連結'),
  170. ])->columnSpanFull(),
  171. Repeater::make('spaceInfos')->label('')->schema([
  172. Translate::make()->schema(fn(string $locale) => [
  173. TextInput::make('title')->label('標題')->columnSpanFull(),
  174. Textarea::make('content')->label('內文')->columnSpanFull(),
  175. ])
  176. ->locales(['zh_TW', 'en'])
  177. ->actions([
  178. Action::make('translateSpaceInfos')
  179. ->label('翻譯')
  180. ->visible(fn ($arguments) => $arguments['locale'] != 'zh_TW')
  181. ->action(function ($set, $get, $arguments) {
  182. $locale = $arguments['locale'] ?? null;
  183. if (!$locale) {
  184. return;
  185. }
  186. $deepLService = app(DeepLService::class);
  187. foreach(['title', 'content'] as $fieldName) {
  188. $sourceText = $get("{$fieldName}.zh_TW");
  189. if (!empty($sourceText)) {
  190. $translateText = $deepLService->translateWordings($sourceText, $locale);
  191. $set("{$fieldName}.{$locale}", $translateText);
  192. }
  193. }
  194. }),
  195. ])
  196. ->columnSpanFull()
  197. ->id(fn($get) => 'spaceInfos_' . $get('item_key')),
  198. ])
  199. ->relationship('spaceInfos')
  200. ->label('')
  201. ->collapsible()
  202. ->reorderableWithButtons()
  203. ->orderColumn('order')
  204. ->cloneable(),
  205. ]),
  206. ])->columnSpanFull(),
  207. ]);
  208. }
  209. public static function table(Table $table): Table
  210. {
  211. return $table
  212. ->columns([
  213. //
  214. TextColumn::make('name')->label('項目名稱')->alignCenter(),
  215. ImageColumn::make('thumbnail_url')->label('縮圖')->alignCenter(),
  216. ImageColumn::make('first_list_img_url')->label('列表圖')->alignCenter(),
  217. TextColumn::make('region.name')->label('地址')->alignCenter()
  218. ->formatStateUsing(fn($record) => $record->region->getTranslation('name', 'zh_TW') . ' | ' . $record->getTranslation('address', 'zh_TW')),
  219. ])
  220. ->filters([
  221. SelectFilter::make('visible')->label('上/下架')
  222. ->options([
  223. 0 => '下架',
  224. 1 => '上架',
  225. ])
  226. ->query(
  227. fn(array $data, Builder $query): Builder => $query->when(
  228. $data['value'],
  229. fn(Builder $query, $value): Builder => $query->where('visible', $data['value'])
  230. )
  231. ),
  232. ])
  233. ->actions([
  234. Tables\Actions\EditAction::make(),
  235. Tables\Actions\DeleteAction::make(),
  236. \Filament\Tables\Actions\Action::make('audit')
  237. ->label(fn($record) => match ($record->visible) {
  238. 0 => '上架',
  239. 1 => '下架',
  240. })
  241. ->color(fn($record) => match ($record->visible) {
  242. 0 => 'warning',
  243. 1 => 'gray',
  244. })
  245. ->icon(fn($record) => match ($record->visible) {
  246. 0 => 'heroicon-m-chevron-double-up',
  247. 1 => 'heroicon-m-chevron-double-down',
  248. })
  249. ->action(function ($record): void {
  250. $record->visible = !$record->visible;
  251. $record->save();
  252. })
  253. ->outlined()
  254. ->requiresConfirmation(),
  255. ])
  256. ->bulkActions([
  257. Tables\Actions\BulkActionGroup::make([
  258. Tables\Actions\DeleteBulkAction::make(),
  259. ]),
  260. ]);
  261. }
  262. public static function getRelations(): array
  263. {
  264. return [
  265. //
  266. ];
  267. }
  268. public static function getPages(): array
  269. {
  270. return [
  271. 'index' => Pages\ListProjects::route('/'),
  272. 'create' => Pages\CreateProject::route('/create'),
  273. 'edit' => Pages\EditProject::route('/{record}/edit'),
  274. ];
  275. }
  276. }