SearchKit - Add links to view multi-record custom data
[civicrm-core.git] / Civi / Api4 / Action / Entity / Get.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
41498ac5 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
41498ac5
TO
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 |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545
TO
17 */
18
19
19b53e5b
C
20namespace Civi\Api4\Action\Entity;
21
22use Civi\Api4\CustomGroup;
807e5cdb 23use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable;
19b53e5b
C
24
25/**
f827fe49
CW
26 * Get the names & docblocks of all APIv4 entities.
27 *
d31fb4e3 28 * Scans for api entities in core, enabled components & enabled extensions.
f827fe49
CW
29 *
30 * Also includes pseudo-entities from multi-record custom groups by default.
19b53e5b
C
31 *
32 * @method $this setIncludeCustom(bool $value)
33 * @method bool getIncludeCustom()
34 */
35class Get extends \Civi\Api4\Generic\BasicGetAction {
36
37 /**
38 * Include custom-field-based pseudo-entities?
39 *
40 * @var bool
41 */
42 protected $includeCustom = TRUE;
43
44 /**
45 * Scan all api directories to discover entities
46 */
47 protected function getRecords() {
48 $entities = [];
7b7c96e6 49 $toGet = $this->_itemsToGet('name');
425c9756 50 $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')],
bd2669ea
CW
51 array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
52 );
d31fb4e3 53 $enabledComponents = array_keys(\CRM_Core_Component::getEnabledComponents());
bd2669ea
CW
54 foreach ($locations as $location) {
55 $dir = \CRM_Utils_File::addTrailingSlash(dirname($location)) . 'Civi/Api4';
19b53e5b
C
56 if (is_dir($dir)) {
57 foreach (glob("$dir/*.php") as $file) {
58 $matches = [];
3ba93c64 59 preg_match('/(\w*)\.php$/', $file, $matches);
449c4e6b 60 $entity = '\Civi\Api4\\' . $matches[1];
7b7c96e6
CW
61 if (
62 (!$toGet || in_array($matches[1], $toGet))
449c4e6b 63 && is_a($entity, '\Civi\Api4\Generic\AbstractEntity', TRUE)
7b7c96e6 64 ) {
449c4e6b 65 $info = $entity::getInfo();
d31fb4e3
CW
66 // Only include DAO entities from enabled components
67 if (empty($info['dao']) || !defined($info['dao'] . '::COMPONENT') || in_array(constant($info['dao'] . '::COMPONENT'), $enabledComponents)) {
68 $entities[$info['name']] = $info;
69 }
19b53e5b 70 }
19b53e5b
C
71 }
72 }
73 }
19b53e5b 74
7b7c96e6
CW
75 // Fetch custom entities unless we've already fetched everything requested
76 if ($this->includeCustom && (!$toGet || array_diff($toGet, array_keys($entities)))) {
19b53e5b
C
77 $this->addCustomEntities($entities);
78 }
79
80 ksort($entities);
81 return $entities;
82 }
83
84 /**
85 * Add custom-field pseudo-entities
86 *
87 * @param $entities
88 * @throws \API_Exception
89 */
90 private function addCustomEntities(&$entities) {
91 $customEntities = CustomGroup::get()
92 ->addWhere('is_multiple', '=', 1)
93 ->addWhere('is_active', '=', 1)
2af06525 94 ->setSelect(['name', 'title', 'help_pre', 'help_post', 'extends', 'icon'])
19b53e5b
C
95 ->setCheckPermissions(FALSE)
96 ->execute();
97 foreach ($customEntities as $customEntity) {
98 $fieldName = 'Custom_' . $customEntity['name'];
807e5cdb 99 $baseEntity = '\Civi\Api4\\' . CustomGroupJoinable::getEntityFromExtends($customEntity['extends']);
19b53e5b
C
100 $entities[$fieldName] = [
101 'name' => $fieldName,
449c4e6b 102 'title' => $customEntity['title'],
9813ae79
CW
103 'title_plural' => $customEntity['title'],
104 'description' => ts('Custom group for %1', [1 => $baseEntity::getInfo()['title_plural']]),
5828ae54
CW
105 'searchable' => TRUE,
106 'type' => ['CustomValue'],
e8854d68
CW
107 'paths' => [
108 'view' => "civicrm/contact/view/cd?reset=1&gid={$customEntity['id']}&recId=[id]&multiRecordDisplay=single",
109 ],
0493ec47
CW
110 'see' => [
111 'https://docs.civicrm.org/user/en/latest/organising-your-data/creating-custom-fields/#multiple-record-fieldsets',
112 '\\Civi\\Api4\\CustomGroup',
113 ],
2af06525 114 'icon' => $customEntity['icon'],
19b53e5b
C
115 ];
116 if (!empty($customEntity['help_pre'])) {
117 $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']);
118 }
119 if (!empty($customEntity['help_post'])) {
120 $pre = empty($entities[$fieldName]['comment']) ? '' : $entities[$fieldName]['comment'] . "\n\n";
121 $entities[$fieldName]['comment'] = $pre . $this->plainTextify($customEntity['help_post']);
122 }
123 }
124 }
125
126 /**
127 * Convert html to plain text.
128 *
129 * @param $input
130 * @return mixed
131 */
132 private function plainTextify($input) {
133 return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5, 'UTF-8');
134 }
135
19b53e5b 136}