afform/mock - Update phpunix.xml.dist for phpunit7
[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 ],
134 [
135 'name' => 'is_public',
136 'data_type' => 'Boolean',
137 ],
e1aca853 138 [
344e8290
CW
139 'name' => 'repeat',
140 'data_type' => 'Mixed',
e1aca853 141 ],
15b11f47
CW
142 [
143 'name' => 'server_route',
144 ],
17535d7f
TO
145 [
146 'name' => 'permission',
147 ],
15b11f47
CW
148 [
149 'name' => 'layout',
150 ],
151 ];
10fd70a3
TO
152
153 if ($self->getAction() === 'get') {
25f2b36b
CW
154 $fields[] = [
155 'name' => 'module_name',
156 ];
157 $fields[] = [
158 'name' => 'directive_name',
159 ];
10fd70a3
TO
160 $fields[] = [
161 'name' => 'has_local',
162 ];
163 $fields[] = [
2d4bfef1 164 'name' => 'has_base',
10fd70a3
TO
165 ];
166 }
167
168 return $fields;
c847b30c 169 }))->setCheckPermissions($checkPermissions);
15b11f47
CW
170 }
171
aff08e16
TO
172 /**
173 * @return array
174 */
175 public static function permissions() {
176 return [
177 "meta" => ["access CiviCRM"],
178 "default" => ["administer CiviCRM"],
3fe34185
CW
179 'prefill' => [],
180 'submit' => [],
aff08e16
TO
181 ];
182 }
7422a230
TO
183
184}