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