Skip to content
Snippets Groups Projects
Commit b6e13973 authored by Seamus Lee's avatar Seamus Lee
Browse files

#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
parent b5c4d6c3
Branches
Tags
No related merge requests found
......@@ -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);
......
......@@ -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>
......@@ -84,5 +84,16 @@
<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>
......@@ -171,6 +171,9 @@ class Afform extends Generic\AbstractEntity {
[
'name' => 'permission',
],
[
'name' => 'redirect',
],
[
'name' => 'layout',
'data_type' => 'Array',
......
......@@ -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';
......
......@@ -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);
};
}
......
......@@ -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,
];
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment