Commit | Line | Data |
---|---|---|
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 |
20 | namespace Civi\Api4\Action\Entity; |
21 | ||
22 | use Civi\Api4\CustomGroup; | |
807e5cdb | 23 | use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable; |
19b53e5b C |
24 | |
25 | /** | |
f827fe49 CW |
26 | * Get the names & docblocks of all APIv4 entities. |
27 | * | |
28 | * Scans for api entities in core + enabled extensions. | |
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 | */ | |
35 | class 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 | ); | |
53 | foreach ($locations as $location) { | |
54 | $dir = \CRM_Utils_File::addTrailingSlash(dirname($location)) . 'Civi/Api4'; | |
19b53e5b C |
55 | if (is_dir($dir)) { |
56 | foreach (glob("$dir/*.php") as $file) { | |
57 | $matches = []; | |
3ba93c64 | 58 | preg_match('/(\w*)\.php$/', $file, $matches); |
449c4e6b | 59 | $entity = '\Civi\Api4\\' . $matches[1]; |
7b7c96e6 CW |
60 | if ( |
61 | (!$toGet || in_array($matches[1], $toGet)) | |
449c4e6b | 62 | && is_a($entity, '\Civi\Api4\Generic\AbstractEntity', TRUE) |
7b7c96e6 | 63 | ) { |
449c4e6b CW |
64 | $info = $entity::getInfo(); |
65 | $entities[$info['name']] = $info; | |
19b53e5b | 66 | } |
19b53e5b C |
67 | } |
68 | } | |
69 | } | |
19b53e5b | 70 | |
7b7c96e6 CW |
71 | // Fetch custom entities unless we've already fetched everything requested |
72 | if ($this->includeCustom && (!$toGet || array_diff($toGet, array_keys($entities)))) { | |
19b53e5b C |
73 | $this->addCustomEntities($entities); |
74 | } | |
75 | ||
76 | ksort($entities); | |
77 | return $entities; | |
78 | } | |
79 | ||
80 | /** | |
81 | * Add custom-field pseudo-entities | |
82 | * | |
83 | * @param $entities | |
84 | * @throws \API_Exception | |
85 | */ | |
86 | private function addCustomEntities(&$entities) { | |
87 | $customEntities = CustomGroup::get() | |
88 | ->addWhere('is_multiple', '=', 1) | |
89 | ->addWhere('is_active', '=', 1) | |
2af06525 | 90 | ->setSelect(['name', 'title', 'help_pre', 'help_post', 'extends', 'icon']) |
19b53e5b C |
91 | ->setCheckPermissions(FALSE) |
92 | ->execute(); | |
93 | foreach ($customEntities as $customEntity) { | |
94 | $fieldName = 'Custom_' . $customEntity['name']; | |
807e5cdb | 95 | $baseEntity = '\Civi\Api4\\' . CustomGroupJoinable::getEntityFromExtends($customEntity['extends']); |
19b53e5b C |
96 | $entities[$fieldName] = [ |
97 | 'name' => $fieldName, | |
449c4e6b | 98 | 'title' => $customEntity['title'], |
9813ae79 CW |
99 | 'title_plural' => $customEntity['title'], |
100 | 'description' => ts('Custom group for %1', [1 => $baseEntity::getInfo()['title_plural']]), | |
0493ec47 CW |
101 | 'see' => [ |
102 | 'https://docs.civicrm.org/user/en/latest/organising-your-data/creating-custom-fields/#multiple-record-fieldsets', | |
103 | '\\Civi\\Api4\\CustomGroup', | |
104 | ], | |
2af06525 | 105 | 'icon' => $customEntity['icon'], |
19b53e5b C |
106 | ]; |
107 | if (!empty($customEntity['help_pre'])) { | |
108 | $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']); | |
109 | } | |
110 | if (!empty($customEntity['help_post'])) { | |
111 | $pre = empty($entities[$fieldName]['comment']) ? '' : $entities[$fieldName]['comment'] . "\n\n"; | |
112 | $entities[$fieldName]['comment'] = $pre . $this->plainTextify($customEntity['help_post']); | |
113 | } | |
114 | } | |
115 | } | |
116 | ||
117 | /** | |
118 | * Convert html to plain text. | |
119 | * | |
120 | * @param $input | |
121 | * @return mixed | |
122 | */ | |
123 | private function plainTextify($input) { | |
124 | return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5, 'UTF-8'); | |
125 | } | |
126 | ||
19b53e5b | 127 | } |