mirror of
https://codeberg.org/artfulrobot/contactcats.git
synced 2025-06-25 04:58:04 +02:00
67 lines
1.8 KiB
PHP
67 lines
1.8 KiB
PHP
<?php
|
|
namespace Civi\Api4\Action\ContactCategory;
|
|
|
|
use Civi;
|
|
use Civi\Api4\Generic\AbstractAction;
|
|
use Civi\Api4\Generic\Result;
|
|
use Civi\Api4\Group;
|
|
use CRM_Core_DAO;
|
|
|
|
/**
|
|
* Sync the data
|
|
*
|
|
* @see \Civi\Api4\Generic\AbstractAction
|
|
*
|
|
* @package ContactCategory
|
|
*/
|
|
class Sync extends \Civi\Api4\Generic\AbstractAction {
|
|
|
|
public function _run(Result $result) {
|
|
$map = [
|
|
['groupID' => 5, 'name' => 'Nice', 'categoryID' => 2],
|
|
];
|
|
|
|
// clear out temp space.
|
|
CRM_Core_DAO::executeQuery("UPDATE civicrm_contactcategory SET next_category = 1;");
|
|
// ensure we have all our contacts covered.
|
|
CRM_Core_DAO::executeQuery(<<<SQL
|
|
INSERT IGNORE INTO civicrm_contactcategory
|
|
SELECT id, 1 as category, 2 next_category
|
|
FROM civicrm_contact
|
|
SQL);
|
|
|
|
foreach ($map as $row) {
|
|
// Get contacts in group.
|
|
$cat = (int) $row['categoryID'];
|
|
$row['groupID'] = (int) $row['groupID'];
|
|
|
|
$group = Group::get(FALSE)->addWhere('id', '=', $row['groupID'])->execute()->first();
|
|
if (!$group) {
|
|
Civi::warning("Group $row[groupID] used for category $row[name] no longer exists.");
|
|
continue;
|
|
}
|
|
$isSmart = !empty($group['saved_search_id']);
|
|
|
|
if (!$isSmart) {
|
|
$sql = <<<SQL
|
|
UPDATE civicrm_contactcategory cc
|
|
INNER JOIN civicrm_group_contact gc ON gc.contact_id = cc.id AND gc.status = 'Added' AND group_id = $row[groupID]
|
|
SET next_category = $cat
|
|
WHERE next_category = 1
|
|
SQL;
|
|
}
|
|
else {
|
|
// @todo refresh cache
|
|
$sql = <<<SQL
|
|
UPDATE civicrm_contactcategory cc
|
|
INNER JOIN civicrm_group_contact_cache gcc ON gcc.contact_id = cc.id AND group_id = $row[groupID]
|
|
SET next_category = $cat
|
|
WHERE next_category = 1
|
|
SQL;
|
|
}
|
|
CRM_Core_DAO::executeQuery($sql);
|
|
}
|
|
|
|
}
|
|
|
|
}
|