Merge pull request #17163 from jitendrapurohit/core-1731
[civicrm-core.git] / Civi / Api4 / Action / Entity / GetLinks.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 */
18
19
20 namespace Civi\Api4\Action\Entity;
21
22 use Civi\Api4\Utils\CoreUtil;
23
24 /**
25 * Get a list of FK links between entities
26 */
27 class GetLinks extends \Civi\Api4\Generic\BasicGetAction {
28
29 public function getRecords() {
30 $result = [];
31 /** @var \Civi\Api4\Service\Schema\SchemaMap $schema */
32 $schema = \Civi::container()->get('schema_map');
33 foreach ($schema->getTables() as $table) {
34 $entity = CoreUtil::getApiNameFromTableName($table->getName());
35 // Since this is an api function, exclude tables that don't have an api
36 if (strpos($entity, 'Custom_') === 0 || class_exists('\Civi\Api4\\' . $entity)) {
37 $item = [
38 'entity' => $entity,
39 'table' => $table->getName(),
40 'links' => [],
41 ];
42 foreach ($table->getTableLinks() as $link) {
43 $link = $link->toArray();
44 $link['entity'] = CoreUtil::getApiNameFromTableName($link['targetTable']);
45 $item['links'][] = $link;
46 }
47 $result[] = $item;
48 }
49 }
50 return $result;
51 }
52
53 public function fields() {
54 return [
55 [
56 'name' => 'entity',
57 ],
58 [
59 'name' => 'table',
60 ],
61 [
62 'name' => 'links',
63 'data_type' => 'Array',
64 ],
65 ];
66 }
67
68 }