EditProject.php 3.2KB

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