4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Generic
;
17 * @method bool getIncludeCustom()
19 class DAOGetFieldsAction
extends BasicGetFieldsAction
{
22 * Get fields for a DAO-based entity.
26 protected function getRecords() {
27 $fieldsToGet = $this->_itemsToGet('name');
28 $typesToGet = $this->_itemsToGet('type');
29 /** @var \Civi\Api4\Service\Spec\SpecGatherer $gatherer */
30 $gatherer = \Civi
::container()->get('spec_gatherer');
31 $includeCustom = TRUE;
33 $includeCustom = in_array('Custom', $typesToGet, TRUE);
35 elseif ($fieldsToGet) {
36 // Any fields name with a dot in it is either custom or an implicit join
37 $includeCustom = strpos(implode('', $fieldsToGet), '.') !== FALSE;
39 $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $includeCustom, $this->values
);
40 $fields = $this->specToArray($spec->getFields($fieldsToGet));
41 foreach ($fieldsToGet ??
[] as $fieldName) {
42 if (empty($fields[$fieldName]) && strpos($fieldName, '.') !== FALSE) {
43 $fkField = $this->getFkFieldSpec($fieldName, $fields);
45 $fkField['name'] = $fieldName;
54 * @param \Civi\Api4\Service\Spec\FieldSpec[] $fields
58 protected function specToArray($fields) {
61 foreach ($fields as $field) {
62 if ($this->loadOptions
) {
63 $field->getOptions($this->values
, $this->loadOptions
, $this->checkPermissions
);
65 $fieldArray[$field->getName()] = $field->toArray();
72 * @param string $fieldName
73 * @param array $fields
75 * @throws \API_Exception
77 private function getFkFieldSpec($fieldName, $fields) {
78 $fieldPath = explode('.', $fieldName);
79 // Search for the first segment alone plus the first and second
80 // No field in the schema contains more than one dot in its name.
81 $searchPaths = [$fieldPath[0], $fieldPath[0] . '.' . $fieldPath[1]];
82 $fkFieldName = array_intersect($searchPaths, array_keys($fields))[0] ??
NULL;
83 if ($fkFieldName && !empty($fields[$fkFieldName]['fk_entity'])) {
84 $newFieldName = substr($fieldName, 1 +
strlen($fkFieldName));
85 return civicrm_api4($fields[$fkFieldName]['fk_entity'], 'getFields', [
86 'checkPermissions' => $this->checkPermissions
,
87 'where' => [['name', '=', $newFieldName]],
88 'loadOptions' => $this->loadOptions
,
89 'action' => $this->action
,
94 public function fields() {
95 $fields = parent
::fields();
98 'data_type' => 'String',
101 'name' => 'help_post',
102 'data_type' => 'String',
105 'name' => 'column_name',
106 'data_type' => 'String',
109 'name' => 'custom_field_id',
110 'data_type' => 'Integer',
113 'name' => 'custom_group_id',
114 'data_type' => 'Integer',
117 'name' => 'sql_filters',
118 'data_type' => 'Array',
122 'name' => 'sql_renderer',
123 'data_type' => 'Array',