Merge branch '5.18' of https://github.com/civicrm/civicrm-core into merge
[civicrm-core.git] / Civi / Api4 / Action / Entity / Get.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 *
35 */
36
37
38 namespace Civi\Api4\Action\Entity;
39
40 use Civi\Api4\CustomGroup;
41 use Civi\Api4\Utils\ReflectionUtils;
42
43 /**
44 * Get entities
45 *
46 * @method $this setIncludeCustom(bool $value)
47 * @method bool getIncludeCustom()
48 */
49 class Get extends \Civi\Api4\Generic\BasicGetAction {
50
51 /**
52 * Include custom-field-based pseudo-entities?
53 *
54 * @var bool
55 */
56 protected $includeCustom = TRUE;
57
58 /**
59 * Scan all api directories to discover entities
60 */
61 protected function getRecords() {
62 $entities = [];
63 $locations = array_merge([\Civi::paths()->getPath('[civicrm.root]/Civi.php')],
64 array_column(\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'filePath')
65 );
66 foreach ($locations as $location) {
67 $dir = \CRM_Utils_File::addTrailingSlash(dirname($location)) . 'Civi/Api4';
68 if (is_dir($dir)) {
69 foreach (glob("$dir/*.php") as $file) {
70 $matches = [];
71 preg_match('/(\w*).php/', $file, $matches);
72 $entity = ['name' => $matches[1]];
73 if ($this->_isFieldSelected('description') || $this->_isFieldSelected('comment')) {
74 $this->addDocs($entity);
75 }
76 $entities[$matches[1]] = $entity;
77 }
78 }
79 }
80 unset($entities['CustomValue']);
81
82 if ($this->includeCustom) {
83 $this->addCustomEntities($entities);
84 }
85
86 ksort($entities);
87 return $entities;
88 }
89
90 /**
91 * Add custom-field pseudo-entities
92 *
93 * @param $entities
94 * @throws \API_Exception
95 */
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)
102 ->execute();
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'],
108 ];
109 if (!empty($customEntity['help_pre'])) {
110 $entities[$fieldName]['comment'] = $this->plainTextify($customEntity['help_pre']);
111 }
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']);
115 }
116 }
117 }
118
119 /**
120 * Convert html to plain text.
121 *
122 * @param $input
123 * @return mixed
124 */
125 private function plainTextify($input) {
126 return html_entity_decode(strip_tags($input), ENT_QUOTES | ENT_HTML5, 'UTF-8');
127 }
128
129 /**
130 * Add info from code docblock.
131 *
132 * @param $entity
133 */
134 private function addDocs(&$entity) {
135 $reflection = new \ReflectionClass("\\Civi\\Api4\\" . $entity['name']);
136 $entity += ReflectionUtils::getCodeDocs($reflection);
137 unset($entity['package'], $entity['method']);
138 }
139
140 }