mirror of
https://codeberg.org/artfulrobot/contactcats.git
synced 2025-06-25 09:48:05 +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']);
|
Civi::log()->debug("Skipping because not due until " . date('H:i j M Y', $settings['updateAfter']), ['=' => 'pop']);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Ensure we're definitely dealing with integer Group IDs.
|
||||||
|
array_walk($settings['groupIDs'], fn (&$groupID) => $groupID = (int) $groupID);
|
||||||
|
|
||||||
// Load category names
|
// Load category names
|
||||||
$catNames = OptionValue::get(FALSE)
|
$catNames = OptionValue::get(FALSE)
|
||||||
|
@ -79,33 +81,25 @@ class Sync extends \Civi\Api4\Generic\AbstractAction {
|
||||||
// The default 'group' isn't a ... group.
|
// The default 'group' isn't a ... group.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Get contacts in group.
|
|
||||||
if (!$groups[$groupID]) {
|
if (!$groups[$groupID]) {
|
||||||
Civi::log()->warning("Group $groupID no longer exists.");
|
Civi::log()->warning("Group $groupID no longer exists.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
// Put unclaimed contacts in this group into this category.
|
||||||
$isSmart = !empty($groups[$groupID]['saved_search_id']);
|
$isSmart = !empty($groups[$groupID]['saved_search_id']);
|
||||||
Civi::log()->debug($groups[$groupID]['title'], ['=' => 'timed', '=start' => "group$groupID"] + compact('isSmart'));
|
Civi::log()->debug($groups[$groupID]['title'] . ($isSmart ? '(smart)' : ''), ['=' => 'timed', '=start' => "group$groupID"]);
|
||||||
if (!$isSmart) {
|
$table = $isSmart ? 'civicrm_group_contact_cache' : 'civicrm_group_contact';
|
||||||
$sql = <<<SQL
|
$statusWhere = $isSmart ? '' : 'AND gc.status = "Added"';
|
||||||
UPDATE civicrm_contact_category cc
|
$sql = <<<SQL
|
||||||
INNER JOIN civicrm_group_contact gc ON gc.contact_id = cc.id AND gc.status = 'Added' AND group_id = $groupID
|
UPDATE civicrm_contact_category cc
|
||||||
SET next_category = $groupID
|
INNER JOIN $table gc ON gc.contact_id = cc.id AND group_id = $groupID $statusWhere
|
||||||
WHERE next_category = 0
|
SET next_category = $groupID
|
||||||
SQL;
|
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;
|
|
||||||
}
|
|
||||||
CRM_Core_DAO::executeQuery($sql)->free();
|
CRM_Core_DAO::executeQuery($sql)->free();
|
||||||
Civi::log()->debug('', ['=' => 'pop']);
|
Civi::log()->debug('', ['=' => 'pop']);
|
||||||
}
|
}
|
||||||
|
|
||||||
Civi::log()->debug("Calculate changes", ['=' => 'timed', '=start' => "changes"]);
|
Civi::log()->debug("Calculate changes", ['=' => 'timed', '=start' => "changes"]);
|
||||||
$changes = CRM_Core_DAO::executeQuery(<<<SQL
|
$changes = CRM_Core_DAO::executeQuery(<<<SQL
|
||||||
SELECT id, category, next_category
|
SELECT id, category, next_category
|
||||||
|
@ -120,21 +114,24 @@ class Sync extends \Civi\Api4\Generic\AbstractAction {
|
||||||
if (empty($batch)) {
|
if (empty($batch)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
$oldCategoryId = (int) ($lastChange[0] ?? 0);
|
||||||
$newCategoryId = (int) ($lastChange[1] ?? 0);
|
$newCategoryId = (int) ($lastChange[1] ?? 0);
|
||||||
// Create activity
|
// Create activity on the first contact
|
||||||
$a = Activity::create(FALSE)
|
$a = Activity::create(FALSE)
|
||||||
->addValue('target_contact_id', $batch[0])
|
->addValue('target_contact_id', $batch[0])
|
||||||
->addValue('activity_type_id:name', 'changed_contact_category')
|
->addValue('activity_type_id:name', 'changed_contact_category')
|
||||||
->addValue('source_contact_id', \CRM_Core_BAO_Domain::getDomain()->contact_id)
|
->addValue('source_contact_id', \CRM_Core_BAO_Domain::getDomain()->contact_id)
|
||||||
->addValue('status_id:name', 'Completed')
|
->addValue('status_id:name', 'Completed')
|
||||||
->addValue('subject', $catNames[$lastChange[0] ?? 0] . ' → ' . $catNames[$newCategoryId])
|
->addValue('subject', $catNames[$oldCategoryId] . ' → ' . $catNames[$newCategoryId])
|
||||||
->execute()->first();
|
->execute()->first();
|
||||||
$activityID = (int) $a['id'];
|
$activityID = (int) $a['id'];
|
||||||
// Join activity to all relevant contacts
|
// Join activity to all relevant contacts
|
||||||
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_activity_contact (contact_id, 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
|
SELECT id contact_id, $activityID activity_id, 3 record_type_id
|
||||||
FROM civicrm_contact_category cc
|
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();
|
")->free();
|
||||||
Civi::log()->debug(count($batch) . " changes: " . $catNames[$lastChange[0] ?? 0] . ' → ' . $catNames[$lastChange[1] ?? 0]);
|
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