mirror of
https://codeberg.org/artfulrobot/contactcats.git
synced 2025-06-24 21:38:06 +02:00
Merge branch 'main' of codeberg.org:artfulrobot/contactcats
This commit is contained in:
commit
5654aad7e6
1 changed files with 21 additions and 24 deletions
|
@ -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 = <<<SQL
|
||||
UPDATE civicrm_contact_category cc
|
||||
INNER JOIN civicrm_group_contact gc ON gc.contact_id = cc.id AND gc.status = 'Added' AND group_id = $groupID
|
||||
SET next_category = $groupID
|
||||
WHERE next_category = 0
|
||||
SQL;
|
||||
}
|
||||
else {
|
||||
// @todo refresh cache
|
||||
$sql = <<<SQL
|
||||
UPDATE civicrm_contact_category cc
|
||||
INNER JOIN civicrm_group_contact_cache gcc ON gcc.contact_id = cc.id AND group_id = $groupID
|
||||
SET next_category = $groupID
|
||||
WHERE next_category = 0
|
||||
SQL;
|
||||
}
|
||||
Civi::log()->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 = <<<SQL
|
||||
UPDATE civicrm_contact_category cc
|
||||
INNER JOIN $table gc ON gc.contact_id = cc.id AND group_id = $groupID $statusWhere
|
||||
SET next_category = $groupID
|
||||
WHERE next_category = 0
|
||||
SQL;
|
||||
CRM_Core_DAO::executeQuery($sql)->free();
|
||||
Civi::log()->debug('', ['=' => 'pop']);
|
||||
}
|
||||
|
||||
Civi::log()->debug("Calculate changes", ['=' => 'timed', '=start' => "changes"]);
|
||||
$changes = CRM_Core_DAO::executeQuery(<<<SQL
|
||||
SELECT id, category, next_category
|
||||
|
@ -120,21 +114,24 @@ class Sync extends \Civi\Api4\Generic\AbstractAction {
|
|||
if (empty($batch)) {
|
||||
return;
|
||||
}
|
||||
$oldCategoryId = (int) ($lastChange[0] ?? 0);
|
||||
$newCategoryId = (int) ($lastChange[1] ?? 0);
|
||||
// Create activity
|
||||
// Create activity on the first contact
|
||||
$a = Activity::create(FALSE)
|
||||
->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]);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue