Skip to content

Commit

Permalink
Merge pull request #24525 from colemanw/isComponentEnabled
Browse files Browse the repository at this point in the history
[REF] DAO - Add helper function to check if component is enabled
  • Loading branch information
demeritcowboy authored Sep 16, 2022
2 parents 5d37888 + 123e3b6 commit 46b33b0
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CRM/Activity/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public static function buildSearchForm(&$form) {
// 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;
Expand Down
4 changes: 2 additions & 2 deletions CRM/Activity/Form/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ public function buildQuickForm() {

// 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;
Expand All @@ -697,7 +697,7 @@ public function buildQuickForm() {
// 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()
) {

Expand Down
7 changes: 4 additions & 3 deletions CRM/Campaign/BAO/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public static function getPermissionedCampaigns(

//do check for component.
if ($doCheckForComponent) {
$campaigns['isCampaignEnabled'] = $isValid = self::isCampaignEnable();
$campaigns['isCampaignEnabled'] = $isValid = self::isComponentEnabled();
}

//do check for permissions.
Expand Down Expand Up @@ -283,11 +283,12 @@ public static function getPermissionedCampaigns(

/**
* 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();
}

/**
Expand Down
13 changes: 7 additions & 6 deletions CRM/Case/BAO/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -63,7 +64,7 @@ public static function add(&$params) {
*/
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'])) {
Expand Down Expand Up @@ -2467,7 +2468,7 @@ public static function checkPermission($activityId, $operation, $actTypeId = NUL
}

//do check for civicase component enabled.
if ($checkComponent && !self::enabled()) {
if ($checkComponent && !self::isComponentEnabled()) {
return $allow;
}

Expand Down Expand Up @@ -2709,7 +2710,7 @@ public static function checkPermission($activityId, $operation, $actTypeId = NUL
* or 'access all cases and activities'
*/
public static function accessCiviCase() {
if (!self::enabled()) {
if (!self::isComponentEnabled()) {
return FALSE;
}

Expand All @@ -2732,7 +2733,7 @@ public static function accessCiviCase() {
* @return bool
*/
public static function accessCase($caseId, $denyClosed = TRUE) {
if (!$caseId || !self::enabled()) {
if (!$caseId || !self::isComponentEnabled()) {
return FALSE;
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,7 @@ public static function buildProfile(
$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
));
Expand Down
10 changes: 10 additions & 0 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -3364,4 +3364,14 @@ private function makeNameFromLabel(): void {
$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);
}

}
2 changes: 1 addition & 1 deletion CRM/Core/EntityTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ protected function getSkippedFields(): array {
// '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;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ protected static function getEntityRefMetadata() {

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);
Expand Down
2 changes: 1 addition & 1 deletion CRM/Mailing/Selector/Browse.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function &getColumnHeaders($action = NULL, $output = NULL) {
]
);

if (CRM_Campaign_BAO_Campaign::isCampaignEnable()) {
if (CRM_Campaign_BAO_Campaign::isComponentEnabled()) {
self::$_columnHeaders[] = [
'name' => ts('Campaign'),
'sort' => 'campaign_id',
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Check/Component/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct() {
* @inheritDoc
*/
public function isEnabled() {
return CRM_Case_BAO_Case::enabled();
return CRM_Case_BAO_Case::isComponentEnabled();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Civi/API/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function create(string $entity, string $action, array $params) {
}
// 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');
Expand Down
2 changes: 1 addition & 1 deletion Civi/Api4/Action/Entity/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function getRecords() {
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());
});
}

Expand Down
2 changes: 1 addition & 1 deletion templates/CRM/Activity/Form/ActivityView.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) )}
<td class="label">{ts}Engagement Level{/ts}</td><td class="view-value">{$values.engagement_level}</td>
{/if}

Expand Down
4 changes: 2 additions & 2 deletions templates/CRM/Contribute/Page/ContributionPage.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<th>{ts}Title{/ts}</th>
<th>{ts}ID{/ts}</th>
<th>{ts}Enabled?{/ts}</th>
{if call_user_func(array('CRM_Campaign_BAO_Campaign','isCampaignEnable'))}
{if call_user_func(array('CRM_Campaign_BAO_Campaign','isComponentEnabled'))}
<th>{ts}Campaign{/ts}</th>
{/if}
<th></th>
Expand All @@ -48,7 +48,7 @@
<td><strong>{$row.title}</strong></td>
<td>{$row.id}</td>
<td id="row_{$row.id}_status">{if $row.is_active eq 1} {ts}Yes{/ts} {else} {ts}No{/ts} {/if}</td>
{if call_user_func(array('CRM_Campaign_BAO_Campaign','isCampaignEnable'))}
{if call_user_func(array('CRM_Campaign_BAO_Campaign','isComponentEnabled'))}
<td>{$row.campaign}</td>
{/if}
<td class="crm-contribution-page-actions right nowrap">
Expand Down
4 changes: 2 additions & 2 deletions templates/CRM/Event/Page/ManageEvent.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<th>{ts}Public?{/ts}</th>
<th>{ts}Starts{/ts}</th>
<th>{ts}Ends{/ts}</th>
{if call_user_func(array('CRM_Campaign_BAO_Campaign','isCampaignEnable'))}
{if call_user_func(array('CRM_Campaign_BAO_Campaign','isComponentEnabled'))}
<th>{ts}Campaign{/ts}</th>
{/if}
<th>{ts}Active?{/ts}</th>
Expand All @@ -64,7 +64,7 @@
<td class="crm-event-is_public">{if $row.is_public eq 1} {ts}Yes{/ts} {else} {ts}No{/ts} {/if}</td>
<td class="crm-event-start_date" data-order="{$row.start_date|crmDate:'%Y-%m-%d'}">{$row.start_date|crmDate:"%b %d, %Y %l:%M %P"}</td>
<td class="crm-event-end_date" data-order="{$row.end_date|crmDate:'%Y-%m-%d'}">{$row.end_date|crmDate:"%b %d, %Y %l:%M %P"}</td>
{if call_user_func(array('CRM_Campaign_BAO_Campaign','isCampaignEnable'))}
{if call_user_func(array('CRM_Campaign_BAO_Campaign','isComponentEnabled'))}
<td class="crm-event-campaign">{$row.campaign}</td>
{/if}
<td class="crm-event_status" id="row_{$row.id}_status">
Expand Down
2 changes: 1 addition & 1 deletion templates/CRM/Mailing/Page/Browse.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<td class="crm-mailing-scheduled">{$row.scheduled}</td>
<td class="crm-mailing-start">{$row.start}</td>
<td class="crm-mailing-end">{$row.end}</td>
{if call_user_func(array('CRM_Campaign_BAO_Campaign','isCampaignEnable'))}
{if call_user_func(array('CRM_Campaign_BAO_Campaign','isComponentEnabled'))}
<td class="crm-mailing-campaign">{$row.campaign}</td>
{/if}
<td>{$row.action|smarty:nodefaults|replace:'xx':$row.id}</td>
Expand Down

0 comments on commit 46b33b0

Please sign in to comment.