Przeglądaj źródła

update with api

rodzic
commit
054cdb3571

+ 20
- 14
app/Filament/Resources/ProjectResource.php Wyświetl plik

5
 use AbdulmajeedJamaan\FilamentTranslatableTabs\TranslatableTabs;
5
 use AbdulmajeedJamaan\FilamentTranslatableTabs\TranslatableTabs;
6
 use App\Filament\Resources\ProjectResource\Pages;
6
 use App\Filament\Resources\ProjectResource\Pages;
7
 use App\Filament\Resources\ProjectResource\RelationManagers;
7
 use App\Filament\Resources\ProjectResource\RelationManagers;
8
+use App\Models\Badge;
8
 use App\Models\Project;
9
 use App\Models\Project;
9
 use App\Models\Region;
10
 use App\Models\Region;
10
 use Filament\Forms;
11
 use Filament\Forms;
82
                             app(DeepLService::class)->createTranslationAction("summaries", ["summaries","address",
83
                             app(DeepLService::class)->createTranslationAction("summaries", ["summaries","address",
83
                             "floor_plan","building_structure","design_unit"])
84
                             "floor_plan","building_structure","design_unit"])
84
                         ])->columnSpanFull()->id("summaries"),
85
                         ])->columnSpanFull()->id("summaries"),
