Merge pull request #17126 from totten/master-upgr-dispatch
[civicrm-core.git] / CRM / Api4 / Page / Api4Explorer.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
bc77d7c0 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
bc77d7c0
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
7d91265c
CW
13use Civi\Api4\Service\Schema\Joinable\Joinable;
14
380f3545
TO
15/**
16 *
17 * @package CRM
ca5cec67 18 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545 19 */
19b53e5b
C
20class CRM_Api4_Page_Api4Explorer extends CRM_Core_Page {
21
22 public function run() {
9cea3619 23 $apiDoc = new ReflectionFunction('civicrm_api4');
d7507e89 24 $groupOptions = civicrm_api4('Group', 'getFields', ['loadOptions' => TRUE, 'select' => ['options', 'name'], 'where' => [['name', 'IN', ['visibility', 'group_type']]]]);
7d91265c
CW
25 // Don't show n-to-many joins in Explorer
26 $entityLinks = (array) civicrm_api4('Entity', 'getLinks', [], ['entity' => 'links']);
27 foreach ($entityLinks as $entity => $links) {
28 $entityLinks[$entity] = array_filter($links, function($link) {
29 return $link['joinType'] != Joinable::JOIN_TYPE_ONE_TO_MANY;
30 });
31 }
19b53e5b
C
32 $vars = [
33 'operators' => \CRM_Core_DAO::acceptedSQLOperators(),
cdeee725 34 'basePath' => Civi::resources()->getUrl('civicrm'),
19b53e5b 35 'schema' => (array) \Civi\Api4\Entity::get()->setChain(['fields' => ['$name', 'getFields']])->execute(),
7d91265c 36 'links' => $entityLinks,
9cea3619 37 'docs' => \Civi\Api4\Utils\ReflectionUtils::parseDocBlock($apiDoc->getDocComment()),
5967c9e8 38 'functions' => self::getSqlFunctions(),
d7507e89 39 'groupOptions' => array_column((array) $groupOptions, 'options', 'name'),
19b53e5b
C
40 ];
41 Civi::resources()
42 ->addVars('api4', $vars)
d7507e89 43 ->addPermissions(['access debug output', 'edit groups', 'administer reserved groups'])
cdeee725 44 ->addScriptFile('civicrm', 'js/load-bootstrap.js')
0b873c9a 45 ->addScriptFile('civicrm', 'bower_components/js-yaml/dist/js-yaml.min.js')
136ca5bb 46 ->addScriptFile('civicrm', 'bower_components/marked/marked.min.js')
19b53e5b
C
47 ->addScriptFile('civicrm', 'bower_components/google-code-prettify/bin/prettify.min.js')
48 ->addStyleFile('civicrm', 'bower_components/google-code-prettify/bin/prettify.min.css');
49
50 $loader = new Civi\Angular\AngularLoader();
51 $loader->setModules(['api4Explorer']);
52 $loader->setPageName('civicrm/api4');
53 $loader->useApp([
54 'defaultRoute' => '/explorer',
55 ]);
56 $loader->load();
57 parent::run();
58 }
59
5967c9e8
CW
60 /**
61 * Gets info about all available sql functions
62 * @return array
63 */
64 public static function getSqlFunctions() {
65 $fns = [];
66 foreach (glob(Civi::paths()->getPath('[civicrm.root]/Civi/Api4/Query/SqlFunction*.php')) as $file) {
67 $matches = [];
68 if (preg_match('/(SqlFunction[A-Z_]+)\.php$/', $file, $matches)) {
69 $className = '\Civi\Api4\Query\\' . $matches[1];
70 if (is_subclass_of($className, '\Civi\Api4\Query\SqlFunction')) {
71 $fns[] = [
72 'name' => $className::getName(),
73 'params' => $className::getParams(),
74 ];
75 }
76 }
77 }
78 return $fns;
79 }
80
19b53e5b 81}