You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReplicateBulkAction.php 4.8KB

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