Merge pull request #23928 from artfulrobot/artfulrobot-cleanup-repeattransaction
[civicrm-core.git] / ext / afform / core / Civi / Api4 / Afform.php
1 <?php
2
3 namespace Civi\Api4;
4
5 use Civi\Api4\Generic\BasicGetFieldsAction;
6
7 /**
8 * User-configurable forms.
9 *
10 * Afform stands for *The Affable Administrative Angular Form Framework*.
11 *
12 * This API provides actions for
13 * 1. **_Managing_ forms:**
14 * The `create`, `get`, `save`, `update`, & `revert` actions read/write form html & json files.
15 * 2. **_Using_ forms:**
16 * The `prefill` and `submit` actions are used for preparing forms and processing submissions.
17 *
18 * @see https://lab.civicrm.org/extensions/afform
19 * @searchable none
20 * @package Civi\Api4
21 */
22 class Afform extends Generic\AbstractEntity {
23
24 /**
25 * @param bool $checkPermissions
26 * @return Action\Afform\Get
27 */
28 public static function get($checkPermissions = TRUE) {
29 return (new Action\Afform\Get('Afform', __FUNCTION__))
30 ->setCheckPermissions($checkPermissions);
31 }
32
33 /**
34 * @param bool $checkPermissions
35 * @return Action\Afform\Create
36 */
37 public static function create($checkPermissions = TRUE) {
38 return (new Action\Afform\Create('Afform', __FUNCTION__))
39 ->setCheckPermissions($checkPermissions);
40 }
41
42 /**
43 * @param bool $checkPermissions
44 * @return Action\Afform\Update
45 */
46 public static function update($checkPermissions = TRUE) {
47 return (new Action\Afform\Update('Afform', __FUNCTION__))
48 ->setCheckPermissions($checkPermissions);
49 }
50
51 /**
52 * @param bool $checkPermissions
53 * @return Action\Afform\Save
54 */
55 public static function save($checkPermissions = TRUE) {
56 return (new Action\Afform\Save('Afform', __FUNCTION__))
57 ->setCheckPermissions($checkPermissions);
58 }
59
60 /**
61 * @param bool $checkPermissions
62 * @return Action\Afform\Convert
63 */
64 public static function convert($checkPermissions = TRUE) {
65 return (new Action\Afform\Convert('Afform', __FUNCTION__))
66 ->setCheckPermissions($checkPermissions);
67 }
68
69 /**
70 * @param bool $checkPermissions
71 * @return Action\Afform\Prefill
72 */
73 public static function prefill($checkPermissions = TRUE) {
74 return (new Action\Afform\Prefill('Afform', __FUNCTION__))
75 ->setCheckPermissions($checkPermissions);
76 }
77
78 /**
79 * @param bool $checkPermissions
80 * @return Action\Afform\Submit
81 */
82 public static function submit($checkPermissions = TRUE) {
83 return (new Action\Afform\Submit('Afform', __FUNCTION__))
84 ->setCheckPermissions($checkPermissions);
85 }
86
87 /**
88 * @param bool $checkPermissions
89 * @return Action\Afform\SubmitFile
90 */
91 public static function submitFile($checkPermissions = TRUE) {
92 return (new Action\Afform\SubmitFile('Afform', __FUNCTION__))
93 ->setCheckPermissions($checkPermissions);
94 }
95
96 /**
97 * @param bool $checkPermissions
98 * @return Action\Afform\GetOptions
99 */
100 public static function getOptions($checkPermissions = TRUE) {
101 return (new Action\Afform\GetOptions('Afform', __FUNCTION__))
102 ->setCheckPermissions($checkPermissions);
103 }
104
105 /**
106 * @param bool $checkPermissions
107 * @return Action\Afform\Revert
108 */
109 public static function revert($checkPermissions = TRUE) {
110 return (new Action\Afform\Revert('Afform', __FUNCTION__))
111 ->setCheckPermissions($checkPermissions);
112 }
113
114 /**
115 * @param bool $checkPermissions
116 * @return Generic\BasicGetFieldsAction
117 */
118 public static function getFields($checkPermissions = TRUE) {
119 return (new Generic\BasicGetFieldsAction('Afform', __FUNCTION__, function(BasicGetFieldsAction $self) {
120 $fields = [
121 [
122 'name' => 'name',
123 ],
124 [
125 'name' => 'type',
126 'options' => $self->pseudoconstantOptions('afform_type'),
127 'suffixes' => ['id', 'name', 'label', 'icon'],
128 ],
129 [
130 'name' => 'requires',
131 'data_type' => 'Array',
132 ],
133 [
134 'name' => 'entity_type',
135 'description' => 'Block used for this entity type',
136 ],
137 [
138 'name' => 'join_entity',
139 'description' => 'Used for blocks that join a sub-entity (e.g. Emails for a Contact)',
140 ],
141 [
142 'name' => 'title',
143 'required' => $self->getAction() === 'create',
144 ],
145 [
146 'name' => 'description',
147 ],
148 [
149 'name' => 'is_dashlet',
150 'data_type' => 'Boolean',
151 ],
152 [
153 'name' => 'is_public',
154 'data_type' => 'Boolean',
155 ],
156 [
157 'name' => 'is_token',
158 'data_type' => 'Boolean',
159 ],
160 [
161 'name' => 'contact_summary',
162 'data_type' => 'String',
163 'options' => [
164 'block' => ts('Contact Summary Block'),
165 'tab' => ts('Contact Summary Tab'),
166 ],
167 ],
168 [
169 'name' => 'icon',
170 'description' => 'Icon shown in the contact summary tab',
171 ],
172 [
173 'name' => 'server_route',
174 ],
175 [
176 'name' => 'permission',
177 ],
178 [
179 'name' => 'redirect',
180 ],
181 [
182 'name' => 'create_submission',
183 'data_type' => 'Boolean',
184 ],
185 [
186 'name' => 'layout',
187 'data_type' => 'Array',
188 'description' => 'HTML form layout; format is controlled by layoutFormat param',
189 ],
190 ];
191 // Calculated fields returned by get action
192 if ($self->getAction() === 'get') {
193 $fields[] = [
194 'name' => 'module_name',
195 'type' => 'Extra',
196 'readonly' => TRUE,
197 ];
198 $fields[] = [
199 'name' => 'directive_name',
200 'type' => 'Extra',
201 'readonly' => TRUE,
202 ];
203 $fields[] = [
204 'name' => 'has_local',
205 'type' => 'Extra',
206 'data_type' => 'Boolean',
207 'description' => 'Whether a local copy is saved on site',
208 'readonly' => TRUE,
209 ];
210 $fields[] = [
211 'name' => 'has_base',
212 'type' => 'Extra',
213 'data_type' => 'Boolean',
214 'description' => 'Is provided by an extension',
215 'readonly' => TRUE,
216 ];
217 $fields[] = [
218 'name' => 'base_module',
219 'type' => 'Extra',
220 'data_type' => 'String',
221 'description' => 'Name of extension which provides this form',
222 'readonly' => TRUE,
223 'options' => $self->getLoadOptions() ? \CRM_Core_PseudoConstant::getExtensions() : TRUE,
224 ];
225 $fields[] = [
226 'name' => 'search_displays',
227 'type' => 'Extra',
228 'data_type' => 'Array',
229 'readonly' => TRUE,
230 'description' => 'Embedded search displays, formatted like ["search-name.display-name"]',
231 ];
232 }
233
234 return $fields;
235 }))->setCheckPermissions($checkPermissions);
236 }
237
238 /**
239 * @return array
240 */
241 public static function permissions() {
242 return [
243 'meta' => ['access CiviCRM'],
244 'default' => [['administer CiviCRM', 'administer afform']],
245 // These all check form-level permissions
246 'get' => [],
247 'getOptions' => [],
248 'prefill' => [],
249 'submit' => [],
250 'submitFile' => [],
251 ];
252 }
253
254 /**
255 * @inheritDoc
256 */
257 public static function getInfo() {
258 $info = parent::getInfo();
259 $info['primary_key'] = ['name'];
260 return $info;
261 }
262
263 }