|
@@ -0,0 +1,114 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace App\Filament\Tables\Actions;
|
|
4
|
+
|
|
5
|
+use Closure;
|
|
6
|
+use Filament\Actions\Concerns\CanReplicateRecords;
|
|
7
|
+use Filament\Actions\Contracts\ReplicatesRecords;
|
|
8
|
+use Filament\Tables\Actions\BulkAction;
|
|
9
|
+use Illuminate\Database\Eloquent\Collection;
|
|
10
|
+use Illuminate\Database\Eloquent\Model;
|
|
11
|
+use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
12
|
+use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
13
|
+use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
14
|
+
|
|
15
|
+class ReplicateBulkAction extends BulkAction implements ReplicatesRecords
|
|
16
|
+{
|
|
17
|
+ use CanReplicateRecords;
|
|
18
|
+
|
|
19
|
+ protected ?Closure $afterReplicaSaved = null;
|
|
20
|
+
|
|
21
|
+ protected array $relationshipsToReplicate = [];
|
|
22
|
+
|
|
23
|
+ public static function getDefaultName(): ?string
|
|
24
|
+ {
|
|
25
|
+ return 'replicate';
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ protected function setUp(): void
|
|
29
|
+ {
|
|
30
|
+ parent::setUp();
|
|
31
|
+
|
|
32
|
+ $this->label(__('Replicate Selected'));
|
|
33
|
+
|
|
34
|
+ $this->modalHeading(fn (): string => __('Replicate selected :label', ['label' => $this->getPluralModelLabel()]));
|
|
35
|
+
|
|
36
|
+ $this->modalSubmitActionLabel(__('Replicate'));
|
|
37
|
+
|
|
38
|
+ $this->successNotificationTitle(__('Replicated'));
|
|
39
|
+
|
|
40
|
+ $this->icon('heroicon-m-square-3-stack-3d');
|
|
41
|
+
|
|
42
|
+ $this->requiresConfirmation();
|
|
43
|
+
|
|
44
|
+ $this->modalIcon('heroicon-o-square-3-stack-3d');
|
|
45
|
+
|
|
46
|
+ $this->action(function () {
|
|
47
|
+ $result = $this->process(function (Collection $records) {
|
|
48
|
+ $records->each(function (Model $record) {
|
|
49
|
+ $this->replica = $record->replicate($this->getExcludedAttributes());
|
|
50
|
+
|
|
51
|
+ $this->replica->fill($record->attributesToArray());
|
|
52
|
+
|
|
53
|
+ $this->callBeforeReplicaSaved();
|
|
54
|
+
|
|
55
|
+ $this->replica->save();
|
|
56
|
+
|
|
57
|
+ $this->replicateRelationships($record, $this->replica);
|
|
58
|
+
|
|
59
|
+ $this->callAfterReplicaSaved($record, $this->replica);
|
|
60
|
+ });
|
|
61
|
+ });
|
|
62
|
+
|
|
63
|
+ try {
|
|
64
|
+ return $result;
|
|
65
|
+ } finally {
|
|
66
|
+ $this->success();
|
|
67
|
+ }
|
|
68
|
+ });
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ public function replicateRelationships(Model $original, Model $replica): void
|
|
72
|
+ {
|
|
73
|
+ foreach ($this->relationshipsToReplicate as $relationship) {
|
|
74
|
+ $relation = $original->$relationship();
|
|
75
|
+
|
|
76
|
+ if ($relation instanceof BelongsToMany) {
|
|
77
|
+ $replica->$relationship()->sync($relation->pluck($relation->getRelated()->getKeyName()));
|
|
78
|
+ } elseif ($relation instanceof HasMany) {
|
|
79
|
+ $relation->each(function (Model $related) use ($replica, $relationship) {
|
|
80
|
+ $relatedReplica = $related->replicate($this->getExcludedAttributes());
|
|
81
|
+ $relatedReplica->{$replica->$relationship()->getForeignKeyName()} = $replica->getKey();
|
|
82
|
+ $relatedReplica->save();
|
|
83
|
+ });
|
|
84
|
+ } elseif ($relation instanceof HasOne && $relation->exists()) {
|
|
85
|
+ $related = $relation->first();
|
|
86
|
+ $relatedReplica = $related->replicate($this->getExcludedAttributes());
|
|
87
|
+ $relatedReplica->{$replica->$relationship()->getForeignKeyName()} = $replica->getKey();
|
|
88
|
+ $relatedReplica->save();
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ public function withReplicatedRelationships(array $relationships): static
|
|
94
|
+ {
|
|
95
|
+ $this->relationshipsToReplicate = $relationships;
|
|
96
|
+
|
|
97
|
+ return $this;
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ public function afterReplicaSaved(Closure $callback): static
|
|
101
|
+ {
|
|
102
|
+ $this->afterReplicaSaved = $callback;
|
|
103
|
+
|
|
104
|
+ return $this;
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ public function callAfterReplicaSaved(Model $original, Model $replica): void
|
|
108
|
+ {
|
|
109
|
+ $this->evaluate($this->afterReplicaSaved, [
|
|
110
|
+ 'original' => $original,
|
|
111
|
+ 'replica' => $replica,
|
|
112
|
+ ]);
|
|
113
|
+ }
|
|
114
|
+}
|