From 123e3b60fc1b12ca8f085430d5f8aaac1fa2afcd Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 15 Sep 2022 12:05:14 -0400 Subject: [PATCH] CRM_Core_DAO - Add helper function to determine if associated component is enabled --- CRM/Activity/BAO/Query.php | 2 +- CRM/Activity/Form/Activity.php | 4 ++-- CRM/Campaign/BAO/Campaign.php | 7 ++++--- CRM/Case/BAO/Case.php | 13 +++++++------ CRM/Core/BAO/UFGroup.php | 2 +- CRM/Core/DAO.php | 10 ++++++++++ CRM/Core/EntityTokens.php | 2 +- CRM/Core/Resources.php | 2 +- CRM/Mailing/Selector/Browse.php | 2 +- CRM/Utils/Check/Component/Case.php | 2 +- Civi/API/Request.php | 2 +- Civi/Api4/Action/Entity/Get.php | 2 +- templates/CRM/Activity/Form/ActivityView.tpl | 2 +- templates/CRM/Contribute/Page/ContributionPage.tpl | 4 ++-- templates/CRM/Event/Page/ManageEvent.tpl | 4 ++-- templates/CRM/Mailing/Page/Browse.tpl | 2 +- 16 files changed, 37 insertions(+), 25 deletions(-) diff --git a/CRM/Activity/BAO/Query.php b/CRM/Activity/BAO/Query.php index d147c9fc6f..03b8957764 100644 --- a/CRM/Activity/BAO/Query.php +++ b/CRM/Activity/BAO/Query.php @@ -500,7 +500,7 @@ class CRM_Activity_BAO_Query { // Add engagement level CRM-7775. $buildEngagementLevel = FALSE; $buildSurveyResult = FALSE; - if (CRM_Campaign_BAO_Campaign::isCampaignEnable() && + if (CRM_Campaign_BAO_Campaign::isComponentEnabled() && CRM_Campaign_BAO_Campaign::accessCampaign() ) { $buildEngagementLevel = TRUE; diff --git a/CRM/Activity/Form/Activity.php b/CRM/Activity/Form/Activity.php index ab071e004a..2886929f92 100644 --- a/CRM/Activity/Form/Activity.php +++ b/CRM/Activity/Form/Activity.php @@ -683,7 +683,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { // Add engagement level CRM-7775 $buildEngagementLevel = FALSE; - if (CRM_Campaign_BAO_Campaign::isCampaignEnable() && + if (CRM_Campaign_BAO_Campaign::isComponentEnabled() && CRM_Campaign_BAO_Campaign::accessCampaign() ) { $buildEngagementLevel = TRUE; @@ -698,7 +698,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { // check for survey activity $this->_isSurveyActivity = FALSE; - if ($this->_activityId && CRM_Campaign_BAO_Campaign::isCampaignEnable() && + if ($this->_activityId && CRM_Campaign_BAO_Campaign::isComponentEnabled() && CRM_Campaign_BAO_Campaign::accessCampaign() ) { diff --git a/CRM/Campaign/BAO/Campaign.php b/CRM/Campaign/BAO/Campaign.php index 0174f2c5e3..2314942c10 100644 --- a/CRM/Campaign/BAO/Campaign.php +++ b/CRM/Campaign/BAO/Campaign.php @@ -255,7 +255,7 @@ Order By camp.title"; //do check for component. if ($doCheckForComponent) { - $campaigns['isCampaignEnabled'] = $isValid = self::isCampaignEnable(); + $campaigns['isCampaignEnabled'] = $isValid = self::isComponentEnabled(); } //do check for permissions. @@ -283,11 +283,12 @@ Order By camp.title"; /** * Is CiviCampaign enabled. - * + * @deprecated * @return bool */ public static function isCampaignEnable(): bool { - return CRM_Core_Component::isEnabled('CiviCampaign'); + CRM_Core_Error::deprecatedFunctionWarning('isComponentEnabled'); + return self::isComponentEnabled(); } /** diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index c45b8dffd3..7ea41f2efe 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -30,11 +30,12 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case implements \Civi\Core\HookInte /** * Is CiviCase enabled? - * + * @deprecated * @return bool */ public static function enabled() { - return CRM_Core_Component::isEnabled('CiviCase'); + CRM_Core_Error::deprecatedFunctionWarning('isComponentEnabled'); + return self::isComponentEnabled(); } /** @@ -63,7 +64,7 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case implements \Civi\Core\HookInte */ public static function on_hook_civicrm_post(\Civi\Core\Event\PostEvent $e): void { // FIXME: The EventScanner ought to skip over disabled components when registering HookInterface - if (!CRM_Core_Component::isEnabled('CiviCase')) { + if (!self::isComponentEnabled()) { return; } if ($e->entity === 'Activity' && in_array($e->action, ['create', 'edit'])) { @@ -2467,7 +2468,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; } //do check for civicase component enabled. - if ($checkComponent && !self::enabled()) { + if ($checkComponent && !self::isComponentEnabled()) { return $allow; } @@ -2709,7 +2710,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; * or 'access all cases and activities' */ public static function accessCiviCase() { - if (!self::enabled()) { + if (!self::isComponentEnabled()) { return FALSE; } @@ -2732,7 +2733,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; * @return bool */ public static function accessCase($caseId, $denyClosed = TRUE) { - if (!$caseId || !self::enabled()) { + if (!$caseId || !self::isComponentEnabled()) { return FALSE; } diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index 063fe93f16..7dc58cb1f5 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -2164,7 +2164,7 @@ AND ( entity_id IS NULL OR entity_id <= 0 ) $form->add('textarea', $name, $title, CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', $fieldName)); } elseif (substr($fieldName, -11) == 'campaign_id') { - if (CRM_Campaign_BAO_Campaign::isCampaignEnable()) { + if (CRM_Campaign_BAO_Campaign::isComponentEnabled()) { $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value($contactId, $form->_componentCampaigns )); diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 7d91617501..90757d0305 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -3366,4 +3366,14 @@ SELECT contact_id $this->name = $name . $suffix; } + /** + * Check if component is enabled for this DAO class + * + * @return bool + */ + public static function isComponentEnabled(): bool { + $daoName = static::class; + return !defined("$daoName::COMPONENT") || CRM_Core_Component::isEnabled($daoName::COMPONENT); + } + } diff --git a/CRM/Core/EntityTokens.php b/CRM/Core/EntityTokens.php index 8f7866971b..9ee0757364 100644 --- a/CRM/Core/EntityTokens.php +++ b/CRM/Core/EntityTokens.php @@ -409,7 +409,7 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { // 'not a real field' offered up by case - seems like an oddity // we should skip at the top level for now. $fields = ['tags']; - if (!CRM_Campaign_BAO_Campaign::isCampaignEnable()) { + if (!CRM_Campaign_BAO_Campaign::isComponentEnabled()) { $fields[] = 'campaign_id'; } return $fields; diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 88a8d64a0e..27c13416ca 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -504,7 +504,7 @@ class CRM_Core_Resources implements CRM_Core_Resources_CollectionAdderInterface foreach (CRM_Core_DAO_AllCoreTables::daoToClass() as $entity => $daoName) { // Skip DAOs of disabled components - if (defined("$daoName::COMPONENT") && !CRM_Core_Component::isEnabled($daoName::COMPONENT)) { + if (!$daoName::isComponentEnabled()) { continue; } $baoName = str_replace('_DAO_', '_BAO_', $daoName); diff --git a/CRM/Mailing/Selector/Browse.php b/CRM/Mailing/Selector/Browse.php index 8151febab9..df1442be6f 100644 --- a/CRM/Mailing/Selector/Browse.php +++ b/CRM/Mailing/Selector/Browse.php @@ -162,7 +162,7 @@ class CRM_Mailing_Selector_Browse extends CRM_Core_Selector_Base implements CRM_ ] ); - if (CRM_Campaign_BAO_Campaign::isCampaignEnable()) { + if (CRM_Campaign_BAO_Campaign::isComponentEnabled()) { self::$_columnHeaders[] = [ 'name' => ts('Campaign'), 'sort' => 'campaign_id', diff --git a/CRM/Utils/Check/Component/Case.php b/CRM/Utils/Check/Component/Case.php index 89f623ed73..d6b4c11488 100644 --- a/CRM/Utils/Check/Component/Case.php +++ b/CRM/Utils/Check/Component/Case.php @@ -40,7 +40,7 @@ class CRM_Utils_Check_Component_Case extends CRM_Utils_Check_Component { * @inheritDoc */ public function isEnabled() { - return CRM_Case_BAO_Case::enabled(); + return CRM_Case_BAO_Case::isComponentEnabled(); } /** diff --git a/Civi/API/Request.php b/Civi/API/Request.php index 55765a9bcb..62cd7605cd 100644 --- a/Civi/API/Request.php +++ b/Civi/API/Request.php @@ -52,7 +52,7 @@ class Request { } // Check enabled components $daoName = \CRM_Core_DAO_AllCoreTables::getFullName($entity); - if ($daoName && defined("{$daoName}::COMPONENT") && !\CRM_Core_Component::isEnabled($daoName::COMPONENT)) { + if ($daoName && !$daoName::isComponentEnabled()) { throw new \Civi\API\Exception\NotImplementedException("$entity API is not available because " . $daoName::COMPONENT . " component is disabled"); } $args = (array) CoreUtil::getInfoItem($entity, 'class_args'); diff --git a/Civi/Api4/Action/Entity/Get.php b/Civi/Api4/Action/Entity/Get.php index 13b96573fe..a7fa82d762 100644 --- a/Civi/Api4/Action/Entity/Get.php +++ b/Civi/Api4/Action/Entity/Get.php @@ -35,7 +35,7 @@ class Get extends \Civi\Api4\Generic\BasicGetAction { return array_filter($provider->getEntities(), function($entity) { // Only include DAO entities from enabled components $daoName = $entity['dao'] ?? NULL; - return (!$daoName || !defined("{$daoName}::COMPONENT") || \CRM_Core_Component::isEnabled($daoName::COMPONENT)); + return (!$daoName || $daoName::isComponentEnabled()); }); } diff --git a/templates/CRM/Activity/Form/ActivityView.tpl b/templates/CRM/Activity/Form/ActivityView.tpl index b0a02df612..35c0297cfe 100644 --- a/templates/CRM/Activity/Form/ActivityView.tpl +++ b/templates/CRM/Activity/Form/ActivityView.tpl @@ -36,7 +36,7 @@ {/if} {if $values.engagement_level AND - call_user_func( array( 'CRM_Campaign_BAO_Campaign', 'isCampaignEnable' ) )} + call_user_func( array( 'CRM_Campaign_BAO_Campaign', 'isComponentEnabled' ) )} {ts}Engagement Level{/ts}{$values.engagement_level} {/if} diff --git a/templates/CRM/Contribute/Page/ContributionPage.tpl b/templates/CRM/Contribute/Page/ContributionPage.tpl index 2321d2524b..330b87da8f 100644 --- a/templates/CRM/Contribute/Page/ContributionPage.tpl +++ b/templates/CRM/Contribute/Page/ContributionPage.tpl @@ -37,7 +37,7 @@ {ts}Title{/ts} {ts}ID{/ts} {ts}Enabled?{/ts} - {if call_user_func(array('CRM_Campaign_BAO_Campaign','isCampaignEnable'))} + {if call_user_func(array('CRM_Campaign_BAO_Campaign','isComponentEnabled'))} {ts}Campaign{/ts} {/if} @@ -48,7 +48,7 @@ {$row.title} {$row.id} {if $row.is_active eq 1} {ts}Yes{/ts} {else} {ts}No{/ts} {/if} - {if call_user_func(array('CRM_Campaign_BAO_Campaign','isCampaignEnable'))} + {if call_user_func(array('CRM_Campaign_BAO_Campaign','isComponentEnabled'))} {$row.campaign} {/if} diff --git a/templates/CRM/Event/Page/ManageEvent.tpl b/templates/CRM/Event/Page/ManageEvent.tpl index b634902a12..c69aa9943f 100644 --- a/templates/CRM/Event/Page/ManageEvent.tpl +++ b/templates/CRM/Event/Page/ManageEvent.tpl @@ -41,7 +41,7 @@ {ts}Public?{/ts} {ts}Starts{/ts} {ts}Ends{/ts} - {if call_user_func(array('CRM_Campaign_BAO_Campaign','isCampaignEnable'))} + {if call_user_func(array('CRM_Campaign_BAO_Campaign','isComponentEnabled'))} {ts}Campaign{/ts} {/if} {ts}Active?{/ts} @@ -64,7 +64,7 @@ {if $row.is_public eq 1} {ts}Yes{/ts} {else} {ts}No{/ts} {/if} {$row.start_date|crmDate:"%b %d, %Y %l:%M %P"} {$row.end_date|crmDate:"%b %d, %Y %l:%M %P"} - {if call_user_func(array('CRM_Campaign_BAO_Campaign','isCampaignEnable'))} + {if call_user_func(array('CRM_Campaign_BAO_Campaign','isComponentEnabled'))} {$row.campaign} {/if} diff --git a/templates/CRM/Mailing/Page/Browse.tpl b/templates/CRM/Mailing/Page/Browse.tpl index c0fdd64d5a..01482f230b 100644 --- a/templates/CRM/Mailing/Page/Browse.tpl +++ b/templates/CRM/Mailing/Page/Browse.tpl @@ -67,7 +67,7 @@ {$row.scheduled} {$row.start} {$row.end} - {if call_user_func(array('CRM_Campaign_BAO_Campaign','isCampaignEnable'))} + {if call_user_func(array('CRM_Campaign_BAO_Campaign','isComponentEnabled'))} {$row.campaign} {/if} {$row.action|smarty:nodefaults|replace:'xx':$row.id} -- 2.25.1