contactcats/Civi/Api4/Action/ContactCategory/Save.php

46 lines
1.2 KiB
PHP

<?php
namespace Civi\Api4\Action\ContactCategory;
use Civi\Api4\Generic\Result;
use CRM_Contactcats_ExtensionUtil as E;
use CRM_Core_DAO;
/**
* Create ContactCategory action
*
* This entity's ID is a unique primary key that references
* contact_id. Here we overwrite the validateValues which normally
*
* @see \Civi\Api4\Generic\AbstractAction
*
*/
class Save extends \Civi\Api4\Generic\DAOSaveAction {
/**
* Allow creating records with ids.
*
* @inheritDoc
*/
public function _run(Result $result) {
$ids = [];
foreach (array_column($this->records, 'id') as $id) {
if ($id) {
$ids[] = (int) $id;
}
}
if ($ids) {
$idsList = implode(",", array_filter($ids));
$preExisting = array_column(CRM_Core_DAO::executeQuery(
"SELECT id FROM civicrm_contact_category WHERE id IN ($idsList)"
)->fetchAll(), 'id');
// Any where we were given an ID that doesn't exist are important.
foreach (array_diff($ids, $preExisting) as $unknownID) {
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_contact_category (id) VALUES ($unknownID)");
}
}
// Now let it proceed as normal.
parent::_run($result);
}
}