Andrew 2 miesięcy temu
rodzic
commit
2c5bfcd9bc

+ 2
- 7
app/Filament/Resources/ProjectResource.php Wyświetl plik

@@ -10,7 +10,6 @@ use App\Service\DeepLService;
10 10
 use Filament\Forms\Components\DatePicker;
11 11
 use Filament\Forms\Components\FileUpload;
12 12
 use Filament\Forms\Components\Group;
13
-use Filament\Forms\Components\Hidden;
14 13
 use Filament\Forms\Components\Radio;
15 14
 use Filament\Forms\Components\Repeater;
16 15
 use Filament\Forms\Components\Section;
@@ -90,7 +89,6 @@ class ProjectResource extends Resource
90 89
                             ])->columnSpanFull()->id('summaries'),
91 90
                         Radio::make('badge_type')->label('')->options([1 => '永續目標', 2 => '取得標章'])->default(1)->inline(),
92 91
                         Repeater::make('badgesTarget')->label('永續目標')->schema([
93
-                            Hidden::make('award_type')->default(1),
94 92
                             Select::make('badge_id')
95 93
                                 ->options(function () {
96 94
                                     return Badge::all()->mapWithKeys(function ($badge) {
@@ -108,11 +106,9 @@ class ProjectResource extends Resource
108 106
                                 ->label('')
109 107
                                 ->required(),
110 108
                         ])
111
-                            ->relationship('targetBadges')
112 109
                             ->reorderableWithButtons()
113
-                            ->orderColumn('sort_order'),
110
+                            ->defaultItems(0),
114 111
                         Repeater::make('badgesAward')->label('取得標章')->schema([
115
-                            Hidden::make('award_type')->default(2),
116 112
                             Select::make('badge_id')
117 113
                                 ->options(function () {
118 114
                                     return Badge::all()->mapWithKeys(function ($badge) {
@@ -136,9 +132,8 @@ class ProjectResource extends Resource
136 132
                                 ->native(false)
137 133
                                 ->closeOnDateSelection(),
138 134
                         ])
139
-                            ->relationship('awardBadges')
140 135
                             ->reorderableWithButtons()
141
-                            ->orderColumn('sort_order'),
136
+                            ->defaultItems(0),
142 137
                     ]),
143 138
                     Tab::make('開發歷程')->schema([
144 139
                         Repeater::make('histories')->label('')->schema([

+ 41
- 30
app/Filament/Resources/ProjectResource/Pages/CreateProject.php Wyświetl plik

@@ -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\CreateRecord;
8
+use Illuminate\Support\Facades\DB;
8 9
 
9 10
 class CreateProject extends CreateRecord
10 11
 {
@@ -21,13 +22,14 @@ class CreateProject extends CreateRecord
21 22
 
22 23
     protected function mutateFormDataBeforeCreate(array $data): array
23 24
     {
24
-        // 暫存標資料
25
-        if (isset($data['badgesTarget'])) {
25
+        // 暫存永續目標資料
26
+        if (isset($data['badgesTarget']) && is_array($data['badgesTarget'])) {
26 27
             $this->badgesTargetData = $data['badgesTarget'];
27 28
             unset($data['badgesTarget']);
28 29
         }
29
-        // 暫存標籤資料
30
-        if (isset($data['badgesAward'])) {
30
+
31
+        // 暫存取得標章資料
32
+        if (isset($data['badgesAward']) && is_array($data['badgesAward'])) {
31 33
             $this->badgesAwardData = $data['badgesAward'];
32 34
             unset($data['badgesAward']);
33 35
         }
@@ -37,40 +39,49 @@ class CreateProject extends CreateRecord
37 39
 
38 40
     protected function afterCreate(): void
39 41
     {
40
-        $syncData = [];
42
+        // 儲存永續目標
41 43
         if (! empty($this->badgesTargetData)) {
42 44
             foreach ($this->badgesTargetData as $index => $item) {
43
-                $syncData[$item['badge_id'].'_1'] = [
44
-                    'badge_id' => $item['badge_id'],
45
-                    'award_type' => 1,
46
-                    'award_date' => null,
47
-                    'sort_order' => $index,
48
-                ];
45
+                if (isset($item['badge_id'])) {
46
+                    DB::table('badgeables')->insert([
47
+                        'badge_id' => $item['badge_id'],
48
+                        'badgeable_id' => $this->record->id,
49
+                        'badgeable_type' => Project::class,
50
+                        'award_type' => 1,
51
+                        'award_date' => null,
52
+                        'sort_order' => $index,
53
+                        'created_at' => now(),
54
+                        'updated_at' => now(),
55
+                    ]);
56
+                }
49 57
             }
50 58
         }
51 59
 
60
+        // 儲存取得標章
52 61
         if (! empty($this->badgesAwardData)) {
53 62
             foreach ($this->badgesAwardData as $index => $item) {
54
-                $syncData[$item['badge_id'].'_2'] = [
55
-                    'badge_id' => $item['badge_id'],
56
-                    'award_type' => 2,
57
-                    'award_date' => $item['award_date'].'-01',
58
-                    'sort_order' => $index,
59
-                ];
60
-            }
61
-        }
63
+                if (isset($item['badge_id'])) {
64
+                    $awardDate = null;
65
+                    if (isset($item['award_date']) && ! empty($item['award_date'])) {
66
+                        // 處理日期格式:如果是 Y-m 格式,加上 -01
67
+                        $awardDate = $item['award_date'];
68
+                        if (strlen($awardDate) === 7) { // Y-m format
69
+                            $awardDate .= '-01';
70
+                        }
71
+                    }
62 72
 
63
-        foreach ($syncData as $data) {
64
-            \DB::table('badgeables')->insert([
65
-                'badge_id' => $data['badge_id'],
66
-                'badgeable_id' => $this->record->id,
67
-                'badgeable_type' => Project::class,
68
-                'award_type' => $data['award_type'],
69
-                'award_date' => $data['award_date'],
70
-                'sort_order' => $data['sort_order'],
71
-                'created_at' => now(),
72
-                'updated_at' => now(),
73
-            ]);
73
+                    DB::table('badgeables')->insert([
74
+                        'badge_id' => $item['badge_id'],
75
+                        'badgeable_id' => $this->record->id,
76
+                        'badgeable_type' => Project::class,
77
+                        'award_type' => 2,
78
+                        'award_date' => $awardDate,
79
+                        'sort_order' => $index,
80
+                        'created_at' => now(),
81
+                        'updated_at' => now(),
82
+                    ]);
83
+                }
84
+            }
74 85
         }
75 86
     }
76 87
 }

+ 59
- 40
app/Filament/Resources/ProjectResource/Pages/EditProject.php Wyświetl plik

@@ -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
 }