Merge pull request #15326 from totten/master-headfoot-2
[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;
25use Civi\Api4\Utils\ActionUtil;
26
27/**
28 * Get fields for an entity.
29 *
30 * @method $this setLoadOptions(bool $value)
31 * @method bool getLoadOptions()
32 * @method $this setAction(string $value)
33 */
34class BasicGetFieldsAction extends BasicGetAction {
35
36 /**
37 * Fetch option lists for fields?
38 *
39 * @var bool
40 */
41 protected $loadOptions = FALSE;
42
43 /**
44 * @var string
45 */
46 protected $action = 'get';
47
48 /**
49 * To implement getFields for your own entity:
50 *
51 * 1. From your entity class add a static getFields method.
52 * 2. That method should construct and return this class.
53 * 3. The 3rd argument passed to this constructor should be a function that returns an
54 * array of fields for your entity's CRUD actions.
55 * 4. For non-crud actions that need a different set of fields, you can override the
56 * list from step 3 on a per-action basis by defining a fields() method in that action.
57 * See for example BasicGetFieldsAction::fields() or GetActions::fields().
58 *
59 * @param Result $result
60 * @throws \Civi\API\Exception\NotImplementedException
61 */
62 public function _run(Result $result) {
63 try {
64 $actionClass = ActionUtil::getAction($this->getEntityName(), $this->getAction());
65 }
66 catch (NotImplementedException $e) {
67 }
68 if (isset($actionClass) && method_exists($actionClass, 'fields')) {
69 $values = $actionClass->fields();
70 }
71 else {
72 $values = $this->getRecords();
73 }
74 $this->padResults($values);
75 $result->exchangeArray($this->queryArray($values));
76 }
77
78 /**
79 * Ensure every result contains, at minimum, the array keys as defined in $this->fields.
80 *
81 * Attempt to set some sensible defaults for some fields.
82 *
83 * In most cases it's not necessary to override this function, even if your entity is really weird.
84 * Instead just override $this->fields and thes function will respect that.
85 *
86 * @param array $values
87 */
88 protected function padResults(&$values) {
89 $fields = array_column($this->fields(), 'name');
90 foreach ($values as &$field) {
91 $defaults = array_intersect_key([
92 'title' => empty($field['name']) ? NULL : ucwords(str_replace('_', ' ', $field['name'])),
93 'entity' => $this->getEntityName(),
94 'required' => FALSE,
95 'options' => !empty($field['pseudoconstant']),
96 'data_type' => \CRM_Utils_Array::value('type', $field, 'String'),
97 ], array_flip($fields));
98 $field += $defaults;
99 if (!$this->loadOptions && isset($defaults['options'])) {
100 $field['options'] = (bool) $field['options'];
101 }
102 $field += array_fill_keys($fields, NULL);
103 }
104 }
105
106 /**
107 * @return string
108 */
109 public function getAction() {
110 // For actions that build on top of other actions, return fields for the simpler action
111 $sub = [
112 'save' => 'create',
113 'replace' => 'create',
114 ];
115 return $sub[$this->action] ?? $this->action;
116 }
117
118 public function fields() {
119 return [
120 [
121 'name' => 'name',
122 'data_type' => 'String',
123 ],
124 [
125 'name' => 'title',
126 'data_type' => 'String',
127 ],
128 [
129 'name' => 'description',
130 'data_type' => 'String',
131 ],
132 [
133 'name' => 'default_value',
134 'data_type' => 'String',
135 ],
136 [
137 'name' => 'required',
138 'data_type' => 'Boolean',
139 ],
140 [
141 'name' => 'required_if',
142 'data_type' => 'String',
143 ],
144 [
145 'name' => 'options',
146 'data_type' => 'Array',
147 ],
148 [
149 'name' => 'data_type',
150 'data_type' => 'String',
151 ],
152 [
153 'name' => 'input_type',
154 'data_type' => 'String',
155 ],
156 [
157 'name' => 'input_attrs',
158 'data_type' => 'Array',
159 ],
160 [
161 'name' => 'fk_entity',
162 'data_type' => 'String',
163 ],
164 [
165 'name' => 'serialize',
166 'data_type' => 'Integer',
167 ],
168 [
169 'name' => 'entity',
170 'data_type' => 'String',
171 ],
172 ];
173 }
174
175}