Commit | Line | Data |
---|---|---|
7422a230 TO |
1 | <?php |
2 | ||
3 | namespace Civi\Api4; | |
4 | ||
f3062b8c | 5 | use 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 | |
09815e9c | 19 | * @searchable false |
7422a230 | 20 | * @package Civi\Api4 |
7422a230 | 21 | */ |
c847b30c | 22 | class Afform extends Generic\AbstractEntity { |
aff08e16 TO |
23 | |
24 | /** | |
c847b30c CW |
25 | * @param bool $checkPermissions |
26 | * @return Action\Afform\Get | |
aff08e16 | 27 | */ |
c847b30c CW |
28 | public static function get($checkPermissions = TRUE) { |
29 | return (new Action\Afform\Get('Afform', __FUNCTION__)) | |
30 | ->setCheckPermissions($checkPermissions); | |
aff08e16 TO |
31 | } |
32 | ||
28b4ace4 | 33 | /** |
c847b30c CW |
34 | * @param bool $checkPermissions |
35 | * @return Action\Afform\Create | |
28b4ace4 | 36 | */ |
c847b30c CW |
37 | public static function create($checkPermissions = TRUE) { |
38 | return (new Action\Afform\Create('Afform', __FUNCTION__)) | |
39 | ->setCheckPermissions($checkPermissions); | |
28b4ace4 CW |
40 | } |
41 | ||
42 | /** | |
c847b30c CW |
43 | * @param bool $checkPermissions |
44 | * @return Action\Afform\Update | |
28b4ace4 | 45 | */ |
c847b30c CW |
46 | public static function update($checkPermissions = TRUE) { |
47 | return (new Action\Afform\Update('Afform', __FUNCTION__, 'name')) | |
48 | ->setCheckPermissions($checkPermissions); | |
28b4ace4 CW |
49 | } |
50 | ||
51 | /** | |
c847b30c CW |
52 | * @param bool $checkPermissions |
53 | * @return Action\Afform\Save | |
28b4ace4 | 54 | */ |
c847b30c CW |
55 | public static function save($checkPermissions = TRUE) { |
56 | return (new Action\Afform\Save('Afform', __FUNCTION__, 'name')) | |
57 | ->setCheckPermissions($checkPermissions); | |
28b4ace4 CW |
58 | } |
59 | ||
5c80fca4 TO |
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 | ||
28b4ace4 | 69 | /** |
c847b30c CW |
70 | * @param bool $checkPermissions |
71 | * @return Action\Afform\Prefill | |
28b4ace4 | 72 | */ |
c847b30c CW |
73 | public static function prefill($checkPermissions = TRUE) { |
74 | return (new Action\Afform\Prefill('Afform', __FUNCTION__)) | |
75 | ->setCheckPermissions($checkPermissions); | |
28b4ace4 CW |
76 | } |
77 | ||
78 | /** | |
c847b30c CW |
79 | * @param bool $checkPermissions |
80 | * @return Action\Afform\Submit | |
28b4ace4 | 81 | */ |
c847b30c CW |
82 | public static function submit($checkPermissions = TRUE) { |
83 | return (new Action\Afform\Submit('Afform', __FUNCTION__)) | |
84 | ->setCheckPermissions($checkPermissions); | |
28b4ace4 CW |
85 | } |
86 | ||
aff08e16 | 87 | /** |
c847b30c CW |
88 | * @param bool $checkPermissions |
89 | * @return Generic\BasicBatchAction | |
aff08e16 | 90 | */ |
c847b30c CW |
91 | public static function revert($checkPermissions = TRUE) { |
92 | return (new BasicBatchAction('Afform', __FUNCTION__, ['name'], function($item, BasicBatchAction $action) { | |
f3062b8c TO |
93 | $scanner = \Civi::service('afform_scanner'); |
94 | $files = [ | |
95 | \CRM_Afform_AfformScanner::METADATA_FILE, | |
0770b5ed | 96 | \CRM_Afform_AfformScanner::LAYOUT_FILE, |
f3062b8c TO |
97 | ]; |
98 | ||
99 | foreach ($files as $file) { | |
100 | $metaPath = $scanner->createSiteLocalPath($item['name'], $file); | |
101 | if (file_exists($metaPath)) { | |
102 | if (!@unlink($metaPath)) { | |
103 | throw new \API_Exception("Failed to remove afform overrides in $file"); | |
104 | } | |
105 | } | |
106 | } | |
107 | ||
108 | // We may have changed list of files covered by the cache. | |
74f862e4 | 109 | _afform_clear(); |
f3062b8c TO |
110 | |
111 | // FIXME if `server_route` changes, then flush the menu cache. | |
112 | // FIXME if asset-caching is enabled, then flush the asset cache | |
113 | ||
114 | return $item; | |
c847b30c | 115 | }))->setCheckPermissions($checkPermissions); |
aff08e16 TO |
116 | } |
117 | ||
c847b30c CW |
118 | /** |
119 | * @param bool $checkPermissions | |
120 | * @return Generic\BasicGetFieldsAction | |
121 | */ | |
122 | public static function getFields($checkPermissions = TRUE) { | |
123 | return (new Generic\BasicGetFieldsAction('Afform', __FUNCTION__, function($self) { | |
10fd70a3 | 124 | $fields = [ |
15b11f47 CW |
125 | [ |
126 | 'name' => 'name', | |
127 | ], | |
9e58ceed CW |
128 | [ |
129 | 'name' => 'type', | |
4f664e9c | 130 | 'options' => $self->pseudoconstantOptions('afform_type'), |
9e58ceed | 131 | ], |
15b11f47 CW |
132 | [ |
133 | 'name' => 'requires', | |
d625938f | 134 | 'data_type' => 'Array', |
15b11f47 | 135 | ], |
e1aca853 CW |
136 | [ |
137 | 'name' => 'block', | |
138 | ], | |
139 | [ | |
344e8290 | 140 | 'name' => 'join', |
e1aca853 | 141 | ], |
15b11f47 CW |
142 | [ |
143 | 'name' => 'title', | |
28b4ace4 | 144 | 'required' => $self->getAction() === 'create', |
15b11f47 CW |
145 | ], |
146 | [ | |
147 | 'name' => 'description', | |
148 | ], | |
1887d8bd TO |
149 | [ |
150 | 'name' => 'is_dashlet', | |
151 | 'data_type' => 'Boolean', | |
152 | ], | |
15b11f47 CW |
153 | [ |
154 | 'name' => 'is_public', | |
155 | 'data_type' => 'Boolean', | |
156 | ], | |
053ab2a5 TO |
157 | [ |
158 | 'name' => 'is_token', | |
159 | 'data_type' => 'Boolean', | |
15b11f47 | 160 | ], |
c63f20d3 CW |
161 | [ |
162 | 'name' => 'contact_summary', | |
163 | 'data_type' => 'String', | |
eee24dde CW |
164 | 'options' => [ |
165 | 'block' => ts('Contact Summary Block'), | |
166 | 'tab' => ts('Contact Summary Tab'), | |
167 | ], | |
c63f20d3 | 168 | ], |
e1aca853 | 169 | [ |
344e8290 CW |
170 | 'name' => 'repeat', |
171 | 'data_type' => 'Mixed', | |
e1aca853 | 172 | ], |
15b11f47 CW |
173 | [ |
174 | 'name' => 'server_route', | |
175 | ], | |
17535d7f TO |
176 | [ |
177 | 'name' => 'permission', | |
178 | ], | |
b6e13973 SL |
179 | [ |
180 | 'name' => 'redirect', | |
181 | ], | |
15b11f47 CW |
182 | [ |
183 | 'name' => 'layout', | |
d625938f | 184 | 'data_type' => 'Array', |
15b11f47 CW |
185 | ], |
186 | ]; | |
c63f20d3 | 187 | // Calculated fields returned by get action |
10fd70a3 | 188 | if ($self->getAction() === 'get') { |
25f2b36b CW |
189 | $fields[] = [ |
190 | 'name' => 'module_name', | |
a0e733f3 | 191 | 'readonly' => TRUE, |
25f2b36b CW |
192 | ]; |
193 | $fields[] = [ | |
194 | 'name' => 'directive_name', | |
a0e733f3 | 195 | 'readonly' => TRUE, |
25f2b36b | 196 | ]; |
10fd70a3 TO |
197 | $fields[] = [ |
198 | 'name' => 'has_local', | |
d625938f | 199 | 'data_type' => 'Boolean', |
a0e733f3 | 200 | 'readonly' => TRUE, |
10fd70a3 TO |
201 | ]; |
202 | $fields[] = [ | |
2d4bfef1 | 203 | 'name' => 'has_base', |
d625938f | 204 | 'data_type' => 'Boolean', |
a0e733f3 | 205 | 'readonly' => TRUE, |
10fd70a3 TO |
206 | ]; |
207 | } | |
208 | ||
209 | return $fields; | |
c847b30c | 210 | }))->setCheckPermissions($checkPermissions); |
15b11f47 CW |
211 | } |
212 | ||
aff08e16 TO |
213 | /** |
214 | * @return array | |
215 | */ | |
216 | public static function permissions() { | |
217 | return [ | |
218 | "meta" => ["access CiviCRM"], | |
219 | "default" => ["administer CiviCRM"], | |
48be26b0 CW |
220 | // These all check form-level permissions |
221 | 'get' => [], | |
3fe34185 CW |
222 | 'prefill' => [], |
223 | 'submit' => [], | |
aff08e16 TO |
224 | ]; |
225 | } | |
7422a230 TO |
226 | |
227 | } |