Add is empty filter to search / api
[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 $operators[] = 'IS EMPTY';
81 $operators[] = 'IS NOT EMPTY';
82 return $operators;
83 }
84
85 /**
86 * For a given API Entity, return the types of custom fields it supports and the column they join to.
87 *
88 * @param string $entityName
89 * @return array|mixed|null
90 * @throws \API_Exception
91 * @throws \Civi\API\Exception\UnauthorizedException
92 */
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) {
96 case 'Contact':
97 return [
98 'extends' => array_merge(['Contact'], array_keys(\CRM_Core_SelectValues::contactType())),
99 'column' => 'id',
100 ];
101
102 case 'Participant':
103 return [
104 'extends' => ['Participant', 'ParticipantRole', 'ParticipantEventName', 'ParticipantEventType'],
105 'column' => 'id',
106 ];
107
108 case 'RelationshipCache':
109 return [
110 'extends' => ['Relationship'],
111 'column' => 'relationship_id',
112 ];
113 }
114 if (array_key_exists($entityName, \CRM_Core_SelectValues::customGroupExtends())) {
115 return [
116 'extends' => [$entityName],
117 'column' => 'id',
118 ];
119 }
120 return NULL;
121 }
122
123 }