Merge pull request #16714 from christianwach/lab-1638
[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 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
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 $vars = [
26 'operators' => \CRM_Core_DAO::acceptedSQLOperators(),
27 'basePath' => Civi::resources()->getUrl('civicrm'),
28 'schema' => (array) \Civi\Api4\Entity::get()->setChain(['fields' => ['$name', 'getFields']])->execute(),
29 'links' => (array) \Civi\Api4\Entity::getLinks()->execute(),
30 'docs' => \Civi\Api4\Utils\ReflectionUtils::parseDocBlock($apiDoc->getDocComment()),
31 'functions' => self::getSqlFunctions(),
32 'groupOptions' => array_column((array) $groupOptions, 'options', 'name'),
33 ];
34 Civi::resources()
35 ->addVars('api4', $vars)
36 ->addPermissions(['access debug output', 'edit groups', 'administer reserved groups'])
37 ->addScriptFile('civicrm', 'js/load-bootstrap.js')
38 ->addScriptFile('civicrm', 'bower_components/js-yaml/dist/js-yaml.min.js')
39 ->addScriptFile('civicrm', 'bower_components/marked/marked.min.js')
40 ->addScriptFile('civicrm', 'bower_components/google-code-prettify/bin/prettify.min.js')
41 ->addStyleFile('civicrm', 'bower_components/google-code-prettify/bin/prettify.min.css');
42
43 $loader = new Civi\Angular\AngularLoader();
44 $loader->setModules(['api4Explorer']);
45 $loader->setPageName('civicrm/api4');
46 $loader->useApp([
47 'defaultRoute' => '/explorer',
48 ]);
49 $loader->load();
50 parent::run();
51 }
52
53 /**
54 * Gets info about all available sql functions
55 * @return array
56 */
57 public static function getSqlFunctions() {
58 $fns = [];
59 foreach (glob(Civi::paths()->getPath('[civicrm.root]/Civi/Api4/Query/SqlFunction*.php')) as $file) {
60 $matches = [];
61 if (preg_match('/(SqlFunction[A-Z_]+)\.php$/', $file, $matches)) {
62 $className = '\Civi\Api4\Query\\' . $matches[1];
63 if (is_subclass_of($className, '\Civi\Api4\Query\SqlFunction')) {
64 $fns[] = [
65 'name' => $className::getName(),
66 'params' => $className::getParams(),
67 ];
68 }
69 }
70 }
71 return $fns;
72 }
73
74 }