summaryrefslogtreecommitdiff
path: root/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Generic/DAOGetFieldsAction.php
blob: e86d99bc28b47b0eba249c6e272af7ed93a87f51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

namespace Civi\Api4\Generic;

use Civi\Api4\Service\Spec\SpecGatherer;
use Civi\Api4\Service\Spec\SpecFormatter;

/**
 * Get fields for a DAO-based entity.
 *
 * @method $this setIncludeCustom(bool $value)
 * @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
   *
   * @return array
   */
  protected function getRecords() {
    $fields = $this->_itemsToGet('name');
    /** @var SpecGatherer $gatherer */
    $gatherer = \Civi::container()->get('spec_gatherer');
    // Any fields name with a dot in it is custom
    if ($fields) {
      $this->includeCustom = strpos(implode('', $fields), '.') !== FALSE;
    }
    $spec = $gatherer->getSpec($this->getEntityName(), $this->action, $this->includeCustom);
    return SpecFormatter::specToArray($spec->getFields($fields), (array) $this->select, $this->loadOptions);
  }

  public function fields() {
    $fields = parent::fields();
    $fields[] = [
      'name' => 'custom_field_id',
      'data_type' => 'Integer',
    ];
    $fields[] = [
      'name' => 'custom_group_id',
      'data_type' => 'Integer',
    ];
    return $fields;
  }

}