Commit | Line | Data |
---|---|---|
28b4ace4 CW |
1 | <?php |
2 | ||
3 | namespace Civi\Api4\Utils; | |
4 | ||
5 | /** | |
6 | * Class AfformSaveTrait | |
7 | * @package Civi\Api4\Action\Afform | |
8 | */ | |
9 | trait AfformSaveTrait { | |
10 | ||
11 | use AfformFormatTrait; | |
12 | ||
13 | protected function writeRecord($item) { | |
14 | /** @var \CRM_Afform_AfformScanner $scanner */ | |
15 | $scanner = \Civi::service('afform_scanner'); | |
16 | ||
28b4ace4 | 17 | // If no name given, create a unique name based on the title |
2f663f46 | 18 | if (empty($item['name'])) { |
6b8f5a2a | 19 | $prefix = !empty($item['join']) ? "afjoin-{$item['join']}" : (!empty($item['block']) ? ('afblock-' . str_replace('*', 'all', $item['block'])) : 'afform'); |
43d8dd79 | 20 | $item['name'] = _afform_angular_module_name($prefix . '-' . \CRM_Utils_String::munge($item['title'], '-')); |
28b4ace4 CW |
21 | $suffix = ''; |
22 | while ( | |
2f663f46 CW |
23 | file_exists($scanner->createSiteLocalPath($item['name'] . $suffix, \CRM_Afform_AfformScanner::METADATA_FILE)) |
24 | || file_exists($scanner->createSiteLocalPath($item['name'] . $suffix, 'aff.html')) | |
28b4ace4 CW |
25 | ) { |
26 | $suffix++; | |
27 | } | |
2f663f46 | 28 | $item['name'] .= $suffix; |
50868e8d | 29 | $orig = NULL; |
28b4ace4 | 30 | } |
48dde125 CW |
31 | elseif (!preg_match('/^[a-zA-Z][-_a-zA-Z0-9]*$/', $item['name'])) { |
32 | throw new \API_Exception("Afform.{$this->getActionName()}: name should begin with a letter and only contain alphanumerics underscores and dashes."); | |
28b4ace4 | 33 | } |
50868e8d CW |
34 | else { |
35 | // Fetch existing metadata | |
e38db494 | 36 | $fields = \Civi\Api4\Afform::getfields()->setCheckPermissions(FALSE)->setAction('create')->addSelect('name')->execute()->column('name'); |
50868e8d | 37 | unset($fields[array_search('layout', $fields)]); |
50868e8d CW |
38 | $orig = \Civi\Api4\Afform::get()->setCheckPermissions(FALSE)->addWhere('name', '=', $item['name'])->setSelect($fields)->execute()->first(); |
39 | } | |
28b4ace4 CW |
40 | |
41 | // FIXME validate all field data. | |
50868e8d | 42 | $item = _afform_fields_filter($item); |
28b4ace4 CW |
43 | |
44 | // Create or update aff.html. | |
50868e8d | 45 | if (isset($item['layout'])) { |
2f663f46 | 46 | $layoutPath = $scanner->createSiteLocalPath($item['name'], 'aff.html'); |
28b4ace4 | 47 | \CRM_Utils_File::createDir(dirname($layoutPath)); |
50868e8d | 48 | file_put_contents($layoutPath, $this->convertInputToHtml($item['layout'])); |
28b4ace4 CW |
49 | // FIXME check for writability then success. Report errors. |
50 | } | |
51 | ||
50868e8d CW |
52 | $meta = $item + (array) $orig; |
53 | unset($meta['layout'], $meta['name']); | |
28b4ace4 | 54 | if (!empty($meta)) { |
2f663f46 | 55 | $metaPath = $scanner->createSiteLocalPath($item['name'], \CRM_Afform_AfformScanner::METADATA_FILE); |
28b4ace4 | 56 | \CRM_Utils_File::createDir(dirname($metaPath)); |
22f49e3e | 57 | file_put_contents($metaPath, json_encode($meta, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
28b4ace4 CW |
58 | // FIXME check for writability then success. Report errors. |
59 | } | |
60 | ||
61 | // We may have changed list of files covered by the cache. | |
62 | _afform_clear(); | |
63 | ||
bca1e4f8 TO |
64 | $isChanged = function($field) use ($item, $orig) { |
65 | return ($item[$field] ?? NULL) !== ($orig[$field] ?? NULL); | |
66 | }; | |
521b232a TO |
67 | |
68 | if ($isChanged('is_dashlet')) { | |
69 | // FIXME: more targetted reconciliation | |
70 | \CRM_Core_ManagedEntities::singleton()->reconcile(); | |
71 | } | |
aabf7fff | 72 | elseif (array_key_exists('is_dashlet', (array) $orig) && $orig['is_dashlet'] && $isChanged('title')) { |
521b232a TO |
73 | // FIXME: more targetted reconciliation |
74 | \CRM_Core_ManagedEntities::singleton()->reconcile(); | |
75 | } | |
76 | ||
bca1e4f8 TO |
77 | // Right now, permission-checks are completely on-demand. |
78 | if ($isChanged('server_route') /* || $isChanged('permission') */) { | |
28b4ace4 CW |
79 | \CRM_Core_Menu::store(); |
80 | \CRM_Core_BAO_Navigation::resetNavigation(); | |
81 | } | |
82 | // FIXME if asset-caching is enabled, then flush the asset cache. | |
83 | ||
e38db494 CW |
84 | $item['module_name'] = _afform_angular_module_name($item['name'], 'camel'); |
85 | $item['directive_name'] = _afform_angular_module_name($item['name'], 'dash'); | |
50868e8d | 86 | return $meta + $item; |
28b4ace4 CW |
87 | } |
88 | ||
89 | } |