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