prevent non empty category removal

This commit is contained in:
Yuri Kuznetsov
2024-09-05 15:47:28 +03:00
parent 55cdab7008
commit 9fb49b339d
2 changed files with 30 additions and 1 deletions
@@ -415,7 +415,9 @@
"select2OrMoreRecords": "Select 2 or more records",
"selectNotMoreThanNumberRecords": "Select not more than {number} records",
"selectAtLeastOneRecord": "Select at least one record",
"duplicateConflict": "A record already exists."
"duplicateConflict": "A record already exists.",
"cannotRemoveCategoryWithChildCategory": "Cannot remove a category that has a child category.",
"cannotRemoveNotEmptyCategory": "Cannot remove a non-empty category."
},
"boolFilters": {
"onlyMy": "Only My",
+27
View File
@@ -313,6 +313,33 @@ class RecordTree extends Record
}
}
/**
* @throws Forbidden
* @throws BadRequest
*/
protected function beforeDeleteEntity(Entity $entity)
{
parent::beforeDeleteEntity($entity);
$childCategory = $this->entityManager
->getRelation($entity, 'children')
->findOne();
if ($childCategory) {
throw Forbidden::createWithBody(
'cannotRemoveCategoryWithChildCategory',
Error\Body::create()->withMessageTranslation('cannotRemoveCategoryWithChildCategory')
);
}
if (!$this->checkItemIsEmpty($entity)) {
throw Forbidden::createWithBody(
'cannotRemoveNotEmptyCategory',
Error\Body::create()->withMessageTranslation('cannotRemoveNotEmptyCategory')
);
}
}
public function update(string $id, stdClass $data, UpdateParams $params): Entity
{
if (!empty($data->parentId) && $data->parentId === $id) {