dev/core#2522 Add in Submit Action and redirect handling post form submit
authorSeamus Lee <seamuslee001@gmail.com>
Fri, 9 Apr 2021 08:21:05 +0000 (18:21 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Fri, 16 Apr 2021 03:37:07 +0000 (13:37 +1000)
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

ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php
ext/afform/admin/ang/afGuiEditor/config-form.html
ext/afform/core/Civi/Api4/Afform.php
ext/afform/core/afform.php
ext/afform/core/ang/af/afForm.component.js
ext/afform/core/api/v3/Afform.php

index ba860905b40ae7366f8a70fb99899a45b4c9cdab..07812515a79f10321dd5fdd9d4f78b4689f5fb00 100644 (file)
@@ -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);
 
index 40d9c33bf1ca515cd4ac1a1b1d17786f0332b412..0bce83afd6d3ebd8d6f87fdfe4f3ae106cd55f78 100644 (file)
@@ -32,7 +32,7 @@
       <label for="af_config_form_server_route">
         {{:: ts('Page:') }}
       </label>
-      <input ng-model="afform.server_route" name="server_route" class="form-control" id="af_config_form_server_route" pattern="^civicrm\/[-0-9a-zA-z\/_]+$" onfocus="this.value = this.value || 'civicrm/'" onblur="if (this.value === 'civicrm/') this.value = ''" title="{{:: ts('Path must begin with &quot;civicrm/&quot;') }}">
+      <input ng-model="afform.server_route" name="server_route" class="form-control" id="af_config_form_server_route" pattern="^civicrm\/[-0-9a-zA-Z\/_]+$" onfocus="this.value = this.value || 'civicrm/'" onblur="if (this.value === 'civicrm/') this.value = ''" title="{{:: ts('Path must begin with &quot;civicrm/&quot;') }}">
       <p class="help-block">{{:: ts('Expose the form as a standalone webpage. (Example: "civicrm/my-form")') }}</p>
     </div>
 
       <p class="help-block">{{:: ts('Choose which contact from the search should match the contact being viewed.') }}</p>
     </div>
   </fieldset>
+  <fieldset>
+
+    <legend>{{:: ts('Submit Actions') }}</legend>
 
+    <div class="form-group" ng-class="{'has-error': !!config_form.redirect.$error.pattern}">
+      <label for="af_config_redirect">
+        {{:: ts('Post-Submit Page') }}
+      </label>
+      <input ng-model="afform.redirect" name="redirect" class="form-control" id="af_config_redirect" title="{{:: ts('Post-Submit Page') }}" pattern="^((http|https):\/\/|\/|civicrm\/)[-0-9a-zA-Z\/_.]\S+$" title="{{:: ts('Post-Submit Page must be either an absolute url, a relative url or a path starting with CiviCRM') }}"/>
+      <p class="help-block">{{:: ts('Enter a URL or path that the form should redirect to following a successful submission.') }}</p>
+    </div>
+  </fieldset>
 </ng-form>
index dfcd80ef4b9f53be0317ddbc6fb7111054b85143..f6212da3020db5b8765c37636c0a3634261779bc 100644 (file)
@@ -171,6 +171,9 @@ class Afform extends Generic\AbstractEntity {
         [
           'name' => 'permission',
         ],
+        [
+          'name' => 'redirect',
+        ],
         [
           'name' => 'layout',
           'data_type' => 'Array',
index 08844f65b7f448396012a061077cadbe9656474a..bebc5a7349d1f3c8133cc22719d950b4130532ae 100644 (file)
@@ -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';
index d7b0d8fc4e0cb2c8ef736f8e1720f1b37af3c86b..2c36a9ec077aa9c9f2c8b4eef5ff81b041fa94f8 100644 (file)
@@ -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;
 
       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);
       };
     }
index 66a235555a0f16d2f0a7bdeca276f2fda28bfa3f..41c20c63a48703177bf26f001444faec0b68fb00 100644 (file)
@@ -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,
+  ];
 }
 
 /**