3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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
53 public static function retrieve(&$params, &$defaults) {
54 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
55 $messageTemplates->copyValues($params);
56 if ($messageTemplates->find(TRUE)) {
57 CRM_Core_DAO
::storeValues($messageTemplates, $defaults);
58 return $messageTemplates;
64 * Update the is_active flag in the db.
67 * Id of the database record.
68 * @param bool $is_active
69 * Value we want to set the is_active field.
72 * DAO object on sucess, NULL otherwise
74 public static function setIsActive($id, $is_active) {
75 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_MessageTemplate', $id, 'is_active', $is_active);
79 * Add the Message Templates.
81 * @param array $params
82 * Reference array contains the values submitted by the form.
87 public static function add(&$params) {
88 $hook = empty($params['id']) ?
'create' : 'edit';
89 CRM_Utils_Hook
::pre($hook, 'MessageTemplate', CRM_Utils_Array
::value('id', $params), $params);
91 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
92 $messageTemplates->copyValues($params);
93 $messageTemplates->save();
95 CRM_Utils_Hook
::post($hook, 'MessageTemplate', $messageTemplates->id
, $messageTemplates);
96 return $messageTemplates;
100 * Delete the Message Templates.
102 * @param int $messageTemplatesID
105 public static function del($messageTemplatesID) {
106 // make sure messageTemplatesID is an integer
107 if (!CRM_Utils_Rule
::positiveInteger($messageTemplatesID)) {
108 CRM_Core_Error
::fatal(ts('Invalid Message template'));
111 // Set mailing msg template col to NULL
112 $query = "UPDATE civicrm_mailing
113 SET msg_template_id = NULL
114 WHERE msg_template_id = %1";
116 $params = array(1 => array($messageTemplatesID, 'Integer'));
117 CRM_Core_DAO
::executeQuery($query, $params);
119 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
120 $messageTemplates->id
= $messageTemplatesID;
121 $messageTemplates->delete();
122 CRM_Core_Session
::setStatus(ts('Selected message template has been deleted.'), ts('Deleted'), 'success');
126 * Get the Message Templates.
133 public static function getMessageTemplates($all = TRUE, $isSMS = FALSE) {
136 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
137 $messageTemplates->is_active
= 1;
138 $messageTemplates->is_sms
= $isSMS;
141 $messageTemplates->workflow_id
= 'NULL';
143 $messageTemplates->find();
144 while ($messageTemplates->fetch()) {
145 $msgTpls[$messageTemplates->id
] = $messageTemplates->msg_title
;
152 * @param int $contactId
154 * @param int $messageTemplateID
159 public static function sendReminder($contactId, $email, $messageTemplateID, $from) {
161 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
162 $messageTemplates->id
= $messageTemplateID;
164 $domain = CRM_Core_BAO_Domain
::getDomain();
166 $hookTokens = array();
168 if ($messageTemplates->find(TRUE)) {
169 $body_text = $messageTemplates->msg_text
;
170 $body_html = $messageTemplates->msg_html
;
171 $body_subject = $messageTemplates->msg_subject
;
173 $body_text = CRM_Utils_String
::htmlToText($body_html);
176 $params = array(array('contact_id', '=', $contactId, 0, 0));
177 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($params);
180 $contact = reset($contact);
182 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
188 // get tokens to be replaced
189 $tokens = array_merge(CRM_Utils_Token
::getTokens($body_text),
190 CRM_Utils_Token
::getTokens($body_html),
191 CRM_Utils_Token
::getTokens($body_subject));
193 // get replacement text for these tokens
194 $returnProperties = array("preferred_mail_format" => 1);
195 if (isset($tokens['contact'])) {
196 foreach ($tokens['contact'] as $key => $value) {
197 $returnProperties[$value] = 1;
200 list($details) = CRM_Utils_Token
::getTokenDetails(array($contactId),
204 'CRM_Core_BAO_MessageTemplate');
205 $contact = reset($details);
208 $hookTokens = array();
209 CRM_Utils_Hook
::tokens($hookTokens);
210 $categories = array_keys($hookTokens);
212 // do replacements in text and html body
213 $type = array('html', 'text');
214 foreach ($type as $key => $value) {
215 $bodyType = "body_{$value}";
217 CRM_Utils_Token
::replaceGreetingTokens($
$bodyType, NULL, $contact['contact_id']);
218 $
$bodyType = CRM_Utils_Token
::replaceDomainTokens($
$bodyType, $domain, TRUE, $tokens, TRUE);
219 $
$bodyType = CRM_Utils_Token
::replaceContactTokens($
$bodyType, $contact, FALSE, $tokens, FALSE, TRUE);
220 $
$bodyType = CRM_Utils_Token
::replaceComponentTokens($
$bodyType, $contact, $tokens, TRUE);
221 $
$bodyType = CRM_Utils_Token
::replaceHookTokens($
$bodyType, $contact, $categories, TRUE);
227 $smarty = CRM_Core_Smarty
::singleton();
232 $
$elem = $smarty->fetch("string:{$$elem}");
235 // do replacements in message subject
236 $messageSubject = CRM_Utils_Token
::replaceContactTokens($body_subject, $contact, FALSE, $tokens);
237 $messageSubject = CRM_Utils_Token
::replaceDomainTokens($messageSubject, $domain, TRUE, $tokens);
238 $messageSubject = CRM_Utils_Token
::replaceComponentTokens($messageSubject, $contact, $tokens, TRUE);
239 $messageSubject = CRM_Utils_Token
::replaceHookTokens($messageSubject, $contact, $categories, TRUE);
241 $messageSubject = $smarty->fetch("string:{$messageSubject}");
243 // set up the parameters for CRM_Utils_Mail::send
245 'groupName' => 'Scheduled Reminder Sender',
247 'toName' => $contact['display_name'],
249 'subject' => $messageSubject,
251 if (!$html ||
$contact['preferred_mail_format'] == 'Text' ||
252 $contact['preferred_mail_format'] == 'Both'
254 // render the & entities in text mode, so that the links work
255 $mailParams['text'] = str_replace('&', '&', $text);
257 if ($html && ($contact['preferred_mail_format'] == 'HTML' ||
258 $contact['preferred_mail_format'] == 'Both'
261 $mailParams['html'] = $html;
264 $result = CRM_Utils_Mail
::send($mailParams);
267 $messageTemplates->free();
273 * Revert a message template to its default subject+text+HTML state
275 * @param int $id id of the template
279 public static function revert($id) {
280 $diverted = new CRM_Core_BAO_MessageTemplate();
281 $diverted->id
= (int) $id;
284 if ($diverted->N
!= 1) {
285 CRM_Core_Error
::fatal(ts('Did not find a message template with id of %1.', array(1 => $id)));
288 $orig = new CRM_Core_BAO_MessageTemplate();
289 $orig->workflow_id
= $diverted->workflow_id
;
290 $orig->is_reserved
= 1;
294 CRM_Core_Error
::fatal(ts('Message template with id of %1 does not have a default to revert to.', array(1 => $id)));
297 $diverted->msg_subject
= $orig->msg_subject
;
298 $diverted->msg_text
= $orig->msg_text
;
299 $diverted->msg_html
= $orig->msg_html
;
300 $diverted->pdf_format_id
= is_null($orig->pdf_format_id
) ?
'null' : $orig->pdf_format_id
;
305 * Send an email from the specified template based on an array of params.
307 * @param array $params
308 * A string-keyed array of function params, see function body for details.
311 * Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
313 public static function sendTemplate($params) {
315 // option group name of the template
317 // option value name of the template
319 // ID of the template
320 'messageTemplateID' => NULL,
321 // contact id if contact tokens are to be replaced
323 // additional template params (other than the ones already set in the template singleton)
324 'tplParams' => array(),
327 // the recipient’s name
329 // the recipient’s email - mail is sent only if set
335 // the Reply-To: header
338 'attachments' => NULL,
339 // whether this is a test email (and hence should include the test banner)
341 // filename of optional PDF version to add as attachment (do not include path)
342 'PDFFilename' => NULL,
344 $params = array_merge($defaults, $params);
346 if ((!$params['groupName'] ||
347 !$params['valueName']
349 !$params['messageTemplateID']
351 CRM_Core_Error
::fatal(ts("Message template's option group and/or option value or ID missing."));
354 if ($params['messageTemplateID']) {
355 // fetch the three elements from the db based on id
356 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
357 FROM civicrm_msg_template mt
358 WHERE mt.id = %1 AND mt.is_default = 1';
359 $sqlParams = array(1 => array($params['messageTemplateID'], 'String'));
362 // fetch the three elements from the db based on option_group and option_value names
363 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
364 FROM civicrm_msg_template mt
365 JOIN civicrm_option_value ov ON workflow_id = ov.id
366 JOIN civicrm_option_group og ON ov.option_group_id = og.id
367 WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1';
368 $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String'));
370 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
374 if ($params['messageTemplateID']) {
375 CRM_Core_Error
::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID'])));
378 CRM_Core_Error
::fatal(ts('No such message template: option group %1, option value %2.', array(
379 1 => $params['groupName'],
380 2 => $params['valueName'],
385 $subject = $dao->subject
;
388 $format = $dao->format
;
391 // add the test banner (if requested)
392 if ($params['isTest']) {
393 $query = "SELECT msg_subject subject, msg_text text, msg_html html
394 FROM civicrm_msg_template mt
395 JOIN civicrm_option_value ov ON workflow_id = ov.id
396 JOIN civicrm_option_group og ON ov.option_group_id = og.id
397 WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1";
398 $testDao = CRM_Core_DAO
::executeQuery($query);
401 $subject = $testDao->subject
. $subject;
402 $text = $testDao->text
. $text;
403 $html = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $html);
407 // replace tokens in the three elements (in subject as if it was the text body)
408 $domain = CRM_Core_BAO_Domain
::getDomain();
409 $hookTokens = array();
410 $mailing = new CRM_Mailing_BAO_Mailing();
411 $mailing->body_text
= $text;
412 $mailing->body_html
= $html;
413 $tokens = $mailing->getTokens();
414 CRM_Utils_Hook
::tokens($hookTokens);
415 $categories = array_keys($hookTokens);
417 $contactID = CRM_Utils_Array
::value('contactId', $params);
420 $contactParams = array('contact_id' => $contactID);
421 $returnProperties = array();
423 if (isset($tokens['text']['contact'])) {
424 foreach ($tokens['text']['contact'] as $name) {
425 $returnProperties[$name] = 1;
429 if (isset($tokens['html']['contact'])) {
430 foreach ($tokens['html']['contact'] as $name) {
431 $returnProperties[$name] = 1;
434 list($contact) = CRM_Utils_Token
::getTokenDetails($contactParams,
437 CRM_Utils_Token
::flattenTokens($tokens),
438 // we should consider adding groupName and valueName here
439 'CRM_Core_BAO_MessageTemplate'
441 $contact = $contact[$contactID];
444 $subject = CRM_Utils_Token
::replaceDomainTokens($subject, $domain, FALSE, $tokens['text'], TRUE);
445 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text'], TRUE);
446 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html'], TRUE);
449 $subject = CRM_Utils_Token
::replaceContactTokens($subject, $contact, FALSE, $tokens['text'], FALSE, TRUE);
450 $text = CRM_Utils_Token
::replaceContactTokens($text, $contact, FALSE, $tokens['text'], FALSE, TRUE);
451 $html = CRM_Utils_Token
::replaceContactTokens($html, $contact, FALSE, $tokens['html'], FALSE, TRUE);
453 $contactArray = array($contactID => $contact);
454 CRM_Utils_Hook
::tokenValues($contactArray,
457 CRM_Utils_Token
::flattenTokens($tokens),
458 // we should consider adding groupName and valueName here
459 'CRM_Core_BAO_MessageTemplate'
461 $contact = $contactArray[$contactID];
463 $subject = CRM_Utils_Token
::replaceHookTokens($subject, $contact, $categories, TRUE);
464 $text = CRM_Utils_Token
::replaceHookTokens($text, $contact, $categories, TRUE);
465 $html = CRM_Utils_Token
::replaceHookTokens($html, $contact, $categories, TRUE);
468 // strip whitespace from ends and turn into a single line
469 $subject = "{strip}$subject{/strip}";
471 // parse the three elements with Smarty
472 $smarty = CRM_Core_Smarty
::singleton();
473 foreach ($params['tplParams'] as $name => $value) {
474 $smarty->assign($name, $value);
481 $
$elem = $smarty->fetch("string:{$$elem}");
484 // send the template, honouring the target user’s preferences (if any)
487 // create the params array
488 $params['subject'] = $subject;
489 $params['text'] = $text;
490 $params['html'] = $html;
492 if ($params['toEmail']) {
493 $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1));
494 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($contactParams);
496 $prefs = array_pop($contact);
498 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') {
499 $params['text'] = NULL;
502 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') {
503 $params['html'] = NULL;
506 $config = CRM_Core_Config
::singleton();
507 if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) {
508 $pdfHtml = CRM_Contribute_BAO_ContributionPage
::addInvoicePdfToEmail($params['contributionId'], $params['contactId']);
509 if (empty($params['attachments'])) {
510 $params['attachments'] = array();
512 $params['attachments'][] = CRM_Utils_Mail
::appendPDF('Invoice.pdf', $pdfHtml, $format);
515 if ($config->doNotAttachPDFReceipt
&&
516 $params['PDFFilename'] &&
519 if (empty($params['attachments'])) {
520 $params['attachments'] = array();
522 $params['attachments'][] = CRM_Utils_Mail
::appendPDF($params['PDFFilename'], $params['html'], $format);
523 if (isset($params['tplParams']['email_comment'])) {
524 $params['html'] = $params['tplParams']['email_comment'];
525 $params['text'] = strip_tags($params['tplParams']['email_comment']);
529 $sent = CRM_Utils_Mail
::send($params);
532 unlink($pdf_filename);
536 return array($sent, $subject, $text, $html);