From 2510921e431e7c514f563ffd7796eacaa1480473 Mon Sep 17 00:00:00 2001 From: Rich Lott / Artful Robot Date: Sat, 9 Mar 2024 23:57:27 +0000 Subject: [PATCH] Fix duplicate activity_contact rows --- Civi/Api4/Action/ContactCategory/Sync.php | 45 +++++++++++------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/Civi/Api4/Action/ContactCategory/Sync.php b/Civi/Api4/Action/ContactCategory/Sync.php index 77bcd01..7290ac3 100644 --- a/Civi/Api4/Action/ContactCategory/Sync.php +++ b/Civi/Api4/Action/ContactCategory/Sync.php @@ -40,6 +40,8 @@ class Sync extends \Civi\Api4\Generic\AbstractAction { Civi::log()->debug("Skipping because not due until " . date('H:i j M Y', $settings['updateAfter']), ['=' => 'pop']); return; } + // Ensure we're definitely dealing with integer Group IDs. + array_walk($settings['groupIDs'], fn (&$groupID) => $groupID = (int) $groupID); // Load category names $catNames = OptionValue::get(FALSE) @@ -79,33 +81,25 @@ class Sync extends \Civi\Api4\Generic\AbstractAction { // The default 'group' isn't a ... group. continue; } - // Get contacts in group. if (!$groups[$groupID]) { Civi::log()->warning("Group $groupID no longer exists."); continue; } + // Put unclaimed contacts in this group into this category. $isSmart = !empty($groups[$groupID]['saved_search_id']); - Civi::log()->debug($groups[$groupID]['title'], ['=' => 'timed', '=start' => "group$groupID"] + compact('isSmart')); - if (!$isSmart) { - $sql = <<debug($groups[$groupID]['title'] . ($isSmart ? '(smart)' : ''), ['=' => 'timed', '=start' => "group$groupID"]); + $table = $isSmart ? 'civicrm_group_contact_cache' : 'civicrm_group_contact'; + $statusWhere = $isSmart ? '' : 'AND gc.status = "Added"'; + $sql = <<free(); Civi::log()->debug('', ['=' => 'pop']); } + Civi::log()->debug("Calculate changes", ['=' => 'timed', '=start' => "changes"]); $changes = CRM_Core_DAO::executeQuery(<<addValue('target_contact_id', $batch[0]) ->addValue('activity_type_id:name', 'changed_contact_category') ->addValue('source_contact_id', \CRM_Core_BAO_Domain::getDomain()->contact_id) ->addValue('status_id:name', 'Completed') - ->addValue('subject', $catNames[$lastChange[0] ?? 0] . ' → ' . $catNames[$newCategoryId]) + ->addValue('subject', $catNames[$oldCategoryId] . ' → ' . $catNames[$newCategoryId]) ->execute()->first(); $activityID = (int) $a['id']; // Join activity to all relevant contacts - CRM_Core_DAO::executeQuery("INSERT INTO civicrm_activity_contact (contact_id, activity_id) - SELECT id contact_id, $activityID activity_id + CRM_Core_DAO::executeQuery("INSERT INTO civicrm_activity_contact (contact_id, activity_id, record_type_id) + SELECT id contact_id, $activityID activity_id, 3 record_type_id FROM civicrm_contact_category cc - WHERE cc.next_category = $newCategoryId + WHERE cc.category = $oldCategoryId + AND cc.next_category = $newCategoryId + AND contact_id <> $batch[0] ")->free(); Civi::log()->debug(count($batch) . " changes: " . $catNames[$lastChange[0] ?? 0] . ' → ' . $catNames[$lastChange[1] ?? 0]); };