Merge pull request #19063 from christianwach/lab-core-2213
[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;
7422a230
TO
6
7/**
7d54c92c
CW
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
7422a230 19 * @package Civi\Api4
7422a230 20 */
c847b30c 21class Afform extends Generic\AbstractEntity {
aff08e16
TO
22
23 /**
c847b30c
CW
24 * @param bool $checkPermissions
25 * @return Action\Afform\Get
aff08e16 26 */
c847b30c
CW
27 public static function get($checkPermissions = TRUE) {
28 return (new Action\Afform\Get('Afform', __FUNCTION__))
29 ->setCheckPermissions($checkPermissions);
aff08e16
TO
30 }
31
28b4ace4 32 /**
c847b30c
CW
33 * @param bool $checkPermissions
34 * @return Action\Afform\Create
28b4ace4 35 */
c847b30c
CW
36 public static function create($checkPermissions = TRUE) {
37 return (new Action\Afform\Create('Afform', __FUNCTION__))
38 ->setCheckPermissions($checkPermissions);
28b4ace4
CW
39 }
40
41 /**
c847b30c
CW
42 * @param bool $checkPermissions
43 * @return Action\Afform\Update
28b4ace4 44 */
c847b30c
CW
45 public static function update($checkPermissions = TRUE) {
46 return (new Action\Afform\Update('Afform', __FUNCTION__, 'name'))
47 ->setCheckPermissions($checkPermissions);
28b4ace4
CW
48 }
49
50 /**
c847b30c
CW
51 * @param bool $checkPermissions
52 * @return Action\Afform\Save
28b4ace4 53 */
c847b30c
CW
54 public static function save($checkPermissions = TRUE) {
55 return (new Action\Afform\Save('Afform', __FUNCTION__, 'name'))
56 ->setCheckPermissions($checkPermissions);
28b4ace4
CW
57 }
58
59 /**
c847b30c
CW
60 * @param bool $checkPermissions
61 * @return Action\Afform\Prefill
28b4ace4 62 */
c847b30c
CW
63 public static function prefill($checkPermissions = TRUE) {
64 return (new Action\Afform\Prefill('Afform', __FUNCTION__))
65 ->setCheckPermissions($checkPermissions);
28b4ace4
CW
66 }
67
68 /**
c847b30c
CW
69 * @param bool $checkPermissions
70 * @return Action\Afform\Submit
28b4ace4 71 */
c847b30c
CW
72 public static function submit($checkPermissions = TRUE) {
73 return (new Action\Afform\Submit('Afform', __FUNCTION__))
74 ->setCheckPermissions($checkPermissions);
28b4ace4
CW
75 }
76
aff08e16 77 /**
c847b30c
CW
78 * @param bool $checkPermissions
79 * @return Generic\BasicBatchAction
aff08e16 80 */
c847b30c
CW
81 public static function revert($checkPermissions = TRUE) {
82 return (new BasicBatchAction('Afform', __FUNCTION__, ['name'], function($item, BasicBatchAction $action) {
f3062b8c
TO
83 $scanner = \Civi::service('afform_scanner');
84 $files = [
85 \CRM_Afform_AfformScanner::METADATA_FILE,
0770b5ed 86 \CRM_Afform_AfformScanner::LAYOUT_FILE,
f3062b8c
TO
87 ];
88
89 foreach ($files as $file) {
90 $metaPath = $scanner->createSiteLocalPath($item['name'], $file);
91 if (file_exists($metaPath)) {
92 if (!@unlink($metaPath)) {
93 throw new \API_Exception("Failed to remove afform overrides in $file");
94 }
95 }
96 }
97
98 // We may have changed list of files covered by the cache.
74f862e4 99 _afform_clear();
f3062b8c
TO
100
101 // FIXME if `server_route` changes, then flush the menu cache.
102 // FIXME if asset-caching is enabled, then flush the asset cache
103
104 return $item;
c847b30c 105 }))->setCheckPermissions($checkPermissions);
aff08e16
TO
106 }
107
c847b30c
CW
108 /**
109 * @param bool $checkPermissions
110 * @return Generic\BasicGetFieldsAction
111 */
112 public static function getFields($checkPermissions = TRUE) {
113 return (new Generic\BasicGetFieldsAction('Afform', __FUNCTION__, function($self) {
10fd70a3 114 $fields = [
15b11f47
CW
115 [
116 'name' => 'name',
117 ],
118 [
119 'name' => 'requires',
120 ],
e1aca853
CW
121 [
122 'name' => 'block',
123 ],
124 [
344e8290 125 'name' => 'join',
e1aca853 126 ],
15b11f47
CW
127 [
128 'name' => 'title',
28b4ace4 129 'required' => $self->getAction() === 'create',
15b11f47
CW
130 ],
131 [
132 'name' => 'description',
133 ],
1887d8bd
TO
134 [
135 'name' => 'is_dashlet',
136 'data_type' => 'Boolean',
137 ],
15b11f47
CW
138 [
139 'name' => 'is_public',
140 'data_type' => 'Boolean',
141 ],
e1aca853 142 [
344e8290
CW
143 'name' => 'repeat',
144 'data_type' => 'Mixed',
e1aca853 145 ],
15b11f47
CW
146 [
147 'name' => 'server_route',
148 ],
17535d7f
TO
149 [
150 'name' => 'permission',
151 ],
15b11f47
CW
152 [
153 'name' => 'layout',
154 ],
155 ];
10fd70a3
TO
156
157 if ($self->getAction() === 'get') {
25f2b36b
CW
158 $fields[] = [
159 'name' => 'module_name',
160 ];
161 $fields[] = [
162 'name' => 'directive_name',
163 ];
10fd70a3
TO
164 $fields[] = [
165 'name' => 'has_local',
166 ];
167 $fields[] = [
2d4bfef1 168 'name' => 'has_base',
10fd70a3
TO
169 ];
170 }
171
172 return $fields;
c847b30c 173 }))->setCheckPermissions($checkPermissions);
15b11f47
CW
174 }
175
aff08e16
TO
176 /**
177 * @return array
178 */
179 public static function permissions() {
180 return [
181 "meta" => ["access CiviCRM"],
182 "default" => ["administer CiviCRM"],
3fe34185
CW
183 'prefill' => [],
184 'submit' => [],
aff08e16
TO
185 ];
186 }
7422a230
TO
187
188}