Merge pull request #18908 from totten/master-composer-oauth
[civicrm-core.git] / ext / search / Civi / Search / Admin.php
CommitLineData
22601c92
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12namespace Civi\Search;
13
14/**
15 * Class Admin
16 * @package Civi\Search
17 */
18class Admin {
19
20 /**
21 * @return array
22 */
23 public static function getAdminSettings():array {
24 return [
25 'operators' => \CRM_Utils_Array::makeNonAssociative(self::getOperators()),
26 'functions' => \CRM_Api4_Page_Api4Explorer::getSqlFunctions(),
e7515b5b 27 'displayTypes' => Display::getDisplayTypes(['name', 'label', 'description', 'icon']),
22601c92
CW
28 ];
29 }
30
31 /**
32 * @return string[]
33 */
34 public static function getOperators():array {
35 return [
36 '=' => '=',
37 '!=' => '≠',
38 '>' => '>',
39 '<' => '<',
40 '>=' => '≥',
41 '<=' => '≤',
42 'CONTAINS' => ts('Contains'),
43 'IN' => ts('Is In'),
44 'NOT IN' => ts('Not In'),
45 'LIKE' => ts('Is Like'),
46 'NOT LIKE' => ts('Not Like'),
47 'BETWEEN' => ts('Is Between'),
48 'NOT BETWEEN' => ts('Not Between'),
49 'IS NULL' => ts('Is Null'),
50 'IS NOT NULL' => ts('Not Null'),
51 ];
52 }
53
54 /**
55 * Fetch all entities the current user has permission to `get`
590c0e3f 56 * @return array
22601c92
CW
57 */
58 public static function getSchema() {
59 $schema = [];
60 $entities = \Civi\Api4\Entity::get()
44111498 61 ->addSelect('name', 'title', 'title_plural', 'description', 'icon')
22601c92 62 ->addWhere('name', '!=', 'Entity')
44111498 63 ->addOrderBy('title_plural')
22601c92
CW
64 ->setChain([
65 'get' => ['$name', 'getActions', ['where' => [['name', '=', 'get']]], ['params']],
66 ])->execute();
67 $getFields = ['name', 'label', 'description', 'options', 'input_type', 'input_attrs', 'data_type', 'serialize', 'fk_entity'];
68 foreach ($entities as $entity) {
69 // Skip if entity doesn't have a 'get' action or the user doesn't have permission to use get
70 if ($entity['get']) {
71 $entity['fields'] = civicrm_api4($entity['name'], 'getFields', [
72 'select' => $getFields,
393840b9 73 'where' => [['name', 'NOT IN', ['api_key', 'hash']]],
22601c92
CW
74 'orderBy' => ['label'],
75 ]);
76 $params = $entity['get'][0];
77 // Entity must support at least these params or it is too weird for search kit
78 if (!array_diff(['select', 'where', 'orderBy', 'limit', 'offset'], array_keys($params))) {
79 \CRM_Utils_Array::remove($params, 'checkPermissions', 'debug', 'chain', 'language', 'select', 'where', 'orderBy', 'limit', 'offset');
80 unset($entity['get']);
81 $schema[] = ['params' => array_keys($params)] + array_filter($entity);
82 }
83 }
84 }
85 return $schema;
86 }
87
88 /**
590c0e3f 89 * @param array $allowedEntities
22601c92
CW
90 * @return array
91 */
590c0e3f 92 public static function getLinks(array $allowedEntities) {
22601c92
CW
93 $results = [];
94 $keys = array_flip(['alias', 'entity', 'joinType']);
95 foreach (civicrm_api4('Entity', 'getLinks', ['where' => [['entity', 'IN', $allowedEntities]]], ['entity' => 'links']) as $entity => $links) {
96 $entityLinks = [];
97 foreach ($links as $link) {
98 if (!empty($link['entity']) && in_array($link['entity'], $allowedEntities)) {
99 // Use entity.alias as array key to avoid duplicates
100 $entityLinks[$link['entity'] . $link['alias']] = array_intersect_key($link, $keys);
101 }
102 }
103 $results[$entity] = array_values($entityLinks);
104 }
105 return array_filter($results);
106 }
107
108}