Skip to content

Commit

Permalink
Move batch delete logic into the pages and files classes
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Dec 7, 2024
1 parent cc99cb6 commit 20cc97f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 44 deletions.
24 changes: 2 additions & 22 deletions config/sections/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@
);
}

$ids = $this->requestBody('ids');
$errors = [];
$ids = $this->requestBody('ids');

// check if the section has enough files after the deletion
if ($section->total() - count($ids) < $section->min()) {
Expand All @@ -245,26 +244,7 @@
);
}

// delete all files and collect errors
foreach ($ids as $id) {
try {
$section->kirby()->file($id)?->delete();
} catch (Throwable $e) {
$errors[] = [
'label' => $id,
'message' => $e->getMessage()
];
}
}

// throw an error if not all files could be deleted
if ($errors !== []) {
throw new Exception(
message: 'Not all files could be deleted',
details: $errors
);
}

$section->models()->delete($ids);
return true;
}
]
Expand Down
24 changes: 2 additions & 22 deletions config/sections/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@
);
}

$ids = $this->requestBody('ids');
$errors = [];
$ids = $this->requestBody('ids');

// check if the section has enough pages after the deletion
if ($section->total() - count($ids) < $section->min()) {
Expand All @@ -344,26 +343,7 @@
);
}

// delete all pages and collect errors
foreach ($ids as $id) {
try {
$section->kirby()->page($id)?->delete();
} catch (Throwable $e) {
$errors[] = [
'label' => $id,
'message' => $e->getMessage()
];
}
}

// throw an error if not all pages could be deleted
if ($errors !== []) {
throw new Exception(
message: 'Not all pages could be deleted',
details: $errors
);
}

$section->models()->delete($ids);
return true;
}
]
Expand Down
37 changes: 37 additions & 0 deletions src/Cms/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Kirby\Cms;

use Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Filesystem\F;
use Kirby\Uuid\HasUuids;
use Throwable;

/**
* The `$files` object extends the general
Expand Down Expand Up @@ -93,6 +96,40 @@ public function changeSort(array $files, int $offset = 0): static
return $this;
}

/**
* Deletes the files with the given IDs
* if they exist in the collection
*
* @throws \Kirby\Exception\Exception If not all files could be deleted
*/
public function delete(array $ids): void
{
$exceptions = [];

// delete all pages and collect errors
foreach ($ids as $id) {
try {
$model = $this->get($id);

if ($model instanceof File === false) {
throw new NotFoundException(
key: 'file.notFound'
);
}

$model->delete();
} catch (Throwable $e) {
$exceptions[$id] = $e;
}
}

if ($exceptions !== []) {
throw new Exception(
message: 'Not all files could be deleted',
);
}
}

/**
* Creates a files collection from an array of props
*/
Expand Down
37 changes: 37 additions & 0 deletions src/Cms/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Kirby\Cms;

use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Uuid\HasUuids;
use Throwable;

/**
* The `$pages` object refers to a
Expand Down Expand Up @@ -118,6 +121,40 @@ public function code(): Files
return $this->files()->filter('type', 'code');
}

/**
* Deletes the pages with the given IDs
* if they exist in the collection
*
* @throws \Kirby\Exception\Exception If not all pages could be deleted
*/
public function delete(array $ids): void
{
$exceptions = [];

// delete all pages and collect errors
foreach ($ids as $id) {
try {
$model = $this->get($id);

if ($model instanceof Page === false) {
throw new NotFoundException(
key: 'page.notFound'
);
}

$model->delete();
} catch (Throwable $e) {
$exceptions[$id] = $e;
}
}

if ($exceptions !== []) {
throw new Exception(
message: 'Not all pages could be deleted',
);
}
}

/**
* Returns all documents of all children
*/
Expand Down

0 comments on commit 20cc97f

Please sign in to comment.