Commit | Line | Data |
---|---|---|
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 | ||
12 | namespace Civi\Search; | |
13 | ||
14 | /** | |
15 | * Class Admin | |
16 | * @package Civi\Search | |
17 | */ | |
18 | class 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() | |
465bc32a | 61 | ->addSelect('name', 'title', 'type', 'title_plural', 'description', 'icon', 'paths') |
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']) { | |
f9cf8797 CW |
71 | // Add paths (but only RUD actions) with translated titles |
72 | foreach ($entity['paths'] as $action => $path) { | |
73 | unset($entity['paths'][$action]); | |
74 | switch ($action) { | |
75 | case 'view': | |
76 | $title = ts('View %1', [1 => $entity['title']]); | |
77 | break; | |
78 | ||
5c385299 | 79 | case 'update': |
f9cf8797 CW |
80 | $title = ts('Edit %1', [1 => $entity['title']]); |
81 | break; | |
82 | ||
83 | case 'delete': | |
84 | $title = ts('Delete %1', [1 => $entity['title']]); | |
85 | break; | |
86 | ||
87 | default: | |
88 | continue 2; | |
89 | } | |
90 | $entity['paths'][] = [ | |
91 | 'path' => $path, | |
92 | 'title' => $title, | |
93 | 'action' => $action, | |
94 | ]; | |
95 | } | |
22601c92 CW |
96 | $entity['fields'] = civicrm_api4($entity['name'], 'getFields', [ |
97 | 'select' => $getFields, | |
393840b9 | 98 | 'where' => [['name', 'NOT IN', ['api_key', 'hash']]], |
22601c92 CW |
99 | 'orderBy' => ['label'], |
100 | ]); | |
101 | $params = $entity['get'][0]; | |
102 | // Entity must support at least these params or it is too weird for search kit | |
103 | if (!array_diff(['select', 'where', 'orderBy', 'limit', 'offset'], array_keys($params))) { | |
104 | \CRM_Utils_Array::remove($params, 'checkPermissions', 'debug', 'chain', 'language', 'select', 'where', 'orderBy', 'limit', 'offset'); | |
105 | unset($entity['get']); | |
106 | $schema[] = ['params' => array_keys($params)] + array_filter($entity); | |
107 | } | |
108 | } | |
109 | } | |
110 | return $schema; | |
111 | } | |
112 | ||
113 | /** | |
590c0e3f | 114 | * @param array $allowedEntities |
22601c92 CW |
115 | * @return array |
116 | */ | |
590c0e3f | 117 | public static function getLinks(array $allowedEntities) { |
22601c92 CW |
118 | $results = []; |
119 | $keys = array_flip(['alias', 'entity', 'joinType']); | |
120 | foreach (civicrm_api4('Entity', 'getLinks', ['where' => [['entity', 'IN', $allowedEntities]]], ['entity' => 'links']) as $entity => $links) { | |
121 | $entityLinks = []; | |
122 | foreach ($links as $link) { | |
123 | if (!empty($link['entity']) && in_array($link['entity'], $allowedEntities)) { | |
124 | // Use entity.alias as array key to avoid duplicates | |
125 | $entityLinks[$link['entity'] . $link['alias']] = array_intersect_key($link, $keys); | |
126 | } | |
127 | } | |
128 | $results[$entity] = array_values($entityLinks); | |
129 | } | |
130 | return array_filter($results); | |
131 | } | |
132 | ||
133 | } |