CRM-15578 - crmMailing2 - Fix regression in "From Address".
[civicrm-core.git] / js / angular-crmMailing2.js
index d75bfbf9fe3f665d7074a5245d9dde65775073d3..0edf9ecd455eaa32762b413136854e462cfd83ca 100644 (file)
@@ -3,13 +3,16 @@
     return CRM.resourceUrls['civicrm'] + '/partials/crmMailing2/' + relPath;
   };
 
-  var crmMailing2 = angular.module('crmMailing2', ['crmUtil', 'ngRoute', 'ui.utils', 'crmUi', 'dialogService']); // TODO ngSanitize, unsavedChanges
+  var crmMailing2 = angular.module('crmMailing2', [
+    'crmUtil', 'crmAttachment', 'ngRoute', 'ui.utils', 'crmUi', 'dialogService'
+  ]); // TODO ngSanitize, unsavedChanges
 
   // Time to wait before triggering AJAX update to recipients list
   var RECIPIENTS_DEBOUNCE_MS = 100;
   var RECIPIENTS_PREVIEW_LIMIT = 10000;
 
-  crmMailing2.config(['$routeProvider',
+  crmMailing2.config([
+    '$routeProvider',
     function ($routeProvider) {
       $routeProvider.when('/mailing2', {
         template: '<div></div>',
         templateUrl: partialUrl('edit.html'),
         controller: 'EditMailingCtrl',
         resolve: {
-          selectedMail: function selectedMail($route, crmMailingMgr) { return crmMailingMgr.getOrCreate($route.current.params.id); }
+          selectedMail: function selectedMail($route, crmMailingMgr) {
+            return crmMailingMgr.getOrCreate($route.current.params.id);
+          }
         }
       });
       $routeProvider.when('/mailing2/:id/unified', {
         templateUrl: partialUrl('edit-unified.html'),
         controller: 'EditMailingCtrl',
         resolve: {
-          selectedMail: function selectedMail($route, crmMailingMgr) { return crmMailingMgr.getOrCreate($route.current.params.id); }
+          selectedMail: function selectedMail($route, crmMailingMgr) {
+            return crmMailingMgr.getOrCreate($route.current.params.id);
+          }
         }
       });
       $routeProvider.when('/mailing2/:id/unified2', {
         templateUrl: partialUrl('edit-unified2.html'),
         controller: 'EditMailingCtrl',
         resolve: {
-          selectedMail: function selectedMail($route, crmMailingMgr) { return crmMailingMgr.getOrCreate($route.current.params.id); }
+          selectedMail: function selectedMail($route, crmMailingMgr) {
+            return crmMailingMgr.getOrCreate($route.current.params.id);
+          }
         }
       });
       $routeProvider.when('/mailing2/:id/wizard', {
         templateUrl: partialUrl('edit-wizard.html'),
         controller: 'EditMailingCtrl',
         resolve: {
-          selectedMail: function selectedMail($route, crmMailingMgr) { return crmMailingMgr.getOrCreate($route.current.params.id); }
+          selectedMail: function selectedMail($route, crmMailingMgr) {
+            return crmMailingMgr.getOrCreate($route.current.params.id);
+          }
         }
       });
     }
     });
   });
 
