3 namespace Civi\Api4\Utils
;
6 * Class AfformSaveTrait
7 * @package Civi\Api4\Action\Afform
9 trait AfformSaveTrait
{
11 use AfformFormatTrait
;
13 protected function writeRecord($item) {
14 /** @var \CRM_Afform_AfformScanner $scanner */
15 $scanner = \Civi
::service('afform_scanner');
17 // If no name given, create a unique name based on the title
18 if (empty($item['name'])) {
19 $prefix = !empty($item['join']) ?
"afjoin-{$item['join']}" : (!empty($item['block']) ?
('afblock-' . str_replace('*', 'all', $item['block'])) : 'afform');
20 $item['name'] = _afform_angular_module_name($prefix . '-' . \CRM_Utils_String
::munge($item['title'], '-'));
23 file_exists($scanner->createSiteLocalPath($item['name'] . $suffix, \CRM_Afform_AfformScanner
::METADATA_FILE
))
24 ||
file_exists($scanner->createSiteLocalPath($item['name'] . $suffix, 'aff.html'))
28 $item['name'] .= $suffix;
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.");
35 // Fetch existing metadata
36 $fields = \Civi\Api4\Afform
::getfields()->setCheckPermissions(FALSE)->setAction('create')->addSelect('name')->execute()->column('name');
37 unset($fields[array_search('layout', $fields)]);
38 $orig = \Civi\Api4\Afform
::get()->setCheckPermissions(FALSE)->addWhere('name', '=', $item['name'])->setSelect($fields)->execute()->first();
41 // FIXME validate all field data.
42 $item = _afform_fields_filter($item);
44 // Create or update aff.html.
45 if (isset($item['layout'])) {
46 $layoutPath = $scanner->createSiteLocalPath($item['name'], 'aff.html');
47 \CRM_Utils_File
::createDir(dirname($layoutPath));
48 file_put_contents($layoutPath, $this->convertInputToHtml($item['layout']));
49 // FIXME check for writability then success. Report errors.
52 $meta = $item +
(array) $orig;
53 unset($meta['layout'], $meta['name']);
55 $metaPath = $scanner->createSiteLocalPath($item['name'], \CRM_Afform_AfformScanner
::METADATA_FILE
);
56 \CRM_Utils_File
::createDir(dirname($metaPath));
57 file_put_contents($metaPath, json_encode($meta, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
));
58 // FIXME check for writability then success. Report errors.
61 // We may have changed list of files covered by the cache.
64 $isChanged = function($field) use ($item, $orig) {
65 return ($item[$field] ??
NULL) !== ($orig[$field] ??
NULL);
68 if ($isChanged('is_dashlet')) {
69 // FIXME: more targetted reconciliation
70 \CRM_Core_ManagedEntities
::singleton()->reconcile();
72 elseif ($orig['is_dashlet'] && $isChanged('title')) {
73 // FIXME: more targetted reconciliation
74 \CRM_Core_ManagedEntities
::singleton()->reconcile();
77 // Right now, permission-checks are completely on-demand.
78 if ($isChanged('server_route') /* || $isChanged('permission') */) {
79 \CRM_Core_Menu
::store();
80 \CRM_Core_BAO_Navigation
::resetNavigation();
82 // FIXME if asset-caching is enabled, then flush the asset cache.
84 $item['module_name'] = _afform_angular_module_name($item['name'], 'camel');
85 $item['directive_name'] = _afform_angular_module_name($item['name'], 'dash');