Merge pull request #23446 from colemanw/searchKitDefaultAgg
[civicrm-core.git] / Civi / Api4 / Generic / DAOGetFieldsAction.php
index 9672a99c8648b627f82988e717d441028a2800e7..e877e6dd8b2b852bc0ff654167fb76fce3386b81 100644 (file)
  +--------------------------------------------------------------------+
  */
 
-/**
- *
- * @package CRM
- * @copyright CiviCRM LLC https://civicrm.org/licensing
- */
-
-
 namespace Civi\Api4\Generic;
 
+use Civi\Api4\Utils\CoreUtil;
+use Civi\Api4\Utils\FormattingUtil;
+
 /**
  * @inheritDoc
  * @method bool getIncludeCustom()
  */
 class DAOGetFieldsAction extends BasicGetFieldsAction {
 
-  /**
-   * Include custom fields for this entity, or only core fields?
-   *
-   * @var bool
-   */
-  protected $includeCustom = TRUE;
-
   /**
    * Get fields for a DAO-based entity.
    *
@@ -39,13 +28,19 @@ class DAOGetFieldsAction extends BasicGetFieldsAction {
    */
   protected function getRecords() {
     $fieldsToGet = $this->_itemsToGet('name');
+    $typesToGet = $this->_itemsToGet('type');
     /** @var \Civi\Api4\Service\Spec\SpecGatherer $gatherer */
     $gatherer = \Civi::container()->get('spec_gatherer');
-    // Any fields name with a dot in it is either custom or an implicit join
-    if ($fieldsToGet) {
-      $this->includeCustom = strpos(implode('', $fieldsToGet), '.') !== FALSE;
+    $includeCustom = TRUE;
+    if ($typesToGet) {
+      $includeCustom = in_array('Custom', $typesToGet, TRUE);
     }
-    $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $this->includeCustom, $this->values);
+    elseif ($fieldsToGet) {
+      // Any fields name with a dot in it is either custom or an implicit join
+      $includeCustom = strpos(implode('', $fieldsToGet), '.') !== FALSE;
+    }
+    $this->formatValues();
+    $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $includeCustom, $this->values);
     $fields = $this->specToArray($spec->getFields($fieldsToGet));
     foreach ($fieldsToGet ?? [] as $fieldName) {
       if (empty($fields[$fieldName]) && strpos($fieldName, '.') !== FALSE) {
@@ -100,6 +95,29 @@ class DAOGetFieldsAction extends BasicGetFieldsAction {
     }
   }
 
+  /**
+   * Special handling for pseudoconstant replacements.
+   *
+   * Normally this would involve calling getFields... but this IS getFields.
+   *
+   * @throws \API_Exception
+   */
+  private function formatValues() {
+    foreach (array_keys($this->values) as $key) {
+      if (strpos($key, ':')) {
+        [$fieldName, $suffix] = explode(':', $key);
+        $context = FormattingUtil::$pseudoConstantContexts[$suffix] ?? NULL;
+        if (!$context) {
+          throw new \API_Exception('Illegal expression');
+        }
+        $baoName = CoreUtil::getBAOFromApiName($this->getEntityName());
+        $options = $baoName::buildOptions($fieldName, $context);
+        $this->values[$fieldName] = FormattingUtil::replacePseudoconstant($options, $this->values[$key], TRUE);
+        unset($this->values[$key]);
+      }
+    }
+  }
+
   public function fields() {
     $fields = parent::fields();
     $fields[] = [
@@ -122,6 +140,16 @@ class DAOGetFieldsAction extends BasicGetFieldsAction {
       'name' => 'custom_group_id',
       'data_type' => 'Integer',
     ];
+    $fields[] = [
+      'name' => 'sql_filters',
+      'data_type' => 'Array',
+      '@internal' => TRUE,
+    ];
+    $fields[] = [
+      'name' => 'sql_renderer',
+      'data_type' => 'Array',
+      '@internal' => TRUE,
+    ];
     return $fields;
   }