[REF] Replace the deprecated system_rebuild_module_data function with equivilant...
[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 * $Id$
18 *
19 */
20
21
19b53e5b
C
22namespace Civi\Api4\Generic;
23
24use Civi\API\Exception\NotImplementedException;
19b53e5b
C
25
26/**
fc95d9a5 27 * Lists information about fields for the $ENTITY entity.
e15f9453
CW
28 *
29 * This field information is also known as "metadata."
30 *
31 * Note that different actions may support different lists of fields.
fc95d9a5 32 * By default this will fetch the field list relevant to `get`,
e15f9453 33 * but a different list may be returned if you specify another action.
19b53e5b 34 *
bb6bfd68
CW
35 * @method $this setLoadOptions(bool|array $value)
36 * @method bool|array getLoadOptions()
19b53e5b 37 * @method $this setAction(string $value)
c752d94b
CW
38 * @method $this setValues(array $values)
39 * @method array getValues()
19b53e5b
C
40 */
41class BasicGetFieldsAction extends BasicGetAction {
42
43 /**
44 * Fetch option lists for fields?
45 *
bb6bfd68
CW
46 * This parameter can be either a boolean or an array of attributes to return from the option list:
47 *
48 * - If `FALSE`, each field's `options` property will be a boolean indicating whether the field has an option list
49 * - If `TRUE`, `options` will be returned as a flat array of the option list's `[id => label]`
50 * - If an array, `options` will be a non-associative array of requested properties:
51 * id, name, label, abbr, description, color, icon
52 * e.g. `loadOptions: ['id', 'name', 'label']` will return an array like `[[id: 1, name: 'Meeting', label: 'Meeting'], ...]`
53 * (note that names and labels are generally ONLY the same when the site's language is set to English).
54 *
55 * @var bool|array
19b53e5b
C
56 */
57 protected $loadOptions = FALSE;
58
59 /**
c752d94b
CW
60 * Fields will be returned appropriate to the specified action (get, create, delete, etc.)
61 *
19b53e5b
C
62 * @var string
63 */
64 protected $action = 'get';
65
c752d94b
CW
66 /**
67 * Fields will be returned appropriate to the specified values (e.g. ['contact_type' => 'Individual'])
68 *
69 * @var array
70 */
71 protected $values = [];
72
19b53e5b
C
73 /**
74 * To implement getFields for your own entity:
75 *
76 * 1. From your entity class add a static getFields method.
77 * 2. That method should construct and return this class.
78 * 3. The 3rd argument passed to this constructor should be a function that returns an
79 * array of fields for your entity's CRUD actions.
80 * 4. For non-crud actions that need a different set of fields, you can override the
81 * list from step 3 on a per-action basis by defining a fields() method in that action.
82 * See for example BasicGetFieldsAction::fields() or GetActions::fields().
83 *
84 * @param Result $result
85 * @throws \Civi\API\Exception\NotImplementedException
86 */
87 public function _run(Result $result) {
88 try {
3a8dc228 89 $actionClass = \Civi\API\Request::create($this->getEntityName(), $this->getAction(), ['version' => 4]);
19b53e5b
C
90 }
91 catch (NotImplementedException $e) {
92 }
93 if (isset($actionClass) && method_exists($actionClass, 'fields')) {
94 $values = $actionClass->fields();
95 }
96 else {
97 $values = $this->getRecords();
98 }
bb6bfd68 99 $this->formatResults($values);
19b53e5b
C
100 $result->exchangeArray($this->queryArray($values));
101 }
102
103 /**
104 * Ensure every result contains, at minimum, the array keys as defined in $this->fields.
105 *
106 * Attempt to set some sensible defaults for some fields.
107 *
bb6bfd68
CW
108 * Format option lists.
109 *
19b53e5b 110 * In most cases it's not necessary to override this function, even if your entity is really weird.
bb6bfd68 111 * Instead just override $this->fields and this function will respect that.
19b53e5b
C
112 *
113 * @param array $values
114 */
bb6bfd68 115 protected function formatResults(&$values) {
19b53e5b 116 $fields = array_column($this->fields(), 'name');
0d20e81c
CW
117 // Enforce field permissions
118 if ($this->checkPermissions) {
119 foreach ($values as $key => $field) {
120 if (!empty($field['permission']) && !\CRM_Core_Permission::check($field['permission'])) {
121 unset($values[$key]);
122 }
123 }
124 }
19b53e5b
C
125 foreach ($values as &$field) {
126 $defaults = array_intersect_key([
127 'title' => empty($field['name']) ? NULL : ucwords(str_replace('_', ' ', $field['name'])),
128 'entity' => $this->getEntityName(),
129 'required' => FALSE,
130 'options' => !empty($field['pseudoconstant']),
131 'data_type' => \CRM_Utils_Array::value('type', $field, 'String'),
132 ], array_flip($fields));
133 $field += $defaults;
bb6bfd68
CW
134 if (isset($defaults['options'])) {
135 $field['options'] = $this->formatOptionList($field['options']);
19b53e5b
C
136 }
137 $field += array_fill_keys($fields, NULL);
138 }
139 }
140
bb6bfd68
CW
141 /**
142 * Transforms option list into the format specified in $this->loadOptions
143 *
144 * @param $options
145 * @return array|bool
146 */
147 private function formatOptionList($options) {
148 if (!$this->loadOptions || !is_array($options)) {
149 return (bool) $options;
150 }
151 if (!$options) {
152 return $options;
153 }
154 $formatted = [];
155 $first = reset($options);
156 // Flat array requested
157 if ($this->loadOptions === TRUE) {
158 // Convert non-associative to flat array
159 if (is_array($first) && isset($first['id'])) {
160 foreach ($options as $option) {
161 $formatted[$option['id']] = $option['label'] ?? $option['name'] ?? $option['id'];
162 }
163 return $formatted;
164 }
165 return $options;
166 }
167 // Non-associative array of multiple properties requested
168 foreach ($options as $id => $option) {
169 // Transform a flat list
170 if (!is_array($option)) {
171 $option = [
172 'id' => $id,
173 'name' => $option,
174 'label' => $option,
175 ];
176 }
177 $formatted[] = array_intersect_key($option, array_flip($this->loadOptions));
178 }
179 return $formatted;
180 }
181
19b53e5b
C
182 /**
183 * @return string
184 */
185 public function getAction() {
186 // For actions that build on top of other actions, return fields for the simpler action
187 $sub = [
188 'save' => 'create',
189 'replace' => 'create',
190 ];
191 return $sub[$this->action] ?? $this->action;
192 }
193
121ec912
CW
194 /**
195 * Add an item to the values array
196 * @param string $fieldName
197 * @param mixed $value
198 * @return $this
199 */
200 public function addValue(string $fieldName, $value) {
201 $this->values[$fieldName] = $value;
202 return $this;
203 }
204
3a8dc228
CW
205 /**
206 * @param bool $includeCustom
207 * @return $this
208 */
209 public function setIncludeCustom(bool $includeCustom) {
210 // Be forgiving if the param doesn't exist and don't throw an exception
211 if (property_exists($this, 'includeCustom')) {
212 $this->includeCustom = $includeCustom;
213 }
214 return $this;
215 }
216
19b53e5b
C
217 public function fields() {
218 return [
219 [
220 'name' => 'name',
221 'data_type' => 'String',
222 ],
223 [
224 'name' => 'title',
225 'data_type' => 'String',
226 ],
227 [
228 'name' => 'description',
229 'data_type' => 'String',
230 ],
231 [
232 'name' => 'default_value',
233 'data_type' => 'String',
234 ],
235 [
236 'name' => 'required',
237 'data_type' => 'Boolean',
238 ],
239 [
240 'name' => 'required_if',
241 'data_type' => 'String',
242 ],
243 [
244 'name' => 'options',
245 'data_type' => 'Array',
246 ],
247 [
248 'name' => 'data_type',
249 'data_type' => 'String',
250 ],
251 [
252 'name' => 'input_type',
253 'data_type' => 'String',
254 ],
255 [
256 'name' => 'input_attrs',
257 'data_type' => 'Array',
258 ],
259 [
260 'name' => 'fk_entity',
261 'data_type' => 'String',
262 ],
263 [
264 'name' => 'serialize',
265 'data_type' => 'Integer',
266 ],
267 [
268 'name' => 'entity',
269 'data_type' => 'String',
270 ],
271 ];
272 }
273
274}