3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
36 require_once 'Mail/mime.php';
39 * Class CRM_Core_BAO_MessageTemplate
41 class CRM_Core_BAO_MessageTemplate
extends CRM_Core_DAO_MessageTemplate
{
44 * Fetch object based on array of properties
46 * @param array $params
47 * (reference ) an assoc array of name/value pairs.
48 * @param array $defaults
49 * (reference ) an assoc array to hold the flattened values.
51 * @return CRM_Core_BAO_MessageTemplate object
54 public static function retrieve(&$params, &$defaults) {
55 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
56 $messageTemplates->copyValues($params);
57 if ($messageTemplates->find(TRUE)) {
58 CRM_Core_DAO
::storeValues($messageTemplates, $defaults);
59 return $messageTemplates;
65 * Update the is_active flag in the db
68 * Id of the database record.
69 * @param bool $is_active
70 * Value we want to set the is_active field.
73 * DAO object on sucess, NULL otherwise
76 public static function setIsActive($id, $is_active) {
77 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_MessageTemplate', $id, 'is_active', $is_active);
81 * Add the Message Templates
83 * @param array $params
84 * Reference array contains the values submitted by the form.
90 public static function add(&$params) {
91 $hook = empty($params['id']) ?
'create' : 'edit';
92 CRM_Utils_Hook
::pre($hook, 'MessageTemplate', CRM_Utils_Array
::value('id', $params), $params);
94 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
95 $messageTemplates->copyValues($params);
96 $messageTemplates->save();
98 CRM_Utils_Hook
::post($hook, 'MessageTemplate', $messageTemplates->id
, $messageTemplates);
99 return $messageTemplates;
103 * Delete the Message Templates
107 * @param int $messageTemplatesID
111 public static function del($messageTemplatesID) {
112 // make sure messageTemplatesID is an integer
113 if (!CRM_Utils_Rule
::positiveInteger($messageTemplatesID)) {
114 CRM_Core_Error
::fatal(ts('Invalid Message template'));
117 // Set mailing msg template col to NULL
118 $query = "UPDATE civicrm_mailing
119 SET msg_template_id = NULL
120 WHERE msg_template_id = %1";
122 $params = array(1 => array($messageTemplatesID, 'Integer'));
123 CRM_Core_DAO
::executeQuery($query, $params);
125 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
126 $messageTemplates->id
= $messageTemplatesID;
127 $messageTemplates->delete();
128 CRM_Core_Session
::setStatus(ts('Selected message template has been deleted.'), ts('Deleted'), 'success');
132 * Get the Message Templates
140 public static function getMessageTemplates($all = TRUE, $isSMS = FALSE) {
143 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
144 $messageTemplates->is_active
= 1;
145 $messageTemplates->is_sms
= $isSMS;
148 $messageTemplates->workflow_id
= 'NULL';
150 $messageTemplates->find();
151 while ($messageTemplates->fetch()) {
152 $msgTpls[$messageTemplates->id
] = $messageTemplates->msg_title
;
159 * @param int $contactId
161 * @param int $messageTemplateID
166 public static function sendReminder($contactId, $email, $messageTemplateID, $from) {
168 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
169 $messageTemplates->id
= $messageTemplateID;
171 $domain = CRM_Core_BAO_Domain
::getDomain();
173 $hookTokens = array();
175 if ($messageTemplates->find(TRUE)) {
176 $body_text = $messageTemplates->msg_text
;
177 $body_html = $messageTemplates->msg_html
;
178 $body_subject = $messageTemplates->msg_subject
;
180 $body_text = CRM_Utils_String
::htmlToText($body_html);
183 $params = array(array('contact_id', '=', $contactId, 0, 0));
184 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($params);
187 $contact = reset($contact);
189 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
195 // get tokens to be replaced
196 $tokens = array_merge(CRM_Utils_Token
::getTokens($body_text),
197 CRM_Utils_Token
::getTokens($body_html),
198 CRM_Utils_Token
::getTokens($body_subject));
200 // get replacement text for these tokens
201 $returnProperties = array("preferred_mail_format" => 1);
202 if (isset($tokens['contact'])) {
203 foreach ($tokens['contact'] as $key => $value) {
204 $returnProperties[$value] = 1;
207 list($details) = CRM_Utils_Token
::getTokenDetails(array($contactId),
211 'CRM_Core_BAO_MessageTemplate');
212 $contact = reset($details);
215 $hookTokens = array();
216 CRM_Utils_Hook
::tokens($hookTokens);
217 $categories = array_keys($hookTokens);
219 // do replacements in text and html body
220 $type = array('html', 'text');
221 foreach ($type as $key => $value) {
222 $bodyType = "body_{$value}";
224 CRM_Utils_Token
::replaceGreetingTokens($
$bodyType, NULL, $contact['contact_id']);
225 $
$bodyType = CRM_Utils_Token
::replaceDomainTokens($
$bodyType, $domain, TRUE, $tokens, TRUE);
226 $
$bodyType = CRM_Utils_Token
::replaceContactTokens($
$bodyType, $contact, FALSE, $tokens, FALSE, TRUE);
227 $
$bodyType = CRM_Utils_Token
::replaceComponentTokens($
$bodyType, $contact, $tokens, TRUE);
228 $
$bodyType = CRM_Utils_Token
::replaceHookTokens($
$bodyType, $contact, $categories, TRUE);
234 $smarty = CRM_Core_Smarty
::singleton();
236 'text', 'html') as $elem) {
237 $
$elem = $smarty->fetch("string:{$$elem}");
240 // do replacements in message subject
241 $messageSubject = CRM_Utils_Token
::replaceContactTokens($body_subject, $contact, FALSE, $tokens);
242 $messageSubject = CRM_Utils_Token
::replaceDomainTokens($messageSubject, $domain, TRUE, $tokens);
243 $messageSubject = CRM_Utils_Token
::replaceComponentTokens($messageSubject, $contact, $tokens, TRUE);
244 $messageSubject = CRM_Utils_Token
::replaceHookTokens($messageSubject, $contact, $categories, TRUE);
246 $messageSubject = $smarty->fetch("string:{$messageSubject}");
248 // set up the parameters for CRM_Utils_Mail::send
250 'groupName' => 'Scheduled Reminder Sender',
252 'toName' => $contact['display_name'],
254 'subject' => $messageSubject,
256 if (!$html ||
$contact['preferred_mail_format'] == 'Text' ||
257 $contact['preferred_mail_format'] == 'Both'
259 // render the & entities in text mode, so that the links work
260 $mailParams['text'] = str_replace('&', '&', $text);
262 if ($html && ($contact['preferred_mail_format'] == 'HTML' ||
263 $contact['preferred_mail_format'] == 'Both'
265 $mailParams['html'] = $html;
268 $result = CRM_Utils_Mail
::send($mailParams);
271 $messageTemplates->free();
277 * Revert a message template to its default subject+text+HTML state
279 * @param int id id of the template
283 public static function revert($id) {
284 $diverted = new self
;
285 $diverted->id
= (int) $id;
288 if ($diverted->N
!= 1) {
289 CRM_Core_Error
::fatal(ts('Did not find a message template with id of %1.', array(1 => $id)));
293 $orig->workflow_id
= $diverted->workflow_id
;
294 $orig->is_reserved
= 1;
298 CRM_Core_Error
::fatal(ts('Message template with id of %1 does not have a default to revert to.', array(1 => $id)));
301 $diverted->msg_subject
= $orig->msg_subject
;
302 $diverted->msg_text
= $orig->msg_text
;
303 $diverted->msg_html
= $orig->msg_html
;
304 $diverted->pdf_format_id
= is_null($orig->pdf_format_id
) ?
'null' : $orig->pdf_format_id
;
309 * Send an email from the specified template based on an array of params
311 * @param array $params
312 * A string-keyed array of function params, see function body for details.
315 * of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
317 public static function sendTemplate($params) {
319 // option group name of the template
321 // option value name of the template
323 // ID of the template
324 'messageTemplateID' => NULL,
325 // contact id if contact tokens are to be replaced
327 // additional template params (other than the ones already set in the template singleton)
328 'tplParams' => array(),
331 // the recipient’s name
333 // the recipient’s email - mail is sent only if set
339 // the Reply-To: header
342 'attachments' => NULL,
343 // whether this is a test email (and hence should include the test banner)
345 // filename of optional PDF version to add as attachment (do not include path)
346 'PDFFilename' => NULL,
348 $params = array_merge($defaults, $params);
350 if ((!$params['groupName'] ||
351 !$params['valueName']
353 !$params['messageTemplateID']
355 CRM_Core_Error
::fatal(ts("Message template's option group and/or option value or ID missing."));
358 if ($params['messageTemplateID']) {
359 // fetch the three elements from the db based on id
360 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
361 FROM civicrm_msg_template mt
362 WHERE mt.id = %1 AND mt.is_default = 1';
363 $sqlParams = array(1 => array($params['messageTemplateID'], 'String'));
366 // fetch the three elements from the db based on option_group and option_value names
367 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
368 FROM civicrm_msg_template mt
369 JOIN civicrm_option_value ov ON workflow_id = ov.id
370 JOIN civicrm_option_group og ON ov.option_group_id = og.id
371 WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1';
372 $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String'));
374 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
378 if ($params['messageTemplateID']) {
379 CRM_Core_Error
::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID'])));
382 CRM_Core_Error
::fatal(ts('No such message template: option group %1, option value %2.', array(1 => $params['groupName'], 2 => $params['valueName'])));
386 $subject = $dao->subject
;
389 $format = $dao->format
;
392 // add the test banner (if requested)
393 if ($params['isTest']) {
394 $query = "SELECT msg_subject subject, msg_text text, msg_html html
395 FROM civicrm_msg_template mt
396 JOIN civicrm_option_value ov ON workflow_id = ov.id
397 JOIN civicrm_option_group og ON ov.option_group_id = og.id
398 WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1";
399 $testDao = CRM_Core_DAO
::executeQuery($query);
402 $subject = $testDao->subject
. $subject;
403 $text = $testDao->text
. $text;
404 $html = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $html);
408 // replace tokens in the three elements (in subject as if it was the text body)
409 $domain = CRM_Core_BAO_Domain
::getDomain();
410 $hookTokens = array();
411 $mailing = new CRM_Mailing_BAO_Mailing
;
412 $mailing->body_text
= $text;
413 $mailing->body_html
= $html;
414 $tokens = $mailing->getTokens();
415 CRM_Utils_Hook
::tokens($hookTokens);
416 $categories = array_keys($hookTokens);
418 $contactID = CRM_Utils_Array
::value('contactId', $params);
421 $contactParams = array('contact_id' => $contactID);
422 $returnProperties = array();
424 if (isset($tokens['text']['contact'])) {
425 foreach ($tokens['text']['contact'] as $name) {
426 $returnProperties[$name] = 1;
430 if (isset($tokens['html']['contact'])) {
431 foreach ($tokens['html']['contact'] as $name) {
432 $returnProperties[$name] = 1;
435 list($contact) = CRM_Utils_Token
::getTokenDetails($contactParams,
438 CRM_Utils_Token
::flattenTokens($tokens),
439 // we should consider adding groupName and valueName here
440 'CRM_Core_BAO_MessageTemplate'
442 $contact = $contact[$contactID];
445 $subject = CRM_Utils_Token
::replaceDomainTokens($subject, $domain, FALSE, $tokens['text'], TRUE);
446 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text'], TRUE);
447 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html'], TRUE);
450 $subject = CRM_Utils_Token
::replaceContactTokens($subject, $contact, FALSE, $tokens['text'], FALSE, TRUE);
451 $text = CRM_Utils_Token
::replaceContactTokens($text, $contact, FALSE, $tokens['text'], FALSE, TRUE);
452 $html = CRM_Utils_Token
::replaceContactTokens($html, $contact, FALSE, $tokens['html'], FALSE, TRUE);
455 $contactArray = array($contactID => $contact);
456 CRM_Utils_Hook
::tokenValues($contactArray,
459 CRM_Utils_Token
::flattenTokens($tokens),
460 // we should consider adding groupName and valueName here
461 'CRM_Core_BAO_MessageTemplate'
463 $contact = $contactArray[$contactID];
465 $subject = CRM_Utils_Token
::replaceHookTokens($subject, $contact, $categories, TRUE);
466 $text = CRM_Utils_Token
::replaceHookTokens($text, $contact, $categories, TRUE);
467 $html = CRM_Utils_Token
::replaceHookTokens($html, $contact, $categories, TRUE);
470 // strip whitespace from ends and turn into a single line
471 $subject = "{strip}$subject{/strip}";
473 // parse the three elements with Smarty
476 $smarty = CRM_Core_Smarty
::singleton();
477 foreach ($params['tplParams'] as $name => $value) {
478 $smarty->assign($name, $value);
481 'subject', 'text', 'html') as $elem) {
482 $
$elem = $smarty->fetch("string:{$$elem}");
485 // send the template, honouring the target user’s preferences (if any)
488 // create the params array
489 $params['subject'] = $subject;
490 $params['text'] = $text;
491 $params['html'] = $html;
493 if ($params['toEmail']) {
494 $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1));
495 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($contactParams);
497 $prefs = array_pop($contact);
499 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') {
500 $params['text'] = NULL;
503 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') {
504 $params['html'] = NULL;
507 $config = CRM_Core_Config
::singleton();
508 if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) {
509 $pdfHtml = CRM_Contribute_BAO_ContributionPage
::addInvoicePdfToEmail($params['contributionId'], $params['contactId']);
510 if (empty($params['attachments'])) {
511 $params['attachments'] = array();
513 $params['attachments'][] = CRM_Utils_Mail
::appendPDF('Invoice.pdf', $pdfHtml, $format);
516 if ($config->doNotAttachPDFReceipt
&&
517 $params['PDFFilename'] &&
520 if (empty($params['attachments'])) {
521 $params['attachments'] = array();
523 $params['attachments'][] = CRM_Utils_Mail
::appendPDF($params['PDFFilename'], $params['html'], $format);
524 if (isset($params['tplParams']['email_comment'])) {
525 $params['html'] = $params['tplParams']['email_comment'];
526 $params['text'] = strip_tags($params['tplParams']['email_comment']);
530 $sent = CRM_Utils_Mail
::send($params);
533 unlink($pdf_filename);
537 return array($sent, $subject, $text, $html);