Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hook_managed - do not try to disable managed entities if is_active is not available to api3.create #20144

Merged
merged 3 commits into from
May 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions CRM/Core/ManagedEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ public function updateExistingEntity($dao, $todo) {
$doUpdate = ($policy == 'always');

if ($doUpdate) {
$defaults = ['id' => $dao->entity_id, 'is_active' => 1];
$defaults = ['id' => $dao->entity_id];
if ($this->isActivationSupported($dao->entity_type)) {
$defaults['is_active'] = 1;
}
$params = array_merge($defaults, $todo['params']);

$manager = CRM_Extension_System::singleton()->getManager();
Expand Down Expand Up @@ -301,10 +304,12 @@ public function updateExistingEntity($dao, $todo) {
* inactive.
*
* @param CRM_Core_DAO_Managed $dao
*
* @throws \CiviCRM_API3_Exception
*/
public function disableEntity($dao) {
// FIXME: if ($dao->entity_type supports is_active) {
if (TRUE) {
public function disableEntity($dao): void {
$entity_type = $dao->entity_type;
if ($this->isActivationSupported($entity_type)) {
// FIXME cascading for payproc types?
$params = [
'version' => 3,
Expand Down Expand Up @@ -479,4 +484,23 @@ protected function onApiError($entity, $action, $params, $result) {
throw new Exception('API error: ' . $result['error_message'] . ' on ' . $entity . '.' . $action);
}

/**
* Determine if an entity supports APIv3-based activation/de-activation.
* @param string $entity_type
*
* @return bool
* @throws \CiviCRM_API3_Exception
*/
private function isActivationSupported(string $entity_type): bool {
if (!isset(Civi::$statics[__CLASS__][__FUNCTION__][$entity_type])) {
$actions = civicrm_api3($entity_type, 'getactions', [])['values'];
Civi::$statics[__CLASS__][__FUNCTION__][$entity_type] = FALSE;
if (in_array('create', $actions, TRUE) && in_array('getfields', $actions)) {
$fields = civicrm_api3($entity_type, 'getfields', ['action' => 'create'])['values'];
Civi::$statics[__CLASS__][__FUNCTION__][$entity_type] = array_key_exists('is_active', $fields);
}
}
return Civi::$statics[__CLASS__][__FUNCTION__][$entity_type];
}

}