APIv4 Search: Improve GROUP_CONCAT with :label prefix
[civicrm-core.git] / Civi / Api4 / Utils / CoreUtil.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19
20 namespace Civi\Api4\Utils;
21
22 use CRM_Core_DAO_AllCoreTables as AllCoreTables;
23
24 require_once 'api/v3/utils.php';
25
26 class CoreUtil {
27
28 /**
29 * todo this class should not rely on api3 code
30 *
31 * @param $entityName
32 *
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
36 */
37 public static function getBAOFromApiName($entityName) {
38 if ($entityName === 'CustomValue' || strpos($entityName, 'Custom_') === 0) {
39 return 'CRM_Core_BAO_CustomValue';
40 }
41 return \_civicrm_api3_get_BAO($entityName);
42 }
43
44 /**
45 * Get table name of given entity
46 *
47 * @param string $entityName
48 *
49 * @return string
50 */
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');
55 }
56 return AllCoreTables::getTableForEntityName($entityName);
57 }
58
59 /**
60 * Given a sql table name, return the name of the api entity.
61 *
62 * @param $tableName
63 * @return string|NULL
64 */
65 public static function getApiNameFromTableName($tableName) {
66 $entityName = AllCoreTables::getBriefName(AllCoreTables::getClassForTable($tableName));
67 if (!$entityName) {
68 $customGroup = \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $tableName, 'name', 'table_name');
69 $entityName = $customGroup ? "Custom_$customGroup" : NULL;
70 }
71 return $entityName;
72 }
73
74 /**
75 * @return string[]
76 */
77 public static function getOperators() {
78 $operators = \CRM_Core_DAO::acceptedSQLOperators();
79 $operators[] = 'CONTAINS';
80 return $operators;
81 }
82
83 }