From: Coleman Watts Date: Tue, 26 Nov 2019 14:13:49 +0000 (-0500) Subject: GUI: Correctly handle naming a newly created form X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=2f663f462b0048e901982a6d92fd3ad7ea94ef43;p=civicrm-core.git GUI: Correctly handle naming a newly created form --- diff --git a/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php b/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php index 6322c50d80..ff0ada980e 100644 --- a/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php +++ b/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php @@ -14,20 +14,19 @@ trait AfformSaveTrait { /** @var \CRM_Afform_AfformScanner $scanner */ $scanner = \Civi::service('afform_scanner'); - $name = $item['name'] ?? NULL; // If no name given, create a unique name based on the title - if (!$name) { - $name = \CRM_Utils_String::munge($item['title'], '-'); + if (empty($item['name'])) { + $item['name'] = _afform_angular_module_name(\CRM_Utils_String::munge($item['title'], '-')); $suffix = ''; while ( - file_exists($scanner->createSiteLocalPath($name . $suffix, \CRM_Afform_AfformScanner::METADATA_FILE)) - || file_exists($scanner->createSiteLocalPath($name . $suffix, 'aff.html')) + file_exists($scanner->createSiteLocalPath($item['name'] . $suffix, \CRM_Afform_AfformScanner::METADATA_FILE)) + || file_exists($scanner->createSiteLocalPath($item['name'] . $suffix, 'aff.html')) ) { $suffix++; } - $name .= $suffix; + $item['name'] .= $suffix; } - elseif (!preg_match('/^[a-zA-Z][a-zA-Z0-9\-]*$/', $name)) { + elseif (!preg_match('/^[a-zA-Z][a-zA-Z0-9\-]*$/', $item['name'])) { throw new \API_Exception("Afform.{$this->getActionName()}: name should use alphanumerics and dashes."); } @@ -36,7 +35,7 @@ trait AfformSaveTrait { // Create or update aff.html. if (isset($updates['layout'])) { - $layoutPath = $scanner->createSiteLocalPath($name, 'aff.html'); + $layoutPath = $scanner->createSiteLocalPath($item['name'], 'aff.html'); \CRM_Utils_File::createDir(dirname($layoutPath)); file_put_contents($layoutPath, $this->convertInputToHtml($updates['layout'])); // FIXME check for writability then success. Report errors. @@ -47,9 +46,9 @@ trait AfformSaveTrait { unset($meta['layout']); unset($meta['name']); if (!empty($meta)) { - $metaPath = $scanner->createSiteLocalPath($name, \CRM_Afform_AfformScanner::METADATA_FILE); + $metaPath = $scanner->createSiteLocalPath($item['name'], \CRM_Afform_AfformScanner::METADATA_FILE); if (file_exists($metaPath)) { - $orig = $scanner->getMeta($name); + $orig = $scanner->getMeta($item['name']); } \CRM_Utils_File::createDir(dirname($metaPath)); file_put_contents($metaPath, json_encode($meta, JSON_PRETTY_PRINT)); diff --git a/ext/afform/gui/ang/afGuiEditor.js b/ext/afform/gui/ang/afGuiEditor.js index 7ad8e075ed..4428b72bb8 100644 --- a/ext/afform/gui/ang/afGuiEditor.js +++ b/ext/afform/gui/ang/afGuiEditor.js @@ -4,7 +4,7 @@ var editingIcon; - angular.module('afGuiEditor').directive('afGuiEditor', function(crmApi4, $parse, $timeout) { + angular.module('afGuiEditor').directive('afGuiEditor', function(crmApi4, $parse, $timeout, $location) { return { restrict: 'A', templateUrl: '~/afGuiEditor/main.html', @@ -152,13 +152,13 @@ }; $scope.save = function() { - $scope.saving = true; - CRM.api4('Afform', 'save', {records: [JSON.parse(angular.toJson($scope.afform))]}) - .then(function () { - $scope.$apply(function () { - $scope.saving = false; - $scope.changesSaved = true; - }); + $scope.saving = $scope.changesSaved = true; + crmApi4('Afform', 'save', {records: [JSON.parse(angular.toJson($scope.afform))]}) + .then(function (data) { + $scope.saving = false; + $scope.afform.name = data[0].name; + // FIXME: This causes an unnecessary reload when saving a new form + $location.search('name', data[0].name); }); }; diff --git a/ext/afform/gui/ang/afGuiEditor/canvas.html b/ext/afform/gui/ang/afGuiEditor/canvas.html index 37d7967506..2d116faac8 100644 --- a/ext/afform/gui/ang/afGuiEditor/canvas.html +++ b/ext/afform/gui/ang/afGuiEditor/canvas.html @@ -4,7 +4,7 @@