Merge pull request #17163 from jitendrapurohit/core-1731
[civicrm-core.git] / Civi / Api4 / Action / Entity / GetLinks.php
CommitLineData
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 */
18
19
19b53e5b
C
20namespace Civi\Api4\Action\Entity;
21
22use Civi\Api4\Utils\CoreUtil;
23
24/**
25 * Get a list of FK links between entities
26 */
27class 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
5e327f37 36 if (strpos($entity, 'Custom_') === 0 || class_exists('\Civi\Api4\\' . $entity)) {
19b53e5b
C
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}