4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace Civi\Api4\Utils
;
22 use CRM_Core_DAO_AllCoreTables
as AllCoreTables
;
24 require_once 'api/v3/utils.php';
29 * todo this class should not rely on api3 code
33 * @return \CRM_Core_DAO|string
34 * The BAO name for use in static calls. Return doc block is hacked to allow
35 * auto-completion of static methods
37 public static function getBAOFromApiName($entityName) {
38 if ($entityName === 'CustomValue' ||
strpos($entityName, 'Custom_') === 0) {
39 return 'CRM_Core_BAO_CustomValue';
41 return \
_civicrm_api3_get_BAO($entityName);
45 * Get table name of given entity
47 * @param string $entityName
51 public static function getTableName($entityName) {
52 if (strpos($entityName, 'Custom_') === 0) {
53 $customGroup = substr($entityName, 7);
54 return \CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroup, 'table_name', 'name');
56 return AllCoreTables
::getTableForEntityName($entityName);
60 * Given a sql table name, return the name of the api entity.
65 public static function getApiNameFromTableName($tableName) {
66 $entityName = AllCoreTables
::getBriefName(AllCoreTables
::getClassForTable($tableName));
68 $customGroup = \CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $tableName, 'name', 'table_name');
69 $entityName = $customGroup ?
"Custom_$customGroup" : NULL;
77 public static function getOperators() {
78 $operators = \CRM_Core_DAO
::acceptedSQLOperators();
79 $operators[] = 'CONTAINS';
80 $operators[] = 'IS EMPTY';
81 $operators[] = 'IS NOT EMPTY';
86 * For a given API Entity, return the types of custom fields it supports and the column they join to.
88 * @param string $entityName
89 * @return array|mixed|null
90 * @throws \API_Exception
91 * @throws \Civi\API\Exception\UnauthorizedException
93 public static function getCustomGroupExtends(string $entityName) {
94 // Custom_group.extends pretty much maps 1-1 with entity names, except for a couple oddballs (Contact, Participant).
95 switch ($entityName) {
98 'extends' => array_merge(['Contact'], array_keys(\CRM_Core_SelectValues
::contactType())),
104 'extends' => ['Participant', 'ParticipantRole', 'ParticipantEventName', 'ParticipantEventType'],
108 case 'RelationshipCache':
110 'extends' => ['Relationship'],
111 'column' => 'relationship_id',
114 if (array_key_exists($entityName, \CRM_Core_SelectValues
::customGroupExtends())) {
116 'extends' => [$entityName],