From aadc85822a6b56c75372d93bba465290ba03de7b 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/Mailing/BAO/Mailing.php | 72 +++++++++++++++++++++++++ CRM/Mailing/Info.php | 1 + CRM/Upgrade/Incremental/php/FourSix.php | 21 ++++++++ CRM/Utils/Hook.php | 17 ++++++ tests/phpunit/api/v3/MailingTest.php | 8 +++ xml/schema/Mailing/Mailing.xml | 18 +++++++ 6 files changed, 137 insertions(+) diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index 8ae764f4f9..150d34cbc2 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -3188,4 +3188,76 @@ AND m.id = %1 return $fieldPerms; } + /** + * Whitelist of possible values for the entity_table field + * @return array + */ + public static function mailingGroupEntityTables($context = NULL) { + return array( + CRM_Contact_BAO_Group::getTableName() => 'Group', + CRM_Mailing_BAO_Mailing::getTableName() => 'Mailing', + ); + } + + /** + * Get the public view url. + * + * @param int $id + * @param bool $absolute + * + * @return string + */ + public static function getPublicViewUrl($id, $absolute = TRUE) { + if ((civicrm_api3('Mailing', 'getvalue', array('id' => $id, 'return' => 'visibility'))) === 'Public Pages') { + return CRM_Utils_System::url('civicrm/mailing/view', array('id' => $id), $absolute, NULL, TRUE, TRUE); + } + } + + /** + * @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/Info.php b/CRM/Mailing/Info.php index 2c06ebf104..44024b5c61 100644 --- a/CRM/Mailing/Info.php +++ b/CRM/Mailing/Info.php @@ -165,6 +165,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/FourSix.php b/CRM/Upgrade/Incremental/php/FourSix.php index 871daa2d2b..cfb2118596 100644 --- a/CRM/Upgrade/Incremental/php/FourSix.php +++ b/CRM/Upgrade/Incremental/php/FourSix.php @@ -285,6 +285,27 @@ class CRM_Upgrade_Incremental_php_FourSix { $this->addTask('Set Remote Submissions setting', 'setRemoteSubmissionsSetting', $rev); } + /** + * Upgrade function. + * + * @param string $rev + */ + public function upgrade_4_6_26($rev) { + $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'task_4_6_x_runSql', $rev); + $this->addTask('Add new CiviMail fields', 'addMailingTemplateType'); + } + + 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; + } + /** * Add Getting Started dashlet to dashboard * diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index 9d1ac30517..e09daccf08 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -862,6 +862,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 deec2f8a44..3ce1df7b22 100755 --- a/tests/phpunit/api/v3/MailingTest.php +++ b/tests/phpunit/api/v3/MailingTest.php @@ -86,6 +86,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 ca61d15cc3..a24fccd48c 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