GUI: Correctly handle naming a newly created form
authorColeman Watts <coleman@civicrm.org>
Tue, 26 Nov 2019 14:13:49 +0000 (09:13 -0500)
committerCiviCRM <info@civicrm.org>
Wed, 16 Sep 2020 02:13:20 +0000 (19:13 -0700)
ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php
ext/afform/gui/ang/afGuiEditor.js
ext/afform/gui/ang/afGuiEditor/canvas.html

index 6322c50d806388907f016d75d2b46d32f8294327..ff0ada980e4450d592f6becd15c3576cd49614a9 100644 (file)
@@ -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));
index 7ad8e075ede3ce9895863311503ebe04a0a1c6a9..4428b72bb892597cb05a6e1a8ba1fcc155ab197c 100644 (file)
@@ -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',
         };
 
         $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);
             });
         };
 
index 37d7967506f6a125484d0e43a5f1929aa1d5ae31..2d116faac87ce37a1fd06c1c6958f5b26f24be34 100644 (file)
@@ -4,7 +4,7 @@
       <div class="btn-group btn-group-md pull-right">
         <button class="btn" ng-class="{'btn-primary': !changesSaved && !saving, 'btn-warning': saving, 'btn-success': changesSaved}" ng-disabled="changesSaved || saving" ng-click="save()">
           <i class="crm-i" ng-class="{'fa-check': !saving, 'fa-spin fa-spinner': saving}"></i>
-          <span ng-if="changesSaved">{{ ts('Saved') }}</span>
+          <span ng-if="changesSaved && !saving">{{ ts('Saved') }}</span>
           <span ng-if="!changesSaved && !saving">{{ ts('Save') }}</span>
           <span ng-if="saving">{{ ts('Saving...') }}</span>
         </button>