Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | require_once 'Mail/mime.php'; | |
c6327d7d | 37 | class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { |
6a488035 TO |
38 | |
39 | /** | |
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 | |
45 | * | |
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 | |
48 | * | |
c6327d7d | 49 | * @return object CRM_Core_BAO_MessageTemplate object |
6a488035 TO |
50 | * @access public |
51 | * @static | |
52 | */ | |
53 | static function retrieve(&$params, &$defaults) { | |
c6327d7d | 54 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 TO |
55 | $messageTemplates->copyValues($params); |
56 | if ($messageTemplates->find(TRUE)) { | |
57 | CRM_Core_DAO::storeValues($messageTemplates, $defaults); | |
58 | return $messageTemplates; | |
59 | } | |
60 | return NULL; | |
61 | } | |
62 | ||
63 | /** | |
64 | * update the is_active flag in the db | |
65 | * | |
66 | * @param int $id id of the database record | |
67 | * @param boolean $is_active value we want to set the is_active field | |
68 | * | |
69 | * @return Object DAO object on sucess, null otherwise | |
70 | * @static | |
71 | */ | |
72 | static function setIsActive($id, $is_active) { | |
c6327d7d | 73 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_MessageTemplate', $id, 'is_active', $is_active); |
6a488035 TO |
74 | } |
75 | ||
76 | /** | |
77 | * function to add the Message Templates | |
78 | * | |
79 | * @param array $params reference array contains the values submitted by the form | |
80 | * | |
81 | * @access public | |
82 | * @static | |
83 | * | |
84 | * @return object | |
85 | */ | |
86 | static function add(&$params) { | |
3c8059c8 KM |
87 | $hook = empty($params['id']) ? 'create' : 'edit'; |
88 | CRM_Utils_Hook::pre($hook, 'MessageTemplate', CRM_Utils_Array::value('id', $params), $params); | |
c39c0aa1 | 89 | |
c6327d7d | 90 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 | 91 | $messageTemplates->copyValues($params); |
6a488035 | 92 | $messageTemplates->save(); |
c39c0aa1 | 93 | |
c9606a53 | 94 | CRM_Utils_Hook::post($hook, 'MessageTemplate', $messageTemplates->id, $messageTemplates); |
6a488035 TO |
95 | return $messageTemplates; |
96 | } | |
97 | ||
98 | /** | |
99 | * function to delete the Message Templates | |
100 | * | |
101 | * @access public | |
102 | * @static | |
103 | * | |
104 | * @return object | |
105 | */ | |
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')); | |
110 | } | |
111 | ||
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"; | |
5f351616 | 116 | |
117 | $params = array(1 => array($messageTemplatesID, 'Integer')); | |
6a488035 TO |
118 | CRM_Core_DAO::executeQuery($query, $params); |
119 | ||
c6327d7d | 120 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 TO |
121 | $messageTemplates->id = $messageTemplatesID; |
122 | $messageTemplates->delete(); | |
123 | CRM_Core_Session::setStatus(ts('Selected message template has been deleted.'), ts('Deleted'), 'success'); | |
124 | } | |
125 | ||
126 | /** | |
127 | * function to get the Message Templates | |
128 | * | |
129 | * @access public | |
130 | * @static | |
131 | * | |
132 | * @return object | |
133 | */ | |
134 | static function getMessageTemplates($all = TRUE) { | |
135 | $msgTpls = array(); | |
136 | ||
c6327d7d | 137 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 TO |
138 | $messageTemplates->is_active = 1; |
139 | ||
140 | if (!$all) { | |
141 | $messageTemplates->workflow_id = 'NULL'; | |
142 | } | |
143 | $messageTemplates->find(); | |
144 | while ($messageTemplates->fetch()) { | |
145 | $msgTpls[$messageTemplates->id] = $messageTemplates->msg_title; | |
146 | } | |
147 | asort($msgTpls); | |
148 | return $msgTpls; | |
149 | } | |
150 | ||
151 | static function sendReminder($contactId, $email, $messageTemplateID, $from) { | |
152 | ||
c6327d7d | 153 | $messageTemplates = new CRM_Core_DAO_MessageTemplate(); |
6a488035 TO |
154 | $messageTemplates->id = $messageTemplateID; |
155 | ||
156 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
157 | $result = NULL; | |
158 | $hookTokens = array(); | |
159 | ||
160 | if ($messageTemplates->find(TRUE)) { | |
161 | $body_text = $messageTemplates->msg_text; | |
162 | $body_html = $messageTemplates->msg_html; | |
163 | $body_subject = $messageTemplates->msg_subject; | |
164 | if (!$body_text) { | |
165 | $body_text = CRM_Utils_String::htmlToText($body_html); | |
166 | } | |
167 | ||
168 | $params = array(array('contact_id', '=', $contactId, 0, 0)); | |
169 | list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params); | |
170 | ||
171 | //CRM-4524 | |
172 | $contact = reset($contact); | |
173 | ||
174 | if (!$contact || is_a($contact, 'CRM_Core_Error')) { | |
175 | return NULL; | |
176 | } | |
177 | ||
178 | //CRM-5734 | |
179 | ||
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)); | |
184 | ||
185 | // get replacement text for these tokens | |
186 | $returnProperties = array("preferred_mail_format" => 1); | |
c39c0aa1 | 187 | if (isset($tokens['contact'])) { |
6a488035 | 188 | foreach ($tokens['contact'] as $key => $value) { |
c39c0aa1 | 189 | $returnProperties[$value] = 1; |
6a488035 TO |
190 | } |
191 | } | |
192 | list($details) = CRM_Utils_Token::getTokenDetails(array($contactId), | |
193 | $returnProperties, | |
194 | null, null, false, | |
195 | $tokens, | |
c6327d7d | 196 | 'CRM_Core_BAO_MessageTemplate'); |
6a488035 TO |
197 | $contact = reset( $details ); |
198 | ||
199 | // call token hook | |
c39c0aa1 | 200 | $hookTokens = array(); |
6a488035 TO |
201 | CRM_Utils_Hook::tokens($hookTokens); |
202 | $categories = array_keys($hookTokens); | |
203 | ||
c39c0aa1 | 204 | // do replacements in text and html body |
6a488035 TO |
205 | $type = array('html', 'text'); |
206 | foreach ($type as $key => $value) { | |
207 | $bodyType = "body_{$value}"; | |
208 | if ($$bodyType) { | |
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); | |
214 | } | |
215 | } | |
216 | $html = $body_html; | |
217 | $text = $body_text; | |
218 | ||
4ee279f4 | 219 | $smarty = CRM_Core_Smarty::singleton(); |
220 | foreach (array( | |
221 | 'text', 'html') as $elem) { | |
222 | $$elem = $smarty->fetch("string:{$$elem}"); | |
223 | } | |
224 | ||
6a488035 TO |
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); | |
230 | ||
4ee279f4 | 231 | $messageSubject = $smarty->fetch("string:{$messageSubject}"); |
232 | ||
6a488035 TO |
233 | // set up the parameters for CRM_Utils_Mail::send |
234 | $mailParams = array( | |
235 | 'groupName' => 'Scheduled Reminder Sender', | |
236 | 'from' => $from, | |
237 | 'toName' => $contact['display_name'], | |
238 | 'toEmail' => $email, | |
239 | 'subject' => $messageSubject, | |
240 | ); | |
241 | if (!$html || $contact['preferred_mail_format'] == 'Text' || | |
242 | $contact['preferred_mail_format'] == 'Both' | |
243 | ) { | |
244 | // render the & entities in text mode, so that the links work | |
245 | $mailParams['text'] = str_replace('&', '&', $text); | |
246 | } | |
247 | if ($html && ($contact['preferred_mail_format'] == 'HTML' || | |
248 | $contact['preferred_mail_format'] == 'Both' | |
249 | )) { | |
250 | $mailParams['html'] = $html; | |
251 | } | |
252 | ||
253 | $result = CRM_Utils_Mail::send($mailParams); | |
254 | } | |
255 | ||
256 | $messageTemplates->free(); | |
257 | ||
258 | return $result; | |
259 | } | |
260 | ||
261 | /** | |
262 | * Revert a message template to its default subject+text+HTML state | |
263 | * | |
264 | * @param integer id id of the template | |
265 | * | |
266 | * @return void | |
267 | */ | |
268 | static function revert($id) { | |
269 | $diverted = new self; | |
270 | $diverted->id = (int) $id; | |
271 | $diverted->find(1); | |
272 | ||
273 | if ($diverted->N != 1) { | |
274 | CRM_Core_Error::fatal(ts('Did not find a message template with id of %1.', array(1 => $id))); | |
275 | } | |
276 | ||
277 | $orig = new self; | |
278 | $orig->workflow_id = $diverted->workflow_id; | |
279 | $orig->is_reserved = 1; | |
280 | $orig->find(1); | |
281 | ||
282 | if ($orig->N != 1) { | |
283 | CRM_Core_Error::fatal(ts('Message template with id of %1 does not have a default to revert to.', array(1 => $id))); | |
284 | } | |
285 | ||
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; | |
290 | $diverted->save(); | |
291 | } | |
292 | ||
293 | /** | |
294 | * Send an email from the specified template based on an array of params | |
295 | * | |
296 | * @param array $params a string-keyed array of function params, see function body for details | |
297 | * | |
298 | * @return array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates | |
299 | */ | |
300 | static function sendTemplate($params) { | |
301 | $defaults = array( | |
302 | // option group name of the template | |
303 | 'groupName' => NULL, | |
304 | // option value name of the template | |
305 | 'valueName' => NULL, | |
306 | // ID of the template | |
307 | 'messageTemplateID' => NULL, | |
308 | // contact id if contact tokens are to be replaced | |
309 | 'contactId' => NULL, | |
310 | // additional template params (other than the ones already set in the template singleton) | |
311 | 'tplParams' => array(), | |
312 | // the From: header | |
313 | 'from' => NULL, | |
314 | // the recipient’s name | |
315 | 'toName' => NULL, | |
316 | // the recipient’s email - mail is sent only if set | |
317 | 'toEmail' => NULL, | |
318 | // the Cc: header | |
319 | 'cc' => NULL, | |
320 | // the Bcc: header | |
321 | 'bcc' => NULL, | |
322 | // the Reply-To: header | |
323 | 'replyTo' => NULL, | |
324 | // email attachments | |
325 | 'attachments' => NULL, | |
326 | // whether this is a test email (and hence should include the test banner) | |
327 | 'isTest' => FALSE, | |
328 | // filename of optional PDF version to add as attachment (do not include path) | |
329 | 'PDFFilename' => NULL, | |
330 | ); | |
331 | $params = array_merge($defaults, $params); | |
332 | ||
333 | if ((!$params['groupName'] || | |
334 | !$params['valueName'] | |
335 | ) && | |
336 | !$params['messageTemplateID'] | |
337 | ) { | |
338 | CRM_Core_Error::fatal(ts("Message template's option group and/or option value or ID missing.")); | |
339 | } | |
340 | ||
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')); | |
347 | } | |
348 | else { | |
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')); | |
356 | } | |
357 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
358 | $dao->fetch(); | |
359 | ||
360 | if (!$dao->N) { | |
361 | if ($params['messageTemplateID']) { | |
362 | CRM_Core_Error::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID']))); | |
363 | } | |
364 | else { | |
365 | CRM_Core_Error::fatal(ts('No such message template: option group %1, option value %2.', array(1 => $params['groupName'], 2 => $params['valueName']))); | |
366 | } | |
367 | } | |
368 | ||
369 | $subject = $dao->subject; | |
370 | $text = $dao->text; | |
371 | $html = $dao->html; | |
372 | $format = $dao->format; | |
373 | $dao->free(); | |
374 | ||
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); | |
383 | $testDao->fetch(); | |
384 | ||
385 | $subject = $testDao->subject . $subject; | |
386 | $text = $testDao->text . $text; | |
387 | $html = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $html); | |
388 | $testDao->free(); | |
389 | } | |
390 | ||
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); | |
400 | ||
401 | $contactID = CRM_Utils_Array::value('contactId', $params); | |
402 | ||
403 | if ($contactID) { | |
404 | $contactParams = array('contact_id' => $contactID); | |
405 | $returnProperties = array(); | |
406 | ||
407 | if (isset($tokens['text']['contact'])) { | |
408 | foreach ($tokens['text']['contact'] as $name) { | |
409 | $returnProperties[$name] = 1; | |
410 | } | |
411 | } | |
412 | ||
413 | if (isset($tokens['html']['contact'])) { | |
414 | foreach ($tokens['html']['contact'] as $name) { | |
415 | $returnProperties[$name] = 1; | |
416 | } | |
417 | } | |
418 | list($contact) = CRM_Utils_Token::getTokenDetails($contactParams, | |
419 | $returnProperties, | |
420 | FALSE, FALSE, NULL, | |
421 | CRM_Utils_Token::flattenTokens($tokens), | |
422 | // we should consider adding groupName and valueName here | |
423 | 'CRM_Core_BAO_MessageTemplate' | |
424 | ); | |
425 | $contact = $contact[$contactID]; | |
426 | } | |
427 | ||
d8ea987e SG |
428 | $subject = CRM_Utils_Token::replaceDomainTokens($subject, $domain, FALSE, $tokens['text'], TRUE); |
429 | $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text'], TRUE); | |
6a488035 TO |
430 | $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html'], TRUE); |
431 | ||
432 | if ($contactID) { | |
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); | |
436 | ||
437 | ||
438 | $contactArray = array($contactID => $contact); | |
439 | CRM_Utils_Hook::tokenValues($contactArray, | |
440 | array($contactID), | |
441 | NULL, | |
442 | CRM_Utils_Token::flattenTokens($tokens), | |
443 | // we should consider adding groupName and valueName here | |
444 | 'CRM_Core_BAO_MessageTemplate' | |
445 | ); | |
446 | $contact = $contactArray[$contactID]; | |
447 | ||
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); | |
451 | } | |
452 | ||
4ee279f4 | 453 | // strip whitespace from ends and turn into a single line |
454 | $subject = "{strip}$subject{/strip}"; | |
455 | ||
456 | // parse the three elements with Smarty | |
457 | ||
458 | ||
459 | $smarty = CRM_Core_Smarty::singleton(); | |
460 | foreach ($params['tplParams'] as $name => $value) { | |
461 | $smarty->assign($name, $value); | |
462 | } | |
463 | foreach (array( | |
464 | 'subject', 'text', 'html') as $elem) { | |
465 | $$elem = $smarty->fetch("string:{$$elem}"); | |
466 | } | |
467 | ||
6a488035 TO |
468 | // send the template, honouring the target user’s preferences (if any) |
469 | $sent = FALSE; | |
470 | ||
471 | // create the params array | |
472 | $params['subject'] = $subject; | |
473 | $params['text'] = $text; | |
474 | $params['html'] = $html; | |
475 | ||
476 | if ($params['toEmail']) { | |
477 | $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1)); | |
478 | list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($contactParams); | |
479 | ||
480 | $prefs = array_pop($contact); | |
481 | ||
482 | if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') { | |
483 | $params['text'] = NULL; | |
484 | } | |
485 | ||
486 | if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') { | |
487 | $params['html'] = NULL; | |
488 | } | |
489 | ||
490 | $config = CRM_Core_Config::singleton(); | |
491 | $pdf_filename = ''; | |
492 | if ($config->doNotAttachPDFReceipt && | |
493 | $params['PDFFilename'] && | |
494 | $params['html'] | |
495 | ) { | |
6a488035 TO |
496 | if (empty($params['attachments'])) { |
497 | $params['attachments'] = array(); | |
498 | } | |
383c047b | 499 | $params['attachments'][] = CRM_Utils_Mail::appendPDF($params['PDFFilename'], $params['html'], $format); |
6a488035 TO |
500 | } |
501 | ||
502 | $sent = CRM_Utils_Mail::send($params); | |
503 | ||
504 | if ($pdf_filename) { | |
505 | unlink($pdf_filename); | |
506 | } | |
507 | } | |
508 | ||
509 | return array($sent, $subject, $text, $html); | |
510 | } | |
511 | } |