Skip to content

Commit

Permalink
AllCoreTables - Cleanup comments, rename functions for consistency
Browse files Browse the repository at this point in the history
These functions are all pretty internal, but if any extension is using them I
gave a long (24 month) noisy deprecation period.
  • Loading branch information
colemanw committed Feb 14, 2024
1 parent a965a81 commit 25bc61b
Show file tree
Hide file tree
Showing 29 changed files with 87 additions and 71 deletions.
2 changes: 1 addition & 1 deletion CRM/Admin/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function preProcess() {
$this->_BAOName = $this->get('BAOName');
// Otherwise, look it up from the api entity name
if (!$this->_BAOName) {
$this->_BAOName = CRM_Core_DAO_AllCoreTables::getBAOClassName(CRM_Core_DAO_AllCoreTables::getFullName($this->getDefaultEntity()));
$this->_BAOName = CRM_Core_DAO_AllCoreTables::getBAOClassName(CRM_Core_DAO_AllCoreTables::getDAONameForEntity($this->getDefaultEntity()));
}
$this->retrieveValues();
$this->setPageTitle($this->_BAOName::getEntityTitle());
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3367,7 +3367,7 @@ public static function on_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) {
!empty($event->object->is_primary) &&
!empty($event->object->contact_id)
) {
$daoClass = CRM_Core_DAO_AllCoreTables::getFullName($event->entity);
$daoClass = CRM_Core_DAO_AllCoreTables::getDAONameForEntity($event->entity);
$dao = new $daoClass();
$dao->contact_id = $event->object->contact_id;
$dao->is_primary = 1;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/CustomGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public static function retrieve($params, &$defaults) {
*/
public static function validateCustomGroupName(CRM_Core_DAO_CustomGroup $group) {
$extends = in_array($group->extends, CRM_Contact_BAO_ContactType::basicTypes(TRUE)) ? 'Contact' : $group->extends;
$extendsDAO = CRM_Core_DAO_AllCoreTables::getFullName($extends);
$extendsDAO = CRM_Core_DAO_AllCoreTables::getDAONameForEntity($extends);
if ($extendsDAO) {
$fields = array_column($extendsDAO::fields(), 'name');
if (in_array($group->name, $fields)) {
Expand Down
4 changes: 2 additions & 2 deletions CRM/Core/BAO/EntityTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public static function buildOptions($fieldName, $context = NULL, $props = []) {
$options = [];
foreach (self::buildOptions($fieldName) as $tableName => $label) {
$bao = CRM_Core_DAO_AllCoreTables::getClassForTable($tableName);
$apiName = CRM_Core_DAO_AllCoreTables::getBriefName($bao);
$apiName = CRM_Core_DAO_AllCoreTables::getEntityNameForClass($bao);
$options[$tableName] = $apiName;
}
}
Expand All @@ -484,7 +484,7 @@ public static function preDeleteOtherEntity($event) {
}
// This is probably fairly mild in terms of helping performance - a case could be made to check if tags
// exist before deleting (further down) as delete is a locking action.
$entity = CRM_Core_DAO_AllCoreTables::getBriefName(get_class($event->object));
$entity = CRM_Core_DAO_AllCoreTables::getEntityNameForClass(get_class($event->object));
if ($entity && !isset(Civi::$statics[__CLASS__]['tagged_entities'][$entity])) {
$tableName = CRM_Core_DAO_AllCoreTables::getTableForEntityName($entity);
$used_for = CRM_Core_OptionGroup::values('tag_used_for');
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/RecurringEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ public static function copyCreateEntity($entityTable, $fromCriteria, $newParams,
CRM_Core_BAO_RecurringEntity::quickAdd($object->id, $newObject->id, $entityTable);
}

CRM_Utils_Hook::copy(CRM_Core_DAO_AllCoreTables::getBriefName($daoName), $newObject);
CRM_Utils_Hook::copy(CRM_Core_DAO_AllCoreTables::getEntityNameForClass($daoName), $newObject);
return $newObject;
}

Expand Down
12 changes: 6 additions & 6 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function __construct() {
public static function getEntityTitle() {
$className = static::class;
CRM_Core_Error::deprecatedWarning("$className needs to be regenerated. Missing getEntityTitle method.");
return CRM_Core_DAO_AllCoreTables::getBriefName($className);
return CRM_Core_DAO_AllCoreTables::getEntityNameForClass($className);
}

/**
Expand Down Expand Up @@ -1017,7 +1017,7 @@ public static function writeRecord(array $record): CRM_Core_DAO {
if ($className === 'CRM_Core_DAO') {
throw new CRM_Core_Exception('Function writeRecord must be called on a subclass of CRM_Core_DAO');
}
$entityName = CRM_Core_DAO_AllCoreTables::getBriefName($className);
$entityName = CRM_Core_DAO_AllCoreTables::getEntityNameForClass($className);

// For legacy reasons, empty values would sometimes be passed around as the string 'null'.
// The DAO treats 'null' the same as '', and an empty string makes a lot more sense!
Expand Down Expand Up @@ -1086,7 +1086,7 @@ public static function deleteRecord(array $record) {
if ($className === 'CRM_Core_DAO') {
throw new CRM_Core_Exception('Function deleteRecord must be called on a subclass of CRM_Core_DAO');
}
$entityName = CRM_Core_DAO_AllCoreTables::getBriefName($className);
$entityName = CRM_Core_DAO_AllCoreTables::getEntityNameForClass($className);
if (empty($record[$idField])) {
throw new CRM_Core_Exception("Cannot delete {$entityName} with no $idField.");
}
Expand Down Expand Up @@ -2054,7 +2054,7 @@ public static function copyGeneric($daoName, $criteria, $newData = NULL, $fields
if (!$blockCopyofCustomValues) {
$newObject->copyCustomFields($object->id, $newObject->id);
}
CRM_Utils_Hook::post('create', CRM_Core_DAO_AllCoreTables::getBriefName($daoName), $newObject->id, $newObject);
CRM_Utils_Hook::post('create', CRM_Core_DAO_AllCoreTables::getEntityNameForClass($daoName), $newObject->id, $newObject);
}

return $newObject;
Expand Down Expand Up @@ -2136,7 +2136,7 @@ protected function copyLocalizable($entityID, $newEntityID, $fieldsToPrefix, $fi
* @param string $parentOperation
*/
public function copyCustomFields($entityID, $newEntityID, $parentOperation = NULL) {
$entity = CRM_Core_DAO_AllCoreTables::getBriefName(get_class($this));
$entity = CRM_Core_DAO_AllCoreTables::getEntityNameForClass(get_class($this));
$tableName = CRM_Core_DAO_AllCoreTables::getTableForClass(get_class($this));
// Obtain custom values for the old entity.
$customParams = $htmlType = [];
Expand Down Expand Up @@ -3293,7 +3293,7 @@ protected static function getDynamicFkAclClauses(string $entityTableField, strin
public static function getSelectWhereClause($tableAlias = NULL, $entityName = NULL, $conditions = []) {
$bao = new static();
$tableAlias ??= $bao->tableName();
$entityName ??= CRM_Core_DAO_AllCoreTables::getBriefName(get_class($bao));
$entityName ??= CRM_Core_DAO_AllCoreTables::getEntityNameForClass(get_class($bao));
$finalClauses = [];
$fields = static::getSupportedFields();
$selectWhereClauses = $bao->addSelectWhereClause($entityName, NULL, $conditions);
Expand Down
72 changes: 44 additions & 28 deletions CRM/Core/DAO/AllCoreTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static function flush(): void {
* @param string $tableName
* @param string $fields_callback
* @param string $links_callback
* @internal
*/
public static function registerEntityType($briefName, $className, $tableName, $fields_callback = NULL, $links_callback = NULL) {
Civi::$statics[__CLASS__]['tables'][$tableName] = $briefName;
Expand All @@ -84,7 +85,7 @@ public static function registerEntityType($briefName, $className, $tableName, $f

/**
* @return array[]
* [EntityName => [table => table_name, class => CRM_Core_DAO_EntityName]][]
* [EntityName => [table => table_name, class => CRM_DAO_ClassName]][]
*/
public static function getEntities(): array {
self::init();
Expand All @@ -102,7 +103,7 @@ private static function getEntitiesByTable(): array {

/**
* @return string[]
* [CRM_Core_DAO_ClassName => EntityName][]
* [CRM_DAO_ClassName => EntityName][]
*/
private static function getEntitiesByClass(): array {
self::init();
Expand All @@ -111,8 +112,6 @@ private static function getEntitiesByClass(): array {

/**
* @deprecated in 5.72 will be removed in 5.90.
* @return array
* Ex: $result['Contact']['table'] == 'civicrm_contact';
*/
public static function get() {
CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_DAO_AllCoreTables::getEntities');
Expand All @@ -128,8 +127,9 @@ public static function get() {
}

/**
* Mapping from table-names to class-names.
* @return array
* List of SQL table names.
* [table_name => CRM_DAO_ClassName][]
*/
public static function tables() {
return array_combine(array_keys(self::getEntitiesByTable()), array_keys(self::getEntitiesByClass()));
Expand Down Expand Up @@ -197,19 +197,16 @@ public static function multilingualize($class, $originalIndices) {
}

/**
* Mapping from entity-names to class-names.
* @return array
* Mapping from brief-names to class-names.
* Ex: $result['Contact'] == 'CRM_Contact_DAO_Contact'.
* [EntityName => CRM_DAO_ClassName][]
*/
public static function daoToClass() {
return array_flip(self::getEntitiesByClass());
}

/**
* @deprecated in 5.72 will be removed in 5.90
* @return array
* Mapping from table-names to class-names.
* Ex: $result['civicrm_contact'] == 'CRM_Contact_DAO_Contact'.
*/
public static function getCoreTables() {
CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_DAO_AllCoreTables::tables');
Expand Down Expand Up @@ -320,10 +317,10 @@ public static function getClasses() {
}

/**
* Get a list of all extant BAO classes.
* Get a list of all extant BAO classes, keyed by entityName.
*
* @return array
* Ex: ['Contact' => 'CRM_Contact_BAO_Contact']
* [EntityName => CRM_BAO_ClassName][]
*/
public static function getBaoClasses() {
$r = [];
Expand Down Expand Up @@ -353,52 +350,70 @@ public static function getClassForTable(string $tableName) {
}

/**
* Given a brief-name, determine the full class-name.
* Given an entity name, determine the DAO class-name.
*
* @param string $briefName
* @param string|null $entityName
* Ex: 'Contact'.
* @return string|CRM_Core_DAO|NULL
* Ex: 'CRM_Contact_DAO_Contact'.
*/
public static function getFullName($briefName) {
return self::getEntities()[$briefName]['class'] ?? NULL;
public static function getDAONameForEntity(?string $entityName) {
return self::getEntities()[$entityName]['class'] ?? NULL;
}

/**
* @deprecated in 5.72 will be removed in 5.96
*/
public static function getFullName($entityName) {
CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_DAO_AllCoreTables::getDAONameForEntity');
return self::getDAONameForEntity((string) $entityName);
}

/**
* Given a full class-name, determine the brief-name.
* Given a DAO or BAO class-name, return the entity name.
*
* @param string $className
* @param string|null $className
* Ex: 'CRM_Contact_DAO_Contact'.
* @return string|NULL
* Ex: 'Contact'.
*/
public static function getBriefName($className): ?string {
public static function getEntityNameForClass(?string $className): ?string {
$className = self::getCanonicalClassName($className);
return self::getEntitiesByClass()[$className] ?? NULL;
}

/**
* @deprecated in 5.72 will be removed in 5.96
*/
public static function getBriefName($className): ?string {
CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_DAO_AllCoreTables::getEntityNameForClass');
return self::getEntityNameForClass((string) $className);
}

/**
* @param string $className DAO or BAO name
* @return string|FALSE SQL table name
*/
public static function getTableForClass($className) {
$entityName = self::getBriefName($className);
$entityName = self::getEntityNameForClass($className);
return self::getEntities()[$entityName]['table'] ?? FALSE;
}

/**
* Convert the entity name into a table name.
*
* @param string $briefName
* @param string $entityName
* e.g. 'Activity'
*
* @return string
* e.g. 'civicrm_activity'
*/
public static function getTableForEntityName($briefName): string {
return self::getEntities()[$briefName]['table'];
public static function getTableForEntityName($entityName): string {
return self::getEntities()[$entityName]['table'];
}

/**
* Convert table name to brief entity name.
* Convert table name to entity name.
*
* @param string $tableName
*
Expand Down Expand Up @@ -496,18 +511,19 @@ public static function getImports($dao, $labelName, $prefix, $foreignDAOs = []):
*
* Apply any third-party alterations to the `fields()`.
*
* TODO: This function should probably take briefName as the key instead of className
* TODO: This function should probably take entityName as the key instead of className
* because the latter is not always unique (e.g. virtual entities)
*
* @param string $className
* @param string $event
* @param mixed $values
* @internal
*/
public static function invoke($className, $event, &$values) {
$briefName = self::getBriefName($className);
$entityName = self::getEntityNameForClass($className);
$entityTypes = self::getEntities();
if (isset($entityTypes[$briefName][$event])) {
foreach ($entityTypes[$briefName][$event] as $filter) {
if (isset($entityTypes[$entityName][$event])) {
foreach ($entityTypes[$entityName][$event] as $filter) {
$args = [$className, &$values];
\Civi\Core\Resolver::singleton()->call($filter, $args);
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/EntityTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ protected function getRelatedTokens(): array {
* @internal function will likely be protected soon.
*/
protected function getPseudoValue(string $realField, string $pseudoKey, $fieldValue): string {
$bao = CRM_Core_DAO_AllCoreTables::getFullName($this->getMetadataForField($realField)['entity']);
$bao = CRM_Core_DAO_AllCoreTables::getDAONameForEntity($this->getMetadataForField($realField)['entity']);
if ($pseudoKey === 'name') {
// There is a theoretical possibility fieldValue could be an array but
// specifically for preferred communication type - but real world usage
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/Form/EntityFormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function buildQuickEntityForm() {
$this->assign('entityFields', $this->entityFields);
$this->assign('entityID', $this->getEntityId());
$this->assign('entityInClassFormat', strtolower(str_replace('_', '-', $this->getDefaultEntity())));
$this->assign('entityTable', CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($this->getDefaultEntity())));
$this->assign('entityTable', CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getDAONameForEntity($this->getDefaultEntity())));
$this->addCustomDataToForm();
$this->addFormButtons();

Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/PseudoConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function get($daoName, $fieldName, $params = [], $context = NULL)
'condition' => [],
'values' => [],
];
$entity = CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
$entity = CRM_Core_DAO_AllCoreTables::getEntityNameForClass($daoName);

// Custom fields are not in the schema
if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
Expand Down
4 changes: 2 additions & 2 deletions CRM/Dedupe/MergeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public function mergeLocations(): void {
foreach ($blocksDAO as $blockDAOs) {
if (!empty($blockDAOs['update'])) {
foreach ($blockDAOs['update'] as $blockDAO) {
$entity = CRM_Core_DAO_AllCoreTables::getBriefName(get_class($blockDAO));
$entity = CRM_Core_DAO_AllCoreTables::getEntityNameForClass(get_class($blockDAO));
$values = ['checkPermissions' => FALSE];
foreach ($blockDAO->fields() as $field) {
if (isset($blockDAO->{$field['name']})) {
Expand All @@ -460,7 +460,7 @@ public function mergeLocations(): void {
}
if (!empty($blockDAOs['delete'])) {
foreach ($blockDAOs['delete'] as $blockDAO) {
$entity = CRM_Core_DAO_AllCoreTables::getBriefName(get_class($blockDAO));
$entity = CRM_Core_DAO_AllCoreTables::getEntityNameForClass(get_class($blockDAO));
civicrm_api4($entity, 'delete', ['where' => [['id', '=', $blockDAO->id]], 'checkPermissions' => FALSE]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Export/Controller/Standalone.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct($title = NULL, $action = CRM_Core_Action::NONE, $mod

// add all the actions
$this->addActions();
$dao = CRM_Core_DAO_AllCoreTables::getFullName($entity);
$dao = CRM_Core_DAO_AllCoreTables::getDAONameForEntity($entity);
CRM_Utils_System::setTitle(ts('Export %1', [1 => $dao::getEntityTitle(TRUE)]));
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ public static function aclGroup($type, $contactID, $tableName, &$allGroups, &$cu
* Values from WHERE or ON clause
*/
public static function selectWhereClause($entity, array &$clauses, int $userId = NULL, array $conditions = []): void {
$entityName = is_object($entity) ? CRM_Core_DAO_AllCoreTables::getBriefName(get_class($entity)) : $entity;
$entityName = is_object($entity) ? CRM_Core_DAO_AllCoreTables::getEntityNameForClass(get_class($entity)) : $entity;
$null = NULL;
$userId ??= (int) CRM_Core_Session::getLoggedInContactID();
self::singleton()->invoke(['entity', 'clauses', 'userId', 'conditions'],
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Recent.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private static function getTokens($str):array {
*/
private static function getIcon($entityType, $entityId) {
$icon = NULL;
$daoClass = CRM_Core_DAO_AllCoreTables::getFullName($entityType);
$daoClass = CRM_Core_DAO_AllCoreTables::getDAONameForEntity($entityType);
if ($daoClass) {
$icon = CRM_Core_DAO_AllCoreTables::getBAOClassName($daoClass)::getEntityIcon($entityType, $entityId);
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct() {
* @return array
*/
public static function mergeSubquery($entityName, $joinColumn = 'id') {
$baoName = CRM_Core_DAO_AllCoreTables::getBAOClassName(CRM_Core_DAO_AllCoreTables::getFullName($entityName));
$baoName = CRM_Core_DAO_AllCoreTables::getBAOClassName(CRM_Core_DAO_AllCoreTables::getDAONameForEntity($entityName));
$bao = new $baoName();
$fields = $bao::getSupportedFields();
$mergeClauses = $subClauses = [];
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ public static function createDefaultCrudLink($crudLinkSpec, $absolute = FALSE) {
$action = strtolower($action);
}

$daoClass = isset($crudLinkSpec['entity']) ? CRM_Core_DAO_AllCoreTables::getFullName($crudLinkSpec['entity']) : CRM_Core_DAO_AllCoreTables::getClassForTable($crudLinkSpec['entity_table']);
$daoClass = isset($crudLinkSpec['entity']) ? CRM_Core_DAO_AllCoreTables::getDAONameForEntity($crudLinkSpec['entity']) : CRM_Core_DAO_AllCoreTables::getClassForTable($crudLinkSpec['entity_table']);
$paths = $daoClass ? $daoClass::getEntityPaths() : [];
$path = $paths[$action] ?? NULL;
if (!$path) {
Expand Down
2 changes: 1 addition & 1 deletion Civi/API/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ protected function getJoinInfo(&$fkField, $stack) {
$entityTable = $this->where[$entityTableParam] ?? NULL;
if ($entityTable && is_string($entityTable) && \CRM_Core_DAO_AllCoreTables::getClassForTable($entityTable)) {
$fkField['FKClassName'] = \CRM_Core_DAO_AllCoreTables::getClassForTable($entityTable);
$fkField['FKApiName'] = \CRM_Core_DAO_AllCoreTables::getBriefName($fkField['FKClassName']);
$fkField['FKApiName'] = \CRM_Core_DAO_AllCoreTables::getEntityNameForClass($fkField['FKClassName']);
}
}
if (!empty($fkField['pseudoconstant']['optionGroupName'])) {
Expand Down
Loading

0 comments on commit 25bc61b

Please sign in to comment.