85
-                        Radio::make("badge_type")->label("標章呈現方式")->options([1 => "永續目標", 2 => "取得標章"])->default(1)->inline(),
86
+                        Radio::make("badge_type")->label("")->options([1 => "永續目標", 2 => "取得標章"])->default(1)->inline(),
86
                         Repeater::make("badgesTarget")->label("永續目標")->schema([
87
                         Repeater::make("badgesTarget")->label("永續目標")->schema([
87
                             Hidden::make("award_type")->default(1),
88
                             Hidden::make("award_type")->default(1),
88
                             Select::make('badge_id')
89
                             Select::make('badge_id')
89
-                                ->relationship('badges', 'title')
90
-                                ->getOptionLabelFromRecordUsing(function ($record) {
91
-                                    $imageHtml = $record->img_url
92
-                                        ? '<img src="' . Storage::url($record->img_url) . '" class="w-6 h-6 rounded-full mr-2 inline-block" />'
93
-                                        : '<div class="w-6 h-6 bg-gray-200 rounded-full mr-2 inline-block"></div>';
94
-
95
-                                    return new \Illuminate\Support\HtmlString($imageHtml . $record->title);
90
+                                ->options(function () {
91
+                                    return Badge::all()->mapWithKeys(function ($badge) {
92
+                                        return [
93
+                                            $badge->id => '<div class="flex items-center gap-2">
94
+                                                <img src="' . Storage::url($badge->img_url) . '" class="w-6 h-6 rounded-full">
95
+                                                <span>' . $badge->title . '</span>
96
+                                            </div>'
97
+                                        ];
98
+                                    });
96
                                 })
99
                                 })
97
                                 ->allowHtml()
100
                                 ->allowHtml()
98
                                 ->preload()
101
                                 ->preload()
103
                         Repeater::make("badgesAward")->label("取得標章")->schema([
106
                         Repeater::make("badgesAward")->label("取得標章")->schema([
104
                             Hidden::make("award_type")->default(2),
107
                             Hidden::make("award_type")->default(2),
105
                             Select::make('badge_id')
108
                             Select::make('badge_id')
106
-                                ->getOptionLabelFromRecordUsing(function ($record) {
107
-                                    $imageHtml = $record->img_url
108
-                                        ? '<img src="' . Storage::url($record->img_url) . '" class="w-6 h-6 rounded-full mr-2 inline-block" />'
109
-                                        : '<div class="w-6 h-6 bg-gray-200 rounded-full mr-2 inline-block"></div>';
110
-
111
-                                    return new \Illuminate\Support\HtmlString($imageHtml . $record->title);
109
+                                ->options(function () {
110
+                                    return Badge::all()->mapWithKeys(function ($badge) {
111
+                                        return [
112
+                                            $badge->id => '<div class="flex items-center gap-2">
113
+                                                <img src="' . Storage::url($badge->img_url) . '" class="w-6 h-6 rounded-full">
114
+                                                <span>' . $badge->title . '</span>
115
+                                            </div>'
116
+                                        ];
117
+                                    });
112
                                 })
118
                                 })
113
                                 ->allowHtml()
119
                                 ->allowHtml()
114
                                 ->preload()
120
                                 ->preload()

+ 3
- 8
app/Filament/Resources/ProjectResource/Pages/CreateProject.php Wyświetl plik

18
 
18
 
19
     protected function mutateFormDataBeforeCreate(array $data): array
19
     protected function mutateFormDataBeforeCreate(array $data): array
20
     {
20
     {
21
-        \Log::info("mutateFormDataBeforeCreate 1", $data);
22
         // 暫存標籤資料
21
         // 暫存標籤資料
23
         if (isset($data['badgesTarget'])) {
22
         if (isset($data['badgesTarget'])) {
24
             $this->badgesTargetData = $data['badgesTarget'];
23
             $this->badgesTargetData = $data['badgesTarget'];
34
 
33
 
35
     protected function afterCreate(): void
34
     protected function afterCreate(): void
36
     {
35
     {
37
-        \Log::info("afterCreate 1");
36
+        $syncData = [];
38
         if (!empty($this->badgesTargetData)) {
37
         if (!empty($this->badgesTargetData)) {
39
             foreach ($this->badgesTargetData as $item) {
38
             foreach ($this->badgesTargetData as $item) {
40
                 $syncData[$item['badge_id'] . '_1'] = [
39
                 $syncData[$item['badge_id'] . '_1'] = [
45
             }
44
             }
46
         }
45
         }
47
 
46
 
48
-        \Log::info("afterCreate 2");
49
         if (!empty($this->badgesAwardData)) {
47
         if (!empty($this->badgesAwardData)) {
50
             foreach ($this->badgesAwardData as $item) {
48
             foreach ($this->badgesAwardData as $item) {
51
                 $syncData[$item['badge_id'] . '_2'] = [
49
                 $syncData[$item['badge_id'] . '_2'] = [
52
                     'badge_id' => $item['badge_id'],
50
                     'badge_id' => $item['badge_id'],
53
                     'award_type' => 2,
51
                     'award_type' => 2,
54
-                    'award_date' => $item['award_date'],
52
+                    'award_date' => $item['award_date'] . "-01",
55
                 ];
53
                 ];
56
             }
54
             }
57
         }
55
         }
58
-        \Log::info("afterCreate 3");
59
-
60
-        \Log::info("", $syncData);
61
 
56
 
62
         foreach ($syncData as $data) {
57
         foreach ($syncData as $data) {
63
             \DB::table('badgeables')->insert([
58
             \DB::table('badgeables')->insert([
64
-                'tag_id' => $data['tag_id'],
59
+                'badge_id' => $data['badge_id'],
65
                 'badgeable_id' => $this->record->id,
60
                 'badgeable_id' => $this->record->id,
66
                 'badgeable_type' => Project::class,
61
                 'badgeable_type' => Project::class,
67
                 'award_type' => $data['award_type'],
62
                 'award_type' => $data['award_type'],

+ 5
- 6
app/Filament/Resources/ProjectResource/Pages/EditProject.php Wyświetl plik

30
                 'award_type' => $item->award_type,
30
                 'award_type' => $item->award_type,
31
             ])->toArray();
31
             ])->toArray();
32
 
32
 
33
-        $data['badgesAwardData'] = \DB::table('badgeables')
33
+        $data['badgesAward'] = \DB::table('badgeables')
34
         ->where('badgeable_id', $project->id)
34
         ->where('badgeable_id', $project->id)
35
         ->where('badgeable_type', Project::class)
35
         ->where('badgeable_type', Project::class)
36
-        ->where('award_type', 1)
36
+        ->where('award_type', 2)
37
         ->get()
37
         ->get()
38
         ->map(fn ($item) => [
38
         ->map(fn ($item) => [
39
             'badge_id' => $item->badge_id,
39
             'badge_id' => $item->badge_id,
40
             'award_date' => $item->award_date,
40
             'award_date' => $item->award_date,
41
             'award_type' => $item->award_type,
41
             'award_type' => $item->award_type,
42
         ])->toArray();
42
         ])->toArray();
43
-
44
         return $data;
43
         return $data;
45
     }
44
     }
46
 
45
 
61
 
60
 
62
     protected function afterSave(): void
61
     protected function afterSave(): void
63
     {
62
     {
64
-
63
+        $syncData = [];
65
         // ✅ 先刪除所有舊的標籤
64
         // ✅ 先刪除所有舊的標籤
66
         \DB::table('badgeables')
65
         \DB::table('badgeables')
67
             ->where('badgeable_id', $this->record->id)
66
             ->where('badgeable_id', $this->record->id)
83
                 $syncData[$item['badge_id'] . '_2'] = [
82
                 $syncData[$item['badge_id'] . '_2'] = [
84
                     'badge_id' => $item['badge_id'],
83
                     'badge_id' => $item['badge_id'],
85
                     'award_type' => 2,
84
                     'award_type' => 2,
86
-                    'award_date' => $item['award_date'],
85
+                    'award_date' => $item['award_date'] . "-01",
87
                 ];
86
                 ];
88
             }
87
             }
89
         }
88
         }
90
 
89
 
91
         foreach ($syncData as $data) {
90
         foreach ($syncData as $data) {
92
             \DB::table('badgeables')->insert([
91
             \DB::table('badgeables')->insert([
93
-                'tag_id' => $data['tag_id'],
92
+                'badge_id' => $data['badge_id'],
94
                 'badgeable_id' => $this->record->id,
93
                 'badgeable_id' => $this->record->id,
95
                 'badgeable_type' => Project::class,
94
                 'badgeable_type' => Project::class,
96
                 'award_type' => $data['award_type'],
95
                 'award_type' => $data['award_type'],

+ 1
- 1
database/migrations/2025_09_18_093938_create_badgeables_table.php Wyświetl plik

15
             $table->id();
15
             $table->id();
16
             $table->foreignId('badge_id')->constrained()->cascadeOnDelete();
16
             $table->foreignId('badge_id')->constrained()->cascadeOnDelete();
17
             $table->morphs('badgeable');
17
             $table->morphs('badgeable');
18
-            $table->date("award_date")->comment("取得標章時刻");
18
+            $table->date("award_date")->nullable()->comment("取得標章時刻");
19
             $table->tinyInteger("award_type")->comment("1. 永續目標 2.取得標章");
19
             $table->tinyInteger("award_type")->comment("1. 永續目標 2.取得標章");
20
             $table->timestamps();
20
             $table->timestamps();
21
         });
21
         });