Add help_pre and help_post to api4 fieldSpec
[civicrm-core.git] / Civi / Api4 / Generic / BasicGetFieldsAction.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
f299f7db 7 | Copyright CiviCRM LLC (c) 2004-2020 |
380f3545
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29/**
30 *
31 * @package CRM
f299f7db 32 * @copyright CiviCRM LLC (c) 2004-2020
380f3545
TO
33 * $Id$
34 *
35 */
36
37
19b53e5b
C
38namespace Civi\Api4\Generic;
39
40use Civi\API\Exception\NotImplementedException;
41use Civi\Api4\Utils\ActionUtil;
42
43/**
44 * Get fields for an entity.
45 *
46 * @method $this setLoadOptions(bool $value)
47 * @method bool getLoadOptions()
48 * @method $this setAction(string $value)
49 */
50class BasicGetFieldsAction extends BasicGetAction {
51
52 /**
53 * Fetch option lists for fields?
54 *
55 * @var bool
56 */
57 protected $loadOptions = FALSE;
58
59 /**
60 * @var string
61 */
62 protected $action = 'get';
63
64 /**
65 * To implement getFields for your own entity:
66 *
67 * 1. From your entity class add a static getFields method.
68 * 2. That method should construct and return this class.
69 * 3. The 3rd argument passed to this constructor should be a function that returns an
70 * array of fields for your entity's CRUD actions.
71 * 4. For non-crud actions that need a different set of fields, you can override the
72 * list from step 3 on a per-action basis by defining a fields() method in that action.
73 * See for example BasicGetFieldsAction::fields() or GetActions::fields().
74 *
75 * @param Result $result
76 * @throws \Civi\API\Exception\NotImplementedException
77 */
78 public function _run(Result $result) {
79 try {
80 $actionClass = ActionUtil::getAction($this->getEntityName(), $this->getAction());
81 }
82 catch (NotImplementedException $e) {
83 }
84 if (isset($actionClass) && method_exists($actionClass, 'fields')) {
85 $values = $actionClass->fields();
86 }
87 else {
88 $values = $this->getRecords();
89 }
90 $this->padResults($values);
91 $result->exchangeArray($this->queryArray($values));
92 }
93
94 /**
95 * Ensure every result contains, at minimum, the array keys as defined in $this->fields.
96 *
97 * Attempt to set some sensible defaults for some fields.
98 *
99 * In most cases it's not necessary to override this function, even if your entity is really weird.
100 * Instead just override $this->fields and thes function will respect that.
101 *
102 * @param array $values
103 */
104 protected function padResults(&$values) {
105 $fields = array_column($this->fields(), 'name');
106 foreach ($values as &$field) {
107 $defaults = array_intersect_key([
108 'title' => empty($field['name']) ? NULL : ucwords(str_replace('_', ' ', $field['name'])),
109 'entity' => $this->getEntityName(),
110 'required' => FALSE,
111 'options' => !empty($field['pseudoconstant']),
112 'data_type' => \CRM_Utils_Array::value('type', $field, 'String'),
113 ], array_flip($fields));
114 $field += $defaults;
115 if (!$this->loadOptions && isset($defaults['options'])) {
116 $field['options'] = (bool) $field['options'];
117 }
118 $field += array_fill_keys($fields, NULL);
119 }
120 }
121
122 /**
123 * @return string
124 */
125 public function getAction() {
126 // For actions that build on top of other actions, return fields for the simpler action
127 $sub = [
128 'save' => 'create',
129 'replace' => 'create',
130 ];
131 return $sub[$this->action] ?? $this->action;
132 }
133
134 public function fields() {
135 return [
136 [
137 'name' => 'name',
138 'data_type' => 'String',
139 ],
140 [
141 'name' => 'title',
142 'data_type' => 'String',
143 ],
144 [
145 'name' => 'description',
146 'data_type' => 'String',
147 ],
148 [
149 'name' => 'default_value',
150 'data_type' => 'String',
151 ],
152 [
153 'name' => 'required',
154 'data_type' => 'Boolean',
155 ],
156 [
157 'name' => 'required_if',
158 'data_type' => 'String',
159 ],
160 [
161 'name' => 'options',
162 'data_type' => 'Array',
163 ],
164 [
165 'name' => 'data_type',
166 'data_type' => 'String',
167 ],
168 [
169 'name' => 'input_type',
170 'data_type' => 'String',
171 ],
172 [
173 'name' => 'input_attrs',
174 'data_type' => 'Array',
175 ],
176 [
177 'name' => 'fk_entity',
178 'data_type' => 'String',
179 ],
180 [
181 'name' => 'serialize',
182 'data_type' => 'Integer',
183 ],
184 [
185 'name' => 'entity',
186 'data_type' => 'String',
187 ],
188 ];
189 }
190
191}