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 $locations = array_merge([\Civi
::paths()->getPath('[civicrm.root]/Civi.php')],
48 array_column(\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
50 foreach ($locations as $location) {
51 $dir = \CRM_Utils_File
::addTrailingSlash(dirname($location)) . 'Civi/Api4';
53 foreach (glob("$dir/*.php") as $file) {
55 preg_match('/(\w*).php/', $file, $matches);
56 $entity = ['name' => $matches[1]];
57 if ($this->_isFieldSelected('description') ||
$this->_isFieldSelected('comment')) {
58 $this->addDocs($entity);
60 $entities[$matches[1]] = $entity;
64 unset($entities['CustomValue']);
66 if ($this->includeCustom
) {
67 $this->addCustomEntities($entities);
75 * Add custom-field pseudo-entities
78 * @throws \API_Exception
80 private function addCustomEntities(&$entities) {
81 $customEntities = CustomGroup
::get()
82 ->addWhere('is_multiple', '=', 1)
83 ->addWhere('is_active', '=', 1)
84 ->setSelect(['name', 'title', 'help_pre', 'help_post', 'extends'])
85 ->setCheckPermissions(FALSE)
87 foreach ($customEntities as $customEntity) {
88 $fieldName = 'Custom_' . $customEntity['name'];
89 $entities[$fieldName] = [
91 'description' => $customEntity['title'] . ' custom group - extends ' . $customEntity['extends'],
93 if (!empty($customEntity['help_pre'])) {
94 $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']);
96 if (!empty($customEntity['help_post'])) {
97 $pre = empty($entities[$fieldName]['comment']) ?
'' : $entities[$fieldName]['comment'] . "\n\n";
98 $entities[$fieldName]['comment'] = $pre . $this->plainTextify($customEntity['help_post']);
104 * Convert html to plain text.
109 private function plainTextify($input) {
110 return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5
, 'UTF-8');
114 * Add info from code docblock.
118 private function addDocs(&$entity) {
119 $reflection = new \
ReflectionClass("\\Civi\\Api4\\" . $entity['name']);
120 $entity +
= ReflectionUtils
::getCodeDocs($reflection);
121 unset($entity['package'], $entity['method']);