Add getFields to api4 classes
[civicrm-core.git] / ext / afform / core / Civi / Api4 / Afform.php
1 <?php
2
3 namespace Civi\Api4;
4
5 use Civi\Api4\Generic\AbstractEntity;
6 use Civi\Api4\Generic\BasicBatchAction;
7 use Civi\Api4\Generic\BasicGetAction;
8 use Civi\Api4\Generic\BasicGetFieldsAction;
9 use Civi\Api4\Generic\BasicUpdateAction;
10
11 /**
12 * Class Afform
13 * @package Civi\Api4
14 */
15 class Afform extends AbstractEntity {
16
17 /**
18 * @return BasicGetAction
19 */
20 public static function get() {
21 return new BasicGetAction('Afform', __FUNCTION__, function(BasicGetAction $action) {
22 /** @var \CRM_Afform_AfformScanner $scanner */
23 $scanner = \Civi::service('afform_scanner');
24 $converter = new \CRM_Afform_ArrayHtml();
25
26 $where = $action->getWhere();
27 if (count($where) === 1 && $where[0][0] === 'name' && $where[0][1] == '=') {
28 $names = [$where[0][2]];
29 }
30 else {
31 $names = array_keys($scanner->findFilePaths());
32 }
33
34 $values = [];
35 foreach ($names as $name) {
36 $record = $scanner->getMeta($name);
37 $layout = $scanner->findFilePath($name, 'aff.html');
38 if ($layout) {
39 // FIXME check for file existence+substance+validity
40 $record['layout'] = $converter->convertHtmlToArray(file_get_contents($layout));
41 }
42 $values[] = $record;
43 }
44
45 return $values;
46 });
47 }
48
49 /**
50 * @return BasicBatchAction
51 */
52 public static function revert() {
53 return new BasicBatchAction('Afform', __FUNCTION__, ['name'], function($item, BasicBatchAction $action) {
54 $scanner = \Civi::service('afform_scanner');
55 $files = [
56 \CRM_Afform_AfformScanner::METADATA_FILE,
57 \CRM_Afform_AfformScanner::LAYOUT_FILE
58 ];
59
60 foreach ($files as $file) {
61 $metaPath = $scanner->createSiteLocalPath($item['name'], $file);
62 if (file_exists($metaPath)) {
63 if (!@unlink($metaPath)) {
64 throw new \API_Exception("Failed to remove afform overrides in $file");
65 }
66 }
67 }
68
69 // We may have changed list of files covered by the cache.
70 $scanner->clear();
71
72 // FIXME if `server_route` changes, then flush the menu cache.
73 // FIXME if asset-caching is enabled, then flush the asset cache
74
75 return $item;
76 });
77 }
78
79 /**
80 * @return BasicUpdateAction
81 */
82 public static function update() {
83 $save = function ($item, BasicUpdateAction $action) {
84 /** @var \CRM_Afform_AfformScanner $scanner */
85 $scanner = \Civi::service('afform_scanner');
86 $converter = new \CRM_Afform_ArrayHtml();
87
88 if (empty($item['name']) || !preg_match('/^[a-zA-Z][a-zA-Z0-9\-]*$/', $item['name'])) {
89 throw new \API_Exception("Afform.create: name is a mandatory field. It should use alphanumerics and dashes.");
90 }
91 $name = $item['name'];
92
93 // FIXME validate all field data.
94 $updates = _afform_fields_filter($item);
95
96 // Create or update aff.html.
97 if (isset($updates['layout'])) {
98 $layoutPath = $scanner->createSiteLocalPath($name, 'aff.html');
99 \ CRM_Utils_File::createDir(dirname($layoutPath));
100 file_put_contents($layoutPath, $converter->convertArrayToHtml($updates['layout']));
101 // FIXME check for writability then success. Report errors.
102 }
103
104 // Create or update *.aff.json.
105 $orig = \Civi\Api4\Afform::get()
106 ->setCheckPermissions($action->getCheckPermissions())
107 ->addWhere('name', '=', $name)
108 ->execute();
109
110 if (isset($orig[0])) {
111 $meta = _afform_fields_filter(array_merge($orig[0], $updates));
112 }
113 else {
114 $meta = $updates;
115 }
116 unset($meta['layout']);
117 unset($meta['name']);
118 if (!empty($meta)) {
119 $metaPath = $scanner->createSiteLocalPath($name, \CRM_Afform_AfformScanner::METADATA_FILE);
120 // printf("[%s] Update meta %s: %s\n", $name, $metaPath, print_R(['updates'=>$updates, 'meta'=>$meta], 1));
121 \CRM_Utils_File::createDir(dirname($metaPath));
122 file_put_contents($metaPath, json_encode($meta, JSON_PRETTY_PRINT));
123 // FIXME check for writability then success. Report errors.
124 }
125
126 // We may have changed list of files covered by the cache.
127 $scanner->clear();
128
129 // FIXME if `server_route` changes, then flush the menu cache.
130 // FIXME if asset-caching is enabled, then flush the asset cache.
131
132 return $updates;
133 };
134 return new BasicUpdateAction('Afform', __FUNCTION__, $save, 'name');
135 }
136
137 public static function getFields() {
138 return new BasicGetFieldsAction('Afform', __FUNCTION__, function() {
139 return [
140 [
141 'name' => 'name',
142 ],
143 [
144 'name' => 'requires',
145 ],
146 [
147 'name' => 'title',
148 ],
149 [
150 'name' => 'description',
151 ],
152 [
153 'name' => 'is_public',
154 'data_type' => 'Boolean',
155 ],
156 [
157 'name' => 'server_route',
158 ],
159 [
160 'name' => 'layout',
161 ],
162 ];
163 });
164 }
165
166 /**
167 * @return array
168 */
169 public static function permissions() {
170 return [
171 "meta" => ["access CiviCRM"],
172 "default" => ["administer CiviCRM"],
173 ];
174 }
175
176 }