Merge pull request #18523 from civicrm/5.30
[civicrm-core.git] / ext / afform / core / Civi / Api4 / Utils / AfformSaveTrait.php
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
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'], '-'));
21 $suffix = '';
22 while (
23 file_exists($scanner->createSiteLocalPath($item['name'] . $suffix, \CRM_Afform_AfformScanner::METADATA_FILE))
24 || file_exists($scanner->createSiteLocalPath($item['name'] . $suffix, 'aff.html'))
25 ) {
26 $suffix++;
27 }
28 $item['name'] .= $suffix;
29 $orig = NULL;
30 }
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.");
33 }
34 else {
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();
39 }
40
41 // FIXME validate all field data.
42 $item = _afform_fields_filter($item);
43
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.
50 }
51
52 $meta = $item + (array) $orig;
53 unset($meta['layout'], $meta['name']);
54 if (!empty($meta)) {
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.
59 }
60
61 // We may have changed list of files covered by the cache.
62 _afform_clear();
63
64 $isChanged = function($field) use ($item, $orig) {
65 return ($item[$field] ?? NULL) !== ($orig[$field] ?? NULL);
66 };
67 // Right now, permission-checks are completely on-demand.
68 if ($isChanged('server_route') /* || $isChanged('permission') */) {
69 \CRM_Core_Menu::store();
70 \CRM_Core_BAO_Navigation::resetNavigation();
71 }
72 // FIXME if asset-caching is enabled, then flush the asset cache.
73
74 $item['module_name'] = _afform_angular_module_name($item['name'], 'camel');
75 $item['directive_name'] = _afform_angular_module_name($item['name'], 'dash');
76 return $meta + $item;
77 }
78
79 }