From 49ca6217ca06b848fd85631d0dd66a0713cc30d4 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Thu, 11 Jul 2024 10:38:39 +0200 Subject: [PATCH] Contact(group)Form: Delete `timeperiod_entry` when the member is deleted --- application/forms/ContactGroupForm.php | 13 +++++++++++-- library/Notifications/Web/Form/ContactForm.php | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/application/forms/ContactGroupForm.php b/application/forms/ContactGroupForm.php index 07608ab1..99a8b9db 100644 --- a/application/forms/ContactGroupForm.php +++ b/application/forms/ContactGroupForm.php @@ -297,15 +297,24 @@ public function removeContactgroup(): void $markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y']; $updateCondition = ['contactgroup_id = ?' => $this->contactgroupId, 'deleted = ?' => 'n']; - $rotationIds = $this->db->fetchCol( + $rotationAndMemberIds = $this->db->fetchPairs( RotationMember::on($this->db) - ->columns('rotation_id') + ->columns(['id', 'rotation_id']) ->filter(Filter::equal('contactgroup_id', $this->contactgroupId)) ->assembleSelect() ); + $rotationMemberIds = array_keys($rotationAndMemberIds); + $rotationIds = array_values($rotationAndMemberIds); + $this->db->update('rotation_member', $markAsDeleted + ['position' => null], $updateCondition); + $this->db->update( + 'timeperiod_entry', + $markAsDeleted, + ['rotation_member_id IN (?)' => $rotationMemberIds, 'deleted = ?' => 'n'] + ); + if (! empty($rotationIds)) { $rotationIdsWithOtherMembers = $this->db->fetchCol( RotationMember::on($this->db) diff --git a/library/Notifications/Web/Form/ContactForm.php b/library/Notifications/Web/Form/ContactForm.php index de8b5cd4..ebbbec60 100644 --- a/library/Notifications/Web/Form/ContactForm.php +++ b/library/Notifications/Web/Form/ContactForm.php @@ -265,15 +265,24 @@ public function removeContact(): void $markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y']; $updateCondition = ['contact_id = ?' => $this->contactId, 'deleted = ?' => 'n']; - $rotationIds = $this->db->fetchCol( + $rotationAndMemberIds = $this->db->fetchPairs( RotationMember::on($this->db) - ->columns('rotation_id') + ->columns(['id', 'rotation_id']) ->filter(Filter::equal('contact_id', $this->contactId)) ->assembleSelect() ); + $rotationMemberIds = array_keys($rotationAndMemberIds); + $rotationIds = array_values($rotationAndMemberIds); + $this->db->update('rotation_member', $markAsDeleted + ['position' => null], $updateCondition); + $this->db->update( + 'timeperiod_entry', + $markAsDeleted, + ['rotation_member_id IN (?)' => $rotationMemberIds, 'deleted = ?' => 'n'] + ); + if (! empty($rotationIds)) { $rotationIdsWithOtherMembers = $this->db->fetchCol( RotationMember::on($this->db)