From 21fb26b8e69abb46d86cd881dcb0215b4fe13bb0 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 11 Feb 2015 16:32:46 -0800 Subject: [PATCH] CRM-15856 - Enforce required tokens in UI --- js/angular-crmMailing.js | 31 +++++++++++++++++++++ js/angular-crmMailing/services.js | 39 +++++++++++++++++++++++++++ partials/crmMailing/body_html.html | 7 +++-- partials/crmMailing/body_text.html | 5 +++- partials/crmMailing/headerFooter.html | 4 ++- 5 files changed, 82 insertions(+), 4 deletions(-) diff --git a/js/angular-crmMailing.js b/js/angular-crmMailing.js index ed5321d7b0..ea8733a8ed 100644 --- a/js/angular-crmMailing.js +++ b/js/angular-crmMailing.js @@ -431,4 +431,35 @@ angular.module('crmMailing').controller('EmailAddrCtrl', function EmailAddrCtrl($scope, crmFromAddresses) { $scope.crmFromAddresses = crmFromAddresses; }); + + var lastEmailTokenAlert = null; + angular.module('crmMailing').controller('EmailBodyCtrl', function EmailBodyCtrl($scope, crmMailingMgr) { + var ts = CRM.ts(null); + + $scope.hasAllTokens = function hasMissingTokens(mailing, field) { + return _.isEmpty(crmMailingMgr.findMissingTokens(mailing, field)); + }; + + $scope.checkTokens = function checkTokens(mailing) { + if (lastEmailTokenAlert) { + lastEmailTokenAlert.close(); + } + var missing = angular.extend( + {}, + crmMailingMgr.findMissingTokens(mailing, 'body_html'), + crmMailingMgr.findMissingTokens(mailing, 'body_text') + ); + if (!_.isEmpty(missing) > 0) { + var buf = '

' + + ts('Before submitting this mailing, you must include an address token and an action token as part of the mailing body, mailing header, or mailing footer.') + + '

'; + lastEmailTokenAlert = CRM.alert(buf, undefined, 'error'); + } + } + }); })(angular, CRM.$, CRM._); diff --git a/js/angular-crmMailing/services.js b/js/angular-crmMailing/services.js index 01b04d3287..3c43dcdaf8 100644 --- a/js/angular-crmMailing/services.js +++ b/js/angular-crmMailing/services.js @@ -181,6 +181,45 @@ } }, + // Search the body, header, and footer for required tokens. + // ex: var msgs = findMissingTokens(mailing, 'body_html'); + findMissingTokens: function(mailing, field) { + var missing = {}; + if (!_.isEmpty(mailing[field])) { + var body = ''; + if (mailing.footer_id) { + var footer = _.where(CRM.crmMailing.headerfooterList, {id: mailing.footer_id}); + body = body + footer[0][field]; + + } + body = body + mailing[field]; + if (mailing.header_id) { + var header = _.where(CRM.crmMailing.headerfooterList, {id: mailing.header_id}); + body = body + header[0][field]; + } + + angular.forEach(CRM.crmMailing.requiredTokens, function(value, token) { + if (!_.isObject(value)) { + if (body.indexOf('{' + token + '}') < 0) { + missing[token] = value; + } + } + else { + var count = 0; + angular.forEach(value, function(nestedValue, nestedToken) { + if (body.indexOf('{' + nestedToken + '}') >= 0) { + count++; + } + }); + if (count == 0) { + angular.extend(missing, value); + } + } + }); + } + return missing; + }, + // Copy all data fields in (mailingFrom) to (mailingTgt) -- except for (excludes) // ex: crmMailingMgr.mergeInto(newMailing, mailingTemplate, ['subject']); mergeInto: function mergeInto(mailingTgt, mailingFrom, excludes) { diff --git a/partials/crmMailing/body_html.html b/partials/crmMailing/body_html.html index 6805929dad..1953d51291 100644 --- a/partials/crmMailing/body_html.html +++ b/partials/crmMailing/body_html.html @@ -6,5 +6,8 @@ Required vars: mailing - - \ No newline at end of file +
+ + +
+ diff --git a/partials/crmMailing/body_text.html b/partials/crmMailing/body_text.html index 2b41e981be..2d4665c8dd 100644 --- a/partials/crmMailing/body_text.html +++ b/partials/crmMailing/body_text.html @@ -5,5 +5,8 @@ Required vars: mailing, crmMailingConst
- +
+ + +
diff --git a/partials/crmMailing/headerFooter.html b/partials/crmMailing/headerFooter.html index bd1d1744ab..11a2910760 100644 --- a/partials/crmMailing/headerFooter.html +++ b/partials/crmMailing/headerFooter.html @@ -3,13 +3,14 @@ Controller: EditMailingCtrl Required vars: mailing, crmMailingConst -->
-
+