diff --git a/src/Actions/DeleteModel.php b/src/Actions/DeleteModel.php index e22be90b..4759e8e6 100644 --- a/src/Actions/DeleteModel.php +++ b/src/Actions/DeleteModel.php @@ -61,7 +61,21 @@ public function bypassesDirtyWarning(): bool public function run($items, $values) { - $items->each->delete(); + $failures = $items->reject(fn ($model) => $model->delete()); + $total = $items->count(); + + if ($failures->isNotEmpty()) { + $success = $total - $failures->count(); + if ($total === 1) { + throw new \Exception(__('Item could not be deleted')); + } elseif ($success === 0) { + throw new \Exception(__('Items could not be deleted')); + } else { + throw new \Exception(__(':success/:total items were deleted', ['total' => $total, 'success' => $success])); + } + } + + return trans_choice('Item deleted|Items deleted', $total); } public function redirect($items, $values) diff --git a/src/Actions/Publish.php b/src/Actions/Publish.php index 9bcafdbc..9a6a3121 100644 --- a/src/Actions/Publish.php +++ b/src/Actions/Publish.php @@ -44,6 +44,20 @@ public function buttonText() public function run($models, $values) { - $models->each(fn ($model) => $model->published(true)->save()); + $failures = $models->reject(fn ($model) => $model->published(true)->save()); + $total = $models->count(); + + if ($failures->isNotEmpty()) { + $success = $total - $failures->count(); + if ($total === 1) { + throw new \Exception(__('Model could not be published')); + } elseif ($success === 0) { + throw new \Exception(__('Model could not be published')); + } else { + throw new \Exception(__(':success/:total models were published', ['total' => $total, 'success' => $success])); + } + } + + return trans_choice('Model published|Models published', $total); } } diff --git a/src/Actions/Unpublish.php b/src/Actions/Unpublish.php index 3fa1d670..0c115ea4 100644 --- a/src/Actions/Unpublish.php +++ b/src/Actions/Unpublish.php @@ -44,6 +44,20 @@ public function buttonText() public function run($models, $values) { - $models->each(fn ($model) => $model->published(false)->save()); + $failures = $models->reject(fn ($model) => $model->published(false)->save()); + $total = $models->count(); + + if ($failures->isNotEmpty()) { + $success = $total - $failures->count(); + if ($total === 1) { + throw new \Exception(__('Model could not be unpublished')); + } elseif ($success === 0) { + throw new \Exception(__('Models could not be unpublished')); + } else { + throw new \Exception(__(':success/:total models were unpublished', ['total' => $total, 'success' => $success])); + } + } + + return trans_choice('Model unpublished|Models unpublished', $total); } } diff --git a/src/Traits/HasRunwayResource.php b/src/Traits/HasRunwayResource.php index f2d4bd7f..61f1ab45 100644 --- a/src/Traits/HasRunwayResource.php +++ b/src/Traits/HasRunwayResource.php @@ -263,7 +263,11 @@ public function publish($options = []) } if ($this->runwayResource()->hasPublishStates()) { - $this->published(true)->save(); + $saved = $this->published(true)->save(); + + if (! $saved) { + return false; + } } return $this; @@ -276,7 +280,11 @@ public function unpublish($options = []) } if ($this->runwayResource()->hasPublishStates()) { - $this->published(false)->save(); + $saved = $this->published(false)->save(); + + if (! $saved) { + return false; + } } return $this;