Merge pull request #17126 from totten/master-upgr-dispatch
[civicrm-core.git] / CRM / Api4 / Page / Api4Explorer.php
index adc1d74326dd8727a288a7d3ebd8b359045632ff..c5280bb2573ebd292bb90ca704d780d17922006c 100644 (file)
  +--------------------------------------------------------------------+
  */
 
+use Civi\Api4\Service\Schema\Joinable\Joinable;
+
 /**
  *
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
- * $Id$
- *
  */
 class CRM_Api4_Page_Api4Explorer extends CRM_Core_Page {
 
   public function run() {
     $apiDoc = new ReflectionFunction('civicrm_api4');
+    $groupOptions = civicrm_api4('Group', 'getFields', ['loadOptions' => TRUE, 'select' => ['options', 'name'], 'where' => [['name', 'IN', ['visibility', 'group_type']]]]);
+    // Don't show n-to-many joins in Explorer
+    $entityLinks = (array) civicrm_api4('Entity', 'getLinks', [], ['entity' => 'links']);
+    foreach ($entityLinks as $entity => $links) {
+      $entityLinks[$entity] = array_filter($links, function($link) {
+        return $link['joinType'] != Joinable::JOIN_TYPE_ONE_TO_MANY;
+      });
+    }
     $vars = [
       'operators' => \CRM_Core_DAO::acceptedSQLOperators(),
       'basePath' => Civi::resources()->getUrl('civicrm'),
       'schema' => (array) \Civi\Api4\Entity::get()->setChain(['fields' => ['$name', 'getFields']])->execute(),
-      'links' => (array) \Civi\Api4\Entity::getLinks()->execute(),
+      'links' => $entityLinks,
       'docs' => \Civi\Api4\Utils\ReflectionUtils::parseDocBlock($apiDoc->getDocComment()),
+      'functions' => self::getSqlFunctions(),
+      'groupOptions' => array_column((array) $groupOptions, 'options', 'name'),
     ];
     Civi::resources()
       ->addVars('api4', $vars)
-      ->addPermissions(['access debug output'])
+      ->addPermissions(['access debug output', 'edit groups', 'administer reserved groups'])
       ->addScriptFile('civicrm', 'js/load-bootstrap.js')
       ->addScriptFile('civicrm', 'bower_components/js-yaml/dist/js-yaml.min.js')
       ->addScriptFile('civicrm', 'bower_components/marked/marked.min.js')
@@ -47,4 +57,25 @@ class CRM_Api4_Page_Api4Explorer extends CRM_Core_Page {
     parent::run();
   }
 
+  /**
+   * Gets info about all available sql functions
+   * @return array
+   */
+  public static function getSqlFunctions() {
+    $fns = [];
+    foreach (glob(Civi::paths()->getPath('[civicrm.root]/Civi/Api4/Query/SqlFunction*.php')) as $file) {
+      $matches = [];
+      if (preg_match('/(SqlFunction[A-Z_]+)\.php$/', $file, $matches)) {
+        $className = '\Civi\Api4\Query\\' . $matches[1];
+        if (is_subclass_of($className, '\Civi\Api4\Query\SqlFunction')) {
+          $fns[] = [
+            'name' => $className::getName(),
+            'params' => $className::getParams(),
+          ];
+        }
+      }
+    }
+    return $fns;
+  }
+
 }