Merge pull request #15837 from totten/master-prtmpl
[civicrm-core.git] / Civi / Api4 / Action / Entity / Get.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21
22 namespace Civi\Api4\Action\Entity;
23
24 use Civi\Api4\CustomGroup;
25 use Civi\Api4\Utils\ReflectionUtils;
26
27 /**
28 * Get entities
29 *
30 * @method $this setIncludeCustom(bool $value)
31 * @method bool getIncludeCustom()
32 */
33 class Get extends \Civi\Api4\Generic\BasicGetAction {
34
35 /**
36 * Include custom-field-based pseudo-entities?
37 *
38 * @var bool
39 */
40 protected $includeCustom = TRUE;
41
42 /**
43 * Scan all api directories to discover entities
44 */
45 protected function getRecords() {
46 $entities = [];
47 $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')],
48 array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
49 );
50 foreach ($locations as $location) {
51 $dir = \CRM_Utils_File::addTrailingSlash(dirname($location)) . 'Civi/Api4';
52 if (is_dir($dir)) {
53 foreach (glob("$dir/*.php") as $file) {
54 $matches = [];
55 preg_match('/(\w*).php/', $file, $matches);
56 $entity = ['name' => $matches[1]];
57 if ($this->_isFieldSelected('description') || $this->_isFieldSelected('comment')) {
58 $this->addDocs($entity);
59 }
60 $entities[$matches[1]] = $entity;
61 }
62 }
63 }
64 unset($entities['CustomValue']);
65
66 if ($this->includeCustom) {
67 $this->addCustomEntities($entities);
68 }
69
70 ksort($entities);
71 return $entities;
72 }
73
74 /**
75 * Add custom-field pseudo-entities
76 *
77 * @param $entities
78 * @throws \API_Exception
79 */
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)
86 ->execute();
87 foreach ($customEntities as $customEntity) {
88 $fieldName = 'Custom_' . $customEntity['name'];
89 $entities[$fieldName] = [
90 'name' => $fieldName,
91 'description' => $customEntity['title'] . ' custom group - extends ' . $customEntity['extends'],
92 ];
93 if (!empty($customEntity['help_pre'])) {
94 $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']);
95 }
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']);
99 }
100 }
101 }
102
103 /**
104 * Convert html to plain text.
105 *
106 * @param $input
107 * @return mixed
108 */
109 private function plainTextify($input) {
110 return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5, 'UTF-8');
111 }
112
113 /**
114 * Add info from code docblock.
115 *
116 * @param $entity
117 */
118 private function addDocs(&$entity) {
119 $reflection = new \ReflectionClass("\\Civi\\Api4\\" . $entity['name']);
120 $entity += ReflectionUtils::getCodeDocs($reflection);
121 unset($entity['package'], $entity['method']);
122 }
123
124 }