EditProject.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Filament\Resources\ProjectResource\Pages;
  3. use App\Filament\Resources\ProjectResource;
  4. use App\Models\Project;
  5. use Filament\Resources\Pages\EditRecord;
  6. class EditProject extends EditRecord
  7. {
  8. protected static string $resource = ProjectResource::class;
  9. protected array $badgesTargetData = [];
  10. protected array $badgesAwardData = [];
  11. protected function getRedirectUrl(): string
  12. {
  13. return $this->getResource()::getUrl('index');
  14. }
  15. protected function mutateFormDataBeforeFill(array $data): array
  16. {
  17. $project = $this->record;
  18. $data['badgesTarget'] = \DB::table('badgeables')
  19. ->where('badgeable_id', $project->id)
  20. ->where('badgeable_type', Project::class)
  21. ->where('award_type', 1)
  22. ->orderBy('sort_order')
  23. ->get()
  24. ->map(fn ($item) => [
  25. 'badge_id' => $item->badge_id,
  26. 'award_type' => $item->award_type,
  27. ])->toArray();
  28. $data['badgesAward'] = \DB::table('badgeables')
  29. ->where('badgeable_id', $project->id)
  30. ->where('badgeable_type', Project::class)
  31. ->where('award_type', 2)
  32. ->orderBy('sort_order')
  33. ->get()
  34. ->map(fn ($item) => [
  35. 'badge_id' => $item->badge_id,
  36. 'award_date' => $item->award_date,
  37. 'award_type' => $item->award_type,
  38. ])->toArray();
  39. return $data;
  40. }
  41. protected function mutateFormDataBeforeSave(array $data): array
  42. {
  43. // 暫存標籤資料
  44. if (isset($data['badgesTarget'])) {
  45. $this->badgesTargetData = $data['badgesTarget'];
  46. unset($data['badgesTarget']);
  47. }
  48. // 暫存標籤資料
  49. if (isset($data['badgesAward'])) {
  50. $this->badgesAwardData = $data['badgesAward'];
  51. unset($data['badgesAward']);
  52. }
  53. return $data;
  54. }
  55. protected function afterSave(): void
  56. {
  57. $syncData = [];
  58. // ✅ 先刪除所有舊的標籤
  59. \DB::table('badgeables')
  60. ->where('badgeable_id', $this->record->id)
  61. ->where('badgeable_type', Project::class)
  62. ->delete();
  63. if (! empty($this->badgesTargetData)) {
  64. foreach ($this->badgesTargetData as $index => $item) {
  65. $syncData[$item['badge_id'].'_1'] = [
  66. 'badge_id' => $item['badge_id'],
  67. 'award_type' => 1,
  68. 'award_date' => null,
  69. 'sort_order' => $index,
  70. ];
  71. }
  72. }
  73. if (! empty($this->badgesAwardData)) {
  74. foreach ($this->badgesAwardData as $index => $item) {
  75. $syncData[$item['badge_id'].'_2'] = [
  76. 'badge_id' => $item['badge_id'],
  77. 'award_type' => 2,
  78. 'award_date' => $item['award_date'].'-01',
  79. 'sort_order' => $index,
  80. ];
  81. }
  82. }
  83. foreach ($syncData as $data) {
  84. \DB::table('badgeables')->insert([
  85. 'badge_id' => $data['badge_id'],
  86. 'badgeable_id' => $this->record->id,
  87. 'badgeable_type' => Project::class,
  88. 'award_type' => $data['award_type'],
  89. 'award_date' => $data['award_date'],
  90. 'sort_order' => $data['sort_order'],
  91. 'created_at' => now(),
  92. 'updated_at' => now(),
  93. ]);
  94. }
  95. }
  96. }