CRM-15801 - crmMailing - Display the "Unsubscribe tracking group" field
[civicrm-core.git] / js / angular-crmMailing.js
index 5bad720dd3b534dcb48753fde1a725cdaa9673db..09fa277fb924b7bdfde86f61a87294e0de7fed2e 100644 (file)
   // Scope members:
   //  - [input] mailing: object
   //  - [output] recipients: array of recipient records
-  angular.module('crmMailing').controller('EditRecipCtrl', function EditRecipCtrl($scope, dialogService, crmApi, crmMailingMgr) {
+  angular.module('crmMailing').controller('EditRecipCtrl', function EditRecipCtrl($scope, dialogService, crmApi, crmMailingMgr, $q, crmMetadata) {
     var ts = $scope.ts = CRM.ts(null);
     $scope.recipients = null;
     $scope.getRecipientsEstimate = function () {
     $scope.getIncludesAsString = function () {
       var first = true;
       var names = '';
-      _.each($scope.mailing.groups.include, function (id) {
+      _.each($scope.mailing.recipients.groups.include, function (id) {
         if (!first) {
           names = names + ', ';
         }
         names = names + group[0].title;
         first = false;
       });
-      _.each($scope.mailing.mailings.include, function (id) {
+      _.each($scope.mailing.recipients.mailings.include, function (id) {
         if (!first) {
           names = names + ', ';
         }
     $scope.getExcludesAsString = function () {
       var first = true;
       var names = '';
-      _.each($scope.mailing.groups.exclude, function (id) {
+      _.each($scope.mailing.recipients.groups.exclude, function (id) {
         if (!first) {
           names = names + ', ';
         }
         names = names + group[0].title;
         first = false;
       });
-      _.each($scope.mailing.mailings.exclude, function (id) {
+      _.each($scope.mailing.recipients.mailings.exclude, function (id) {
         if (!first) {
           names = names + ', ';
         }
         });
       });
     }, RECIPIENTS_DEBOUNCE_MS);
-    $scope.$watchCollection("mailing.groups.include", refreshRecipients);
-    $scope.$watchCollection("mailing.groups.exclude", refreshRecipients);
-    $scope.$watchCollection("mailing.mailings.include", refreshRecipients);
-    $scope.$watchCollection("mailing.mailings.exclude", refreshRecipients);
+    $scope.$watchCollection("mailing.recipients.groups.include", refreshRecipients);
+    $scope.$watchCollection("mailing.recipients.groups.exclude", refreshRecipients);
+    $scope.$watchCollection("mailing.recipients.mailings.include", refreshRecipients);
+    $scope.$watchCollection("mailing.recipients.mailings.exclude", refreshRecipients);
 
     $scope.previewRecipients = function previewRecipients() {
       var model = {
         recipients: $scope.recipients
       };
-      var options = {
+      var options = CRM.utils.adjustDialogDefaults({
         autoOpen: false,
-        modal: true,
         title: ts('Preview (%1)', {
           1: $scope.getRecipientsEstimate()
         })
-      };
+      });
       dialogService.open('recipDialog', '~/crmMailing/dialog/recipients.html', model, options);
     };
+
+    // Open a dialog for editing the advanced recipient options.
+    $scope.editOptions = function editOptions(mailing) {
+      var options = CRM.utils.adjustDialogDefaults({
+        autoOpen: false,
+        title: ts('Edit Options')
+      });
+      $q.when(crmMetadata.getFields('Mailing')).then(function(fields) {
+        var model = {
+          fields: fields,
+          mailing: mailing
+        };
+        dialogService.open('previewComponentDialog', '~/crmMailing/dialog/recipientOptions.html', model, options);
+      });
+    };
   });
 
   // Controller for the "Preview Recipients" dialog
     $scope.ts = CRM.ts(null);
   });
 
+  // Controller for the "Recipients: Edit Options" dialog
+  // Note: Expects $scope.model to be an object with properties:
+  //   - "mailing" (APIv3 mailing object)
+  //   - "fields" (list of fields)
+  angular.module('crmMailing').controller('EditRecipOptionsDialogCtrl', function EditRecipOptionsDialogCtrl($scope) {
+    $scope.ts = CRM.ts(null);
+  });
+
   // Controller for the "Preview Mailing Component" segment
   // which displays header/footer/auto-responder
-  angular.module('crmMailing').controller('PreviewComponentCtrl', function PreviewMailingDialogCtrl($scope, dialogService) {
+  angular.module('crmMailing').controller('PreviewComponentCtrl', function PreviewComponentCtrl($scope, dialogService) {
     var ts = $scope.ts = CRM.ts(null);
 
     $scope.previewComponent = function previewComponent(title, componentId) {
         }));
         return;
       }
-      var options = {
+      var options = CRM.utils.adjustDialogDefaults({
         autoOpen: false,
-        modal: true,
         title: title // component[0].name
-      };
+      });
       dialogService.open('previewComponentDialog', '~/crmMailing/dialog/previewComponent.html', component[0], options);
     };
   });
 
-  // Controller for the "Preview Mailing" dialog
+  // Controller for the "Preview Mailing Component" dialog
   // Note: Expects $scope.model to be an object with properties:
   //   - "name"
   //   - "subject"
   //   - "body_html"
   //   - "body_text"
-  angular.module('crmMailing').controller('PreviewComponentDialogCtrl', function PreviewMailingDialogCtrl($scope) {
+  angular.module('crmMailing').controller('PreviewComponentDialogCtrl', function PreviewComponentDialogCtrl($scope) {
     $scope.ts = CRM.ts(null);
   });
 
           msg_html: mailing.body_html
         }
       };
-      var options = {
+      var options = CRM.utils.adjustDialogDefaults({
         autoOpen: false,
-        modal: true,
         title: ts('Save Template')
-      };
+      });
       return dialogService.open('saveTemplateDialog', '~/crmMailing/dialog/saveTemplate.html', model, options)
         .then(function (item) {
           mailing.msg_template_id = item.id;
       // When using dialogService with a button bar, the major button actions
       // need to be registered with the dialog widget (and not embedded in
       // the body of the dialog).
-      var buttons = {};
-      buttons[ts('Save')] = function () {
-        $scope.save().then(function (item) {
-          dialogService.close('saveTemplateDialog', item);
-        });
-      };
-      buttons[ts('Cancel')] = function () {
-        dialogService.cancel('saveTemplateDialog');
-      };
+      var buttons = [
+        {
+          text: ts('Save'),
+          icons: {primary: 'ui-icon-check'},
+          click: function () {
+            $scope.save().then(function (item) {
+              dialogService.close('saveTemplateDialog', item);
+            });
+          }
+        },
+        {
+          text: ts('Cancel'),
+          icons: {primary: 'ui-icon-close'},
+          click: function () {
+            dialogService.cancel('saveTemplateDialog');
+          }
+        }
+      ];
       dialogService.setButtons('saveTemplateDialog', buttons);
     }
 
     setTimeout(scopeApply(init), 0);
   });
 
-  angular.module('crmMailing').controller('EmailAddrCtrl', function EmailAddrCtrl($scope, crmFromAddresses) {
+  angular.module('crmMailing').controller('EmailAddrCtrl', function EmailAddrCtrl($scope, crmFromAddresses, crmUiAlert) {
+    var ts = CRM.ts(null);
+    function changeAlert(winnerField, loserField) {
+      crmUiAlert({
+        title: ts('Conflict'),
+        text: ts('The "%1" option conflicts with the "%2" option. The "%2" option has been disabled.', {
+          1: winnerField,
+          2: loserField
+        })
+      });
+    }
+
     $scope.crmFromAddresses = crmFromAddresses;
+    $scope.checkReplyToChange = function checkReplyToChange(mailing) {
+      if (!_.isEmpty(mailing.replyto_email) && mailing.override_verp == '0') {
+        mailing.override_verp = '1';
+        changeAlert(ts('Reply-To'), ts('Track Replies'));
+      }
+    };
+    $scope.checkVerpChange = function checkVerpChange(mailing) {
+      if (!_.isEmpty(mailing.replyto_email) && mailing.override_verp == '0') {
+        mailing.replyto_email = '';
+        changeAlert(ts('Track Replies'), ts('Reply-To'));
+      }
+    };
+  });
+
+  var lastEmailTokenAlert = null;
+  angular.module('crmMailing').controller('EmailBodyCtrl', function EmailBodyCtrl($scope, crmMailingMgr, crmUiAlert, $timeout) {
+    var ts = CRM.ts(null);
+
+    // ex: if (!hasAllTokens(myMailing, 'body_text)) alert('Oh noes!');
+    $scope.hasAllTokens = function hasAllTokens(mailing, field) {
+      return _.isEmpty(crmMailingMgr.findMissingTokens(mailing, field));
+    };
+
+    // ex: checkTokens(myMailing, 'body_text', 'insert:body_text')
+    // ex: checkTokens(myMailing, '*')
+    $scope.checkTokens = function checkTokens(mailing, field, insertEvent) {
+      if (lastEmailTokenAlert) {
+        lastEmailTokenAlert.close();
+      }
+      var missing, insertable;
+      if (field == '*') {
+        insertable = false;
+        missing = angular.extend({},
+          crmMailingMgr.findMissingTokens(mailing, 'body_html'),
+          crmMailingMgr.findMissingTokens(mailing, 'body_text')
+        );
+      } else {
+        insertable = !_.isEmpty(insertEvent);
+        missing = crmMailingMgr.findMissingTokens(mailing, field);
+      }
+      if (!_.isEmpty(missing)) {
+        lastEmailTokenAlert = crmUiAlert({
+          type: 'error',
+          title: ts('Required tokens'),
+          templateUrl: '~/crmMailing/dialog/tokenAlert.html',
+          scope: angular.extend($scope.$new(), {
+            insertable: insertable,
+            insertToken: function(token) {
+              $timeout(function(){
+                $scope.$broadcast(insertEvent, '{' + token + '}');
+                $timeout(function(){
+                  checkTokens(mailing, field, insertEvent);
+                });
+              });
+            },
+            missing: missing
+          })
+        });
+      }
+    };
+  });
+
+  angular.module('crmMailing').controller('EditUnsubGroupCtrl', function EditUnsubGroupCtrl($scope) {
+    // CRM.crmMailing.groupNames is a global constant - since it doesn't change, we can digest & cache.
+    var mandatoryIds = [];
+    _.each(CRM.crmMailing.groupNames, function(grp){
+      if (grp.is_hidden == "1") {
+        mandatoryIds.push(parseInt(grp.id));
+      }
+    });
+
+    $scope.isUnsubGroupRequired = function isUnsubGroupRequired(mailing) {
+      return _.intersection(mandatoryIds, mailing.recipients.groups.include).length > 0;
+    };
   });
 })(angular, CRM.$, CRM._);