diff --git a/app/resources/locales/en_US/error.po b/app/resources/locales/en_US/error.po index 6731c0a12..b78c8eae1 100644 --- a/app/resources/locales/en_US/error.po +++ b/app/resources/locales/en_US/error.po @@ -127,6 +127,9 @@ msgstr "Authenticator is not currently locked" msgid "Cos.active" msgstr "Requested CO {0} is not active" +msgid "Cous.children" +msgstr "Cous has {0} child(ren) and cannot be deleted" + msgid "EmailAddresses.mail.delivery" msgstr "No verified Email Address is available for Person {0}" diff --git a/app/src/Model/Table/CousTable.php b/app/src/Model/Table/CousTable.php index 80c672cbe..098a89f2f 100644 --- a/app/src/Model/Table/CousTable.php +++ b/app/src/Model/Table/CousTable.php @@ -149,6 +149,11 @@ public function initialize(array $config): void { */ public function buildRules(RulesChecker $rules): RulesChecker { + // AR-COU-2 A COU may not be deleted if it has any children. + $rules->addDelete([$this, 'ruleHasChildren'], + 'hasChildrenDelete', + ['errorField' => 'parent_id']); + // AR-COU-3 Two COUs within the same CO cannot share the same name $rules->add($rules->isUnique(['name', 'co_id'], __d('error', 'exists', [__d('controller', 'Cous', [1])]))); @@ -284,6 +289,27 @@ public function postClone( dataSource: $targetDataSource ); } + + /** + * Application Rule to determine if the group has children. + * + * @since COmanage Registry v5.2.0 + * @param Entity $entity Entity to be validated + * @param array $options Application rule options + * @return boolean true if the Rule check passes, false otherwise + */ + + public function ruleHasChildren($entity, $options) { + $count = $this->find('all') + ->where(['parent_id' => $entity->id]) + ->count(); + + if($count > 0) { + return __d('error', 'Cous.children', [$count]); + } + + return true; + } /** * Perform initial setup for a COU.