From 1770206626a5fdbc9cb83b2a9b3f72aec440933a Mon Sep 17 00:00:00 2001 From: Benn Oshrin Date: Mon, 20 Jun 2022 16:04:44 -0400 Subject: [PATCH] Additional commit for Groups (CFM-38) --- app/src/Model/Table/CosTable.php | 14 ++++++++++---- app/src/Model/Table/CousTable.php | 22 +++++++++++++++++++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/src/Model/Table/CosTable.php b/app/src/Model/Table/CosTable.php index 57c400158..b5f6e9fa1 100644 --- a/app/src/Model/Table/CosTable.php +++ b/app/src/Model/Table/CosTable.php @@ -170,10 +170,16 @@ public function findCOmanageCO(Query $query): Query { */ public function localAfterSave(\Cake\Event\EventInterface $event, \Cake\Datasource\EntityInterface $entity, \ArrayObject $options) { - if($entity->isNew() && !empty($entity->id)) { - // Run setup for new CO - - $this->setup($entity->id); + if(!empty($entity->id)) { + if($entity->isNew()) { + // Run setup for new CO + + $this->setup($entity->id); + } elseif($entity->getOriginal('name') != $entity->get('name')) { + // AR-CO-7 The name was changed, so we may need to update the system groups + + $this->Groups->addDefaults(coId: $entity->id, couId: null, rename: true); + } } return true; diff --git a/app/src/Model/Table/CousTable.php b/app/src/Model/Table/CousTable.php index e5847fd95..7b9506a0f 100644 --- a/app/src/Model/Table/CousTable.php +++ b/app/src/Model/Table/CousTable.php @@ -131,10 +131,23 @@ public function buildRules(RulesChecker $rules): RulesChecker { */ public function localAfterSave(\Cake\Event\EventInterface $event, \Cake\Datasource\EntityInterface $entity, \ArrayObject $options) { + if(!empty($entity->id)) { + if($entity->isNew()) { + // Run setup for new COU + + $this->setup(id: $entity->id, coId: $entity->co_id); + } elseif($entity->getOriginal('name') != $entity->get('name')) { + // AR-COU-5 The name was changed, so we may need to update the system groups + + $this->Groups->addDefaults(coId: $entity->co_id, couId: $entity->id, rename: true); + } + } + + if($entity->isNew() && !empty($entity->id)) { // Run setup for new COU - $this->setup($entity->id, $entity->co_id); + $this->setup(id: $entity->id, coId: $entity->co_id); } return true; @@ -187,7 +200,10 @@ public function potentialParents(int $coId, int $id=null, bool $hierarchy=false) public function rulePotentialParent($entity, $options) { // We want negative logic since we want to fail if we're editing the COmanage CO if(!empty($entity->parent_id)) { - $potentialParents = $this->potentialParents($entity->co_id, (!empty($entity->id) ? $entity->id : null)); + $potentialParents = $this->potentialParents( + coId: $entity->co_id, + id: (!empty($entity->id) ? $entity->id : null) + ); if(!isset($potentialParents[$entity->parent_id])) { return __d('error', 'cou.parent'); @@ -208,7 +224,7 @@ public function rulePotentialParent($entity, $options) { public function setup(int $id, int $coId): bool { // AR-COU-4 Create the default groups - $this->Groups->addDefaults($coId, $id); + $this->Groups->addDefaults(coId: $coId, couId: $id); return true; }