APIv4 - Fix getFields to respect default_value from getFields
[civicrm-core.git] / Civi / Api4 / Generic / BasicGetFieldsAction.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
41498ac5 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
41498ac5
TO
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 |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545
TO
17 */
18
19
19b53e5b
C
20namespace Civi\Api4\Generic;
21
22use Civi\API\Exception\NotImplementedException;
19b53e5b
C
23
24/**
fc95d9a5 25 * Lists information about fields for the $ENTITY entity.
e15f9453
CW
26 *
27 * This field information is also known as "metadata."
28 *
29 * Note that different actions may support different lists of fields.
fc95d9a5 30 * By default this will fetch the field list relevant to `get`,
e15f9453 31 * but a different list may be returned if you specify another action.
19b53e5b 32 *
bb6bfd68
CW
33 * @method $this setLoadOptions(bool|array $value)
34 * @method bool|array getLoadOptions()
19b53e5b 35 * @method $this setAction(string $value)
c752d94b
CW
36 * @method $this setValues(array $values)
37 * @method array getValues()
19b53e5b
C
38 */
39class BasicGetFieldsAction extends BasicGetAction {
40
41 /**
42 * Fetch option lists for fields?
43 *
bb6bfd68
CW
44 * This parameter can be either a boolean or an array of attributes to return from the option list:
45 *
46 * - If `FALSE`, each field's `options` property will be a boolean indicating whether the field has an option list
47 * - If `TRUE`, `options` will be returned as a flat array of the option list's `[id => label]`
48 * - If an array, `options` will be a non-associative array of requested properties:
49 * id, name, label, abbr, description, color, icon
50 * e.g. `loadOptions: ['id', 'name', 'label']` will return an array like `[[id: 1, name: 'Meeting', label: 'Meeting'], ...]`
51 * (note that names and labels are generally ONLY the same when the site's language is set to English).
52 *
53 * @var bool|array
19b53e5b
C
54 */
55 protected $loadOptions = FALSE;
56
57 /**
c752d94b
CW
58 * Fields will be returned appropriate to the specified action (get, create, delete, etc.)
59 *
19b53e5b
C
60 * @var string
61 */
62 protected $action = 'get';
63
c752d94b
CW
64 /**
65 * Fields will be returned appropriate to the specified values (e.g. ['contact_type' => 'Individual'])
66 *
67 * @var array
68 */
69 protected $values = [];
70
19b53e5b
C
71 /**
72 * To implement getFields for your own entity:
73 *
74 * 1. From your entity class add a static getFields method.
75 * 2. That method should construct and return this class.
76 * 3. The 3rd argument passed to this constructor should be a function that returns an
77 * array of fields for your entity's CRUD actions.
78 * 4. For non-crud actions that need a different set of fields, you can override the
79 * list from step 3 on a per-action basis by defining a fields() method in that action.
80 * See for example BasicGetFieldsAction::fields() or GetActions::fields().
81 *
82 * @param Result $result
83 * @throws \Civi\API\Exception\NotImplementedException
84 */
85 public function _run(Result $result) {
86 try {
3a8dc228 87 $actionClass = \Civi\API\Request::create($this->getEntityName(), $this->getAction(), ['version' => 4]);
19b53e5b
C
88 }
89 catch (NotImplementedException $e) {
90 }
91 if (isset($actionClass) && method_exists($actionClass, 'fields')) {
92 $values = $actionClass->fields();
93 }
94 else {
95 $values = $this->getRecords();
96 }
bb6bfd68 97 $this->formatResults($values);
651c4c95 98 $this->queryArray($values, $result);
19b53e5b
C
99 }
100
101 /**
102 * Ensure every result contains, at minimum, the array keys as defined in $this->fields.
103 *
104 * Attempt to set some sensible defaults for some fields.
105 *
bb6bfd68
CW
106 * Format option lists.
107 *
19b53e5b 108 * In most cases it's not necessary to override this function, even if your entity is really weird.
bb6bfd68 109 * Instead just override $this->fields and this function will respect that.
19b53e5b
C
110 *
111 * @param array $values
112 */
bb6bfd68 113 protected function formatResults(&$values) {
88620a59
CW
114 $fieldDefaults = array_column($this->fields(), 'default_value', 'name') +
115 array_fill_keys(array_column($this->fields(), 'name'), NULL);
0d20e81c
CW
116 // Enforce field permissions
117 if ($this->checkPermissions) {
118 foreach ($values as $key => $field) {
119 if (!empty($field['permission']) && !\CRM_Core_Permission::check($field['permission'])) {
120 unset($values[$key]);
121 }
122 }
123 }
19b53e5b
C
124 foreach ($values as &$field) {
125 $defaults = array_intersect_key([
126 'title' => empty($field['name']) ? NULL : ucwords(str_replace('_', ' ', $field['name'])),
127 'entity' => $this->getEntityName(),
19b53e5b 128 'options' => !empty($field['pseudoconstant']),
88620a59
CW
129 ], $fieldDefaults);
130 $field += $defaults + $fieldDefaults;
131 if (array_key_exists('label', $fieldDefaults)) {
132 $field['label'] = $field['label'] ?? $field['title'] ?? $field['name'];
133 }
bb6bfd68
CW
134 if (isset($defaults['options'])) {
135 $field['options'] = $this->formatOptionList($field['options']);
19b53e5b 136 }
19b53e5b
C
137 }
138 }
139
bb6bfd68
CW
140 /**
141 * Transforms option list into the format specified in $this->loadOptions
142 *
143 * @param $options
144 * @return array|bool
145 */
146 private function formatOptionList($options) {
147 if (!$this->loadOptions || !is_array($options)) {
148 return (bool) $options;
149 }
150 if (!$options) {
151 return $options;
152 }
153 $formatted = [];
154 $first = reset($options);
155 // Flat array requested
156 if ($this->loadOptions === TRUE) {
157 // Convert non-associative to flat array
158 if (is_array($first) && isset($first['id'])) {
159 foreach ($options as $option) {
160 $formatted[$option['id']] = $option['label'] ?? $option['name'] ?? $option['id'];
161 }
162 return $formatted;
163 }
164 return $options;
165 }
166 // Non-associative array of multiple properties requested
167 foreach ($options as $id => $option) {
168 // Transform a flat list
169 if (!is_array($option)) {
170 $option = [
171 'id' => $id,
172 'name' => $option,
173 'label' => $option,
174 ];
175 }
176 $formatted[] = array_intersect_key($option, array_flip($this->loadOptions));
177 }
178 return $formatted;
179 }
180
19b53e5b
C
181 /**
182 * @return string
183 */
184 public function getAction() {
185 // For actions that build on top of other actions, return fields for the simpler action
186 $sub = [
187 'save' => 'create',
188 'replace' => 'create',
189 ];
190 return $sub[$this->action] ?? $this->action;
191 }
192
121ec912
CW
193 /**
194 * Add an item to the values array
195 * @param string $fieldName
196 * @param mixed $value
197 * @return $this
198 */
199 public function addValue(string $fieldName, $value) {
200 $this->values[$fieldName] = $value;
201 return $this;
202 }
203
3a8dc228
CW
204 /**
205 * @param bool $includeCustom
206 * @return $this
207 */
208 public function setIncludeCustom(bool $includeCustom) {
209 // Be forgiving if the param doesn't exist and don't throw an exception
210 if (property_exists($this, 'includeCustom')) {
211 $this->includeCustom = $includeCustom;
212 }
213 return $this;
214 }
215
4f664e9c
CW
216 /**
217 * Helper function to retrieve options from an option group (for non-DAO entities).
218 *
219 * @param string $optionGroupName
220 */
221 public function pseudoconstantOptions(string $optionGroupName) {
222 if ($this->getLoadOptions()) {
223 $options = \CRM_Core_OptionValue::getValues(['name' => $optionGroupName]);
224 foreach ($options as &$option) {
225 $option['id'] = $option['value'];
226 }
227 }
228 else {
229 $options = TRUE;
230 }
231 return $options;
232 }
233
19b53e5b
C
234 public function fields() {
235 return [
236 [
237 'name' => 'name',
238 'data_type' => 'String',
b6b6cb2d 239 'description' => ts('Unique field identifier'),
19b53e5b
C
240 ],
241 [
242 'name' => 'title',
243 'data_type' => 'String',
b6b6cb2d
CW
244 'description' => ts('Technical name of field, shown in API and exports'),
245 ],
246 [
247 'name' => 'label',
248 'data_type' => 'String',
249 'description' => ts('User-facing label, shown on most forms and displays'),
19b53e5b
C
250 ],
251 [
252 'name' => 'description',
253 'data_type' => 'String',
b6b6cb2d 254 'description' => ts('Explanation of the purpose of the field'),
19b53e5b
C
255 ],
256 [
257 'name' => 'default_value',
258 'data_type' => 'String',
259 ],
260 [
261 'name' => 'required',
262 'data_type' => 'Boolean',
88620a59 263 'default_value' => FALSE,
19b53e5b
C
264 ],
265 [
266 'name' => 'required_if',
267 'data_type' => 'String',
268 ],
269 [
270 'name' => 'options',
271 'data_type' => 'Array',
88620a59 272 'default_value' => FALSE,
19b53e5b
C
273 ],
274 [
275 'name' => 'data_type',
88620a59 276 'default_value' => 'String',
de2b4328 277 'options' => [
c0e68893 278 'Array' => ts('Array'),
de2b4328 279 'Boolean' => ts('Boolean'),
c0e68893 280 'Date' => ts('Date'),
281 'Float' => ts('Float'),
282 'Integer' => ts('Integer'),
de2b4328
CW
283 'String' => ts('String'),
284 'Text' => ts('Text'),
de2b4328 285 'Timestamp' => ts('Timestamp'),
de2b4328 286 ],
19b53e5b
C
287 ],
288 [
289 'name' => 'input_type',
290 'data_type' => 'String',
de2b4328 291 'options' => [
783a2874
CW
292 'ChainSelect' => ts('Chain-Select'),
293 'CheckBox' => ts('Checkboxes'),
294 'Date' => ts('Date Picker'),
295 'EntityRef' => ts('Autocomplete Entity'),
c0e68893 296 'File' => ts('File'),
297 'Number' => ts('Number'),
783a2874 298 'Radio' => ts('Radio Buttons'),
c0e68893 299 'Select' => ts('Select'),
300 'Text' => ts('Text'),
de2b4328 301 ],
19b53e5b
C
302 ],
303 [
304 'name' => 'input_attrs',
305 'data_type' => 'Array',
306 ],
307 [
308 'name' => 'fk_entity',
309 'data_type' => 'String',
310 ],
311 [
312 'name' => 'serialize',
313 'data_type' => 'Integer',
314 ],
315 [
316 'name' => 'entity',
317 'data_type' => 'String',
318 ],
34745448
CW
319 [
320 'name' => 'readonly',
321 'data_type' => 'Boolean',
6fb85546 322 'description' => 'True for auto-increment, calculated, or otherwise non-editable fields.',
88620a59 323 'default_value' => FALSE,
34745448 324 ],
96f09dda
CW
325 [
326 'name' => 'output_formatters',
327 'data_type' => 'Array',
328 ],
19b53e5b
C
329 ];
330 }
331
332}