Add user-facing titles for sql functions
[civicrm-core.git] / CRM / Api4 / Page / Api4Explorer.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 use Civi\Api4\Service\Schema\Joinable\Joinable;
14
15 /**
16 *
17 * @package CRM
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 */
20 class CRM_Api4_Page_Api4Explorer extends CRM_Core_Page {
21
22 public function run() {
23 $apiDoc = new ReflectionFunction('civicrm_api4');
24 $groupOptions = civicrm_api4('Group', 'getFields', ['loadOptions' => TRUE, 'select' => ['options', 'name'], 'where' => [['name', 'IN', ['visibility', 'group_type']]]]);
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 }
32 $vars = [
33 'operators' => \CRM_Core_DAO::acceptedSQLOperators(),
34 'basePath' => Civi::resources()->getUrl('civicrm'),
35 'schema' => (array) \Civi\Api4\Entity::get()->setChain(['fields' => ['$name', 'getFields']])->execute(),
36 'links' => $entityLinks,
37 'docs' => \Civi\Api4\Utils\ReflectionUtils::parseDocBlock($apiDoc->getDocComment()),
38 'functions' => self::getSqlFunctions(),
39 'groupOptions' => array_column((array) $groupOptions, 'options', 'name'),
40 ];
41 Civi::resources()
42 ->addVars('api4', $vars)
43 ->addPermissions(['access debug output', 'edit groups', 'administer reserved groups'])
44 ->addScriptFile('civicrm', 'js/load-bootstrap.js')
45 ->addScriptFile('civicrm', 'bower_components/js-yaml/dist/js-yaml.min.js')
46 ->addScriptFile('civicrm', 'bower_components/marked/marked.min.js')
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
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 'title' => $className::getTitle(),
74 'params' => $className::getParams(),
75 'category' => $className::getCategory(),
76 ];
77 }
78 }
79 }
80 return $fns;
81 }
82
83 }