Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
41498ac5 | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
41498ac5 TO |
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 | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | * $Id$ |
18 | * | |
19 | */ | |
20 | ||
21 | ||
19b53e5b C |
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 = []; | |
7b7c96e6 | 47 | $toGet = $this->_itemsToGet('name'); |
425c9756 | 48 | $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')], |
bd2669ea CW |
49 | array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath') |
50 | ); | |
51 | foreach ($locations as $location) { | |
52 | $dir = \CRM_Utils_File::addTrailingSlash(dirname($location)) . 'Civi/Api4'; | |
19b53e5b C |
53 | if (is_dir($dir)) { |
54 | foreach (glob("$dir/*.php") as $file) { | |
55 | $matches = []; | |
56 | preg_match('/(\w*).php/', $file, $matches); | |
7b7c96e6 CW |
57 | if ( |
58 | (!$toGet || in_array($matches[1], $toGet)) | |
59 | && is_a('\Civi\Api4\\' . $matches[1], '\Civi\Api4\Generic\AbstractEntity', TRUE) | |
60 | ) { | |
61 | $entity = ['name' => $matches[1]]; | |
62 | if ($this->_isFieldSelected('description') || $this->_isFieldSelected('comment')) { | |
63 | $this->addDocs($entity); | |
64 | } | |
65 | $entities[$matches[1]] = $entity; | |
19b53e5b | 66 | } |
19b53e5b C |
67 | } |
68 | } | |
69 | } | |
19b53e5b | 70 | |
7b7c96e6 CW |
71 | // Fetch custom entities unless we've already fetched everything requested |
72 | if ($this->includeCustom && (!$toGet || array_diff($toGet, array_keys($entities)))) { | |
19b53e5b C |
73 | $this->addCustomEntities($entities); |
74 | } | |
75 | ||
76 | ksort($entities); | |
77 | return $entities; | |
78 | } | |
79 | ||
80 | /** | |
81 | * Add custom-field pseudo-entities | |
82 | * | |
83 | * @param $entities | |
84 | * @throws \API_Exception | |
85 | */ | |
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) | |
92 | ->execute(); | |
93 | foreach ($customEntities as $customEntity) { | |
94 | $fieldName = 'Custom_' . $customEntity['name']; | |
95 | $entities[$fieldName] = [ | |
96 | 'name' => $fieldName, | |
97 | 'description' => $customEntity['title'] . ' custom group - extends ' . $customEntity['extends'], | |
98 | ]; | |
99 | if (!empty($customEntity['help_pre'])) { | |
100 | $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']); | |
101 | } | |
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']); | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | /** | |
110 | * Convert html to plain text. | |
111 | * | |
112 | * @param $input | |
113 | * @return mixed | |
114 | */ | |
115 | private function plainTextify($input) { | |
116 | return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5, 'UTF-8'); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Add info from code docblock. | |
121 | * | |
122 | * @param $entity | |
123 | */ | |
124 | private function addDocs(&$entity) { | |
125 | $reflection = new \ReflectionClass("\\Civi\\Api4\\" . $entity['name']); | |
126 | $entity += ReflectionUtils::getCodeDocs($reflection); | |
127 | unset($entity['package'], $entity['method']); | |
128 | } | |
129 | ||
130 | } |