From 703875d8a8fe2e2e5a43e08f776381bcbf3d98c2 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 28 Nov 2016 14:09:20 -0800 Subject: [PATCH] CRM-19690 - Declare Mailing.template_type, Mailing.template_options, Hook::mailingTemplateTypes. This adds two new fields to the schema. Template types can be declared (along with preferred editor) using hook_civicrm_mailingTemplateTypes, e.g. ``` function mymod_civicrm_mailingTemplateTypes(&$types) { $types[] = array( 'name' => 'moasico', 'editorUrl' => '~/crmMosaico/EditMailingCtrl/mosaico.html', ); } ``` Note: This only stores data. Other commits will make use of that data in more meaningful ways. --- CRM/Core/DAO/AllCoreTables.data.php | 2 +- CRM/Mailing/BAO/Mailing.php | 47 +++++++++++++++++++++++ CRM/Mailing/DAO/Mailing.php | 33 +++++++++++++++- CRM/Mailing/Info.php | 1 + CRM/Upgrade/Incremental/php/FourSeven.php | 21 ++++++++++ CRM/Utils/Hook.php | 17 ++++++++ tests/phpunit/api/v3/MailingTest.php | 8 ++++ xml/schema/Mailing/Mailing.xml | 18 +++++++++ 8 files changed, 145 insertions(+), 2 deletions(-) diff --git a/CRM/Core/DAO/AllCoreTables.data.php b/CRM/Core/DAO/AllCoreTables.data.php index 89b41a453f..4bcc9b1564 100644 --- a/CRM/Core/DAO/AllCoreTables.data.php +++ b/CRM/Core/DAO/AllCoreTables.data.php @@ -24,7 +24,7 @@ | see the CiviCRM license FAQ at http://civicrm.org/licensing | +--------------------------------------------------------------------+ */ -// (GenCodeChecksum:bd14c54d35d01e466eec41f1605ba862) +// (GenCodeChecksum:1f9e47fc8d0661ec0b31d4cbbba6783c) return array( 'CRM_Core_DAO_AddressFormat' => array( 'name' => 'AddressFormat', diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index ec91ca3acf..fd76fa8537 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -3197,4 +3197,51 @@ AND m.id = %1 } } + /** + * @return array + * A list of template-types, keyed by name. Each defines: + * - editorUrl: string, Angular template name + * + * Ex: $templateTypes['mosaico']['editorUrl'] = '~/crmMosaico/editor.html'. + */ + public static function getTemplateTypes() { + if (!isset(Civi::$statics[__CLASS__]['templateTypes'])) { + $types = array(); + $types[] = array( + 'name' => 'traditional', + 'editorUrl' => CRM_Mailing_Info::workflowEnabled() ? '~/crmMailing/EditMailingCtrl/workflow.html' : '~/crmMailing/EditMailingCtrl/2step.html', + 'weight' => 0, + ); + + CRM_Utils_Hook::mailingTemplateTypes($types); + + $defaults = array('weight' => 0); + foreach (array_keys($types) as $typeName) { + $types[$typeName] = array_merge($defaults, $types[$typeName]); + } + usort($types, function ($a, $b) { + if ($a['weight'] === $b['weight']) { + return 0; + } + return $a['weight'] < $b['weight'] ? -1 : 1; + }); + + Civi::$statics[__CLASS__]['templateTypes'] = $types; + } + + return Civi::$statics[__CLASS__]['templateTypes']; + } + + /** + * @return array + * Array(string $name => string $label). + */ + public static function getTemplateTypeNames() { + $r = array(); + foreach (self::getTemplateTypes() as $type) { + $r[$type['name']] = $type['name']; + } + return $r; + } + } diff --git a/CRM/Mailing/DAO/Mailing.php b/CRM/Mailing/DAO/Mailing.php index b95f0eea05..bedf306e38 100644 --- a/CRM/Mailing/DAO/Mailing.php +++ b/CRM/Mailing/DAO/Mailing.php @@ -30,7 +30,7 @@ * * Generated from xml/schema/CRM/Mailing/Mailing.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:2ced8fea80d92e36fe16baa7daa73c7f) + * (GenCodeChecksum:99efa41e4294197973111d4d5ced5972) */ require_once 'CRM/Core/DAO.php'; require_once 'CRM/Utils/Type.php'; @@ -124,6 +124,18 @@ class CRM_Mailing_DAO_Mailing extends CRM_Core_DAO { * @var string */ public $replyto_email; + /** + * The language/processing system used for email templates. + * + * @var string + */ + public $template_type; + /** + * Advanced options used by the email templating system. (JSON encoded) + * + * @var longtext + */ + public $template_options; /** * Subject of mailing * @@ -444,6 +456,25 @@ class CRM_Mailing_DAO_Mailing extends CRM_Core_DAO { 'type' => 'Text', ) , ) , + 'template_type' => array( + 'name' => 'template_type', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => ts('Template Type') , + 'description' => 'The language/processing system used for email templates.', + 'required' => true, + 'maxlength' => 64, + 'size' => CRM_Utils_Type::BIG, + 'default' => 'traditional', + 'pseudoconstant' => array( + 'callback' => 'CRM_Mailing_BAO_Mailing::getTemplateTypeNames', + ) + ) , + 'template_options' => array( + 'name' => 'template_options', + 'type' => CRM_Utils_Type::T_LONGTEXT, + 'title' => ts('Template Options (JSON)') , + 'description' => 'Advanced options used by the email templating system. (JSON encoded)', + ) , 'subject' => array( 'name' => 'subject', 'type' => CRM_Utils_Type::T_STRING, diff --git a/CRM/Mailing/Info.php b/CRM/Mailing/Info.php index 3444383c03..e946b52763 100644 --- a/CRM/Mailing/Info.php +++ b/CRM/Mailing/Info.php @@ -166,6 +166,7 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info { CRM_Core_Resources::singleton() ->addSetting(array( 'crmMailing' => array( + 'templateTypes' => CRM_Mailing_BAO_Mailing::getTemplateTypes(), 'civiMails' => $civiMails['values'], 'campaignEnabled' => in_array('CiviCampaign', $config->enableComponents), 'groupNames' => $groupNames['values'], diff --git a/CRM/Upgrade/Incremental/php/FourSeven.php b/CRM/Upgrade/Incremental/php/FourSeven.php index 140eafc0e2..e51dee5950 100644 --- a/CRM/Upgrade/Incremental/php/FourSeven.php +++ b/CRM/Upgrade/Incremental/php/FourSeven.php @@ -287,6 +287,16 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev); } + /** + * Upgrade function. + * + * @param string $rev + */ + public function upgrade_4_7_16($rev) { + $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev); + $this->addTask('Add new CiviMail fields', 'addMailingTemplateType'); + } + /* * Important! All upgrade functions MUST add a 'runSql' task. * Uncomment and use the following template for a new upgrade version @@ -879,6 +889,17 @@ FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_ return TRUE; } + public static function addMailingTemplateType() { + if (!CRM_Core_DAO::checkFieldExists('civicrm_mailing', 'template_type', FALSE)) { + CRM_Core_DAO::executeQuery(' + ALTER TABLE civicrm_mailing + ADD COLUMN `template_type` varchar(64) NOT NULL DEFAULT \'traditional\' COMMENT \'The language/processing system used for email templates.\', + ADD COLUMN `template_options` longtext COMMENT \'Advanced options used by the email templating system. (JSON encoded)\' + '); + } + return TRUE; + } + /** * CRM-18651 Add DataType column to Option Group Table * @return bool diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index b762478c61..30deaad3eb 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -896,6 +896,23 @@ abstract class CRM_Utils_Hook { ); } + /** + * (Experimental) Modify the list of template-types used for CiviMail composition. + * + * @param array $types + * Sequentially indexed list of template types. Each type specifies: + * - name: string + * - editorUrl: string, Angular template URL + * - weight: int, priority when picking a default value for new mailings + * @return mixed + */ + public static function mailingTemplateTypes(&$types) { + return self::singleton()->invoke(1, $types, self::$_nullObject, self::$_nullObject, + self::$_nullObject, self::$_nullObject, self::$_nullObject, + 'civicrm_mailingTemplateTypes' + ); + } + /** * This hook is called when composing the array of membershipTypes and their cost during a membership registration * (new or renewal). diff --git a/tests/phpunit/api/v3/MailingTest.php b/tests/phpunit/api/v3/MailingTest.php index 7378b63acd..d8c65c17d6 100644 --- a/tests/phpunit/api/v3/MailingTest.php +++ b/tests/phpunit/api/v3/MailingTest.php @@ -85,6 +85,14 @@ class api_v3_MailingTest extends CiviUnitTestCase { $this->getAndCheck($this->_params, $result['id'], 'mailing'); } + /** + * + */ + public function testTemplateTypeOptions() { + $types = $this->callAPISuccess('Mailing', 'getoptions', array('field' => 'template_type')); + $this->assertTrue(isset($types['values']['traditional'])); + } + /** * The Mailing.create API supports magic properties "groups[include,enclude]" and "mailings[include,exclude]". * Make sure these work diff --git a/xml/schema/Mailing/Mailing.xml b/xml/schema/Mailing/Mailing.xml index d655f55359..20a0fe6bf4 100644 --- a/xml/schema/Mailing/Mailing.xml +++ b/xml/schema/Mailing/Mailing.xml @@ -159,6 +159,24 @@ Text + + template_type + Template Type + varchar + 64 + 'traditional' + true + The language/processing system used for email templates. + + CRM_Mailing_BAO_Mailing::getTemplateTypeNames + + + + template_options + Template Options (JSON) + longtext + Advanced options used by the email templating system. (JSON encoded) + subject varchar -- 2.25.1