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