From aea64f2086e46ac8fbba73a0e59aba0b1a0c0e4d Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 18 Jul 2022 19:54:47 -0400 Subject: [PATCH] Afform - Post-submit redirect tokens --- ext/afform/admin/ang/afGuiEditor.css | 4 ++ .../ang/afGuiEditor/afGuiTokenSelect.html | 6 +++ .../admin/ang/afGuiEditor/afGuiTokenSelect.js | 52 +++++++++++++++++++ .../admin/ang/afGuiEditor/config-form.html | 5 +- .../core/Civi/Api4/Action/Afform/Submit.php | 4 +- ext/afform/core/ang/af/afForm.component.js | 22 +++++++- 6 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 ext/afform/admin/ang/afGuiEditor/afGuiTokenSelect.html create mode 100644 ext/afform/admin/ang/afGuiEditor/afGuiTokenSelect.js diff --git a/ext/afform/admin/ang/afGuiEditor.css b/ext/afform/admin/ang/afGuiEditor.css index 585a397845..3aa53d4bbc 100644 --- a/ext/afform/admin/ang/afGuiEditor.css +++ b/ext/afform/admin/ang/afGuiEditor.css @@ -483,6 +483,10 @@ body.af-gui-dragging { right: 0; } +#afGuiEditor .input-group-addon { + padding: 0; +} + #afGuiEditor .af-gui-text-h1 { font-weight: bolder; font-size: 16px; diff --git a/ext/afform/admin/ang/afGuiEditor/afGuiTokenSelect.html b/ext/afform/admin/ang/afGuiEditor/afGuiTokenSelect.html new file mode 100644 index 0000000000..1f37c9aab4 --- /dev/null +++ b/ext/afform/admin/ang/afGuiEditor/afGuiTokenSelect.html @@ -0,0 +1,6 @@ + + + diff --git a/ext/afform/admin/ang/afGuiEditor/afGuiTokenSelect.js b/ext/afform/admin/ang/afGuiEditor/afGuiTokenSelect.js new file mode 100644 index 0000000000..30e349e51f --- /dev/null +++ b/ext/afform/admin/ang/afGuiEditor/afGuiTokenSelect.js @@ -0,0 +1,52 @@ +(function(angular, $, _) { + "use strict"; + + angular.module('afGuiEditor').component('afGuiTokenSelect', { + bindings: { + model: '<', + field: '@' + }, + require: { + editor: '^afGuiEditor' + }, + templateUrl: '~/afGuiEditor/afGuiTokenSelect.html', + controller: function ($scope, $element) { + var ts = $scope.ts = CRM.ts('org.civicrm.afform_admin'), + ctrl = this; + + this.$onInit = function() { + // Because this widget is so small, some placeholder text is helpful once it's open + $element.on('select2-open', function() { + $('#select2-drop > .select2-search > input').attr('placeholder', ts('Insert Token')); + }); + }; + + this.insertToken = function(key) { + ctrl.model[ctrl.field] = (ctrl.model[ctrl.field] || '') + '[' + key + ']'; + }; + + this.getTokens = function() { + var tokens = _.transform(ctrl.editor.getEntities(), function(tokens, entity) { + tokens.push({id: entity.name + '.0.id', text: entity.label + ' ' + ts('ID')}); + }, []); + tokens.push({id: 'token', text: ts('Submission JWT')}); + return { + results: tokens + }; + }; + + this.tokenSelectSettings = { + data: this.getTokens, + // The crm-action-menu icon doesn't show without a placeholder + placeholder: ' ', + // Make this widget very compact + width: '52px', + containerCss: {minWidth: '52px'}, + // Make the dropdown wider than the widget + dropdownCss: {width: '250px'} + }; + + } + }); + +})(angular, CRM.$, CRM._); diff --git a/ext/afform/admin/ang/afGuiEditor/config-form.html b/ext/afform/admin/ang/afGuiEditor/config-form.html index ca85a02cb1..aa7563f09a 100644 --- a/ext/afform/admin/ang/afGuiEditor/config-form.html +++ b/ext/afform/admin/ang/afGuiEditor/config-form.html @@ -95,7 +95,10 @@ - +
+ + +

{{:: ts('Enter a URL or path that the form should redirect to following a successful submission.') }}

diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php index 068fc657db..3e453c52ea 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php @@ -78,9 +78,7 @@ class Submit extends AbstractProcessor { // Return ids and a token for uploading files return [ - [ - 'token' => $this->generatePostSubmitToken(), - ], + ['token' => $this->generatePostSubmitToken()] + $this->_entityIds, ]; } diff --git a/ext/afform/core/ang/af/afForm.component.js b/ext/afform/core/ang/af/afForm.component.js index 7abbf65a59..79514948a4 100644 --- a/ext/afform/core/ang/af/afForm.component.js +++ b/ext/afform/core/ang/af/afForm.component.js @@ -4,11 +4,12 @@ bindings: { ctrl: '@' }, - controller: function($scope, $element, $timeout, crmApi4, crmStatus, $window, $location, FileUploader) { + controller: function($scope, $element, $timeout, crmApi4, crmStatus, $window, $location, $parse, FileUploader) { var schema = {}, data = {}, status, args, + submissionResponse, ctrl = this; this.$onInit = function() { @@ -84,7 +85,7 @@ } else if (metaData.redirect) { - var url = metaData.redirect; + var url = replaceTokens(metaData.redirect, submissionResponse[0]); if (url.indexOf('civicrm/') === 0) { url = CRM.url(url); } else if (url.indexOf('/') === 0) { @@ -94,6 +95,22 @@ } } + function replaceTokens(str, vars) { + function recurse(stack, values) { + _.each(values, function(value, key) { + console.log('value:' + value, stack); + if (_.isArray(value) || _.isPlainObject(value)) { + recurse(stack.concat([key]), value); + } else { + var token = (stack.length ? stack.join('.') + '.' : '') + key; + str = str.replace(new RegExp(_.escapeRegExp('[' + token + ']'), 'g'), value); + } + }); + } + recurse([], vars); + return str; + } + this.submit = function() { status = CRM.status({}); $element.block(); @@ -103,6 +120,7 @@ args: args, values: data} ).then(function(response) { + submissionResponse = response; if (ctrl.fileUploader.getNotUploadedItems().length) { _.each(ctrl.fileUploader.getNotUploadedItems(), function(file) { file.formData.push({ -- 2.25.1