Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ReplicateBulkAction.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. class ReplicateBulkAction extends BulkAction implements ReplicatesRecords
  13. {
  14. use CanReplicateRecords;
  15. protected ?Closure $afterReplicaSaved = null;
  16. protected array $relationshipsToReplicate = [];
  17. public static function getDefaultName(): ?string
  18. {
  19. return 'replicate';
  20. }
  21. protected function setUp(): void
  22. {
  23. parent::setUp();
  24. $this->label(__('Replicate Selected'));
  25. $this->modalHeading(fn (): string => __('Replicate selected :label', ['label' => $this->getPluralModelLabel()]));
  26. $this->modalSubmitActionLabel(__('Replicate'));
  27. $this->successNotificationTitle(__('Replicated'));
  28. $this->icon('heroicon-m-square-3-stack-3d');
  29. $this->requiresConfirmation();
  30. $this->modalIcon('heroicon-o-square-3-stack-3d');
  31. $this->action(function () {
  32. $result = $this->process(function (Collection $records) {
  33. $records->each(function (Model $record) {
  34. $this->replica = $record->replicate($this->getExcludedAttributes());
  35. $this->replica->fill($record->attributesToArray());
  36. $this->callBeforeReplicaSaved();
  37. $this->replica->save();
  38. $this->replicateRelationships($record, $this->replica);
  39. $this->callAfterReplicaSaved($record, $this->replica);
  40. });
  41. });
  42. try {
  43. return $result;
  44. } finally {
  45. $this->success();
  46. }
  47. });
  48. }
  49. public function replicateRelationships(Model $original, Model $replica): void
  50. {
  51. foreach ($this->relationshipsToReplicate as $relationship) {
  52. $relation = $original->$relationship();
  53. if ($relation instanceof BelongsToMany) {
  54. $replica->$relationship()->sync($relation->pluck($relation->getRelated()->getKeyName()));
  55. } elseif ($relation instanceof HasMany) {
  56. $relation->each(function (Model $related) use ($replica, $relationship) {
  57. $relatedReplica = $related->replicate($this->getExcludedAttributes());
  58. $relatedReplica->{$replica->$relationship()->getForeignKeyName()} = $replica->getKey();
  59. $relatedReplica->save();
  60. });
  61. } elseif ($relation instanceof HasOne && $relation->exists()) {
  62. $related = $relation->first();
  63. $relatedReplica = $related->replicate($this->getExcludedAttributes());
  64. $relatedReplica->{$replica->$relationship()->getForeignKeyName()} = $replica->getKey();
  65. $relatedReplica->save();
  66. }
  67. }
  68. }
  69. public function withReplicatedRelationships(array $relationships): static
  70. {
  71. $this->relationshipsToReplicate = $relationships;
  72. return $this;
  73. }
  74. public function afterReplicaSaved(Closure $callback): static
  75. {
  76. $this->afterReplicaSaved = $callback;
  77. return $this;
  78. }
  79. public function callAfterReplicaSaved(Model $original, Model $replica): void
  80. {
  81. $this->evaluate($this->afterReplicaSaved, [
  82. 'original' => $original,
  83. 'replica' => $replica,
  84. ]);
  85. }
  86. }