Merge pull request #23100 from civicrm/5.48
[civicrm-core.git] / Civi / Api4 / Generic / DAOGetFieldsAction.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 namespace Civi\Api4\Generic;
14
15 /**
16 * @inheritDoc
17 * @method bool getIncludeCustom()
18 */
19 class DAOGetFieldsAction extends BasicGetFieldsAction {
20
21 /**
22 * Get fields for a DAO-based entity.
23 *
24 * @return array
25 */
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;
32 if ($typesToGet) {
33 $includeCustom = in_array('Custom', $typesToGet, TRUE);
34 }
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;
38 }
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);
44 if ($fkField) {
45 $fkField['name'] = $fieldName;
46 $fields[] = $fkField;
47 }
48 }
49 }
50 return $fields;
51 }
52
53 /**
54 * @param \Civi\Api4\Service\Spec\FieldSpec[] $fields
55 *
56 * @return array
57 */
58 protected function specToArray($fields) {
59 $fieldArray = [];
60
61 foreach ($fields as $field) {
62 if ($this->loadOptions) {
63 $field->getOptions($this->values, $this->loadOptions, $this->checkPermissions);
64 }
65 $fieldArray[$field->getName()] = $field->toArray();
66 }
67
68 return $fieldArray;
69 }
70
71 /**
72 * @param string $fieldName
73 * @param array $fields
74 * @return array|null
75 * @throws \API_Exception
76 */
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,
90 ])->first();
91 }
92 }
93
94 public function fields() {
95 $fields = parent::fields();
96 $fields[] = [
97 'name' => 'help_pre',
98 'data_type' => 'String',
99 ];
100 $fields[] = [
101 'name' => 'help_post',
102 'data_type' => 'String',
103 ];
104 $fields[] = [
105 'name' => 'column_name',
106 'data_type' => 'String',
107 ];
108 $fields[] = [
109 'name' => 'custom_field_id',
110 'data_type' => 'Integer',
111 ];
112 $fields[] = [
113 'name' => 'custom_group_id',
114 'data_type' => 'Integer',
115 ];
116 $fields[] = [
117 'name' => 'sql_filters',
118 'data_type' => 'Array',
119 '@internal' => TRUE,
120 ];
121 $fields[] = [
122 'name' => 'sql_renderer',
123 'data_type' => 'Array',
124 '@internal' => TRUE,
125 ];
126 return $fields;
127 }
128
129 }