Merge pull request #17431 from civicrm/5.26
[civicrm-core.git] / Civi / Api4 / Utils / CoreUtil.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
41498ac5 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
41498ac5
TO
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 |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545
TO
17 * $Id$
18 *
19 */
20
21
19b53e5b
C
22namespace Civi\Api4\Utils;
23
19b53e5b
C
24use CRM_Core_DAO_AllCoreTables as AllCoreTables;
25
26require_once 'api/v3/utils.php';
27
28class CoreUtil {
29
30 /**
31 * todo this class should not rely on api3 code
32 *
33 * @param $entityName
34 *
35 * @return \CRM_Core_DAO|string
36 * The BAO name for use in static calls. Return doc block is hacked to allow
37 * auto-completion of static methods
38 */
39 public static function getBAOFromApiName($entityName) {
40 if ($entityName === 'CustomValue' || strpos($entityName, 'Custom_') === 0) {
5e327f37 41 return 'CRM_Core_BAO_CustomValue';
19b53e5b
C
42 }
43 return \_civicrm_api3_get_BAO($entityName);
44 }
45
46 /**
5e327f37 47 * Get table name of given entity
19b53e5b 48 *
5e327f37 49 * @param string $entityName
19b53e5b
C
50 *
51 * @return string
52 */
5e327f37
CW
53 public static function getTableName($entityName) {
54 if (strpos($entityName, 'Custom_') === 0) {
55 $customGroup = substr($entityName, 7);
56 return \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroup, 'table_name', 'name');
57 }
58 return AllCoreTables::getTableForEntityName($entityName);
19b53e5b
C
59 }
60
61 /**
62 * Given a sql table name, return the name of the api entity.
63 *
64 * @param $tableName
5e327f37 65 * @return string|NULL
19b53e5b
C
66 */
67 public static function getApiNameFromTableName($tableName) {
5e327f37
CW
68 $entityName = AllCoreTables::getBriefName(AllCoreTables::getClassForTable($tableName));
69 if (!$entityName) {
70 $customGroup = \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $tableName, 'name', 'table_name');
71 $entityName = $customGroup ? "Custom_$customGroup" : NULL;
72 }
73 return $entityName;
19b53e5b
C
74 }
75
76}