Commit | Line | Data |
---|---|---|
7422a230 TO |
1 | <?php |
2 | ||
3 | namespace Civi\Api4; | |
4 | ||
f3062b8c TO |
5 | use Civi\Api4\Generic\AbstractEntity; |
6 | use Civi\Api4\Generic\BasicBatchAction; | |
7 | use Civi\Api4\Generic\BasicGetAction; | |
15b11f47 | 8 | use Civi\Api4\Generic\BasicGetFieldsAction; |
f3062b8c | 9 | use Civi\Api4\Generic\BasicUpdateAction; |
7422a230 TO |
10 | |
11 | /** | |
12 | * Class Afform | |
13 | * @package Civi\Api4 | |
7422a230 | 14 | */ |
f3062b8c | 15 | class Afform extends AbstractEntity { |
aff08e16 TO |
16 | |
17 | /** | |
0770b5ed | 18 | * @return \Civi\Api4\Generic\BasicGetAction |
aff08e16 TO |
19 | */ |
20 | public static function get() { | |
f3062b8c TO |
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 | }); | |
aff08e16 TO |
47 | } |
48 | ||
49 | /** | |
0770b5ed | 50 | * @return \Civi\Api4\Generic\BasicBatchAction |
aff08e16 TO |
51 | */ |
52 | public static function revert() { | |
f3062b8c TO |
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, | |
0770b5ed | 57 | \CRM_Afform_AfformScanner::LAYOUT_FILE, |
f3062b8c TO |
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 | }); | |
aff08e16 TO |
77 | } |
78 | ||
79 | /** | |
0770b5ed | 80 | * @return \Civi\Api4\Generic\BasicUpdateAction |
aff08e16 TO |
81 | */ |
82 | public static function update() { | |
0770b5ed | 83 | return new BasicUpdateAction('Afform', __FUNCTION__, 'name', function ($item, BasicUpdateAction $action) { |
f3062b8c TO |
84 | /** @var \CRM_Afform_AfformScanner $scanner */ |
85 | $scanner = \Civi::service('afform_scanner'); | |
86 | $converter = new \CRM_Afform_ArrayHtml(); | |
aff08e16 | 87 | |
f3062b8c TO |
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; | |
0770b5ed | 133 | }); |
f3062b8c | 134 | } |
aff08e16 | 135 | |
15b11f47 CW |
136 | public static function getFields() { |
137 | return new BasicGetFieldsAction('Afform', __FUNCTION__, function() { | |
138 | return [ | |
139 | [ | |
140 | 'name' => 'name', | |
141 | ], | |
142 | [ | |
143 | 'name' => 'requires', | |
144 | ], | |
145 | [ | |
146 | 'name' => 'title', | |
147 | ], | |
148 | [ | |
149 | 'name' => 'description', | |
150 | ], | |
151 | [ | |
152 | 'name' => 'is_public', | |
153 | 'data_type' => 'Boolean', | |
154 | ], | |
155 | [ | |
156 | 'name' => 'server_route', | |
157 | ], | |
158 | [ | |
159 | 'name' => 'layout', | |
160 | ], | |
161 | ]; | |
162 | }); | |
163 | } | |
164 | ||
aff08e16 TO |
165 | /** |
166 | * @return array | |
167 | */ | |
168 | public static function permissions() { | |
169 | return [ | |
170 | "meta" => ["access CiviCRM"], | |
171 | "default" => ["administer CiviCRM"], | |
172 | ]; | |
173 | } | |
7422a230 TO |
174 | |
175 | } |