From 3ac6e1072ef0434188b0f23288f12e1cbaac3916 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 19 Dec 2016 15:04:37 -0800 Subject: [PATCH] CRM-19690 - crmMailing - Pick editor layout using template_type This PR allows one to display a different CiviMail composition UI depending on the `template_type`. Key details: 1. When creating a mailing, use the path `civicrm/a/#/mailing/new` to create a mailing with the default `template_type` (aka first-preferred, by weight). 2. When creating a mailing, use the path `civicrm/a/#/mailing/new/{template_type}` to create a mailing with a specific `template_type`. 3. When editing a mailing, it checks the `template_type` and loads the appropriate editor. 4. Some of the boilerplate from `2step.html`, `unified.html`, etal has been moved to `base.html`. Note that this breaks some hidden functionality -- before, you could switch among a few different layouts (`2step`, `unified`, `unified2`, `wizard`) by manually editing the URL (e.g. `civicrm/a/#/mailing/2/unified`). Now, to change the layout of the traditional-style mailings, you can implement a hook, e.g. ``` function mymod_civicrm_mailingTemplateTypes(&$types) { foreach ($types as &$typeDef) { if ($typeDef['name'] === 'traditional') { $typeDef['editorUrl'] = '~/crmMailing/EditMailingCtrl/unified.html'; } } } ``` Relatedly, if a downstream system has found some other way to patch/hack a replacement for the CiviMail layout, then they'll need to update it. (Specifically, port the changes on display in `2step.html` or any of the other layouts.). However, this has always been a hidden trick with unrealistic UX -- and we've refrained from documenting/institutionalizing it... until now. With this PR, overloading CiviMail UI becomes a supported feature (by way of the public-facing `hook_civicrm_mailingTypes`). --- ang/crmMailing.js | 73 +++++++++++--------- ang/crmMailing/CreateMailingCtrl.js | 5 +- ang/crmMailing/EditMailingCtrl.js | 6 +- ang/crmMailing/EditMailingCtrl/2step.html | 13 +--- ang/crmMailing/EditMailingCtrl/base.html | 8 +++ ang/crmMailing/EditMailingCtrl/unified.html | 12 +--- ang/crmMailing/EditMailingCtrl/unified2.html | 12 +--- ang/crmMailing/EditMailingCtrl/wizard.html | 12 +--- ang/crmMailing/EditMailingCtrl/workflow.html | 14 ++-- ang/crmMailing/services.js | 3 + tests/phpunit/Civi/Angular/ManagerTest.php | 2 +- 11 files changed, 75 insertions(+), 85 deletions(-) create mode 100644 ang/crmMailing/EditMailingCtrl/base.html diff --git a/ang/crmMailing.js b/ang/crmMailing.js index 7254b8a06e..7945ae6429 100644 --- a/ang/crmMailing.js +++ b/ang/crmMailing.js @@ -12,41 +12,50 @@ controller: 'ListMailingsCtrl' }); - var editorPaths = { - '': '~/crmMailing/EditMailingCtrl/2step.html', - '/unified': '~/crmMailing/EditMailingCtrl/unified.html', - '/unified2': '~/crmMailing/EditMailingCtrl/unified2.html', - '/wizard': '~/crmMailing/EditMailingCtrl/wizard.html' - }; - angular.forEach(editorPaths, function(editTemplate, pathSuffix) { - if (CRM && CRM.crmMailing && CRM.crmMailing.workflowEnabled) { - editTemplate = '~/crmMailing/EditMailingCtrl/workflow.html'; // override + if (!CRM || !CRM.crmMailing) { + return; + } + + $routeProvider.when('/mailing/new', { + template: '

' + ts('Initializing...') + '

', + controller: 'CreateMailingCtrl', + resolve: { + selectedMail: function(crmMailingMgr) { + var m = crmMailingMgr.create({ + template_type: CRM.crmMailing.templateTypes[0].name + }); + return crmMailingMgr.save(m); + } } - $routeProvider.when('/mailing/new' + pathSuffix, { - template: '

' + ts('Initializing...') + '

', - controller: 'CreateMailingCtrl', - resolve: { - selectedMail: function(crmMailingMgr) { - var m = crmMailingMgr.create(); - return crmMailingMgr.save(m); - } + }); + + $routeProvider.when('/mailing/new/:templateType', { + template: '

' + ts('Initializing...') + '

', + controller: 'CreateMailingCtrl', + resolve: { + selectedMail: function($route, crmMailingMgr) { + var m = crmMailingMgr.create({ + template_type: $route.current.params.templateType + }); + return crmMailingMgr.save(m); } - }); - $routeProvider.when('/mailing/:id' + pathSuffix, { - templateUrl: editTemplate, - controller: 'EditMailingCtrl', - resolve: { - selectedMail: function($route, crmMailingMgr) { - return crmMailingMgr.get($route.current.params.id); - }, - attachments: function($route, CrmAttachments) { - var attachments = new CrmAttachments(function () { - return {entity_table: 'civicrm_mailing', entity_id: $route.current.params.id}; - }); - return attachments.load(); - } + } + }); + + $routeProvider.when('/mailing/:id', { + templateUrl: '~/crmMailing/EditMailingCtrl/base.html', + controller: 'EditMailingCtrl', + resolve: { + selectedMail: function($route, crmMailingMgr) { + return crmMailingMgr.get($route.current.params.id); + }, + attachments: function($route, CrmAttachments) { + var attachments = new CrmAttachments(function () { + return {entity_table: 'civicrm_mailing', entity_id: $route.current.params.id}; + }); + return attachments.load(); } - }); + } }); } ]); diff --git a/ang/crmMailing/CreateMailingCtrl.js b/ang/crmMailing/CreateMailingCtrl.js index 9baecadbf2..08a6171311 100644 --- a/ang/crmMailing/CreateMailingCtrl.js +++ b/ang/crmMailing/CreateMailingCtrl.js @@ -1,10 +1,7 @@ (function(angular, $, _) { angular.module('crmMailing').controller('CreateMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location) { - // Transition URL "/mailing/new/foo" => "/mailing/123/foo" - var parts = $location.path().split('/'); // e.g. "/mailing/new" or "/mailing/123/wizard" - parts[2] = selectedMail.id; - $location.path(parts.join('/')); + $location.path("/mailing/" + selectedMail.id); $location.replace(); }); diff --git a/ang/crmMailing/EditMailingCtrl.js b/ang/crmMailing/EditMailingCtrl.js index 2620497550..91f6db3f3a 100644 --- a/ang/crmMailing/EditMailingCtrl.js +++ b/ang/crmMailing/EditMailingCtrl.js @@ -13,6 +13,10 @@ var block = $scope.block = crmBlocker(); var myAutosave = null; + var templateTypes = _.where(CRM.crmMailing.templateTypes, {name: selectedMail.template_type}); + if (!templateTypes[0]) throw 'Unrecognized template type: ' + selectedMail.template_type; + $scope.mailingEditorUrl = templateTypes[0].editorUrl; + $scope.isSubmitted = function isSubmitted() { return _.size($scope.mailing.jobs) > 0; }; @@ -43,7 +47,7 @@ // @return Promise $scope.submit = function submit(options) { options = options || {}; - if (block.check() || $scope.crmMailing.$invalid) { + if (block.check()) { return; } diff --git a/ang/crmMailing/EditMailingCtrl/2step.html b/ang/crmMailing/EditMailingCtrl/2step.html index da35ce2bfe..d1431f1093 100644 --- a/ang/crmMailing/EditMailingCtrl/2step.html +++ b/ang/crmMailing/EditMailingCtrl/2step.html @@ -1,11 +1,4 @@ -
- -
- {{ts('This mailing has been submitted.')}} -
- -
- +
@@ -49,7 +42,7 @@
- +
{{ts('Submit Mailing')}}
@@ -66,4 +59,4 @@
- +
diff --git a/ang/crmMailing/EditMailingCtrl/base.html b/ang/crmMailing/EditMailingCtrl/base.html new file mode 100644 index 0000000000..92a97b242c --- /dev/null +++ b/ang/crmMailing/EditMailingCtrl/base.html @@ -0,0 +1,8 @@ +
+ +
+ {{ts('This mailing has been submitted.')}} +
+ +
+
\ No newline at end of file diff --git a/ang/crmMailing/EditMailingCtrl/unified.html b/ang/crmMailing/EditMailingCtrl/unified.html index 216e61725d..3d772b73af 100644 --- a/ang/crmMailing/EditMailingCtrl/unified.html +++ b/ang/crmMailing/EditMailingCtrl/unified.html @@ -1,10 +1,4 @@ -
- -
- {{ts('This mailing has been submitted.')}} -
- -
+
@@ -43,7 +37,7 @@
- +
- +
diff --git a/ang/crmMailing/EditMailingCtrl/unified2.html b/ang/crmMailing/EditMailingCtrl/unified2.html index 4ceb20ce07..c99957012b 100644 --- a/ang/crmMailing/EditMailingCtrl/unified2.html +++ b/ang/crmMailing/EditMailingCtrl/unified2.html @@ -1,10 +1,4 @@ -
- -
- {{ts('This mailing has been submitted.')}} -
- -
+
@@ -39,7 +33,7 @@
- +
- +
diff --git a/ang/crmMailing/EditMailingCtrl/wizard.html b/ang/crmMailing/EditMailingCtrl/wizard.html index 01f35e4379..9f3264f330 100644 --- a/ang/crmMailing/EditMailingCtrl/wizard.html +++ b/ang/crmMailing/EditMailingCtrl/wizard.html @@ -1,10 +1,4 @@ -
- -
- {{ts('This mailing has been submitted.')}} -
- -
+
@@ -51,7 +45,7 @@
- +
{{ts('Submit Mailing')}}
@@ -68,4 +62,4 @@
- +
diff --git a/ang/crmMailing/EditMailingCtrl/workflow.html b/ang/crmMailing/EditMailingCtrl/workflow.html index 29c65908f1..8bcec13b58 100644 --- a/ang/crmMailing/EditMailingCtrl/workflow.html +++ b/ang/crmMailing/EditMailingCtrl/workflow.html @@ -1,10 +1,4 @@ -
- -
- {{ts('This mailing has been submitted.')}} -
- -
+
diff --git a/ang/crmMailing/services.js b/ang/crmMailing/services.js index ad83a00283..6c60d338b3 100644 --- a/ang/crmMailing/services.js +++ b/ang/crmMailing/services.js @@ -155,6 +155,9 @@ groups: {include: [], exclude: [], base: []}, mailings: {include: [], exclude: []} }, + template_type: "traditional", + // Workaround CRM-19756 w/template_options.nonce + template_options: {nonce: 1}, name: "", campaign_id: null, replyto_email: "", diff --git a/tests/phpunit/Civi/Angular/ManagerTest.php b/tests/phpunit/Civi/Angular/ManagerTest.php index 6d70990fd9..2aca5468ee 100644 --- a/tests/phpunit/Civi/Angular/ManagerTest.php +++ b/tests/phpunit/Civi/Angular/ManagerTest.php @@ -109,7 +109,7 @@ class ManagerTest extends \CiviUnitTestCase { */ public function testGetPartials() { $partials = $this->angular->getPartials('crmMailing'); - $this->assertRegExp('/\assertRegExp('/ng-form="crmMailing/', $partials['~/crmMailing/EditMailingCtrl/2step.html']); // If crmMailing changes, feel free to use a different example. } -- 2.25.1