Merge pull request #6350 from PalanteJon/CRM-16939-4.6
[civicrm-core.git] / ang / crmMailing / services.js
index b0503376dad12ec7b93e6888e5b6db18c45dd570..c76da9238f53be8cc50aac5d9539848c4e2cabce 100644 (file)
     window.tpls = tpls;
     var lastModifiedTpl = null;
     return {
+      // Get a template
+      // @param id MessageTemplate id  (per APIv3)
       // @return Promise MessageTemplate (per APIv3)
       get: function get(id) {
-        id = '' + id; // parseInt(id);
-        var dfr = $q.defer();
-        var tpl = _.where(tpls, {id: id});
-        if (id && tpl && tpl[0]) {
-          dfr.resolve(tpl[0]);
-        }
-        else {
-          dfr.reject(id);
-        }
-        return dfr.promise;
+        return crmApi('MessageTemplate', 'getsingle', {
+           "return": "id,msg_subject,msg_html,msg_title,msg_text",
+           "id": id
+        });
       },
       // Save a template
       // @param tpl MessageTemplate (per APIv3) For new templates, omit "id"
         });
       },
 
+      previewRecipientCount: function previewRecipientCount(mailing) {
+        // To get list of recipients, we tentatively save the mailing and
+        // get the resulting recipients -- then rollback any changes.
+        var params = angular.extend({}, mailing, mailing.recipients, {
+          name: 'placeholder', // for previewing recipients on new, incomplete mailing
+          subject: 'placeholder', // for previewing recipients on new, incomplete mailing
+          options: {force_rollback: 1},
+          'api.mailing_job.create': 1, // note: exact match to API default
+          'api.MailingRecipients.getcount': {
+            mailing_id: '$value.id'
+          }
+        });
+        delete params.recipients; // the content was merged in
+        return qApi('Mailing', 'create', params).then(function (recipResult) {
+          // changes rolled back, so we don't care about updating mailing
+          return recipResult.values[recipResult.id]['api.MailingRecipients.getcount'];
+        });
+      },
+
       // Save a (draft) mailing
       // @param mailing Object (per APIv3)
       // @return Promise
             });
             result = dialogService.open('previewDialog', templates[mode], content, options);
           });
-        crmStatus({start: ts('Previewing'), success: ''}, p);
+        crmStatus({start: ts('Previewing...'), success: ''}, p);
         return result;
       },