From 11ee29de371efcfff86c627fb688903a6cff4292 Mon Sep 17 00:00:00 2001
From: Jens Schuppe <schuppe@systopia.de>
Date: Tue, 23 Nov 2021 13:12:16 +0100
Subject: [PATCH] Use getEntityNameForTable() instead of getClassForTable(), as
 this might yield ambiguous results

---
 CRM/Core/DAO.php                  | 2 +-
 CRM/Core/DynamicFKAccessTrait.php | 2 +-
 CRM/Core/Form/RecurringEntity.php | 2 +-
 CRM/Dedupe/BAO/DedupeRule.php     | 2 +-
 CRM/Report/Form.php               | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php
index 906c3aeb9f..0104f8614c 100644
--- a/CRM/Core/DAO.php
+++ b/CRM/Core/DAO.php
@@ -3080,7 +3080,7 @@ SELECT contact_id
         $relatedEntities = $this->buildOptions('entity_table', 'get');
         foreach ((array) $relatedEntities as $table => $ent) {
           if (!empty($ent)) {
-            $ent = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($table));
+            $ent = CRM_Core_DAO_AllCoreTables::getEntityNameForTable($table);
             $subquery = CRM_Utils_SQL::mergeSubquery($ent);
             if ($subquery) {
               $relatedClauses[] = "(entity_table = '$table' AND entity_id " . implode(' AND entity_id ', $subquery) . ")";
diff --git a/CRM/Core/DynamicFKAccessTrait.php b/CRM/Core/DynamicFKAccessTrait.php
index 041ec3741e..d88401b576 100644
--- a/CRM/Core/DynamicFKAccessTrait.php
+++ b/CRM/Core/DynamicFKAccessTrait.php
@@ -38,7 +38,7 @@ trait CRM_Core_DynamicFKAccessTrait {
       $table = CRM_Core_DAO::getFieldValue(__CLASS__, $record['id'], 'entity_table');
     }
     if ($eid && $table) {
-      $targetEntity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($table));
+      $targetEntity = CRM_Core_DAO_AllCoreTables::getEntityNameForTable($table);
       if ($targetEntity === NULL) {
         throw new \API_Exception(sprintf('Cannot resolve permissions for dynamic foreign key in "%s". Invalid table reference "%s".',
           static::getTableName(), $table));
diff --git a/CRM/Core/Form/RecurringEntity.php b/CRM/Core/Form/RecurringEntity.php
index 053346a886..8db1ec2284 100644
--- a/CRM/Core/Form/RecurringEntity.php
+++ b/CRM/Core/Form/RecurringEntity.php
@@ -94,7 +94,7 @@ class CRM_Core_Form_RecurringEntity {
     }
 
     // Assign variables
-    $entityType = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($entityTable));
+    $entityType = CRM_Core_DAO_AllCoreTables::getEntityNameForTable($entityTable);
     $tpl = CRM_Core_Smarty::singleton();
     $tpl->assign('recurringEntityType', ts($entityType));
     $tpl->assign('currentEntityId', self::$_entityId);
diff --git a/CRM/Dedupe/BAO/DedupeRule.php b/CRM/Dedupe/BAO/DedupeRule.php
index 16683b5e5e..a1d9d63944 100644
--- a/CRM/Dedupe/BAO/DedupeRule.php
+++ b/CRM/Dedupe/BAO/DedupeRule.php
@@ -237,7 +237,7 @@ class CRM_Dedupe_BAO_DedupeRule extends CRM_Dedupe_DAO_DedupeRule {
    * @throws \CiviCRM_API3_Exception
    */
   public function getFieldType($fieldName) {
-    $entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($this->rule_table));
+    $entity = CRM_Core_DAO_AllCoreTables::getEntityNameForTable($this->rule_table);
     if (!$entity) {
       // This means we have stored a custom field rather than an entity name in rule_table, figure out the entity.
       $entity = civicrm_api3('CustomGroup', 'getvalue', ['table_name' => $this->rule_table, 'return' => 'extends']);
diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php
index 2d015a178e..0d117c6129 100644
--- a/CRM/Report/Form.php
+++ b/CRM/Report/Form.php
@@ -5029,7 +5029,7 @@ LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_a
   public function setEntityRefDefaults(&$field, $table) {
     $field['attributes'] = $field['attributes'] ? $field['attributes'] : [];
     $field['attributes'] += [
-      'entity' => CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($table)),
+      'entity' => CRM_Core_DAO_AllCoreTables::getEntityNameForTable($table),
       'multiple' => TRUE,
       'placeholder' => ts('- select -'),
     ];
-- 
2.25.1