From 744bebee83b22ee74a91e5afee9181c8d752c52d Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 30 Nov 2014 08:55:19 -0800 Subject: [PATCH] CRM-15578 - crmMailing2 - Implement fields/msg_template_id.html --- CRM/Mailing/Info.php | 2 +- js/angular-crmMailing2-services.js | 45 ++++++- js/angular-crmMailing2.js | 110 ++++++++++++++++++ partials/crmMailing2/dialog/saveTemplate.html | 17 +++ .../crmMailing2/field/msg_template_id.html | 12 ++ partials/crmMailing2/mailing.html | 11 ++ 6 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 partials/crmMailing2/dialog/saveTemplate.html create mode 100644 partials/crmMailing2/field/msg_template_id.html diff --git a/CRM/Mailing/Info.php b/CRM/Mailing/Info.php index a713f81a74..9316ebf49a 100644 --- a/CRM/Mailing/Info.php +++ b/CRM/Mailing/Info.php @@ -108,7 +108,7 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info { 'contact_id' => $contactID, )); $mesTemplate = civicrm_api3('MessageTemplate', 'get', array( 'sequential' => 1, - 'return' => array("msg_html", "id", "msg_title", "msg_subject"), + 'return' => array("msg_html", "id", "msg_title", "msg_subject", "msg_text"), 'workflow_id' => array('IS NULL' => ""), )); } diff --git a/js/angular-crmMailing2-services.js b/js/angular-crmMailing2-services.js index 1a34ceffc2..7cdb56f5cc 100644 --- a/js/angular-crmMailing2-services.js +++ b/js/angular-crmMailing2-services.js @@ -55,6 +55,50 @@ }; }); + crmMailing2.factory('crmMsgTemplates', function($q, crmApi) { + var tpls = _.map(CRM.crmMailing.mesTemplate, function(tpl){ + return _.extend({}, tpl, { + //id: tpl parseInt(tpl.id) + }); + }); + window.tpls = tpls; + var lastModifiedTpl = null; + return { + // @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; + }, + // Save a template + // @param tpl MessageTemplate (per APIv3) For new templates, omit "id" + // @return Promise MessageTemplate (per APIv3) + save: function(tpl) { + return crmApi('MessageTemplate', 'create', tpl).then(function(response){ + if (!tpl.id) { + tpl.id = ''+response.id; //parseInt(response.id); + tpls.push(tpl); + } + lastModifiedTpl = tpl + return tpl; + }); + }, + // @return Object MessageTemplate (per APIv3) + getLastModifiedTpl: function() { + return lastModifiedTpl; + }, + getAll: function getAll() { + return tpls; + } + }; + }); + // The crmMailingMgr service provides business logic for loading, saving, previewing, etc crmMailing2.factory('crmMailingMgr', function($q, crmApi, crmFromAddresses) { var pickDefaultMailComponent = function pickDefaultMailComponent(type) { @@ -212,5 +256,4 @@ } }; }); - })(angular, CRM.$, CRM._); diff --git a/js/angular-crmMailing2.js b/js/angular-crmMailing2.js index eb3da81568..48d3c24006 100644 --- a/js/angular-crmMailing2.js +++ b/js/angular-crmMailing2.js @@ -300,4 +300,114 @@ $scope.ts = CRM.ts('CiviMail'); }); + // 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() { + var model = { + selected_id: $scope.mailing.msg_template_id, + tpl: { + msg_title: '', + msg_subject: $scope.mailing.subject, + msg_text: $scope.mailing.body_text, + msg_html: $scope.mailing.body_html + } + }; + var options = { + autoOpen: false, + modal: true, + 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); + return item; + }); + }; + + // @param int id + // @return Promise + $scope.loadTemplate = function loadTemplate(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; + }); + }; + }); + + // Controller for the "Save Message Template" dialog + // Scope members: + // - [input] "model": Object + // - "selected_id": int + // - "tpl": Object + // - "msg_subject": string + // - "msg_text": string + // - "msg_html": string + crmMailing2.controller('SaveMsgTemplateDialogCtrl', function SaveMsgTemplateDialogCtrl($scope, crmMsgTemplates, dialogService) { + var ts = $scope.ts = CRM.ts('CiviMail'); + $scope.saveOpt = {mode: '', newTitle: ''}; + $scope.selected = null; + + $scope.save = function save() { + var tpl = _.extend({}, $scope.model.tpl); + switch ($scope.saveOpt.mode) { + case 'add': + tpl.msg_title = $scope.saveOpt.newTitle; + break; + case 'update': + tpl.id = $scope.selected.id; + tpl.msg_title = $scope.selected.msg_title; + break; + default: + throw 'SaveMsgTemplateDialogCtrl: Unrecognized mode: ' + $scope.saveOpt.mode; + } + return crmMsgTemplates.save(tpl) + .then(function (item) { + CRM.status(ts('Saved')); + return item; + }); + }; + + function scopeApply(f) { + return function () { + var args = arguments; + $scope.$apply(function () { + f.apply(args); + }); + }; + } + + function init() { + crmMsgTemplates.get($scope.model.selected_id).then( + function (tpl) { + $scope.saveOpt.mode = 'update'; + $scope.selected = tpl; + }, + function () { + $scope.saveOpt.mode = 'add'; + $scope.selected = null; + } + ); + // 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'); + }; + dialogService.setButtons('saveTemplateDialog', buttons); + } + setTimeout(scopeApply(init), 0); + }); })(angular, CRM.$, CRM._); diff --git a/partials/crmMailing2/dialog/saveTemplate.html b/partials/crmMailing2/dialog/saveTemplate.html new file mode 100644 index 0000000000..1e5c723f58 --- /dev/null +++ b/partials/crmMailing2/dialog/saveTemplate.html @@ -0,0 +1,17 @@ +
+

{{ts('Save the current mailing as a template.')}}

+ +
+ +
+
+ + +
+
diff --git a/partials/crmMailing2/field/msg_template_id.html b/partials/crmMailing2/field/msg_template_id.html new file mode 100644 index 0000000000..ff2e6cec01 --- /dev/null +++ b/partials/crmMailing2/field/msg_template_id.html @@ -0,0 +1,12 @@ + + + + diff --git a/partials/crmMailing2/mailing.html b/partials/crmMailing2/mailing.html index 5388e29624..130e1e43dc 100644 --- a/partials/crmMailing2/mailing.html +++ b/partials/crmMailing2/mailing.html @@ -3,6 +3,17 @@ Controller: EditMailingCtrl Required vars: mailing, crmMailingConst FIXME: Don't hardcode table-based layout! --> + + + + + + + +
+ + +
-- 2.25.1