4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2019
38 namespace Civi\Api4\Action\Entity
;
40 use Civi\Api4\CustomGroup
;
41 use Civi\Api4\Utils\ReflectionUtils
;
46 * @method $this setIncludeCustom(bool $value)
47 * @method bool getIncludeCustom()
49 class Get
extends \Civi\Api4\Generic\BasicGetAction
{
52 * Include custom-field-based pseudo-entities?
56 protected $includeCustom = TRUE;
59 * Scan all api directories to discover entities
61 protected function getRecords() {
63 $locations = array_merge([\Civi
::paths()->getPath('[civicrm.root]/Civi.php')],
64 array_column(\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
66 foreach ($locations as $location) {
67 $dir = \CRM_Utils_File
::addTrailingSlash(dirname($location)) . 'Civi/Api4';
69 foreach (glob("$dir/*.php") as $file) {
71 preg_match('/(\w*).php/', $file, $matches);
72 $entity = ['name' => $matches[1]];
73 if ($this->_isFieldSelected('description') ||
$this->_isFieldSelected('comment')) {
74 $this->addDocs($entity);
76 $entities[$matches[1]] = $entity;
80 unset($entities['CustomValue']);
82 if ($this->includeCustom
) {
83 $this->addCustomEntities($entities);
91 * Add custom-field pseudo-entities
94 * @throws \API_Exception
96 private function addCustomEntities(&$entities) {
97 $customEntities = CustomGroup
::get()
98 ->addWhere('is_multiple', '=', 1)
99 ->addWhere('is_active', '=', 1)
100 ->setSelect(['name', 'title', 'help_pre', 'help_post', 'extends'])
101 ->setCheckPermissions(FALSE)
103 foreach ($customEntities as $customEntity) {
104 $fieldName = 'Custom_' . $customEntity['name'];
105 $entities[$fieldName] = [
106 'name' => $fieldName,
107 'description' => $customEntity['title'] . ' custom group - extends ' . $customEntity['extends'],
109 if (!empty($customEntity['help_pre'])) {
110 $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']);
112 if (!empty($customEntity['help_post'])) {
113 $pre = empty($entities[$fieldName]['comment']) ?
'' : $entities[$fieldName]['comment'] . "\n\n";
114 $entities[$fieldName]['comment'] = $pre . $this->plainTextify($customEntity['help_post']);
120 * Convert html to plain text.
125 private function plainTextify($input) {
126 return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5
, 'UTF-8');
130 * Add info from code docblock.
134 private function addDocs(&$entity) {
135 $reflection = new \
ReflectionClass("\\Civi\\Api4\\" . $entity['name']);
136 $entity +
= ReflectionUtils
::getCodeDocs($reflection);
137 unset($entity['package'], $entity['method']);