-  crmMailing2.controller('EditMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location, crmMailingMgr, crmFromAddresses, crmStatus) {
+  crmMailing2.controller('EditMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location, crmMailingMgr, crmStatus, CrmAttachments) {
     $scope.mailing = selectedMail;
+    $scope.attachments = new CrmAttachments(function () {
+      return {entity_table: 'civicrm_mailing', entity_id: $scope.mailing.id};
+    });
+    $scope.attachments.load();
     $scope.crmMailingConst = CRM.crmMailing;
-    $scope.crmFromAddresses = crmFromAddresses;
 
     $scope.partialUrl = partialUrl;
     var ts = $scope.ts = CRM.ts('CiviMail');
 
     // @return Promise
     $scope.submit = function submit() {
-      return crmStatus({start: ts('Submitting...'), success: ts('Submitted')},
-        crmMailingMgr.submit($scope.mailing)
-      );
+      var promise = crmMailingMgr.save($scope.mailing)
+        .then(function () {
+          // pre-condition: the mailing exists *before* saving attachments to it
+          return $scope.attachments.save();
+        })
+        .then(function () {
+          return crmMailingMgr.submit($scope.mailing);
+        });
+      return crmStatus({start: ts('Submitting...'), success: ts('Submitted')}, promise);
     };
     // @return Promise
     $scope.save = function save() {
       return crmStatus(null,
-        crmMailingMgr.save($scope.mailing)
+        crmMailingMgr
+          .save($scope.mailing)
+          .then(function () {
+            // pre-condition: the mailing exists *before* saving attachments to it
+            return $scope.attachments.save();
+          })
       );
     };
     // @return Promise
     };
 
     // Transition URL "/mailing2/new" => "/mailing2/123" as soon as ID is known
-    $scope.$watch('mailing.id', function(newValue, oldValue) {
+    $scope.$watch('mailing.id', function (newValue, oldValue) {
       if (newValue && newValue != oldValue) {
         var parts = $location.path().split('/'); // e.g. "/mailing2/new" or "/mailing2/123/wizard"
         parts[2] = newValue;
         // FIXME: Angular unnecessarily refreshes UI
       }
     });
-
-    $scope.fromPlaceholder = {
-      label: crmFromAddresses.getByAuthorEmail($scope.mailing.from_name, $scope.mailing.from_email, true).label
-    };
-    $scope.$watch('fromPlaceholder.label', function(newValue){
-      var addr = crmFromAddresses.getByLabel(newValue);
-      $scope.mailing.from_name = addr.author;
-      $scope.mailing.from_email = addr.email;
-    });
   });
 
   // Controller for the edit-recipients fields (
     $scope.recipients = null;
     $scope.getRecipientsEstimate = function () {
       var ts = $scope.ts;
-      if ($scope.recipients == null)
+      if ($scope.recipients == null) {
         return ts('(Estimating)');
-      if ($scope.recipients.length == 0)
+      }
+      if ($scope.recipients.length == 0) {
         return ts('No recipients');
-      if ($scope.recipients.length == 1)
+      }
+      if ($scope.recipients.length == 1) {
         return ts('~1 recipient');
-      if (RECIPIENTS_PREVIEW_LIMIT > 0 && $scope.recipients.length >= RECIPIENTS_PREVIEW_LIMIT)
+      }
+      if (RECIPIENTS_PREVIEW_LIMIT > 0 && $scope.recipients.length >= RECIPIENTS_PREVIEW_LIMIT) {
         return ts('>%1 recipients', {1: RECIPIENTS_PREVIEW_LIMIT});
+      }
       return ts('~%1 recipients', {1: $scope.recipients.length});
     };
-    $scope.getIncludesAsString = function() {
+    $scope.getIncludesAsString = function () {
       var first = true;
       var names = '';
-      _.each($scope.mailing.groups.include, function(id){
-        if (!first) names = names + ', ';
-        var group = _.where(CRM.crmMailing.groupNames, {id: ''+id});
+      _.each($scope.mailing.groups.include, function (id) {
+        if (!first) {
+          names = names + ', ';
+        }
+        var group = _.where(CRM.crmMailing.groupNames, {id: '' + id});
         names = names + group[0].title;
         first = false;
       });
-      _.each($scope.mailing.mailings.include, function(id){
-        if (!first) names = names + ', ';
-        var oldMailing = _.where(CRM.crmMailing.civiMails, {id: ''+id});
+      _.each($scope.mailing.mailings.include, function (id) {
+        if (!first) {
+          names = names + ', ';
+        }
+        var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id});
         names = names + oldMailing[0].name;
         first = false;
       });
       return names;
     };
-    $scope.getExcludesAsString = function() {
+    $scope.getExcludesAsString = function () {
       var first = true;
       var names = '';
-      _.each($scope.mailing.groups.exclude, function(id){
-        if (!first) names = names + ', ';
-        var group = _.where(CRM.crmMailing.groupNames, {id: ''+id});
+      _.each($scope.mailing.groups.exclude, function (id) {
+        if (!first) {
+          names = names + ', ';
+        }
+        var group = _.where(CRM.crmMailing.groupNames, {id: '' + id});
         names = names + group[0].title;
         first = false;
       });
-      _.each($scope.mailing.mailings.exclude, function(id){
-        if (!first) names = names + ', ';
-        var oldMailing = _.where(CRM.crmMailing.civiMails, {id: ''+id});
+      _.each($scope.mailing.mailings.exclude, function (id) {
+        if (!first) {
+          names = names + ', ';
+        }
+        var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id});
         names = names + oldMailing[0].name;
         first = false;
       });
   // Controller for the "Preview Mailing" segment
   // Note: Expects $scope.model to be an object with properties:
   //   - mailing: object
-  crmMailing2.controller('PreviewMailingCtrl', function ($scope, dialogService, crmMailingMgr) {
+  //   - attachments: object
+  crmMailing2.controller('PreviewMailingCtrl', function ($scope, dialogService, crmMailingMgr, crmStatus) {
     var ts = $scope.ts = CRM.ts('CiviMail');
 
     $scope.testContact = {email: CRM.crmMailing.defaultTestEmail};
     $scope.previewDialog = function previewDialog(template) {
       var p = crmMailingMgr
         .preview($scope.mailing)
-        .then(function(content){
+        .then(function (content) {
           var options = {
             autoOpen: false,
             modal: true,
           };
           dialogService.open('previewDialog', template, content, options);
         });
-        CRM.status({start: ts('Previewing'), success: ''}, CRM.toJqPromise(p));
+      CRM.status({start: ts('Previewing'), success: ''}, CRM.toJqPromise(p));
     };
     $scope.sendTestToContact = function sendTestToContact() {
-      $scope.sendTest($scope.mailing, $scope.testContact.email, null);
+      $scope.sendTest($scope.mailing, $scope.attachments, $scope.testContact.email, null);
     };
     $scope.sendTestToGroup = function sendTestToGroup() {
-      $scope.sendTest($scope.mailing, null, $scope.testGroup.gid);
+      $scope.sendTest($scope.mailing, $scope.attachments, null, $scope.testGroup.gid);
     };
-    $scope.sendTest = function sendTest(mailing, testEmail, testGroup) {
-      var promise = crmMailingMgr.sendTest(mailing, testEmail, testGroup).then(function(deliveryInfos){
-        var count = Object.keys(deliveryInfos).length;
-        if (count === 0) {
-          CRM.alert(ts('Could not identify any recipients. Perhaps the group is empty?'));
-        }
-      });
-      CRM.status({
-        start: ts('Sending...'),
-        success: ts('Sent')
-      }, CRM.toJqPromise(promise));
+    $scope.sendTest = function sendTest(mailing, attachments, testEmail, testGroup) {
+      var promise = crmMailingMgr.save(mailing)
+          .then(function () {
+            return attachments.save();
+          })
+          .then(function () {
+            return crmMailingMgr.sendTest(mailing, testEmail, testGroup);
+          })
+          .then(function (deliveryInfos) {
+            var count = Object.keys(deliveryInfos).length;
+            if (count === 0) {
+              CRM.alert(ts('Could not identify any recipients. Perhaps the group is empty?'));
+            }
+          })
+        ;
+      return crmStatus({start: ts('Sending...'), success: ts('Sent')}, promise);
     };
   });
 
     var ts = $scope.ts = CRM.ts('CiviMail');
 
     $scope.previewComponent = function previewComponent(title, componentId) {
-      var component = _.where(CRM.crmMailing.headerfooterList, {id: ""+componentId});
+      var component = _.where(CRM.crmMailing.headerfooterList, {id: "" + componentId});
       if (!component || !component[0]) {
         CRM.alert(ts('Invalid component ID (%1)', {
           1: componentId
   });
 
   // Controller for the in-place msg-template management
-  // Scope members:
-  //  - [input] mailing: object
   crmMailing2.controller('MsgTemplateCtrl', function MsgTemplateCtrl($scope, crmMsgTemplates, dialogService, $parse) {
     var ts = $scope.ts = CRM.ts('CiviMail');
     $scope.crmMsgTemplates = crmMsgTemplates;
 
     // @return Promise MessageTemplate (per APIv3)
-    $scope.saveTemplate = function saveTemplate() {
+    $scope.saveTemplate = function saveTemplate(mailing) {
       var model = {
-        selected_id: $scope.mailing.msg_template_id,
+        selected_id: mailing.msg_template_id,
         tpl: {
           msg_title: '',
-          msg_subject: $scope.mailing.subject,
-          msg_text: $scope.mailing.body_text,
-          msg_html: $scope.mailing.body_html
+          msg_subject: mailing.subject,
+          msg_text: mailing.body_text,
+          msg_html: mailing.body_html
         }
       };
       var options = {
         title: ts('Save Template')
       };
       return dialogService.open('saveTemplateDialog', partialUrl('dialog/saveTemplate.html'), model, options)
-        .then(function(item){
-          $parse('mailing.msg_template_id').assign($scope, item.id);
+        .then(function (item) {
+          mailing.msg_template_id = item.id;
           return item;
         });
     };
 
     // @param int id
     // @return Promise
-    $scope.loadTemplate = function loadTemplate(id) {
+    $scope.loadTemplate = function loadTemplate(mailing, id) {
       return crmMsgTemplates.get(id).then(function (tpl) {
-        $scope.mailing.subject = tpl.msg_subject;
-        $scope.mailing.body_text = tpl.msg_text;
-        $scope.mailing.body_html = tpl.msg_html;
+        mailing.subject = tpl.msg_subject;
+        mailing.body_text = tpl.msg_text;
+        mailing.body_html = tpl.msg_html;
       });
     };
   });
       };
       dialogService.setButtons('saveTemplateDialog', buttons);
     }
+
     setTimeout(scopeApply(init), 0);
   });
 
+  crmMailing2.controller('EmailAddrCtrl', function EmailAddrCtrl($scope, crmFromAddresses){
+    $scope.crmFromAddresses = crmFromAddresses;
+  });
+
   // Controller for schedule-editing widget.
   // Scope members:
   //  - [input] mailing: object