From: Tim Otten Date: Fri, 7 May 2021 01:52:13 +0000 (-1000) Subject: (REF) Extract method ManagedEntities::isActivationSupported() X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=fc62516602d36c546436c285363422c6c3d8c246;p=civicrm-core.git (REF) Extract method ManagedEntities::isActivationSupported() --- diff --git a/CRM/Core/ManagedEntities.php b/CRM/Core/ManagedEntities.php index d35ef2d104..4668273b50 100644 --- a/CRM/Core/ManagedEntities.php +++ b/CRM/Core/ManagedEntities.php @@ -305,13 +305,8 @@ class CRM_Core_ManagedEntities { * @throws \CiviCRM_API3_Exception */ public function disableEntity($dao): void { - $actions = civicrm_api3($dao->entity_type, 'getactions', [])['values']; - $supportsDisable = FALSE; - if (in_array('create', $actions, TRUE) && in_array('getfields', $actions)) { - $fields = civicrm_api3($dao->entity_type, 'getfields', ['action' => 'create'])['values']; - $supportsDisable = array_key_exists('is_active', $fields); - } - if ($supportsDisable) { + $entity_type = $dao->entity_type; + if ($this->isActivationSupported($entity_type)) { // FIXME cascading for payproc types? $params = [ 'version' => 3, @@ -486,4 +481,23 @@ class CRM_Core_ManagedEntities { 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]; + } + }