From b6e1397372f97612f86f7b28d7023a5512537ffe Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 9 Apr 2021 18:21:05 +1000 Subject: [PATCH] dev/core#2522 Add in Submit Action and redirect handling post form submit Move fields to the bottom of the config form and also rework the submission code as per suggestion from coleman and also ensure that the submit_action and redirect are exposed in the front end metadata Remove debugging Revmoe submit action field as no longer required Make improvements as per Coleman's review Update text as per Coleamn and fix regex to allos for ? = # & options in urls Fix Regex to do some wildcard matching --- .../admin/Civi/AfformAdmin/AfformAdminMeta.php | 1 - ext/afform/admin/ang/afGuiEditor/config-form.html | 13 ++++++++++++- ext/afform/core/Civi/Api4/Afform.php | 3 +++ ext/afform/core/afform.php | 7 ++++++- ext/afform/core/ang/af/afForm.component.js | 14 +++++++++++++- ext/afform/core/api/v3/Afform.php | 4 ++++ 6 files changed, 38 insertions(+), 4 deletions(-) diff --git a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php index ba860905b4..07812515a7 100644 --- a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php +++ b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php @@ -233,7 +233,6 @@ class AfformAdminMeta { 'description' => $perm['description'] ?? NULL, ]; } - $dateRanges = \CRM_Utils_Array::makeNonAssociative(\CRM_Core_OptionGroup::values('relative_date_filters'), 'id', 'label'); $data['dateRanges'] = array_merge([['id' => '{}', 'label' => E::ts('Choose Date Range')]], $dateRanges); diff --git a/ext/afform/admin/ang/afGuiEditor/config-form.html b/ext/afform/admin/ang/afGuiEditor/config-form.html index 40d9c33bf1..0bce83afd6 100644 --- a/ext/afform/admin/ang/afGuiEditor/config-form.html +++ b/ext/afform/admin/ang/afGuiEditor/config-form.html @@ -32,7 +32,7 @@ - +

{{:: ts('Expose the form as a standalone webpage. (Example: "civicrm/my-form")') }}

@@ -84,5 +84,16 @@

{{:: ts('Choose which contact from the search should match the contact being viewed.') }}

+
+ + {{:: ts('Submit Actions') }} +
+ + +

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

+
+
diff --git a/ext/afform/core/Civi/Api4/Afform.php b/ext/afform/core/Civi/Api4/Afform.php index dfcd80ef4b..f6212da302 100644 --- a/ext/afform/core/Civi/Api4/Afform.php +++ b/ext/afform/core/Civi/Api4/Afform.php @@ -171,6 +171,9 @@ class Afform extends Generic\AbstractEntity { [ 'name' => 'permission', ], + [ + 'name' => 'redirect', + ], [ 'name' => 'layout', 'data_type' => 'Array', diff --git a/ext/afform/core/afform.php b/ext/afform/core/afform.php index 08844f65b7..bebc5a7349 100644 --- a/ext/afform/core/afform.php +++ b/ext/afform/core/afform.php @@ -358,10 +358,15 @@ function afform_civicrm_buildAsset($asset, $params, &$mimeType, &$content) { } $moduleName = _afform_angular_module_name($params['name'], 'camel'); + $formMetaData = (array) civicrm_api4('Afform', 'get', [ + 'checkPermissions' => FALSE, + 'select' => ['redirect', 'name'], + 'where' => [['name', '=', $params['name']]], + ], 0); $smarty = CRM_Core_Smarty::singleton(); $smarty->assign('afform', [ 'camel' => $moduleName, - 'meta' => ['name' => $params['name']], + 'meta' => $formMetaData, 'templateUrl' => "~/$moduleName/$moduleName.aff.html", ]); $mimeType = 'text/javascript'; diff --git a/ext/afform/core/ang/af/afForm.component.js b/ext/afform/core/ang/af/afForm.component.js index d7b0d8fc4e..2c36a9ec07 100644 --- a/ext/afform/core/ang/af/afForm.component.js +++ b/ext/afform/core/ang/af/afForm.component.js @@ -4,7 +4,7 @@ bindings: { ctrl: '@' }, - controller: function($scope, $routeParams, $timeout, crmApi4, crmStatus) { + controller: function($scope, $routeParams, $timeout, crmApi4, crmStatus, $window, $location) { var schema = {}, data = {}, ctrl = this; @@ -54,6 +54,18 @@ this.submit = function submit() { var submission = crmApi4('Afform', 'submit', {name: ctrl.getFormMeta().name, args: $routeParams, values: data}); + var metaData = ctrl.getFormMeta(); + if (metaData.redirect) { + submission.then(function() { + var url = metaData.redirect; + if (url.indexOf('civicrm/') === 0) { + url = CRM.url(url); + } else if (url.indexOf('/') === 0) { + url = $location.protocol() + '://' + $location.host() + url; + } + $window.location.href = url; + }); + } return crmStatus({start: ts('Saving'), success: ts('Saved')}, submission); }; } diff --git a/ext/afform/core/api/v3/Afform.php b/ext/afform/core/api/v3/Afform.php index 66a235555a..41c20c63a4 100644 --- a/ext/afform/core/api/v3/Afform.php +++ b/ext/afform/core/api/v3/Afform.php @@ -73,6 +73,10 @@ function _civicrm_api3_afform_get_spec(&$fields) { 'title' => 'Public', 'type' => CRM_Utils_Type::T_BOOLEAN, ]; + $fields['redirect'] = [ + 'title' => 'Redirect URL', + 'type' => CRM_Utils_Type::T_STRING, + ]; } /** -- 2.25.1