diff --git a/config/sections/files.php b/config/sections/files.php index 31fb2a5766..ef2c103073 100644 --- a/config/sections/files.php +++ b/config/sections/files.php @@ -2,8 +2,6 @@ use Kirby\Cms\File; use Kirby\Cms\Files; -use Kirby\Exception\Exception; -use Kirby\Exception\PermissionException; use Kirby\Toolkit\I18n; return [ @@ -226,30 +224,9 @@ 'pattern' => 'delete', 'method' => 'DELETE', 'action' => function () { - $section = $this->section(); - - // check if batch deletion is allowed - if ($section->batch() === false) { - throw new PermissionException( - message: 'The section does not support batch actions' - ); - } - - $ids = $this->requestBody('ids'); - $min = $section->min(); - - // check if the section has enough files after the deletion - if ($section->total() - count($ids) < $min) { - throw new Exception( - message: I18n::template('error.section.files.min.' . I18n::form($min), [ - 'min' => $min, - 'section' => $section->headline() - ]) - ); - } - - $section->models()->delete($ids); - return true; + return $this->section()->deleteSelected( + ids: $this->requestBody('ids'), + ); } ] ]; diff --git a/config/sections/mixins/batch.php b/config/sections/mixins/batch.php index 6c917635ca..25128f253a 100644 --- a/config/sections/mixins/batch.php +++ b/config/sections/mixins/batch.php @@ -1,5 +1,9 @@ [ /** @@ -9,4 +13,33 @@ return $batch; }, ], + 'methods' => [ + 'deleteSelected' => function (array $ids): bool { + if ($ids === []) { + return true; + } + + // check if batch deletion is allowed + if ($this->batch() === false) { + throw new PermissionException( + message: 'The section does not support batch actions' + ); + } + + $min = $this->min(); + + // check if the section has enough items after the deletion + if ($this->total() - count($ids) < $min) { + throw new Exception( + message: I18n::template('error.section.' . $this->type() . '.min.' . I18n::form($min), [ + 'min' => $min, + 'section' => $this->headline() + ]) + ); + } + + $this->models()->delete($ids); + return true; + } + ] ]; diff --git a/config/sections/pages.php b/config/sections/pages.php index 8288f8b242..e53a577a39 100644 --- a/config/sections/pages.php +++ b/config/sections/pages.php @@ -4,9 +4,7 @@ use Kirby\Cms\Page; use Kirby\Cms\Pages; use Kirby\Cms\Site; -use Kirby\Exception\Exception; use Kirby\Exception\InvalidArgumentException; -use Kirby\Exception\PermissionException; use Kirby\Toolkit\A; use Kirby\Toolkit\I18n; @@ -325,30 +323,9 @@ 'pattern' => 'delete', 'method' => 'DELETE', 'action' => function () { - $section = $this->section(); - - // check if batch deletion is allowed - if ($section->batch() === false) { - throw new PermissionException( - message: 'The section does not support batch actions' - ); - } - - $ids = $this->requestBody('ids'); - $min = $section->min(); - - // check if the section has enough pages after the deletion - if ($section->total() - count($ids) < $min) { - throw new Exception( - message: I18n::template('error.section.pages.min.' . I18n::form($min), [ - 'min' => $min, - 'section' => $section->headline() - ]) - ); - } - - $section->models()->delete($ids); - return true; + return $this->section()->deleteSelected( + ids: $this->requestBody('ids'), + ); } ] ];