3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 namespace Civi\Search
;
16 * @package Civi\Search
23 public static function getAdminSettings():array {
24 $schema = self
::getSchema();
27 'joins' => self
::getJoins(array_column($schema, NULL, 'name')),
28 'operators' => \CRM_Utils_Array
::makeNonAssociative(self
::getOperators()),
29 'functions' => \CRM_Api4_Page_Api4Explorer
::getSqlFunctions(),
30 'displayTypes' => Display
::getDisplayTypes(['name', 'label', 'description', 'icon']),
37 public static function getOperators():array {
45 'CONTAINS' => ts('Contains'),
46 'IN' => ts('Is One Of'),
47 'NOT IN' => ts('Not One Of'),
48 'LIKE' => ts('Is Like'),
49 'NOT LIKE' => ts('Not Like'),
50 'BETWEEN' => ts('Is Between'),
51 'NOT BETWEEN' => ts('Not Between'),
52 'IS NULL' => ts('Is Null'),
53 'IS NOT NULL' => ts('Not Null'),
58 * Fetch all entities the current user has permission to `get`
61 public static function getSchema() {
63 $entities = \Civi\Api4\Entity
::get()
64 ->addSelect('name', 'title', 'type', 'title_plural', 'description', 'icon', 'paths', 'dao', 'bridge')
65 ->addWhere('searchable', '=', TRUE)
66 ->addOrderBy('title_plural')
68 'get' => ['$name', 'getActions', ['where' => [['name', '=', 'get']]], ['params']],
70 $getFields = ['name', 'label', 'description', 'options', 'input_type', 'input_attrs', 'data_type', 'serialize', 'fk_entity'];
71 foreach ($entities as $entity) {
72 // Skip if entity doesn't have a 'get' action or the user doesn't have permission to use get
74 // Add paths (but only RUD actions) with translated titles
75 foreach ($entity['paths'] as $action => $path) {
76 unset($entity['paths'][$action]);
79 $title = ts('View %1', [1 => $entity['title']]);
83 $title = ts('Edit %1', [1 => $entity['title']]);
87 $title = ts('Delete %1', [1 => $entity['title']]);
93 $entity['paths'][] = [
99 $entity['fields'] = (array) civicrm_api4($entity['name'], 'getFields', [
100 'select' => $getFields,
101 'where' => [['name', 'NOT IN', ['api_key', 'hash']]],
102 'orderBy' => ['label'],
104 $params = $entity['get'][0];
105 // Entity must support at least these params or it is too weird for search kit
106 if (!array_diff(['select', 'where', 'orderBy', 'limit', 'offset'], array_keys($params))) {
107 \CRM_Utils_Array
::remove($params, 'checkPermissions', 'debug', 'chain', 'language', 'select', 'where', 'orderBy', 'limit', 'offset');
108 unset($entity['get']);
109 $schema[] = ['params' => array_keys($params)] +
array_filter($entity);
117 * @param array $allowedEntities
120 public static function getJoins(array $allowedEntities) {
122 foreach ($allowedEntities as $entity) {
123 if (!empty($entity['dao'])) {
124 /* @var \CRM_Core_DAO $daoClass */
125 $daoClass = $entity['dao'];
126 $references = $daoClass::getReferenceColumns();
127 // Only the first bridge reference gets processed, so if it's dynamic we want to be sure it's first in the list
128 usort($references, function($reference) {
129 return is_a($reference, 'CRM_Core_Reference_Dynamic') ?
-1 : 1;
131 $fields = array_column($entity['fields'], NULL, 'name');
132 $bridge = in_array('EntityBridge', $entity['type']) ?
$entity['name'] : NULL;
133 foreach ($references as $reference) {
134 $keyField = $fields[$reference->getReferenceKey()] ??
NULL;
135 // Exclude any joins that are better represented by pseudoconstants
136 if (is_a($reference, 'CRM_Core_Reference_OptionValue')
137 ||
!$keyField ||
!empty($keyField['options'])
138 // Limit bridge joins to just the first
139 ||
$bridge && array_search($keyField['name'], $entity['bridge']) !== 0
140 // Sanity check - table should match
141 ||
$daoClass::getTableName() !== $reference->getReferenceTable()
145 foreach ($reference->getTargetEntities() as $targetTable => $targetEntityName) {
146 if (!isset($allowedEntities[$targetEntityName]) ||
$targetEntityName === $entity['name']) {
149 $targetEntity = $allowedEntities[$targetEntityName];
150 // Dynamic references use a column like "entity_table"
151 $dynamicCol = $reference->getTypeColumn();
152 // Non-bridge joins directly between 2 entities
154 // Add the straight 1-1 join
155 $alias = $entity['name'] . '_' . $targetEntityName . '_' . $keyField['name'];
156 $joins[$entity['name']][] = [
157 'label' => $entity['title'] . ' ' . $targetEntity['title'],
158 'description' => $dynamicCol ?
'' : $keyField['label'],
159 'entity' => $targetEntityName,
160 'conditions' => self
::getJoinConditions($keyField['name'], $alias . '.' . $reference->getTargetKey(), $targetTable, $dynamicCol),
164 // Flip the conditions & add the reverse (1-n) join
165 $alias = $targetEntityName . '_' . $entity['name'] . '_' . $keyField['name'];
166 $joins[$targetEntityName][] = [
167 'label' => $targetEntity['title'] . ' ' . $entity['title_plural'],
168 'description' => $dynamicCol ?
'' : $keyField['label'],
169 'entity' => $entity['name'],
170 'conditions' => self
::getJoinConditions($reference->getTargetKey(), $alias . '.' . $keyField['name'], $targetTable, $dynamicCol ?
$alias . '.' . $dynamicCol : NULL),
175 // Bridge joins (sanity check - bridge must specify exactly 2 FK fields)
176 elseif (count($entity['bridge']) === 2) {
177 // Get the other entity being linked through this bridge
178 $baseKey = array_search($reference->getReferenceKey(), $entity['bridge']) ?
$entity['bridge'][0] : $entity['bridge'][1];
179 $baseEntity = $allowedEntities[$fields[$baseKey]['fk_entity']] ??
NULL;
183 // Add joins for the two entities that connect through this bridge (n-n)
184 $symmetric = $baseEntity['name'] === $targetEntityName;
185 $targetsTitle = $symmetric ?
$allowedEntities[$bridge]['title_plural'] : $targetEntity['title_plural'];
186 $alias = $baseEntity['name'] . "_{$bridge}_" . $targetEntityName;
187 $joins[$baseEntity['name']][] = [
188 'label' => $baseEntity['title'] . ' ' . $targetsTitle,
189 'description' => ts('Multiple %1 per %2', [1 => $targetsTitle, 2 => $baseEntity['title']]),
190 'entity' => $targetEntityName,
191 'conditions' => array_merge(
193 self
::getJoinConditions('id', $alias . '.' . $baseKey, NULL, NULL)
200 $alias = $targetEntityName . "_{$bridge}_" . $baseEntity['name'];
201 $joins[$targetEntityName][] = [
202 'label' => $targetEntity['title'] . ' ' . $baseEntity['title_plural'],
203 'description' => ts('Multiple %1 per %2', [1 => $baseEntity['title_plural'], 2 => $targetEntity['title']]),
204 'entity' => $baseEntity['name'],
205 'conditions' => array_merge(
207 self
::getJoinConditions($reference->getTargetKey(), $alias . '.' . $keyField['name'], $targetTable, $dynamicCol ?
$alias . '.' . $dynamicCol : NULL)
223 * Boilerplate join clause
225 * @param string $nearCol
226 * @param string $farCol
227 * @param string $targetTable
228 * @param string|null $dynamicCol
231 private static function getJoinConditions($nearCol, $farCol, $targetTable, $dynamicCol) {