Merge pull request #22104 from civicrm/5.44
[civicrm-core.git] / ext / afform / core / Civi / Api4 / Afform.php
1 <?php
2
3 namespace Civi\Api4;
4
5 use Civi\Api4\Generic\BasicBatchAction;
6 use Civi\Api4\Generic\BasicGetFieldsAction;
7
8 /**
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
20 * @searchable none
21 * @package Civi\Api4
22 */
23 class Afform extends Generic\AbstractEntity {
24
25 /**
26 * @param bool $checkPermissions
27 * @return Action\Afform\Get
28 */
29 public static function get($checkPermissions = TRUE) {
30 return (new Action\Afform\Get('Afform', __FUNCTION__))
31 ->setCheckPermissions($checkPermissions);
32 }
33
34 /**
35 * @param bool $checkPermissions
36 * @return Action\Afform\Create
37 */
38 public static function create($checkPermissions = TRUE) {
39 return (new Action\Afform\Create('Afform', __FUNCTION__))
40 ->setCheckPermissions($checkPermissions);
41 }
42
43 /**
44 * @param bool $checkPermissions
45 * @return Action\Afform\Update
46 */
47 public static function update($checkPermissions = TRUE) {
48 return (new Action\Afform\Update('Afform', __FUNCTION__))
49 ->setCheckPermissions($checkPermissions);
50 }
51
52 /**
53 * @param bool $checkPermissions
54 * @return Action\Afform\Save
55 */
56 public static function save($checkPermissions = TRUE) {
57 return (new Action\Afform\Save('Afform', __FUNCTION__))
58 ->setCheckPermissions($checkPermissions);
59 }
60
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
70 /**
71 * @param bool $checkPermissions
72 * @return Action\Afform\Prefill
73 */
74 public static function prefill($checkPermissions = TRUE) {
75 return (new Action\Afform\Prefill('Afform', __FUNCTION__))
76 ->setCheckPermissions($checkPermissions);
77 }
78
79 /**
80 * @param bool $checkPermissions
81 * @return Action\Afform\Submit
82 */
83 public static function submit($checkPermissions = TRUE) {
84 return (new Action\Afform\Submit('Afform', __FUNCTION__))
85 ->setCheckPermissions($checkPermissions);
86 }
87
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
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
106 /**
107 * @param bool $checkPermissions
108 * @return Generic\BasicBatchAction
109 */
110 public static function revert($checkPermissions = TRUE) {
111 return (new BasicBatchAction('Afform', __FUNCTION__, function($item, BasicBatchAction $action) {
112 $scanner = \Civi::service('afform_scanner');
113 $files = [
114 \CRM_Afform_AfformScanner::METADATA_FILE,
115 \CRM_Afform_AfformScanner::LAYOUT_FILE,
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.
128 _afform_clear();
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;
134 }))->setCheckPermissions($checkPermissions);
135 }
136
137 /**
138 * @param bool $checkPermissions
139 * @return Generic\BasicGetFieldsAction
140 */
141 public static function getFields($checkPermissions = TRUE) {
142 return (new Generic\BasicGetFieldsAction('Afform', __FUNCTION__, function(BasicGetFieldsAction $self) {
143 $fields = [
144 [
145 'name' => 'name',
146 ],
147 [
148 'name' => 'type',
149 'options' => $self->pseudoconstantOptions('afform_type'),
150 'suffixes' => ['id', 'name', 'label', 'icon'],
151 ],
152 [
153 'name' => 'requires',
154 'data_type' => 'Array',
155 ],
156 [
157 'name' => 'entity_type',
158 'description' => 'Block used for this entity type',
159 ],
160 [
161 'name' => 'join_entity',
162 'description' => 'Used for blocks that join a sub-entity (e.g. Emails for a Contact)',
163 ],
164 [
165 'name' => 'title',
166 'required' => $self->getAction() === 'create',
167 ],
168 [
169 'name' => 'description',
170 ],
171 [
172 'name' => 'is_dashlet',
173 'data_type' => 'Boolean',
174 ],
175 [
176 'name' => 'is_public',
177 'data_type' => 'Boolean',
178 ],
179 [
180 'name' => 'is_token',
181 'data_type' => 'Boolean',
182 ],
183 [
184 'name' => 'contact_summary',
185 'data_type' => 'String',
186 'options' => [
187 'block' => ts('Contact Summary Block'),
188 'tab' => ts('Contact Summary Tab'),
189 ],
190 ],
191 [
192 'name' => 'server_route',
193 ],
194 [
195 'name' => 'permission',
196 ],
197 [
198 'name' => 'redirect',
199 ],
200 [
201 'name' => 'create_submission',
202 'data_type' => 'Boolean',
203 ],
204 [
205 'name' => 'layout',
206 'data_type' => 'Array',
207 'description' => 'HTML form layout; format is controlled by layoutFormat param',
208 ],
209 ];
210 // Calculated fields returned by get action
211 if ($self->getAction() === 'get') {
212 $fields[] = [
213 'name' => 'module_name',
214 'type' => 'Extra',
215 'readonly' => TRUE,
216 ];
217 $fields[] = [
218 'name' => 'directive_name',
219 'type' => 'Extra',
220 'readonly' => TRUE,
221 ];
222 $fields[] = [
223 'name' => 'has_local',
224 'type' => 'Extra',
225 'data_type' => 'Boolean',
226 'description' => 'Whether a local copy is saved on site',
227 'readonly' => TRUE,
228 ];
229 $fields[] = [
230 'name' => 'has_base',
231 'type' => 'Extra',
232 'data_type' => 'Boolean',
233 'description' => 'Is provided by an extension',
234 'readonly' => TRUE,
235 ];
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 ];
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 ];
251 }
252
253 return $fields;
254 }))->setCheckPermissions($checkPermissions);
255 }
256
257 /**
258 * @return array
259 */
260 public static function permissions() {
261 return [
262 "meta" => ["access CiviCRM"],
263 "default" => ["administer CiviCRM"],
264 // These all check form-level permissions
265 'get' => [],
266 'getOptions' => [],
267 'prefill' => [],
268 'submit' => [],
269 'submitFile' => [],
270 ];
271 }
272
273 /**
274 * @inheritDoc
275 */
276 public static function getInfo() {
277 $info = parent::getInfo();
278 $info['primary_key'] = ['name'];
279 return $info;
280 }
281
282 }