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
;
30 * @method $this setIncludeCustom(bool $value)
31 * @method bool getIncludeCustom()
33 class Get
extends \Civi\Api4\Generic\BasicGetAction
{
36 * Include custom-field-based pseudo-entities?
40 protected $includeCustom = TRUE;
43 * Scan all api directories to discover entities
45 protected function getRecords() {
47 $toGet = $this->_itemsToGet('name');
48 $locations = array_merge([\Civi
::paths()->getPath('[civicrm.root]/Civi.php')],
49 array_column(\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
51 foreach ($locations as $location) {
52 $dir = \CRM_Utils_File
::addTrailingSlash(dirname($location)) . 'Civi/Api4';
54 foreach (glob("$dir/*.php") as $file) {
56 preg_match('/(\w*).php/', $file, $matches);
58 (!$toGet ||
in_array($matches[1], $toGet))
59 && is_a('\Civi\Api4\\' . $matches[1], '\Civi\Api4\Generic\AbstractEntity', TRUE)
61 $entity = ['name' => $matches[1]];
62 if ($this->_isFieldSelected('description') ||
$this->_isFieldSelected('comment')) {
63 $this->addDocs($entity);
65 $entities[$matches[1]] = $entity;
71 // Fetch custom entities unless we've already fetched everything requested
72 if ($this->includeCustom
&& (!$toGet ||
array_diff($toGet, array_keys($entities)))) {
73 $this->addCustomEntities($entities);
81 * Add custom-field pseudo-entities
84 * @throws \API_Exception
86 private function addCustomEntities(&$entities) {
87 $customEntities = CustomGroup
::get()
88 ->addWhere('is_multiple', '=', 1)
89 ->addWhere('is_active', '=', 1)
90 ->setSelect(['name', 'title', 'help_pre', 'help_post', 'extends'])
91 ->setCheckPermissions(FALSE)
93 foreach ($customEntities as $customEntity) {
94 $fieldName = 'Custom_' . $customEntity['name'];
95 $entities[$fieldName] = [
97 'description' => $customEntity['title'] . ' custom group - extends ' . $customEntity['extends'],
99 if (!empty($customEntity['help_pre'])) {
100 $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']);
102 if (!empty($customEntity['help_post'])) {
103 $pre = empty($entities[$fieldName]['comment']) ?
'' : $entities[$fieldName]['comment'] . "\n\n";
104 $entities[$fieldName]['comment'] = $pre . $this->plainTextify($customEntity['help_post']);
110 * Convert html to plain text.
115 private function plainTextify($input) {
116 return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5
, 'UTF-8');
120 * Add info from code docblock.
124 private function addDocs(&$entity) {
125 $reflection = new \
ReflectionClass("\\Civi\\Api4\\" . $entity['name']);
126 $entity +
= ReflectionUtils
::getCodeDocs($reflection);
127 unset($entity['package'], $entity['method']);