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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace Civi\Api4\Generic
;
24 * @method bool getIncludeCustom()
26 class DAOGetFieldsAction
extends BasicGetFieldsAction
{
29 * Get fields for a DAO-based entity.
33 protected function getRecords() {
34 $fieldsToGet = $this->_itemsToGet('name');
35 $typesToGet = $this->_itemsToGet('type');
36 /** @var \Civi\Api4\Service\Spec\SpecGatherer $gatherer */
37 $gatherer = \Civi
::container()->get('spec_gatherer');
38 $includeCustom = TRUE;
40 $includeCustom = in_array('Custom', $typesToGet, TRUE);
42 elseif ($fieldsToGet) {
43 // Any fields name with a dot in it is either custom or an implicit join
44 $includeCustom = strpos(implode('', $fieldsToGet), '.') !== FALSE;
46 $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $includeCustom, $this->values
);
47 $fields = $this->specToArray($spec->getFields($fieldsToGet));
48 foreach ($fieldsToGet ??
[] as $fieldName) {
49 if (empty($fields[$fieldName]) && strpos($fieldName, '.') !== FALSE) {
50 $fkField = $this->getFkFieldSpec($fieldName, $fields);
52 $fkField['name'] = $fieldName;
61 * @param \Civi\Api4\Service\Spec\FieldSpec[] $fields
65 protected function specToArray($fields) {
68 foreach ($fields as $field) {
69 if ($this->loadOptions
) {
70 $field->getOptions($this->values
, $this->loadOptions
, $this->checkPermissions
);
72 $fieldArray[$field->getName()] = $field->toArray();
79 * @param string $fieldName
80 * @param array $fields
82 * @throws \API_Exception
84 private function getFkFieldSpec($fieldName, $fields) {
85 $fieldPath = explode('.', $fieldName);
86 // Search for the first segment alone plus the first and second
87 // No field in the schema contains more than one dot in its name.
88 $searchPaths = [$fieldPath[0], $fieldPath[0] . '.' . $fieldPath[1]];
89 $fkFieldName = array_intersect($searchPaths, array_keys($fields))[0] ??
NULL;
90 if ($fkFieldName && !empty($fields[$fkFieldName]['fk_entity'])) {
91 $newFieldName = substr($fieldName, 1 +
strlen($fkFieldName));
92 return civicrm_api4($fields[$fkFieldName]['fk_entity'], 'getFields', [
93 'checkPermissions' => $this->checkPermissions
,
94 'where' => [['name', '=', $newFieldName]],
95 'loadOptions' => $this->loadOptions
,
96 'action' => $this->action
,
101 public function fields() {
102 $fields = parent
::fields();
104 'name' => 'help_pre',
105 'data_type' => 'String',
108 'name' => 'help_post',
109 'data_type' => 'String',
112 'name' => 'column_name',
113 'data_type' => 'String',
116 'name' => 'custom_field_id',
117 'data_type' => 'Integer',
120 'name' => 'custom_group_id',
121 'data_type' => 'Integer',
124 'name' => 'sql_filters',
125 'data_type' => 'Array',