3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
34 require_once 'Mail/mime.php';
37 * Class CRM_Core_BAO_MessageTemplate.
39 class CRM_Core_BAO_MessageTemplate
extends CRM_Core_DAO_MessageTemplate
{
42 * Fetch object based on array of properties.
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
46 * @param array $defaults
47 * (reference ) an assoc array to hold the flattened values.
49 * @return CRM_Core_BAO_MessageTemplate
51 public static function retrieve(&$params, &$defaults) {
52 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
53 $messageTemplates->copyValues($params);
54 if ($messageTemplates->find(TRUE)) {
55 CRM_Core_DAO
::storeValues($messageTemplates, $defaults);
56 return $messageTemplates;
62 * Update the is_active flag in the db.
65 * Id of the database record.
66 * @param bool $is_active
67 * Value we want to set the is_active field.
70 * DAO object on success, NULL otherwise
72 public static function setIsActive($id, $is_active) {
73 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_MessageTemplate', $id, 'is_active', $is_active);
77 * Add the Message Templates.
79 * @param array $params
80 * Reference array contains the values submitted by the form.
85 public static function add(&$params) {
86 $hook = empty($params['id']) ?
'create' : 'edit';
87 CRM_Utils_Hook
::pre($hook, 'MessageTemplate', CRM_Utils_Array
::value('id', $params), $params);
89 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
90 $messageTemplates->copyValues($params);
91 $messageTemplates->save();
93 CRM_Utils_Hook
::post($hook, 'MessageTemplate', $messageTemplates->id
, $messageTemplates);
94 return $messageTemplates;
98 * Delete the Message Templates.
100 * @param int $messageTemplatesID
102 public static function del($messageTemplatesID) {
103 // make sure messageTemplatesID is an integer
104 if (!CRM_Utils_Rule
::positiveInteger($messageTemplatesID)) {
105 CRM_Core_Error
::fatal(ts('Invalid Message template'));
108 // Set mailing msg template col to NULL
109 $query = "UPDATE civicrm_mailing
110 SET msg_template_id = NULL
111 WHERE msg_template_id = %1";
113 $params = array(1 => array($messageTemplatesID, 'Integer'));
114 CRM_Core_DAO
::executeQuery($query, $params);
116 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
117 $messageTemplates->id
= $messageTemplatesID;
118 $messageTemplates->delete();
119 CRM_Core_Session
::setStatus(ts('Selected message template has been deleted.'), ts('Deleted'), 'success');
123 * Get the Message Templates.
132 public static function getMessageTemplates($all = TRUE, $isSMS = FALSE) {
135 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
136 $messageTemplates->is_active
= 1;
137 $messageTemplates->is_sms
= $isSMS;
140 $messageTemplates->workflow_id
= 'NULL';
142 $messageTemplates->find();
143 while ($messageTemplates->fetch()) {
144 $msgTpls[$messageTemplates->id
] = $messageTemplates->msg_title
;
151 * @param int $contactId
153 * @param int $messageTemplateID
158 public static function sendReminder($contactId, $email, $messageTemplateID, $from) {
160 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
161 $messageTemplates->id
= $messageTemplateID;
163 $domain = CRM_Core_BAO_Domain
::getDomain();
165 $hookTokens = array();
167 if ($messageTemplates->find(TRUE)) {
168 $body_text = $messageTemplates->msg_text
;
169 $body_html = $messageTemplates->msg_html
;
170 $body_subject = $messageTemplates->msg_subject
;
172 $body_text = CRM_Utils_String
::htmlToText($body_html);
175 $params = array(array('contact_id', '=', $contactId, 0, 0));
176 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($params);
179 $contact = reset($contact);
181 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
187 // get tokens to be replaced
188 $tokens = array_merge(CRM_Utils_Token
::getTokens($body_text),
189 CRM_Utils_Token
::getTokens($body_html),
190 CRM_Utils_Token
::getTokens($body_subject));
192 // get replacement text for these tokens
193 $returnProperties = array("preferred_mail_format" => 1);
194 if (isset($tokens['contact'])) {
195 foreach ($tokens['contact'] as $key => $value) {
196 $returnProperties[$value] = 1;
199 list($details) = CRM_Utils_Token
::getTokenDetails(array($contactId),
203 'CRM_Core_BAO_MessageTemplate');
204 $contact = reset($details);
207 $hookTokens = array();
208 CRM_Utils_Hook
::tokens($hookTokens);
209 $categories = array_keys($hookTokens);
211 // do replacements in text and html body
212 $type = array('html', 'text');
213 foreach ($type as $key => $value) {
214 $bodyType = "body_{$value}";
216 CRM_Utils_Token
::replaceGreetingTokens($
$bodyType, NULL, $contact['contact_id']);
217 $
$bodyType = CRM_Utils_Token
::replaceDomainTokens($
$bodyType, $domain, TRUE, $tokens, TRUE);
218 $
$bodyType = CRM_Utils_Token
::replaceContactTokens($
$bodyType, $contact, FALSE, $tokens, FALSE, TRUE);
219 $
$bodyType = CRM_Utils_Token
::replaceComponentTokens($
$bodyType, $contact, $tokens, TRUE);
220 $
$bodyType = CRM_Utils_Token
::replaceHookTokens($
$bodyType, $contact, $categories, TRUE);
226 $smarty = CRM_Core_Smarty
::singleton();
231 $
$elem = $smarty->fetch("string:{$$elem}");
234 // do replacements in message subject
235 $messageSubject = CRM_Utils_Token
::replaceContactTokens($body_subject, $contact, FALSE, $tokens);
236 $messageSubject = CRM_Utils_Token
::replaceDomainTokens($messageSubject, $domain, TRUE, $tokens);
237 $messageSubject = CRM_Utils_Token
::replaceComponentTokens($messageSubject, $contact, $tokens, TRUE);
238 $messageSubject = CRM_Utils_Token
::replaceHookTokens($messageSubject, $contact, $categories, TRUE);
240 $messageSubject = $smarty->fetch("string:{$messageSubject}");
242 // set up the parameters for CRM_Utils_Mail::send
244 'groupName' => 'Scheduled Reminder Sender',
246 'toName' => $contact['display_name'],
248 'subject' => $messageSubject,
250 if (!$html ||
$contact['preferred_mail_format'] == 'Text' ||
251 $contact['preferred_mail_format'] == 'Both'
253 // render the & entities in text mode, so that the links work
254 $mailParams['text'] = str_replace('&', '&', $text);
256 if ($html && ($contact['preferred_mail_format'] == 'HTML' ||
257 $contact['preferred_mail_format'] == 'Both'
260 $mailParams['html'] = $html;
263 $result = CRM_Utils_Mail
::send($mailParams);
266 $messageTemplates->free();
272 * Revert a message template to its default subject+text+HTML state.
274 * @param int $id id of the template
276 public static function revert($id) {
277 $diverted = new CRM_Core_BAO_MessageTemplate();
278 $diverted->id
= (int) $id;
281 if ($diverted->N
!= 1) {
282 CRM_Core_Error
::fatal(ts('Did not find a message template with id of %1.', array(1 => $id)));
285 $orig = new CRM_Core_BAO_MessageTemplate();
286 $orig->workflow_id
= $diverted->workflow_id
;
287 $orig->is_reserved
= 1;
291 CRM_Core_Error
::fatal(ts('Message template with id of %1 does not have a default to revert to.', array(1 => $id)));
294 $diverted->msg_subject
= $orig->msg_subject
;
295 $diverted->msg_text
= $orig->msg_text
;
296 $diverted->msg_html
= $orig->msg_html
;
297 $diverted->pdf_format_id
= is_null($orig->pdf_format_id
) ?
'null' : $orig->pdf_format_id
;
302 * Send an email from the specified template based on an array of params.
304 * @param array $params
305 * A string-keyed array of function params, see function body for details.
308 * Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
310 public static function sendTemplate($params) {
312 // option group name of the template
314 // option value name of the template
316 // ID of the template
317 'messageTemplateID' => NULL,
318 // contact id if contact tokens are to be replaced
320 // additional template params (other than the ones already set in the template singleton)
321 'tplParams' => array(),
324 // the recipient’s name
326 // the recipient’s email - mail is sent only if set
332 // the Reply-To: header
335 'attachments' => NULL,
336 // whether this is a test email (and hence should include the test banner)
338 // filename of optional PDF version to add as attachment (do not include path)
339 'PDFFilename' => NULL,
341 $params = array_merge($defaults, $params);
343 CRM_Utils_Hook
::alterMailParams($params, 'messageTemplate');
345 if ((!$params['groupName'] ||
346 !$params['valueName']
348 !$params['messageTemplateID']
350 CRM_Core_Error
::fatal(ts("Message template's option group and/or option value or ID missing."));
353 if ($params['messageTemplateID']) {
354 // fetch the three elements from the db based on id
355 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
356 FROM civicrm_msg_template mt
357 WHERE mt.id = %1 AND mt.is_default = 1';
358 $sqlParams = array(1 => array($params['messageTemplateID'], 'String'));
361 // fetch the three elements from the db based on option_group and option_value names
362 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
363 FROM civicrm_msg_template mt
364 JOIN civicrm_option_value ov ON workflow_id = ov.id
365 JOIN civicrm_option_group og ON ov.option_group_id = og.id
366 WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1';
367 $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String'));
369 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
373 if ($params['messageTemplateID']) {
374 CRM_Core_Error
::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID'])));
377 CRM_Core_Error
::fatal(ts('No such message template: option group %1, option value %2.', array(
378 1 => $params['groupName'],
379 2 => $params['valueName'],
384 $mailContent = array(
385 'subject' => $dao->subject
,
386 'text' => $dao->text
,
387 'html' => $dao->html
,
388 'format' => $dao->format
,
392 CRM_Utils_Hook
::alterMailContent($mailContent);
394 // add the test banner (if requested)
395 if ($params['isTest']) {
396 $query = "SELECT msg_subject subject, msg_text text, msg_html html
397 FROM civicrm_msg_template mt
398 JOIN civicrm_option_value ov ON workflow_id = ov.id
399 JOIN civicrm_option_group og ON ov.option_group_id = og.id
400 WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1";
401 $testDao = CRM_Core_DAO
::executeQuery($query);
404 $mailContent['subject'] = $testDao->subject
. $mailContent['subject'];
405 $mailContent['text'] = $testDao->text
. $mailContent['text'];
406 $mailContent['html'] = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $mailContent['html']);
410 // replace tokens in the three elements (in subject as if it was the text body)
411 $domain = CRM_Core_BAO_Domain
::getDomain();
412 $hookTokens = array();
413 $mailing = new CRM_Mailing_BAO_Mailing();
414 $mailing->subject
= $mailContent['subject'];
415 $mailing->body_text
= $mailContent['text'];
416 $mailing->body_html
= $mailContent['html'];
417 $tokens = $mailing->getTokens();
418 CRM_Utils_Hook
::tokens($hookTokens);
419 $categories = array_keys($hookTokens);
421 $contactID = CRM_Utils_Array
::value('contactId', $params);
424 $contactParams = array('contact_id' => $contactID);
425 $returnProperties = array();
427 if (isset($tokens['subject']['contact'])) {
428 foreach ($tokens['subject']['contact'] as $name) {
429 $returnProperties[$name] = 1;
433 if (isset($tokens['text']['contact'])) {
434 foreach ($tokens['text']['contact'] as $name) {
435 $returnProperties[$name] = 1;
439 if (isset($tokens['html']['contact'])) {
440 foreach ($tokens['html']['contact'] as $name) {
441 $returnProperties[$name] = 1;
445 // @todo CRM-17253 don't resolve contact details if there are no tokens
446 // effectively comment out this next (performance-expensive) line
447 // but unfortunately testing is a bit think on the ground to that needs to
449 list($contact) = CRM_Utils_Token
::getTokenDetails($contactParams,
452 CRM_Utils_Token
::flattenTokens($tokens),
453 // we should consider adding groupName and valueName here
454 'CRM_Core_BAO_MessageTemplate'
456 $contact = $contact[$contactID];
459 $mailContent['subject'] = CRM_Utils_Token
::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['text'], TRUE);
460 $mailContent['text'] = CRM_Utils_Token
::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], TRUE);
461 $mailContent['html'] = CRM_Utils_Token
::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], TRUE);
464 $mailContent['subject'] = CRM_Utils_Token
::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['text'], FALSE, TRUE);
465 $mailContent['text'] = CRM_Utils_Token
::replaceContactTokens($mailContent['text'], $contact, FALSE, $tokens['text'], FALSE, TRUE);
466 $mailContent['html'] = CRM_Utils_Token
::replaceContactTokens($mailContent['html'], $contact, FALSE, $tokens['html'], FALSE, TRUE);
468 $contactArray = array($contactID => $contact);
469 CRM_Utils_Hook
::tokenValues($contactArray,
472 CRM_Utils_Token
::flattenTokens($tokens),
473 // we should consider adding groupName and valueName here
474 'CRM_Core_BAO_MessageTemplate'
476 $contact = $contactArray[$contactID];
478 $mailContent['subject'] = CRM_Utils_Token
::replaceHookTokens($mailContent['subject'], $contact, $categories, TRUE);
479 $mailContent['text'] = CRM_Utils_Token
::replaceHookTokens($mailContent['text'], $contact, $categories, TRUE);
480 $mailContent['html'] = CRM_Utils_Token
::replaceHookTokens($mailContent['html'], $contact, $categories, TRUE);
483 // strip whitespace from ends and turn into a single line
484 $mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}";
486 // parse the three elements with Smarty
487 $smarty = CRM_Core_Smarty
::singleton();
488 foreach ($params['tplParams'] as $name => $value) {
489 $smarty->assign($name, $value);
496 $
$elem = $smarty->fetch("string:{$$elem}");
499 // send the template, honouring the target user’s preferences (if any)
502 // create the params array
503 $params['subject'] = $mailContent['subject'];
504 $params['text'] = $mailContent['text'];
505 $params['html'] = $mailContent['html'];
507 if ($params['toEmail']) {
508 $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1));
509 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($contactParams);
511 $prefs = array_pop($contact);
513 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') {
514 $params['text'] = NULL;
517 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') {
518 $params['html'] = NULL;
521 $config = CRM_Core_Config
::singleton();
522 if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) {
523 $pdfHtml = CRM_Contribute_BAO_ContributionPage
::addInvoicePdfToEmail($params['contributionId'], $params['contactId']);
524 if (empty($params['attachments'])) {
525 $params['attachments'] = array();
527 $params['attachments'][] = CRM_Utils_Mail
::appendPDF('Invoice.pdf', $pdfHtml, $mailContent['format']);
530 if ($config->doNotAttachPDFReceipt
&&
531 $params['PDFFilename'] &&
534 if (empty($params['attachments'])) {
535 $params['attachments'] = array();
537 $params['attachments'][] = CRM_Utils_Mail
::appendPDF($params['PDFFilename'], $params['html'], $mailContent['format']);
538 if (isset($params['tplParams']['email_comment'])) {
539 $params['html'] = $params['tplParams']['email_comment'];
540 $params['text'] = strip_tags($params['tplParams']['email_comment']);
544 $sent = CRM_Utils_Mail
::send($params);
547 unlink($pdf_filename);
551 return array($sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']);