initial commit

This commit is contained in:
Rich Lott / Artful Robot 2024-02-27 18:34:28 +00:00
commit 3b393be4f4
16 changed files with 1594 additions and 0 deletions

View file

@ -0,0 +1,67 @@
<?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);
}
}
}

View file

@ -0,0 +1,22 @@
<?php
namespace Civi\Api4;
use Civi\Api4\Action\ContactCategory\Sync;
/**
* ContactCategory entity.
*
* Provided by the Contact Categories extension.
*
* @package Civi\Api4
*/
class ContactCategory extends Generic\DAOEntity {
/**
*
*/
public function sync(): Sync {
return new Sync('ContactCategory', 'sync');
}
}