3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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
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.
130 public static function getMessageTemplates($all = TRUE, $isSMS = FALSE) {
133 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
134 $messageTemplates->is_active
= 1;
135 $messageTemplates->is_sms
= $isSMS;
138 $messageTemplates->workflow_id
= 'NULL';
140 $messageTemplates->find();
141 while ($messageTemplates->fetch()) {
142 $msgTpls[$messageTemplates->id
] = $messageTemplates->msg_title
;
149 * @param int $contactId
151 * @param int $messageTemplateID
156 public static function sendReminder($contactId, $email, $messageTemplateID, $from) {
158 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
159 $messageTemplates->id
= $messageTemplateID;
161 $domain = CRM_Core_BAO_Domain
::getDomain();
163 $hookTokens = array();
165 if ($messageTemplates->find(TRUE)) {
166 $body_text = $messageTemplates->msg_text
;
167 $body_html = $messageTemplates->msg_html
;
168 $body_subject = $messageTemplates->msg_subject
;
170 $body_text = CRM_Utils_String
::htmlToText($body_html);
173 $params = array(array('contact_id', '=', $contactId, 0, 0));
174 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($params);
177 $contact = reset($contact);
179 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
185 // get tokens to be replaced
186 $tokens = array_merge(CRM_Utils_Token
::getTokens($body_text),
187 CRM_Utils_Token
::getTokens($body_html),
188 CRM_Utils_Token
::getTokens($body_subject));
190 // get replacement text for these tokens
191 $returnProperties = array("preferred_mail_format" => 1);
192 if (isset($tokens['contact'])) {
193 foreach ($tokens['contact'] as $key => $value) {
194 $returnProperties[$value] = 1;
197 list($details) = CRM_Utils_Token
::getTokenDetails(array($contactId),
201 'CRM_Core_BAO_MessageTemplate');
202 $contact = reset($details);
205 $hookTokens = array();
206 CRM_Utils_Hook
::tokens($hookTokens);
207 $categories = array_keys($hookTokens);
209 // do replacements in text and html body
210 $type = array('html', 'text');
211 foreach ($type as $key => $value) {
212 $bodyType = "body_{$value}";
214 CRM_Utils_Token
::replaceGreetingTokens($
$bodyType, NULL, $contact['contact_id']);
215 $
$bodyType = CRM_Utils_Token
::replaceDomainTokens($
$bodyType, $domain, TRUE, $tokens, TRUE);
216 $
$bodyType = CRM_Utils_Token
::replaceContactTokens($
$bodyType, $contact, FALSE, $tokens, FALSE, TRUE);
217 $
$bodyType = CRM_Utils_Token
::replaceComponentTokens($
$bodyType, $contact, $tokens, TRUE);
218 $
$bodyType = CRM_Utils_Token
::replaceHookTokens($
$bodyType, $contact, $categories, TRUE);
224 $smarty = CRM_Core_Smarty
::singleton();
229 $
$elem = $smarty->fetch("string:{$$elem}");
232 // do replacements in message subject
233 $messageSubject = CRM_Utils_Token
::replaceContactTokens($body_subject, $contact, FALSE, $tokens);
234 $messageSubject = CRM_Utils_Token
::replaceDomainTokens($messageSubject, $domain, TRUE, $tokens);
235 $messageSubject = CRM_Utils_Token
::replaceComponentTokens($messageSubject, $contact, $tokens, TRUE);
236 $messageSubject = CRM_Utils_Token
::replaceHookTokens($messageSubject, $contact, $categories, TRUE);
238 $messageSubject = $smarty->fetch("string:{$messageSubject}");
240 // set up the parameters for CRM_Utils_Mail::send
242 'groupName' => 'Scheduled Reminder Sender',
244 'toName' => $contact['display_name'],
246 'subject' => $messageSubject,
248 if (!$html ||
$contact['preferred_mail_format'] == 'Text' ||
249 $contact['preferred_mail_format'] == 'Both'
251 // render the & entities in text mode, so that the links work
252 $mailParams['text'] = str_replace('&', '&', $text);
254 if ($html && ($contact['preferred_mail_format'] == 'HTML' ||
255 $contact['preferred_mail_format'] == 'Both'
258 $mailParams['html'] = $html;
261 $result = CRM_Utils_Mail
::send($mailParams);
264 $messageTemplates->free();
270 * Revert a message template to its default subject+text+HTML state.
272 * @param int $id id of the template
274 public static function revert($id) {
275 $diverted = new CRM_Core_BAO_MessageTemplate();
276 $diverted->id
= (int) $id;
279 if ($diverted->N
!= 1) {
280 CRM_Core_Error
::fatal(ts('Did not find a message template with id of %1.', array(1 => $id)));
283 $orig = new CRM_Core_BAO_MessageTemplate();
284 $orig->workflow_id
= $diverted->workflow_id
;
285 $orig->is_reserved
= 1;
289 CRM_Core_Error
::fatal(ts('Message template with id of %1 does not have a default to revert to.', array(1 => $id)));
292 $diverted->msg_subject
= $orig->msg_subject
;
293 $diverted->msg_text
= $orig->msg_text
;
294 $diverted->msg_html
= $orig->msg_html
;
295 $diverted->pdf_format_id
= is_null($orig->pdf_format_id
) ?
'null' : $orig->pdf_format_id
;
300 * Send an email from the specified template based on an array of params.
302 * @param array $params
303 * A string-keyed array of function params, see function body for details.
306 * Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
308 public static function sendTemplate($params) {
310 // option group name of the template
312 // option value name of the template
314 // ID of the template
315 'messageTemplateID' => NULL,
316 // contact id if contact tokens are to be replaced
318 // additional template params (other than the ones already set in the template singleton)
319 'tplParams' => array(),
322 // the recipient’s name
324 // the recipient’s email - mail is sent only if set
330 // the Reply-To: header
333 'attachments' => NULL,
334 // whether this is a test email (and hence should include the test banner)
336 // filename of optional PDF version to add as attachment (do not include path)
337 'PDFFilename' => NULL,
339 $params = array_merge($defaults, $params);
341 if ((!$params['groupName'] ||
342 !$params['valueName']
344 !$params['messageTemplateID']
346 CRM_Core_Error
::fatal(ts("Message template's option group and/or option value or ID missing."));
349 if ($params['messageTemplateID']) {
350 // fetch the three elements from the db based on id
351 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
352 FROM civicrm_msg_template mt
353 WHERE mt.id = %1 AND mt.is_default = 1';
354 $sqlParams = array(1 => array($params['messageTemplateID'], 'String'));
357 // fetch the three elements from the db based on option_group and option_value names
358 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
359 FROM civicrm_msg_template mt
360 JOIN civicrm_option_value ov ON workflow_id = ov.id
361 JOIN civicrm_option_group og ON ov.option_group_id = og.id
362 WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1';
363 $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String'));
365 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
369 if ($params['messageTemplateID']) {
370 CRM_Core_Error
::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID'])));
373 CRM_Core_Error
::fatal(ts('No such message template: option group %1, option value %2.', array(
374 1 => $params['groupName'],
375 2 => $params['valueName'],
380 $subject = $dao->subject
;
383 $format = $dao->format
;
386 // add the test banner (if requested)
387 if ($params['isTest']) {
388 $query = "SELECT msg_subject subject, msg_text text, msg_html html
389 FROM civicrm_msg_template mt
390 JOIN civicrm_option_value ov ON workflow_id = ov.id
391 JOIN civicrm_option_group og ON ov.option_group_id = og.id
392 WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1";
393 $testDao = CRM_Core_DAO
::executeQuery($query);
396 $subject = $testDao->subject
. $subject;
397 $text = $testDao->text
. $text;
398 $html = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $html);
402 // replace tokens in the three elements (in subject as if it was the text body)
403 $domain = CRM_Core_BAO_Domain
::getDomain();
404 $hookTokens = array();
405 $mailing = new CRM_Mailing_BAO_Mailing();
406 $mailing->body_text
= $text;
407 $mailing->body_html
= $html;
408 $tokens = $mailing->getTokens();
409 CRM_Utils_Hook
::tokens($hookTokens);
410 $categories = array_keys($hookTokens);
412 $contactID = CRM_Utils_Array
::value('contactId', $params);
415 $contactParams = array('contact_id' => $contactID);
416 $returnProperties = array();
418 if (isset($tokens['text']['contact'])) {
419 foreach ($tokens['text']['contact'] as $name) {
420 $returnProperties[$name] = 1;
424 if (isset($tokens['html']['contact'])) {
425 foreach ($tokens['html']['contact'] as $name) {
426 $returnProperties[$name] = 1;
429 list($contact) = CRM_Utils_Token
::getTokenDetails($contactParams,
432 CRM_Utils_Token
::flattenTokens($tokens),
433 // we should consider adding groupName and valueName here
434 'CRM_Core_BAO_MessageTemplate'
436 $contact = $contact[$contactID];
439 $subject = CRM_Utils_Token
::replaceDomainTokens($subject, $domain, FALSE, $tokens['text'], TRUE);
440 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text'], TRUE);
441 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html'], TRUE);
444 $subject = CRM_Utils_Token
::replaceContactTokens($subject, $contact, FALSE, $tokens['text'], FALSE, TRUE);
445 $text = CRM_Utils_Token
::replaceContactTokens($text, $contact, FALSE, $tokens['text'], FALSE, TRUE);
446 $html = CRM_Utils_Token
::replaceContactTokens($html, $contact, FALSE, $tokens['html'], FALSE, TRUE);
448 $contactArray = array($contactID => $contact);
449 CRM_Utils_Hook
::tokenValues($contactArray,
452 CRM_Utils_Token
::flattenTokens($tokens),
453 // we should consider adding groupName and valueName here
454 'CRM_Core_BAO_MessageTemplate'
456 $contact = $contactArray[$contactID];
458 $subject = CRM_Utils_Token
::replaceHookTokens($subject, $contact, $categories, TRUE);
459 $text = CRM_Utils_Token
::replaceHookTokens($text, $contact, $categories, TRUE);
460 $html = CRM_Utils_Token
::replaceHookTokens($html, $contact, $categories, TRUE);
463 // strip whitespace from ends and turn into a single line
464 $subject = "{strip}$subject{/strip}";
466 // parse the three elements with Smarty
467 $smarty = CRM_Core_Smarty
::singleton();
468 foreach ($params['tplParams'] as $name => $value) {
469 $smarty->assign($name, $value);
476 $
$elem = $smarty->fetch("string:{$$elem}");
479 // send the template, honouring the target user’s preferences (if any)
482 // create the params array
483 $params['subject'] = $subject;
484 $params['text'] = $text;
485 $params['html'] = $html;
487 if ($params['toEmail']) {
488 $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1));
489 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($contactParams);
491 $prefs = array_pop($contact);
493 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') {
494 $params['text'] = NULL;
497 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') {
498 $params['html'] = NULL;
501 $config = CRM_Core_Config
::singleton();
502 if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) {
503 $pdfHtml = CRM_Contribute_BAO_ContributionPage
::addInvoicePdfToEmail($params['contributionId'], $params['contactId']);
504 if (empty($params['attachments'])) {
505 $params['attachments'] = array();
507 $params['attachments'][] = CRM_Utils_Mail
::appendPDF('Invoice.pdf', $pdfHtml, $format);
510 if ($config->doNotAttachPDFReceipt
&&
511 $params['PDFFilename'] &&
514 if (empty($params['attachments'])) {
515 $params['attachments'] = array();
517 $params['attachments'][] = CRM_Utils_Mail
::appendPDF($params['PDFFilename'], $params['html'], $format);
518 if (isset($params['tplParams']['email_comment'])) {
519 $params['html'] = $params['tplParams']['email_comment'];
520 $params['text'] = strip_tags($params['tplParams']['email_comment']);
524 $sent = CRM_Utils_Mail
::send($params);
527 unlink($pdf_filename);
531 return array($sent, $subject, $text, $html);