4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace Civi\Api4\Action\Entity
;
24 use Civi\Api4\CustomGroup
;
25 use Civi\Api4\Utils\ReflectionUtils
;
28 * Get the names & docblocks of all APIv4 entities.
30 * Scans for api entities in core + enabled extensions.
32 * Also includes pseudo-entities from multi-record custom groups by default.
34 * @method $this setIncludeCustom(bool $value)
35 * @method bool getIncludeCustom()
37 class Get
extends \Civi\Api4\Generic\BasicGetAction
{
40 * Include custom-field-based pseudo-entities?
44 protected $includeCustom = TRUE;
47 * Scan all api directories to discover entities
49 protected function getRecords() {
51 $toGet = $this->_itemsToGet('name');
52 $getDocs = $this->_isFieldSelected('description', 'comment', 'see');
53 $locations = array_merge([\Civi
::paths()->getPath('[civicrm.root]/Civi.php')],
54 array_column(\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
56 foreach ($locations as $location) {
57 $dir = \CRM_Utils_File
::addTrailingSlash(dirname($location)) . 'Civi/Api4';
59 foreach (glob("$dir/*.php") as $file) {
61 preg_match('/(\w*).php/', $file, $matches);
63 (!$toGet ||
in_array($matches[1], $toGet))
64 && is_a('\Civi\Api4\\' . $matches[1], '\Civi\Api4\Generic\AbstractEntity', TRUE)
66 $entity = ['name' => $matches[1]];
68 $this->addDocs($entity);
70 $entities[$matches[1]] = $entity;
76 // Fetch custom entities unless we've already fetched everything requested
77 if ($this->includeCustom
&& (!$toGet ||
array_diff($toGet, array_keys($entities)))) {
78 $this->addCustomEntities($entities);
86 * Add custom-field pseudo-entities
89 * @throws \API_Exception
91 private function addCustomEntities(&$entities) {
92 $customEntities = CustomGroup
::get()
93 ->addWhere('is_multiple', '=', 1)
94 ->addWhere('is_active', '=', 1)
95 ->setSelect(['name', 'title', 'help_pre', 'help_post', 'extends'])
96 ->setCheckPermissions(FALSE)
98 foreach ($customEntities as $customEntity) {
99 $fieldName = 'Custom_' . $customEntity['name'];
100 $entities[$fieldName] = [
101 'name' => $fieldName,
102 'description' => $customEntity['title'] . ' custom group - extends ' . $customEntity['extends'],
104 'https://docs.civicrm.org/user/en/latest/organising-your-data/creating-custom-fields/#multiple-record-fieldsets',
105 '\\Civi\\Api4\\CustomGroup',
108 if (!empty($customEntity['help_pre'])) {
109 $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']);
111 if (!empty($customEntity['help_post'])) {
112 $pre = empty($entities[$fieldName]['comment']) ?
'' : $entities[$fieldName]['comment'] . "\n\n";
113 $entities[$fieldName]['comment'] = $pre . $this->plainTextify($customEntity['help_post']);
119 * Convert html to plain text.
124 private function plainTextify($input) {
125 return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5
, 'UTF-8');
129 * Add info from code docblock.
133 private function addDocs(&$entity) {
134 $reflection = new \
ReflectionClass("\\Civi\\Api4\\" . $entity['name']);
135 $entity +
= ReflectionUtils
::getCodeDocs($reflection, NULL, ['entity' => $entity['name']]);
136 unset($entity['package'], $entity['method']);