Andrew 2 месяцев назад
Родитель
Сommit
2c5bfcd9bc

+ 2
- 7
app/Filament/Resources/ProjectResource.php Просмотреть файл

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

+ 41
- 30
app/Filament/Resources/ProjectResource/Pages/CreateProject.php Просмотреть файл

5
 use App\Filament\Resources\ProjectResource;
5
 use App\Filament\Resources\ProjectResource;
6
 use App\Models\Project;
6
 use App\Models\Project;
7
 use Filament\Resources\Pages\CreateRecord;
7
 use Filament\Resources\Pages\CreateRecord;
8
+use Illuminate\Support\Facades\DB;
8
 
9
 
9
 class CreateProject extends CreateRecord
10
 class CreateProject extends CreateRecord
10
 {
11
 {
21
 
22
 
22
     protected function mutateFormDataBeforeCreate(array $data): array
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
             $this->badgesTargetData = $data['badgesTarget'];
27
             $this->badgesTargetData = $data['badgesTarget'];
27
             unset($data['badgesTarget']);
28
             unset($data['badgesTarget']);
28
         }
29
         }
29
-        // 暫存標籤資料
30
-        if (isset($data['badgesAward'])) {
30
+
31
+        // 暫存取得標章資料
32
+        if (isset($data['badgesAward']) && is_array($data['badgesAward'])) {
31
             $this->badgesAwardData = $data['badgesAward'];
33
             $this->badgesAwardData = $data['badgesAward'];
32
             unset($data['badgesAward']);
34
             unset($data['badgesAward']);
33
         }
35
         }
37
 
39
 
38
     protected function afterCreate(): void
40
     protected function afterCreate(): void
39
     {
41
     {
40
-        $syncData = [];
42
+        // 儲存永續目標
41
         if (! empty($this->badgesTargetData)) {
43
         if (! empty($this->badgesTargetData)) {
42
             foreach ($this->badgesTargetData as $index => $item) {
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
         if (! empty($this->badgesAwardData)) {
61
         if (! empty($this->badgesAwardData)) {
53
             foreach ($this->badgesAwardData as $index => $item) {
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 Просмотреть файл

5
 use App\Filament\Resources\ProjectResource;
5
 use App\Filament\Resources\ProjectResource;
6
 use App\Models\Project;
6
 use App\Models\Project;
7
 use Filament\Resources\Pages\EditRecord;
7
 use Filament\Resources\Pages\EditRecord;
8
+use Illuminate\Support\Facades\DB;
8
 
9
 
9
 class EditProject extends EditRecord
10
 class EditProject extends EditRecord
10
 {
11
 {
23
     {
24
     {
24
         $project = $this->record;
25
         $project = $this->record;
25
 
26
 
26
-        $data['badgesTarget'] = \DB::table('badgeables')
27
+        // 載入永續目標
28
+        $data['badgesTarget'] = DB::table('badgeables')
27
             ->where('badgeable_id', $project->id)
29
             ->where('badgeable_id', $project->id)
28
             ->where('badgeable_type', Project::class)
30
             ->where('badgeable_type', Project::class)
29
             ->where('award_type', 1)
31
             ->where('award_type', 1)
31
             ->get()
33
             ->get()
32
             ->map(fn ($item) => [
34
             ->map(fn ($item) => [
33
                 'badge_id' => $item->badge_id,
35
                 'badge_id' => $item->badge_id,
34
-                'award_type' => $item->award_type,
35
             ])->toArray();
36
             ])->toArray();
36
 
37
 
37
-        $data['badgesAward'] = \DB::table('badgeables')
38
+        // 載入取得標章
39
+        $data['badgesAward'] = DB::table('badgeables')
38
             ->where('badgeable_id', $project->id)
40
             ->where('badgeable_id', $project->id)
39
             ->where('badgeable_type', Project::class)
41
             ->where('badgeable_type', Project::class)
40
             ->where('award_type', 2)
42
             ->where('award_type', 2)
41
             ->orderBy('sort_order')
43
             ->orderBy('sort_order')
42
             ->get()
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
         return $data;
58
         return $data;
50
     }
59
     }
51
 
60
 
52
     protected function mutateFormDataBeforeSave(array $data): array
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
             $this->badgesTargetData = $data['badgesTarget'];
65
             $this->badgesTargetData = $data['badgesTarget'];
57
             unset($data['badgesTarget']);
66
             unset($data['badgesTarget']);
58
         }
67
         }
59
-        // 暫存標籤資料
60
-        if (isset($data['badgesAward'])) {
68
+
69
+        // 暫存取得標章資料
70
+        if (isset($data['badgesAward']) && is_array($data['badgesAward'])) {
61
             $this->badgesAwardData = $data['badgesAward'];
71
             $this->badgesAwardData = $data['badgesAward'];
62
             unset($data['badgesAward']);
72
             unset($data['badgesAward']);
63
         }
73
         }
67
 
77
 
68
     protected function afterSave(): void
78
     protected function afterSave(): void
69
     {
79
     {
70
-        $syncData = [];
71
-        // ✅ 先刪除所有舊的標籤
72
-        \DB::table('badgeables')
80
+        // 先刪除所有舊的標籤關聯
81
+        DB::table('badgeables')
73
             ->where('badgeable_id', $this->record->id)
82
             ->where('badgeable_id', $this->record->id)
74
             ->where('badgeable_type', Project::class)
83
             ->where('badgeable_type', Project::class)
75
             ->delete();
84
             ->delete();
76
 
85
 
86
+        // 儲存永續目標
77
         if (! empty($this->badgesTargetData)) {
87
         if (! empty($this->badgesTargetData)) {
78
             foreach ($this->badgesTargetData as $index => $item) {
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
         if (! empty($this->badgesAwardData)) {
105
         if (! empty($this->badgesAwardData)) {
89
             foreach ($this->badgesAwardData as $index => $item) {
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
 }