Skip to content

Commit

Permalink
Move logic into batch mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Dec 10, 2024
1 parent b2ff6f7 commit bbc7423
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 52 deletions.
29 changes: 3 additions & 26 deletions config/sections/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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'),
);
}
]
];
Expand Down
33 changes: 33 additions & 0 deletions config/sections/mixins/batch.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Kirby\Exception\Exception;
use Kirby\Exception\PermissionException;
use Kirby\Toolkit\I18n;

return [
'props' => [
/**
Expand All @@ -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;
}
]
];
29 changes: 3 additions & 26 deletions config/sections/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'),
);
}
]
];
Expand Down

0 comments on commit bbc7423

Please sign in to comment.