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