CRM-21206 Ensure that the recipients field of AB Test results is filled out
authorSeamus Lee <seamuslee001@gmail.com>
Thu, 21 Sep 2017 01:59:45 +0000 (11:59 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Wed, 22 Nov 2017 23:33:50 +0000 (10:33 +1100)
CRM-21206 Fix building list of group ids and mailing ids

Update to correct version of patch

ang/crmMailing/ViewRecipCtrl.js

index 652b89e3a8881743a31b6351a1d41ed9eaf21d6e..d72793fdb66fe9776320e56cd346d51d46d39123 100644 (file)
@@ -1,11 +1,83 @@
 (function(angular, $, _) {
 
   angular.module('crmMailing').controller('ViewRecipCtrl', function ViewRecipCtrl($scope) {
+    var mids = [];
+    var gids = [];
+    var groupNames = [];
+    var mailings = [];
+    var civimailings = [];
+    var civimails = [];
+
+    function getGroupNames(mailing) {
+      if (-1 == mailings.indexOf(mailing.id)) {
+        mailings.push(mailing.id);
+        _.each(mailing.recipients.groups.include, function(id) {
+          if (-1 == gids.indexOf(id)) {
+            gids.push(id);
+          }
+        });
+        _.each(mailing.recipients.groups.exclude, function(id) {
+          if (-1 == gids.indexOf(id)) {
+            gids.push(id);
+          }
+        });
+        _.each(mailing.recipients.groups.base, function(id) {
+          if (-1 == gids.indexOf(id)) {
+            gids.push(id);
+          }
+        });
+        if (!_.isEmpty(gids)) {
+          CRM.api3('Group', 'get', {'id': {"IN": gids}}).then(function(result) {
+            _.each(result.values, function(grp) {
+              if (_.isEmpty(_.where(groupNames, {id: parseInt(grp.id)}))) {
+                groupNames.push({id: parseInt(grp.id), title: grp.title, is_hidden: grp.is_hidden});
+              }
+            });
+            CRM.crmMailing.groupNames = groupNames;
+            $scope.$parent.crmMailingConst.groupNames = groupNames;
+          });
+        }
+      }
+    }
+
+    function getCiviMails(mailing) {
+      if (-1 == civimailings.indexOf(mailing.id)) {
+        civimailings.push(mailing.id);
+        _.each(mailing.recipients.mailings.include, function(id) {
+          if (-1 == mids.indexOf(id)) {
+            mids.push(id);
+          }
+        });
+        _.each(mailing.recipients.mailings.exclude, function(id) {
+          if (-1 == mids.indexOf(id)) {
+            mids.push(id);
+          }
+        });
+        if (!_.isEmpty(mids)) {
+          CRM.api3('Mailing', 'get', {'id': {"IN": mids}}).then(function(result) {
+            _.each(result.values, function(mail) {
+              if (_.isEmpty(_.where(civimails, {id: parseInt(mail.id)}))) {
+                civimails.push({id: parseInt(mail.id), name: mail.label});
+              }
+            });
+            CRM.crmMailing.civiMails = civimails;
+            $scope.$parent.crmMailingConst.civiMails = civimails;
+          });
+        }
+      }
+    }
+
     $scope.getIncludesAsString = function(mailing) {
       var first = true;
       var names = '';
+      if (_.isEmpty(CRM.crmMailing.groupNames)) {
+        getGroupNames(mailing);
+      }
+      if (_.isEmpty(CRM.crmMailing.civiMails)) {
+        getCiviMails(mailing);
+      }
       _.each(mailing.recipients.groups.include, function(id) {
-        var group = _.where(CRM.crmMailing.groupNames, {id: '' + id});
+        var group = _.where(CRM.crmMailing.groupNames, {id: parseInt(id)});
         if (group.length) {
           if (!first) {
             names = names + ', ';
@@ -15,7 +87,7 @@
         }
       });
       _.each(mailing.recipients.mailings.include, function(id) {
-        var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id});
+        var oldMailing = _.where(CRM.crmMailing.civiMails, {id: parseInt(id)});
         if (oldMailing.length) {
           if (!first) {
             names = names + ', ';
       var first = true;
       var names = '';
       _.each(mailing.recipients.groups.exclude, function(id) {
-        var group = _.where(CRM.crmMailing.groupNames, {id: '' + id});
+        var group = _.where(CRM.crmMailing.groupNames, {id: parseInt(id)});
         if (group.length) {
           if (!first) {
             names = names + ', ';
         }
       });
       _.each(mailing.recipients.mailings.exclude, function(id) {
-        var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id});
+        var oldMailing = _.where(CRM.crmMailing.civiMails, {id: parseInt(id)});
         if (oldMailing.length) {
           if (!first) {
             names = names + ', ';