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
;
15 use Civi\Api4\Utils\CoreUtil
;
16 use Civi\Api4\Utils\FormattingUtil
;
20 * @method bool getIncludeCustom()
22 class DAOGetFieldsAction
extends BasicGetFieldsAction
{
25 * Get fields for a DAO-based entity.
29 protected function getRecords() {
30 $fieldsToGet = $this->_itemsToGet('name');
31 $typesToGet = $this->_itemsToGet('type');
32 /** @var \Civi\Api4\Service\Spec\SpecGatherer $gatherer */
33 $gatherer = \Civi
::container()->get('spec_gatherer');
34 $includeCustom = TRUE;
36 $includeCustom = in_array('Custom', $typesToGet, TRUE);
38 elseif ($fieldsToGet) {
39 // Any fields name with a dot in it is either custom or an implicit join
40 $includeCustom = strpos(implode('', $fieldsToGet), '.') !== FALSE;
42 $this->formatValues();
43 $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $includeCustom, $this->values
);
44 $fields = $this->specToArray($spec->getFields($fieldsToGet));
45 foreach ($fieldsToGet ??
[] as $fieldName) {
46 if (empty($fields[$fieldName]) && strpos($fieldName, '.') !== FALSE) {
47 $fkField = $this->getFkFieldSpec($fieldName, $fields);
49 $fkField['name'] = $fieldName;
58 * @param \Civi\Api4\Service\Spec\FieldSpec[] $fields
62 protected function specToArray($fields) {
65 foreach ($fields as $field) {
66 if ($this->loadOptions
) {
67 $field->getOptions($this->values
, $this->loadOptions
, $this->checkPermissions
);
69 $fieldArray[$field->getName()] = $field->toArray();
76 * @param string $fieldName
77 * @param array $fields
79 * @throws \API_Exception
81 private function getFkFieldSpec($fieldName, $fields) {
82 $fieldPath = explode('.', $fieldName);
83 // Search for the first segment alone plus the first and second
84 // No field in the schema contains more than one dot in its name.
85 $searchPaths = [$fieldPath[0], $fieldPath[0] . '.' . $fieldPath[1]];
86 $fkFieldName = array_intersect($searchPaths, array_keys($fields))[0] ??
NULL;
87 if ($fkFieldName && !empty($fields[$fkFieldName]['fk_entity'])) {
88 $newFieldName = substr($fieldName, 1 +
strlen($fkFieldName));
89 return civicrm_api4($fields[$fkFieldName]['fk_entity'], 'getFields', [
90 'checkPermissions' => $this->checkPermissions
,
91 'where' => [['name', '=', $newFieldName]],
92 'loadOptions' => $this->loadOptions
,
93 'action' => $this->action
,
99 * Special handling for pseudoconstant replacements.
101 * Normally this would involve calling getFields... but this IS getFields.
103 * @throws \API_Exception
105 private function formatValues() {
106 foreach (array_keys($this->values
) as $key) {
107 if (strpos($key, ':')) {
108 [$fieldName, $suffix] = explode(':', $key);
109 $context = FormattingUtil
::$pseudoConstantContexts[$suffix] ??
NULL;
111 throw new \
API_Exception('Illegal expression');
113 $baoName = CoreUtil
::getBAOFromApiName($this->getEntityName());
114 $options = $baoName::buildOptions($fieldName, $context);
115 $this->values
[$fieldName] = FormattingUtil
::replacePseudoconstant($options, $this->values
[$key], TRUE);
116 unset($this->values
[$key]);
121 public function fields() {
122 $fields = parent
::fields();
124 'name' => 'help_pre',
125 'data_type' => 'String',
128 'name' => 'help_post',
129 'data_type' => 'String',
132 'name' => 'column_name',
133 'data_type' => 'String',
136 'name' => 'custom_field_id',
137 'data_type' => 'Integer',
140 'name' => 'custom_group_id',
141 'data_type' => 'Integer',
144 'name' => 'sql_filters',
145 'data_type' => 'Array',
149 'name' => 'sql_renderer',
150 'data_type' => 'Array',