// populate action links
$this->action($messageTemplate, $action, $values[$messageTemplate->id], $links, CRM_Core_Permission::EDIT);
- if (!$messageTemplate->workflow_id) {
+ if (!$messageTemplate->workflow_name) {
$userTemplates[$messageTemplate->id] = $values[$messageTemplate->id];
}
elseif (!$messageTemplate->is_reserved) {
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
+use Civi\Api4\MessageTemplate;
+
require_once 'Mail/mime.php';
/**
* @return array
* Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
* @throws \CRM_Core_Exception
+ * @throws \API_Exception
*/
public static function sendTemplate($params) {
$defaults = [
- // option group name of the template
- 'groupName' => NULL,
// option value name of the template
'valueName' => NULL,
// ID of the template
CRM_Utils_Hook::alterMailParams($params, 'messageTemplate');
- if ((!$params['groupName'] ||
- !$params['valueName']
- ) &&
- !$params['messageTemplateID']
- ) {
- throw new CRM_Core_Exception(ts("Message template's option group and/or option value or ID missing."));
+ if (!$params['valueName'] && !$params['messageTemplateID']) {
+ throw new CRM_Core_Exception(ts("Message template's option value or ID missing."));
}
+ $apiCall = MessageTemplate::get()
+ ->setCheckPermissions(FALSE)
+ ->addSelect('msg_subject', 'msg_text', 'msg_html', 'pdf_format_id', 'id')
+ ->addWhere('is_default', '=', 1);
+
if ($params['messageTemplateID']) {
- // fetch the three elements from the db based on id
- $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
- FROM civicrm_msg_template mt
- WHERE mt.id = %1 AND mt.is_default = 1';
- $sqlParams = [1 => [$params['messageTemplateID'], 'String']];
+ $apiCall->addWhere('id', '=', (int) $params['messageTemplateID']);
}
else {
- // fetch the three elements from the db based on option_group and option_value names
- $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
- FROM civicrm_msg_template mt
- JOIN civicrm_option_value ov ON workflow_id = ov.id
- JOIN civicrm_option_group og ON ov.option_group_id = og.id
- WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1';
- $sqlParams = [1 => [$params['groupName'], 'String'], 2 => [$params['valueName'], 'String']];
+ $apiCall->addWhere('workflow_name', '=', $params['valueName']);
}
- $dao = CRM_Core_DAO::executeQuery($query, $sqlParams);
- $dao->fetch();
+ $messageTemplate = $apiCall->execute()->first();
- if (!$dao->N) {
+ if (empty($messageTemplate['id'])) {
if ($params['messageTemplateID']) {
throw new CRM_Core_Exception(ts('No such message template: id=%1.', [1 => $params['messageTemplateID']]));
}
- throw new CRM_Core_Exception(ts('No such message template: option group %1, option value %2.', [
- 1 => $params['groupName'],
- 2 => $params['valueName'],
- ]));
+ throw new CRM_Core_Exception(ts('No message template with workflow name %2.', [2 => $params['valueName']]));
}
$mailContent = [
- 'subject' => $dao->subject,
- 'text' => $dao->text,
- 'html' => $dao->html,
- 'format' => $dao->format,
- 'groupName' => $params['groupName'],
+ 'subject' => $messageTemplate['msg_subject'],
+ 'text' => $messageTemplate['msg_text'],
+ 'html' => $messageTemplate['msg_html'],
+ 'format' => $messageTemplate['pdf_format_id'],
+ // Group name is a deprecated parameter. At some point it will not be passed out.
+ // https://github.com/civicrm/civicrm-core/pull/17180
+ 'groupName' => $params['groupName'] ?? NULL,
'valueName' => $params['valueName'],
'messageTemplateID' => $params['messageTemplateID'],
];
if ($params['isTest']) {
$query = "SELECT msg_subject subject, msg_text text, msg_html html
FROM civicrm_msg_template mt
- JOIN civicrm_option_value ov ON workflow_id = ov.id
- JOIN civicrm_option_group og ON ov.option_group_id = og.id
- WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1";
+ WHERE workflow_name = 'test_preview' AND mt.is_default = 1";
$testDao = CRM_Core_DAO::executeQuery($query);
$testDao->fetch();
$returnProperties,
FALSE, FALSE, NULL,
CRM_Utils_Token::flattenTokens($tokens),
- // we should consider adding groupName and valueName here
+ // we should consider adding valueName here
'CRM_Core_BAO_MessageTemplate'
);
$contact = $contact[$contactID];
[$contactID],
NULL,
CRM_Utils_Token::flattenTokens($tokens),
- // we should consider adding groupName and valueName here
+ // we should consider adding valueName here
'CRM_Core_BAO_MessageTemplate'
);
$contact = $contactArray[$contactID];
*
* Generated from xml/schema/CRM/Core/MessageTemplate.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:41d8acdc31465a852930f88cf1604224)
+ * (GenCodeChecksum:90ed9b8ad7299f01d2f83ea90b7b7a15)
*/
/**
*/
public $workflow_id;
+ /**
+ * @var string
+ */
+ public $workflow_name;
+
/**
* is this the default message template for the workflow referenced by workflow_id?
*
'workflow_id' => [
'name' => 'workflow_id',
'type' => CRM_Utils_Type::T_INT,
- 'title' => ts('Message Template Workflow'),
+ 'title' => ts('Deprecated field for Message Template Workflow.'),
'description' => ts('a pseudo-FK to civicrm_option_value'),
'where' => 'civicrm_msg_template.workflow_id',
'table_name' => 'civicrm_msg_template',
'bao' => 'CRM_Core_BAO_MessageTemplate',
'localizable' => 0,
],
+ 'workflow_name' => [
+ 'name' => 'workflow_name',
+ 'type' => CRM_Utils_Type::T_STRING,
+ 'title' => ts('Message Template Workflow Name'),
+ 'maxlength' => 255,
+ 'size' => CRM_Utils_Type::HUGE,
+ 'where' => 'civicrm_msg_template.workflow_name',
+ 'table_name' => 'civicrm_msg_template',
+ 'entity' => 'MessageTemplate',
+ 'bao' => 'CRM_Core_BAO_MessageTemplate',
+ 'localizable' => 0,
+ ],
'is_default' => [
'name' => 'is_default',
'type' => CRM_Utils_Type::T_BOOLEAN,
public function upgrade_5_26_alpha1($rev) {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
$this->addTask('Add option value for nl_BE', 'addNLBEOptionValue');
+ $this->addTask('Add workflow_name to civicrm_msg_template', 'addColumn', 'civicrm_msg_template', 'workflow_name',
+ "VARCHAR(255) DEFAULT NULL COMMENT 'Name of workflow'", FALSE, '5.26.0');
+ $this->addTask('Populate workflow_name in civicrm_msg_template', 'populateWorkflowName');
+
// Additional tasks here...
// Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex.
// The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable.
}
+ /**
+ * Update workflow_name based on workflow_id values.
+ */
+ public function populateWorkflowName() {
+ CRM_Core_DAO::executeQuery('UPDATE civicrm_msg_template
+ LEFT JOIN civicrm_option_value ov ON ov.id = workflow_id
+ SET workflow_name = ov.name'
+ );
+ return TRUE;
+ }
+
/**
* Add option value for nl_BE language.
*
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
- * $Id$
- *
*/
* Sends a template.
*
* @param array $params
+ *
* @throws API_Exception
+ * @throws \CRM_Core_Exception
*/
function civicrm_api3_message_template_send($params) {
// Change external param names to internal ones
}
}
if (empty($params['messageTemplateID'])) {
- if (empty($params['groupName']) || empty($params['valueName'])) {
+ if (empty($params['valueName'])) {
// Can't use civicrm_api3_verify_mandatory for this because it would give the wrong field names
throw new API_Exception(
- "Mandatory key(s) missing from params array: requires id or option_group_name + option_value_name",
- "mandatory_missing",
- ["fields" => ['id', 'option_group_name', 'option_value_name']]
+ 'Mandatory key(s) missing from params array: requires id or option_value_name',
+ 'mandatory_missing',
+ ['fields' => ['id', 'option_value_name']]
);
}
}
$params['id']['api.aliases'] = ['messageTemplateID', 'message_template_id'];
$params['id']['type'] = CRM_Utils_Type::T_INT;
- $params['option_group_name']['description'] = 'option group name of the template (required if no id supplied)';
- $params['option_group_name']['title'] = 'Option Group Name';
- $params['option_group_name']['api.aliases'] = ['groupName'];
- $params['option_group_name']['type'] = CRM_Utils_Type::T_STRING;
-
$params['option_value_name']['description'] = 'option value name of the template (required if no id supplied)';
$params['option_value_name']['title'] = 'Option Value Name';
$params['option_value_name']['api.aliases'] = ['valueName'];
parent::tearDown();
}
+ /**
+ * Test message template send.
+ *
+ * @throws \CRM_Core_Exception
+ */
public function testCaseActivityCopyTemplate() {
$client_id = $this->individualCreate();
$contact_id = $this->individualCreate();
'idHash' => substr(sha1(CIVICRM_SITE_KEY . '1234'), 0, 7),
];
- list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(
+ list($sent, $subject, $message) = CRM_Core_BAO_MessageTemplate::sendTemplate(
[
- 'groupName' => 'msg_tpl_workflow_case',
'valueName' => 'case_activity',
'contactId' => $contact_id,
'tplParams' => $tplParams,
/**
* Test civicrm_activity_create() with valid parameters.
+ *
+ * @throws \CRM_Core_Exception
*/
public function testActivityCreate() {
</field>
<field>
<name>workflow_id</name>
- <title>Message Template Workflow</title>
+ <title>Deprecated field for Message Template Workflow.</title>
<type>int unsigned</type>
<comment>a pseudo-FK to civicrm_option_value</comment>
<add>3.1</add>
</field>
+ <field>
+ <name>workflow_name</name>
+ <title>Message Template Workflow Name</title>
+ <type>varchar</type>
+ <length>255</length>
+ <add>5.26</add>
+ </field>
<field>
<name>is_default</name>
<title>Message Template Is Default?</title>
{/foreach}
INSERT INTO civicrm_msg_template
- (msg_title, msg_subject, msg_text, msg_html, workflow_id, is_default, is_reserved) VALUES
+ (msg_title, msg_subject, msg_text, msg_html, workflow_name, workflow_id, is_default, is_reserved) VALUES
{foreach from=$ovNames key=gName item=ovs name=for_groups}
{foreach from=$ovs key=vName item=title name=for_values}
{fetch assign=subject file="`$gencodeXmlDir`/templates/message_templates/`$vName`_subject.tpl"}
{fetch assign=text file="`$gencodeXmlDir`/templates/message_templates/`$vName`_text.tpl"}
{fetch assign=html file="`$gencodeXmlDir`/templates/message_templates/`$vName`_html.tpl"}
- ('{$title}', '{$subject|escape:"quotes"}', '{$text|escape:"quotes"}', '{$html|escape:"quotes"}', @tpl_ovid_{$vName}, 1, 0),
- ('{$title}', '{$subject|escape:"quotes"}', '{$text|escape:"quotes"}', '{$html|escape:"quotes"}', @tpl_ovid_{$vName}, 0, 1) {if $smarty.foreach.for_groups.last and $smarty.foreach.for_values.last};{else},{/if}
+ ('{$title}', '{$subject|escape:"quotes"}', '{$text|escape:"quotes"}', '{$html|escape:"quotes"}', '{$vName}', @tpl_ovid_{$vName}, 1, 0),
+ ('{$title}', '{$subject|escape:"quotes"}', '{$text|escape:"quotes"}', '{$html|escape:"quotes"}', '{$vName}', @tpl_ovid_{$vName}, 0, 1) {if $smarty.foreach.for_groups.last and $smarty.foreach.for_values.last};{else},{/if}
{/foreach}
{/foreach}