/** @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.");
}
// 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.
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));
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);
});
};
<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>