EditProject.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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['badgesAwardData'] = \DB::table('badgeables')
  27. ->where('badgeable_id', $project->id)
  28. ->where('badgeable_type', Project::class)
  29. ->where('award_type', 1)
  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. // ✅ 先刪除所有舊的標籤
  55. \DB::table('badgeables')
  56. ->where('badgeable_id', $this->record->id)
  57. ->where('badgeable_type', Project::class)
  58. ->delete();
  59. if (!empty($this->badgesTargetData)) {
  60. foreach ($this->badgesTargetData as $item) {
  61. $syncData[$item['badge_id'] . '_1'] = [
  62. 'badge_id' => $item['badge_id'],
  63. 'award_type' => 1,
  64. 'award_date' => null,
  65. ];
  66. }
  67. }
  68. if (!empty($this->badgesAwardData)) {
  69. foreach ($this->badgesAwardData as $item) {
  70. $syncData[$item['badge_id'] . '_2'] = [
  71. 'badge_id' => $item['badge_id'],
  72. 'award_type' => 2,
  73. 'award_date' => $item['award_date'],
  74. ];
  75. }
  76. }
  77. foreach ($syncData as $data) {
  78. \DB::table('badgeables')->insert([
  79. 'tag_id' => $data['tag_id'],
  80. 'badgeable_id' => $this->record->id,
  81. 'badgeable_type' => Project::class,
  82. 'award_type' => $data['award_type'],
  83. 'award_date' => $data['award_date'],
  84. 'created_at' => now(),
  85. 'updated_at' => now(),
  86. ]);
  87. }
  88. }
  89. }