EditProject.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. use Illuminate\Support\Facades\DB;
  7. class EditProject extends EditRecord
  8. {
  9. protected static string $resource = ProjectResource::class;
  10. protected array $badgesTargetData = [];
  11. protected array $badgesAwardData = [];
  12. protected function getRedirectUrl(): string
  13. {
  14. return $this->getResource()::getUrl('index');
  15. }
  16. protected function mutateFormDataBeforeFill(array $data): array
  17. {
  18. $project = $this->record;
  19. // 載入永續目標
  20. $data['badgesTarget'] = DB::table('badgeables')
  21. ->where('badgeable_id', $project->id)
  22. ->where('badgeable_type', Project::class)
  23. ->where('award_type', 1)
  24. ->orderBy('sort_order')
  25. ->get()
  26. ->map(fn ($item) => [
  27. 'badge_id' => $item->badge_id,
  28. ])->toArray();
  29. // 載入取得標章
  30. $data['badgesAward'] = DB::table('badgeables')
  31. ->where('badgeable_id', $project->id)
  32. ->where('badgeable_type', Project::class)
  33. ->where('award_type', 2)
  34. ->orderBy('sort_order')
  35. ->get()
  36. ->map(function ($item) {
  37. // 轉換日期格式從 Y-m-d 到 Y-m
  38. $awardDate = null;
  39. if ($item->award_date) {
  40. $awardDate = substr($item->award_date, 0, 7); // 取前7個字元 (Y-m)
  41. }
  42. return [
  43. 'badge_id' => $item->badge_id,
  44. 'award_date' => $awardDate,
  45. ];
  46. })->toArray();
  47. return $data;
  48. }
  49. protected function mutateFormDataBeforeSave(array $data): array
  50. {
  51. // 暫存永續目標資料
  52. if (isset($data['badgesTarget']) && is_array($data['badgesTarget'])) {
  53. $this->badgesTargetData = $data['badgesTarget'];
  54. unset($data['badgesTarget']);
  55. }
  56. // 暫存取得標章資料
  57. if (isset($data['badgesAward']) && is_array($data['badgesAward'])) {
  58. $this->badgesAwardData = $data['badgesAward'];
  59. unset($data['badgesAward']);
  60. }
  61. return $data;
  62. }
  63. protected function afterSave(): void
  64. {
  65. // 先刪除所有舊的標籤關聯
  66. DB::table('badgeables')
  67. ->where('badgeable_id', $this->record->id)
  68. ->where('badgeable_type', Project::class)
  69. ->delete();
  70. // 儲存永續目標
  71. if (! empty($this->badgesTargetData)) {
  72. foreach ($this->badgesTargetData as $index => $item) {
  73. if (isset($item['badge_id'])) {
  74. DB::table('badgeables')->insert([
  75. 'badge_id' => $item['badge_id'],
  76. 'badgeable_id' => $this->record->id,
  77. 'badgeable_type' => Project::class,
  78. 'award_type' => 1,
  79. 'award_date' => null,
  80. 'sort_order' => $index,
  81. 'created_at' => now(),
  82. 'updated_at' => now(),
  83. ]);
  84. }
  85. }
  86. }
  87. // 儲存取得標章
  88. if (! empty($this->badgesAwardData)) {
  89. foreach ($this->badgesAwardData as $index => $item) {
  90. if (isset($item['badge_id'])) {
  91. $awardDate = null;
  92. if (isset($item['award_date']) && ! empty($item['award_date'])) {
  93. // 處理日期格式:如果是 Y-m 格式,加上 -01
  94. $awardDate = $item['award_date'];
  95. if (strlen($awardDate) === 7) { // Y-m format
  96. $awardDate .= '-01';
  97. }
  98. }
  99. DB::table('badgeables')->insert([
  100. 'badge_id' => $item['badge_id'],
  101. 'badgeable_id' => $this->record->id,
  102. 'badgeable_type' => Project::class,
  103. 'award_type' => 2,
  104. 'award_date' => $awardDate,
  105. 'sort_order' => $index,
  106. 'created_at' => now(),
  107. 'updated_at' => now(),
  108. ]);
  109. }
  110. }
  111. }
  112. }
  113. }