ProjectResource.php 14KB

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