3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
36 require_once 'Mail/mime.php';
37 class CRM_Core_BAO_MessageTemplate
extends CRM_Core_DAO_MessageTemplate
{
40 * Takes a bunch of params that are needed to match certain criteria and
41 * retrieves the relevant objects. Typically the valid params are only
42 * contact_id. We'll tweak this function to be more full featured over a period
43 * of time. This is the inverse function of create. It also stores all the retrieved
44 * values in the default array
46 * @param array $params (reference ) an assoc array of name/value pairs
47 * @param array $defaults (reference ) an assoc array to hold the flattened values
49 * @return object CRM_Core_BAO_MessageTemplate object
53 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
66 * @param int $id id of the database record
67 * @param boolean $is_active value we want to set the is_active field
69 * @return Object DAO object on sucess, null otherwise
72 static function setIsActive($id, $is_active) {
73 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_MessageTemplate', $id, 'is_active', $is_active);
77 * function to add the Message Templates
79 * @param array $params reference array contains the values submitted by the form
86 static function add(&$params) {
87 $hook = empty($params['id']) ?
'create' : 'edit';
88 CRM_Utils_Hook
::pre($hook, 'MessageTemplate', CRM_Utils_Array
::value('id', $params), $params);
90 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
91 $messageTemplates->copyValues($params);
92 $messageTemplates->save();
94 CRM_Utils_Hook
::post($hook, 'MessageTemplate', $messageTemplates->id
, $messageTemplates);
95 return $messageTemplates;
99 * function to delete the Message Templates
106 static function del($messageTemplatesID) {
107 // make sure messageTemplatesID is an integer
108 if (!CRM_Utils_Rule
::positiveInteger($messageTemplatesID)) {
109 CRM_Core_Error
::fatal(ts('Invalid Message template'));
112 // Set mailing msg template col to NULL
113 $query = "UPDATE civicrm_mailing
114 SET msg_template_id = NULL
115 WHERE msg_template_id = %1";
117 $params = array(1 => array($messageTemplatesID, 'Integer'));
118 CRM_Core_DAO
::executeQuery($query, $params);
120 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
121 $messageTemplates->id
= $messageTemplatesID;
122 $messageTemplates->delete();
123 CRM_Core_Session
::setStatus(ts('Selected message template has been deleted.'), ts('Deleted'), 'success');
127 * function to get the Message Templates
134 static function getMessageTemplates($all = TRUE) {
137 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
138 $messageTemplates->is_active
= 1;
141 $messageTemplates->workflow_id
= 'NULL';
143 $messageTemplates->find();
144 while ($messageTemplates->fetch()) {
145 $msgTpls[$messageTemplates->id
] = $messageTemplates->msg_title
;
151 static function sendReminder($contactId, $email, $messageTemplateID, $from) {
153 $messageTemplates = new CRM_Core_DAO_MessageTemplate();
154 $messageTemplates->id
= $messageTemplateID;
156 $domain = CRM_Core_BAO_Domain
::getDomain();
158 $hookTokens = array();
160 if ($messageTemplates->find(TRUE)) {
161 $body_text = $messageTemplates->msg_text
;
162 $body_html = $messageTemplates->msg_html
;
163 $body_subject = $messageTemplates->msg_subject
;
165 $body_text = CRM_Utils_String
::htmlToText($body_html);
168 $params = array(array('contact_id', '=', $contactId, 0, 0));
169 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($params);
172 $contact = reset($contact);
174 if (!$contact ||
is_a($contact, 'CRM_Core_Error')) {
180 // get tokens to be replaced
181 $tokens = array_merge(CRM_Utils_Token
::getTokens($body_text),
182 CRM_Utils_Token
::getTokens($body_html),
183 CRM_Utils_Token
::getTokens($body_subject));
185 // get replacement text for these tokens
186 $returnProperties = array("preferred_mail_format" => 1);
187 if (isset($tokens['contact'])) {
188 foreach ($tokens['contact'] as $key => $value) {
189 $returnProperties[$value] = 1;
192 list($details) = CRM_Utils_Token
::getTokenDetails(array($contactId),
196 'CRM_Core_BAO_MessageTemplate');
197 $contact = reset( $details );
200 $hookTokens = array();
201 CRM_Utils_Hook
::tokens($hookTokens);
202 $categories = array_keys($hookTokens);
204 // do replacements in text and html body
205 $type = array('html', 'text');
206 foreach ($type as $key => $value) {
207 $bodyType = "body_{$value}";
209 CRM_Utils_Token
::replaceGreetingTokens($
$bodyType, NULL, $contact['contact_id']);
210 $
$bodyType = CRM_Utils_Token
::replaceDomainTokens($
$bodyType, $domain, true, $tokens, true);
211 $
$bodyType = CRM_Utils_Token
::replaceContactTokens($
$bodyType, $contact, false, $tokens, false, true);
212 $
$bodyType = CRM_Utils_Token
::replaceComponentTokens($
$bodyType, $contact, $tokens, true);
213 $
$bodyType = CRM_Utils_Token
::replaceHookTokens($
$bodyType, $contact , $categories, true);
219 $smarty = CRM_Core_Smarty
::singleton();
221 'text', 'html') as $elem) {
222 $
$elem = $smarty->fetch("string:{$$elem}");
225 // do replacements in message subject
226 $messageSubject = CRM_Utils_Token
::replaceContactTokens($body_subject, $contact, false, $tokens);
227 $messageSubject = CRM_Utils_Token
::replaceDomainTokens($messageSubject, $domain, true, $tokens);
228 $messageSubject = CRM_Utils_Token
::replaceComponentTokens($messageSubject, $contact, $tokens, true);
229 $messageSubject = CRM_Utils_Token
::replaceHookTokens($messageSubject, $contact, $categories, true);
231 $messageSubject = $smarty->fetch("string:{$messageSubject}");
233 // set up the parameters for CRM_Utils_Mail::send
235 'groupName' => 'Scheduled Reminder Sender',
237 'toName' => $contact['display_name'],
239 'subject' => $messageSubject,
241 if (!$html ||
$contact['preferred_mail_format'] == 'Text' ||
242 $contact['preferred_mail_format'] == 'Both'
244 // render the & entities in text mode, so that the links work
245 $mailParams['text'] = str_replace('&', '&', $text);
247 if ($html && ($contact['preferred_mail_format'] == 'HTML' ||
248 $contact['preferred_mail_format'] == 'Both'
250 $mailParams['html'] = $html;
253 $result = CRM_Utils_Mail
::send($mailParams);
256 $messageTemplates->free();
262 * Revert a message template to its default subject+text+HTML state
264 * @param integer id id of the template
268 static function revert($id) {
269 $diverted = new self
;
270 $diverted->id
= (int) $id;
273 if ($diverted->N
!= 1) {
274 CRM_Core_Error
::fatal(ts('Did not find a message template with id of %1.', array(1 => $id)));
278 $orig->workflow_id
= $diverted->workflow_id
;
279 $orig->is_reserved
= 1;
283 CRM_Core_Error
::fatal(ts('Message template with id of %1 does not have a default to revert to.', array(1 => $id)));
286 $diverted->msg_subject
= $orig->msg_subject
;
287 $diverted->msg_text
= $orig->msg_text
;
288 $diverted->msg_html
= $orig->msg_html
;
289 $diverted->pdf_format_id
= is_null($orig->pdf_format_id
) ?
'null' : $orig->pdf_format_id
;
294 * Send an email from the specified template based on an array of params
296 * @param array $params a string-keyed array of function params, see function body for details
298 * @return array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
300 static function sendTemplate($params) {
302 // option group name of the template
304 // option value name of the template
306 // ID of the template
307 'messageTemplateID' => NULL,
308 // contact id if contact tokens are to be replaced
310 // additional template params (other than the ones already set in the template singleton)
311 'tplParams' => array(),
314 // the recipient’s name
316 // the recipient’s email - mail is sent only if set
322 // the Reply-To: header
325 'attachments' => NULL,
326 // whether this is a test email (and hence should include the test banner)
328 // filename of optional PDF version to add as attachment (do not include path)
329 'PDFFilename' => NULL,
331 $params = array_merge($defaults, $params);
333 if ((!$params['groupName'] ||
334 !$params['valueName']
336 !$params['messageTemplateID']
338 CRM_Core_Error
::fatal(ts("Message template's option group and/or option value or ID missing."));
341 if ($params['messageTemplateID']) {
342 // fetch the three elements from the db based on id
343 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
344 FROM civicrm_msg_template mt
345 WHERE mt.id = %1 AND mt.is_default = 1';
346 $sqlParams = array(1 => array($params['messageTemplateID'], 'String'));
349 // fetch the three elements from the db based on option_group and option_value names
350 $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
351 FROM civicrm_msg_template mt
352 JOIN civicrm_option_value ov ON workflow_id = ov.id
353 JOIN civicrm_option_group og ON ov.option_group_id = og.id
354 WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1';
355 $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String'));
357 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
361 if ($params['messageTemplateID']) {
362 CRM_Core_Error
::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID'])));
365 CRM_Core_Error
::fatal(ts('No such message template: option group %1, option value %2.', array(1 => $params['groupName'], 2 => $params['valueName'])));
369 $subject = $dao->subject
;
372 $format = $dao->format
;
375 // add the test banner (if requested)
376 if ($params['isTest']) {
377 $query = "SELECT msg_subject subject, msg_text text, msg_html html
378 FROM civicrm_msg_template mt
379 JOIN civicrm_option_value ov ON workflow_id = ov.id
380 JOIN civicrm_option_group og ON ov.option_group_id = og.id
381 WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1";
382 $testDao = CRM_Core_DAO
::executeQuery($query);
385 $subject = $testDao->subject
. $subject;
386 $text = $testDao->text
. $text;
387 $html = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $html);
391 // replace tokens in the three elements (in subject as if it was the text body)
392 $domain = CRM_Core_BAO_Domain
::getDomain();
393 $hookTokens = array();
394 $mailing = new CRM_Mailing_BAO_Mailing
;
395 $mailing->body_text
= $text;
396 $mailing->body_html
= $html;
397 $tokens = $mailing->getTokens();
398 CRM_Utils_Hook
::tokens($hookTokens);
399 $categories = array_keys($hookTokens);
401 $contactID = CRM_Utils_Array
::value('contactId', $params);
404 $contactParams = array('contact_id' => $contactID);
405 $returnProperties = array();
407 if (isset($tokens['text']['contact'])) {
408 foreach ($tokens['text']['contact'] as $name) {
409 $returnProperties[$name] = 1;
413 if (isset($tokens['html']['contact'])) {
414 foreach ($tokens['html']['contact'] as $name) {
415 $returnProperties[$name] = 1;
418 list($contact) = CRM_Utils_Token
::getTokenDetails($contactParams,
421 CRM_Utils_Token
::flattenTokens($tokens),
422 // we should consider adding groupName and valueName here
423 'CRM_Core_BAO_MessageTemplate'
425 $contact = $contact[$contactID];
428 $subject = CRM_Utils_Token
::replaceDomainTokens($subject, $domain, TRUE, $tokens['text'], TRUE);
429 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, TRUE, $tokens['text'], TRUE);
430 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html'], TRUE);
433 $subject = CRM_Utils_Token
::replaceContactTokens($subject, $contact, FALSE, $tokens['text'], FALSE, TRUE);
434 $text = CRM_Utils_Token
::replaceContactTokens($text, $contact, FALSE, $tokens['text'], FALSE, TRUE);
435 $html = CRM_Utils_Token
::replaceContactTokens($html, $contact, FALSE, $tokens['html'], FALSE, TRUE);
438 $contactArray = array($contactID => $contact);
439 CRM_Utils_Hook
::tokenValues($contactArray,
442 CRM_Utils_Token
::flattenTokens($tokens),
443 // we should consider adding groupName and valueName here
444 'CRM_Core_BAO_MessageTemplate'
446 $contact = $contactArray[$contactID];
448 $subject = CRM_Utils_Token
::replaceHookTokens($subject, $contact, $categories, TRUE);
449 $text = CRM_Utils_Token
::replaceHookTokens($text, $contact, $categories, TRUE);
450 $html = CRM_Utils_Token
::replaceHookTokens($html, $contact, $categories, TRUE);
453 // strip whitespace from ends and turn into a single line
454 $subject = "{strip}$subject{/strip}";
456 // parse the three elements with Smarty
459 $smarty = CRM_Core_Smarty
::singleton();
460 foreach ($params['tplParams'] as $name => $value) {
461 $smarty->assign($name, $value);
464 'subject', 'text', 'html') as $elem) {
465 $
$elem = $smarty->fetch("string:{$$elem}");
468 // send the template, honouring the target user’s preferences (if any)
471 // create the params array
472 $params['subject'] = $subject;
473 $params['text'] = $text;
474 $params['html'] = $html;
476 if ($params['toEmail']) {
477 $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1));
478 list($contact, $_) = CRM_Contact_BAO_Query
::apiQuery($contactParams);
480 $prefs = array_pop($contact);
482 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') {
483 $params['text'] = NULL;
486 if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') {
487 $params['html'] = NULL;
490 $config = CRM_Core_Config
::singleton();
492 if ($config->doNotAttachPDFReceipt
&&
493 $params['PDFFilename'] &&
496 $pdf_filename = $config->templateCompileDir
. CRM_Utils_File
::makeFileName($params['PDFFilename']);
499 //xmlns attribute is required in XHTML but it is invalid in HTML,
500 //Also the namespace "xmlns=http://www.w3.org/1999/xhtml" is default,
501 //and will be added to the <html> tag even if you do not include it.
502 $html = preg_replace('/(<html)(.+?xmlns=["\'].[^\s]+["\'])(.+)?(>)/', '\1\3\4', $params['html']);
504 file_put_contents($pdf_filename, CRM_Utils_PDF_Utils
::html2pdf($html,
505 $params['PDFFilename'],
511 if (empty($params['attachments'])) {
512 $params['attachments'] = array();
514 $params['attachments'][] = array(
515 'fullPath' => $pdf_filename,
516 'mime_type' => 'application/pdf',
517 'cleanName' => $params['PDFFilename'],
521 $sent = CRM_Utils_Mail
::send($params);
524 unlink($pdf_filename);
528 return array($sent, $subject, $text, $html);