Merge pull request #20807 from davidjosephhayes/wordpress-profile-signon-2
[civicrm-core.git] / CRM / Mailing / BAO / Mailing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 require_once 'Mail/mime.php';
18
19 /**
20 * Class CRM_Mailing_BAO_Mailing
21 */
22 class CRM_Mailing_BAO_Mailing extends CRM_Mailing_DAO_Mailing {
23
24 /**
25 * An array that holds the complete templates
26 * including any headers or footers that need to be prepended
27 * or appended to the body.
28 * @var array
29 */
30 private $preparedTemplates = NULL;
31
32 /**
33 * An array that holds the complete templates
34 * including any headers or footers that need to be prepended
35 * or appended to the body.
36 * @var array
37 */
38 private $templates = NULL;
39
40 /**
41 * An array that holds the tokens that are specifically found in our text and html bodies.
42 * @var array
43 */
44 private $tokens = NULL;
45
46 /**
47 * An array that holds the tokens that are specifically found in our text and html bodies.
48 * @var array
49 */
50 private $flattenedTokens = NULL;
51
52 /**
53 * The header associated with this mailing.
54 * @var string
55 */
56 private $header = NULL;
57
58 /**
59 * The footer associated with this mailing.
60 * @var string
61 */
62 private $footer = NULL;
63
64 /**
65 * The HTML content of the message.
66 * @var string
67 */
68 private $html = NULL;
69
70 /**
71 * The text content of the message.
72 * @var string
73 */
74 private $text = NULL;
75
76 /**
77 * Cached BAO for the domain.
78 * @var int
79 */
80 private $_domain = NULL;
81
82 /**
83 * Class constructor.
84 */
85 public function __construct() {
86 parent::__construct();
87 }
88
89 /**
90 * @deprecated
91 *
92 * @param int $mailingID
93 *
94 * @return int
95 */
96 public static function getRecipientsCount($mailingID) {
97 //rebuild the recipients
98 self::getRecipients($mailingID);
99
100 return civicrm_api3('MailingRecipients', 'getcount', ['mailing_id' => $mailingID]);
101 }
102
103 /**
104 * This function retrieve recipients of selected mailing groups.
105 *
106 * @param int $mailingID
107 *
108 * @return void
109 */
110 public static function getRecipients($mailingID) {
111 // load mailing object
112 $mailingObj = new self();
113 $mailingObj->id = $mailingID;
114 $mailingObj->find(TRUE);
115
116 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
117 $contact = CRM_Contact_DAO_Contact::getTableName();
118 $isSMSmode = (!CRM_Utils_System::isNull($mailingObj->sms_provider_id));
119
120 $mailingGroup = new CRM_Mailing_DAO_MailingGroup();
121 $recipientsGroup = $excludeSmartGroupIDs = $includeSmartGroupIDs = $priorMailingIDs = [];
122 $dao = CRM_Utils_SQL_Select::from('civicrm_mailing_group')
123 ->select('GROUP_CONCAT(DISTINCT entity_id SEPARATOR ",") as group_ids, group_type, entity_table')
124 ->where('mailing_id = #mailing_id AND entity_table RLIKE "^civicrm_(group.*|mailing)$" ')
125 ->groupBy(['group_type', 'entity_table'])
126 ->param('!groupTableName', CRM_Contact_BAO_Group::getTableName())
127 ->param('#mailing_id', $mailingID)
128 ->execute();
129 while ($dao->fetch()) {
130 if ($dao->entity_table == 'civicrm_mailing') {
131 $priorMailingIDs[$dao->group_type] = explode(',', $dao->group_ids);
132 }
133 else {
134 $recipientsGroup[$dao->group_type] = empty($recipientsGroup[$dao->group_type]) ? explode(',', $dao->group_ids) : array_merge($recipientsGroup[$dao->group_type], explode(',', $dao->group_ids));
135 }
136 }
137
138 // there is no need to proceed further if no mailing group is selected to include recipients,
139 // but before return clear the mailing recipients populated earlier since as per current params no group is selected
140 if (empty($recipientsGroup['Include']) && empty($priorMailingIDs['Include'])) {
141 CRM_Core_DAO::executeQuery(" DELETE FROM civicrm_mailing_recipients WHERE mailing_id = %1 ", [
142 1 => [
143 $mailingID,
144 'Integer',
145 ],
146 ]);
147 return;
148 }
149
150 list($location_filter, $order_by) = self::getLocationFilterAndOrderBy($mailingObj->email_selection_method, $mailingObj->location_type_id);
151
152 // get all the saved searches AND hierarchical groups
153 // and load them in the cache
154 foreach ($recipientsGroup as $groupType => $groupIDs) {
155 $groupDAO = CRM_Utils_SQL_Select::from('civicrm_group')
156 ->where('id IN (#groupIDs)')
157 ->where('saved_search_id != 0 OR saved_search_id IS NOT NULL OR children IS NOT NULL')
158 ->param('#groupIDs', $groupIDs)
159 ->execute();
160 while ($groupDAO->fetch()) {
161 // hidden smart groups always have a cache date and there is no other way
162 // we can rebuilt the contact list from UI so consider such smart group
163 if ($groupDAO->cache_date == NULL || $groupDAO->is_hidden) {
164 CRM_Contact_BAO_GroupContactCache::load($groupDAO);
165 }
166 if ($groupType == 'Include') {
167 $includeSmartGroupIDs[] = $groupDAO->id;
168 }
169 elseif ($groupType == 'Exclude') {
170 $excludeSmartGroupIDs[] = $groupDAO->id;
171 }
172 //NOTE: Do nothing for base
173 }
174 }
175
176 // Create a temp table for contact exclusion.
177 $excludeTempTable = CRM_Utils_SQL_TempTable::build()->setCategory('exrecipient')->setMemory()->createWithColumns('contact_id int primary key');
178 $excludeTempTablename = $excludeTempTable->getName();
179 // populate exclude temp-table with recipients to be excluded from the list
180 // on basis of selected recipients groups and/or previous mailing
181 if (!empty($recipientsGroup['Exclude'])) {
182 CRM_Utils_SQL_Select::from('civicrm_group_contact')
183 ->select('DISTINCT contact_id')
184 ->where('status = "Added" AND group_id IN (#groups)')
185 ->param('#groups', $recipientsGroup['Exclude'])
186 ->insertInto($excludeTempTablename, ['contact_id'])
187 ->execute();
188
189 if (count($excludeSmartGroupIDs)) {
190 CRM_Utils_SQL_Select::from('civicrm_group_contact_cache')
191 ->select('contact_id')
192 ->where('group_id IN (#groups)')
193 ->param('#groups', $excludeSmartGroupIDs)
194 ->insertIgnoreInto($excludeTempTablename, ['contact_id'])
195 ->execute();
196 }
197 }
198 if (!empty($priorMailingIDs['Exclude'])) {
199 CRM_Utils_SQL_Select::from('civicrm_mailing_recipients')
200 ->select('DISTINCT contact_id')
201 ->where('mailing_id IN (#mailings)')
202 ->param('#mailings', $priorMailingIDs['Exclude'])
203 ->insertIgnoreInto($excludeTempTablename, ['contact_id'])
204 ->execute();
205 }
206
207 if (!empty($recipientsGroup['Base'])) {
208 CRM_Utils_SQL_Select::from('civicrm_group_contact')
209 ->select('DISTINCT contact_id')
210 ->where('status = "Removed" AND group_id IN (#groups)')
211 ->param('#groups', $recipientsGroup['Base'])
212 ->insertIgnoreInto($excludeTempTablename, ['contact_id'])
213 ->execute();
214 }
215
216 $entityColumn = $isSMSmode ? 'phone_id' : 'email_id';
217 $entityTable = $isSMSmode ? CRM_Core_DAO_Phone::getTableName() : CRM_Core_DAO_Email::getTableName();
218 // Get all the group contacts we want to include.
219 $includedTempTable = CRM_Utils_SQL_TempTable::build()->setCategory('inrecipient')->setMemory()->createWithColumns('contact_id int primary key, ' . $entityColumn . ' int');
220 $includedTempTablename = $includedTempTable->getName();
221
222 if ($isSMSmode) {
223 $criteria = [
224 'is_deleted' => CRM_Utils_SQL_Select::fragment()->where("$contact.is_deleted = 0"),
225 'is_opt_out' => CRM_Utils_SQL_Select::fragment()->where("$contact.is_opt_out = 0"),
226 'is_deceased' => CRM_Utils_SQL_Select::fragment()->where("$contact.is_deceased <> 1"),
227 'do_not_sms' => CRM_Utils_SQL_Select::fragment()->where("$contact.do_not_sms = 0"),
228 'location_filter' => CRM_Utils_SQL_Select::fragment()->where("$entityTable.phone_type_id = " . CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_Phone', 'phone_type_id', 'Mobile')),
229 'phone_not_null' => CRM_Utils_SQL_Select::fragment()->where("$entityTable.phone IS NOT NULL"),
230 'phone_not_empty' => CRM_Utils_SQL_Select::fragment()->where("$entityTable.phone != ''"),
231 'mailing_id' => CRM_Utils_SQL_Select::fragment()->where("mg.mailing_id = #mailingID"),
232 'temp_contact_null' => CRM_Utils_SQL_Select::fragment()->where('temp.contact_id IS null'),
233 'order_by' => CRM_Utils_SQL_Select::fragment()->orderBy("$entityTable.is_primary"),
234 ];
235 }
236 else {
237 // Criteria to filter recipients that need to be included
238 $criteria = [
239 'is_deleted' => CRM_Utils_SQL_Select::fragment()->where("$contact.is_deleted = 0"),
240 'do_not_email' => CRM_Utils_SQL_Select::fragment()->where("$contact.do_not_email = 0"),
241 'is_opt_out' => CRM_Utils_SQL_Select::fragment()->where("$contact.is_opt_out = 0"),
242 'is_deceased' => CRM_Utils_SQL_Select::fragment()->where("$contact.is_deceased <> 1"),
243 'location_filter' => CRM_Utils_SQL_Select::fragment()->where($location_filter),
244 'email_not_null' => CRM_Utils_SQL_Select::fragment()->where("$entityTable.email IS NOT NULL"),
245 'email_not_empty' => CRM_Utils_SQL_Select::fragment()->where("$entityTable.email != ''"),
246 'email_not_on_hold' => CRM_Utils_SQL_Select::fragment()->where("$entityTable.on_hold = 0"),
247 'mailing_id' => CRM_Utils_SQL_Select::fragment()->where("mg.mailing_id = #mailingID"),
248 'temp_contact_null' => CRM_Utils_SQL_Select::fragment()->where('temp.contact_id IS NULL'),
249 'order_by' => CRM_Utils_SQL_Select::fragment()->orderBy($order_by),
250 ];
251 }
252
253 // Allow user to alter query responsible to fetch mailing recipients before build,
254 // by changing the mail filters identified $params
255 CRM_Utils_Hook::alterMailingRecipients($mailingObj, $criteria, 'pre');
256
257 // Get the group contacts, but only those which are not in the
258 // exclusion temp table.
259 if (!empty($recipientsGroup['Include'])) {
260 CRM_Utils_SQL_Select::from($entityTable)
261 ->select("$contact.id as contact_id, $entityTable.id as $entityColumn")
262 ->join($contact, " INNER JOIN $contact ON $entityTable.contact_id = $contact.id ")
263 ->join('gc', " INNER JOIN civicrm_group_contact gc ON gc.contact_id = $contact.id ")
264 ->join('mg', " INNER JOIN civicrm_mailing_group mg ON gc.group_id = mg.entity_id AND mg.search_id IS NULL ")
265 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ")
266 ->where('gc.group_id IN (#groups) AND gc.status = "Added"')
267 ->merge($criteria)
268 ->groupBy(["$contact.id", "$entityTable.id"])
269 ->replaceInto($includedTempTablename, ['contact_id', $entityColumn])
270 ->param('#groups', $recipientsGroup['Include'])
271 ->param('#mailingID', $mailingID)
272 ->execute();
273 }
274
275 // Get recipients selected in prior mailings
276 if (!empty($priorMailingIDs['Include'])) {
277 CRM_Utils_SQL_Select::from('civicrm_mailing_recipients')
278 ->select("DISTINCT civicrm_mailing_recipients.contact_id, $entityColumn")
279 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON civicrm_mailing_recipients.contact_id = temp.contact_id ")
280 ->where('mailing_id IN (#mailings)')
281 ->where('temp.contact_id IS NULL')
282 ->param('#mailings', $priorMailingIDs['Include'])
283 ->insertIgnoreInto($includedTempTablename, [
284 'contact_id',
285 $entityColumn,
286 ])
287 ->execute();
288 }
289
290 if (count($includeSmartGroupIDs)) {
291 $query = CRM_Utils_SQL_Select::from($contact)
292 ->select("$contact.id as contact_id, $entityTable.id as $entityColumn")
293 ->join($entityTable, " INNER JOIN $entityTable ON $entityTable.contact_id = $contact.id ")
294 ->join('gc', " INNER JOIN civicrm_group_contact_cache gc ON $contact.id = gc.contact_id ")
295 ->join('gcr', " LEFT JOIN civicrm_group_contact gcr ON gc.group_id = gcr.group_id AND gc.contact_id = gcr.contact_id")
296 ->join('mg', " INNER JOIN civicrm_mailing_group mg ON gc.group_id = mg.entity_id AND mg.search_id IS NULL ")
297 ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ")
298 ->where('gc.group_id IN (#groups)')
299 ->where('gcr.status IS NULL OR gcr.status != "Removed"')
300 ->merge($criteria)
301 ->replaceInto($includedTempTablename, ['contact_id', $entityColumn])
302 ->param('#groups', $includeSmartGroupIDs)
303 ->param('#mailingID', $mailingID)
304 ->execute();
305 }
306
307 // Construct the filtered search queries.
308 $dao = CRM_Utils_SQL_Select::from('civicrm_mailing_group')
309 ->select('search_id, search_args, entity_id')
310 ->where('search_id IS NOT NULL AND mailing_id = #mailingID')
311 ->param('#mailingID', $mailingID)
312 ->execute();
313 while ($dao->fetch()) {
314 $customSQL = CRM_Contact_BAO_SearchCustom::civiMailSQL($dao->search_id,
315 $dao->search_args,
316 $dao->entity_id
317 );
318 $query = "REPLACE INTO {$includedTempTablename} ($entityColumn, contact_id) {$customSQL} ";
319 $mailingGroup->query($query);
320 }
321
322 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause();
323
324 // clear all the mailing recipients before populating
325 CRM_Core_DAO::executeQuery(" DELETE FROM civicrm_mailing_recipients WHERE mailing_id = %1 ", [
326 1 => [
327 $mailingID,
328 'Integer',
329 ],
330 ]);
331
332 $selectClause = ['#mailingID', 'i.contact_id', "i.$entityColumn"];
333 // CRM-3975
334 $orderBy = ["i.contact_id", "i.$entityColumn"];
335
336 $query = CRM_Utils_SQL_Select::from('civicrm_contact contact_a')
337 ->join('i', " INNER JOIN {$includedTempTablename} i ON contact_a.id = i.contact_id ");
338 if (!$isSMSmode && $mailingObj->dedupe_email) {
339 $orderBy = ["MIN(i.contact_id)", "MIN(i.$entityColumn)"];
340 $query = $query->join('e', " INNER JOIN civicrm_email e ON e.id = i.email_id ")
341 ->groupBy("e.email");
342 if (CRM_Utils_SQL::supportsFullGroupBy()) {
343 $selectClause = [
344 '#mailingID',
345 'ANY_VALUE(i.contact_id) contact_id',
346 "ANY_VALUE(i.$entityColumn) $entityColumn",
347 "e.email",
348 ];
349 }
350 }
351
352 $query = $query->select($selectClause)->orderBy($orderBy);
353 if (!CRM_Utils_System::isNull($aclFrom)) {
354 $query = $query->join('acl', $aclFrom);
355 }
356 if (!CRM_Utils_System::isNull($aclWhere)) {
357 $query = $query->where($aclWhere);
358 }
359
360 // this mean if dedupe_email AND the mysql 5.7 supports ONLY_FULL_GROUP_BY mode then as
361 // SELECT must contain 'email' column as its used in GROUP BY, so in order to resolve This
362 // here the whole SQL code is wrapped up in FROM table i and not selecting email column for INSERT
363 if ($key = array_search('e.email', $selectClause)) {
364 unset($selectClause[$key]);
365 $sql = $query->toSQL();
366 CRM_Utils_SQL_Select::from("( $sql ) AS i ")
367 ->select($selectClause)
368 ->insertInto('civicrm_mailing_recipients', ['mailing_id', 'contact_id', $entityColumn])
369 ->param('#mailingID', $mailingID)
370 ->execute();
371 }
372 else {
373 $query->insertInto('civicrm_mailing_recipients', ['mailing_id', 'contact_id', $entityColumn])
374 ->param('#mailingID', $mailingID)
375 ->execute();
376 }
377
378 // if we need to add all emails marked bulk, do it as a post filter
379 // on the mailing recipients table
380 if (CRM_Core_BAO_Email::isMultipleBulkMail()) {
381 self::addMultipleEmails($mailingID);
382 }
383
384 // Delete the temp table.
385 $mailingGroup->reset();
386 $excludeTempTable->drop();
387 $includedTempTable->drop();
388
389 CRM_Utils_Hook::alterMailingRecipients($mailingObj, $criteria, 'post');
390 }
391
392 /**
393 * Function to retrieve location filter and order by clause later used by SQL query that is used to fetch and include mailing recipients
394 *
395 * @param string $email_selection_method
396 * @param int $location_type_id
397 *
398 * @return array
399 */
400 public static function getLocationFilterAndOrderBy($email_selection_method, $location_type_id) {
401 if ($email_selection_method !== 'automatic' && !$location_type_id) {
402 throw new \CRM_Core_Exception(ts('You have selected an email Selection Method without specifying a Location Type. Please go back and change your recipient settings (using the wrench icon next to "Recipients").'));
403 }
404 $email = CRM_Core_DAO_Email::getTableName();
405 // Note: When determining the ORDER that results are returned, it's
406 // the record that comes last that counts. That's because we are
407 // INSERT'ing INTO a table with a primary id so that last record
408 // over writes any previous record.
409 switch ($email_selection_method) {
410 case 'location-exclude':
411 $location_filter = "($email.location_type_id != $location_type_id)";
412 // If there is more than one email that doesn't match the location,
413 // prefer the one marked is_bulkmail, followed by is_primary.
414 $orderBy = ["$email.is_bulkmail", "$email.is_primary"];
415 break;
416
417 case 'location-only':
418 $location_filter = "($email.location_type_id = $location_type_id)";
419 // If there is more than one email of the desired location, prefer
420 // the one marked is_bulkmail, followed by is_primary.
421 $orderBy = ["$email.is_bulkmail", "$email.is_primary"];
422 break;
423
424 case 'location-prefer':
425 $location_filter = "($email.is_bulkmail = 1 OR $email.is_primary = 1 OR $email.location_type_id = $location_type_id)";
426 // ORDER BY is more complicated because we have to set an arbitrary
427 // order that prefers the location that we want. We do that using
428 // the FIELD function. For more info, see:
429 // https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field
430 // We assign the location type we want the value "1" by putting it
431 // in the first position after we name the field. All other location
432 // types are left out, so they will be assigned the value 0. That
433 // means, they will all be equally tied for first place, with our
434 // location being last.
435 $orderBy = [
436 "FIELD($email.location_type_id, $location_type_id)",
437 "$email.is_bulkmail",
438 "$email.is_primary",
439 ];
440 break;
441
442 case 'automatic':
443 // fall through to default
444 default:
445 $location_filter = "($email.is_bulkmail = 1 OR $email.is_primary = 1)";
446 $orderBy = ["$email.is_bulkmail"];
447 }
448
449 return [$location_filter, $orderBy];
450 }
451
452 /**
453 * Returns the regex patterns that are used for preparing the text and html templates.
454 *
455 * @param bool $onlyHrefs
456 *
457 * @return array|string
458 */
459 private function getPatterns($onlyHrefs = FALSE) {
460
461 $patterns = [];
462
463 $protos = '(https?|ftp|mailto)';
464 $letters = '\w';
465 $gunk = '\{\}/#~:.?+=&;%@!\,\-\|\(\)\*';
466 $punc = '.:?\-';
467 $any = "{$letters}{$gunk}{$punc}";
468 if ($onlyHrefs) {
469 $pattern = "\\bhref[ ]*=[ ]*([\"'])?(($protos:[$any]+?(?=[$punc]*[^$any]|$)))([\"'])?";
470 }
471 else {
472 $pattern = "\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))";
473 }
474
475 $patterns[] = $pattern;
476 $patterns[] = '\\\\\{\w+\.\w+\\\\\}|\{\{\w+\.\w+\}\}';
477 $patterns[] = '\{\w+\.\w+\}';
478
479 $patterns = '{' . implode('|', $patterns) . '}imu';
480
481 return $patterns;
482 }
483
484 /**
485 * Returns an array that denotes the type of token that we are dealing with
486 * we use the type later on when we are doing a token replacement lookup
487 *
488 * @param string $token
489 * The token for which we will be doing adata lookup.
490 *
491 * @return array
492 * An array that holds the token itself and the type.
493 * the type will tell us which function to use for the data lookup
494 * if we need to do a lookup at all
495 */
496 public function &getDataFunc($token) {
497 static $_categories = NULL;
498 static $_categoryString = NULL;
499 if (!$_categories) {
500 $_categories = [
501 'domain' => NULL,
502 'action' => NULL,
503 'mailing' => NULL,
504 'contact' => NULL,
505 ];
506
507 CRM_Utils_Hook::tokens($_categories);
508 $_categoryString = implode('|', array_keys($_categories));
509 }
510
511 $funcStruct = ['type' => NULL, 'token' => $token];
512 $matches = [];
513 if ((preg_match('/^href/i', $token) || preg_match('/^http/i', $token))) {
514 // it is a url so we need to check to see if there are any tokens embedded
515 // if so then call this function again to get the token dataFunc
516 // and assign the type 'embedded' so that the data retrieving function
517 // will know what how to handle this token.
518 if (preg_match_all('/(\{\w+\.\w+\})/', $token, $matches)) {
519 $funcStruct['type'] = 'embedded_url';
520 $funcStruct['embed_parts'] = $funcStruct['token'] = [];
521 foreach ($matches[1] as $match) {
522 $preg_token = '/' . preg_quote($match, '/') . '/';
523 $list = preg_split($preg_token, $token, 2);
524 $funcStruct['embed_parts'][] = $list[0];
525 $token = $list[1];
526 $funcStruct['token'][] = $this->getDataFunc($match);
527 }
528 // fixed truncated url, CRM-7113
529 if ($token) {
530 $funcStruct['embed_parts'][] = $token;
531 }
532 }
533 else {
534 $funcStruct['type'] = 'url';
535 }
536 }
537 elseif (preg_match('/^\{(' . $_categoryString . ')\.(\w+)\}$/', $token, $matches)) {
538 $funcStruct['type'] = $matches[1];
539 $funcStruct['token'] = $matches[2];
540 }
541 elseif (preg_match('/\\\\\{(\w+\.\w+)\\\\\}|\{\{(\w+\.\w+)\}\}/', $token, $matches)) {
542 // we are an escaped token
543 // so remove the escape chars
544 $unescaped_token = preg_replace('/\{\{|\}\}|\\\\\{|\\\\\}/', '', $matches[0]);
545 $funcStruct['token'] = '{' . $unescaped_token . '}';
546 }
547 return $funcStruct;
548 }
549
550 /**
551 * Prepares the text and html templates
552 * for generating the emails and returns a copy of the
553 * prepared templates
554 *
555 * @deprecated
556 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
557 */
558 private function getPreparedTemplates() {
559 if (!$this->preparedTemplates) {
560 $patterns['html'] = $this->getPatterns(TRUE);
561 $patterns['subject'] = $patterns['text'] = $this->getPatterns();
562 $templates = $this->getTemplates();
563
564 $this->preparedTemplates = [];
565
566 foreach ([
567 'html',
568 'text',
569 'subject',
570 ] as $key) {
571 if (!isset($templates[$key])) {
572 continue;
573 }
574
575 $matches = [];
576 $tokens = [];
577 $split_template = [];
578
579 $email = $templates[$key];
580 preg_match_all($patterns[$key], $email, $matches, PREG_PATTERN_ORDER);
581 foreach ($matches[0] as $idx => $token) {
582 $preg_token = '/' . preg_quote($token, '/') . '/im';
583 list($split_template[], $email) = preg_split($preg_token, $email, 2);
584 array_push($tokens, $this->getDataFunc($token));
585 }
586 if ($email) {
587 $split_template[] = $email;
588 }
589 $this->preparedTemplates[$key]['template'] = $split_template;
590 $this->preparedTemplates[$key]['tokens'] = $tokens;
591 }
592 }
593 return ($this->preparedTemplates);
594 }
595
596 /**
597 * Retrieve a ref to an array that holds the email and text templates for this email
598 * assembles the complete template including the header and footer
599 * that the user has uploaded or declared (if they have done that)
600 *
601 * @return array
602 * reference to an assoc array
603 */
604 public function getTemplates() {
605 if (!$this->templates) {
606 $this->getHeaderFooter();
607 $this->templates = [];
608 if ($this->body_text || !empty($this->header)) {
609 $template = [];
610 if (!empty($this->header->body_text)) {
611 $template[] = $this->header->body_text;
612 }
613 elseif (!empty($this->header->body_html)) {
614 $template[] = CRM_Utils_String::htmlToText($this->header->body_html);
615 }
616
617 if ($this->body_text) {
618 $template[] = $this->body_text;
619 }
620 else {
621 $template[] = CRM_Utils_String::htmlToText($this->body_html);
622 }
623
624 if (!empty($this->footer->body_text)) {
625 $template[] = $this->footer->body_text;
626 }
627 elseif (!empty($this->footer->body_html)) {
628 $template[] = CRM_Utils_String::htmlToText($this->footer->body_html);
629 }
630
631 $this->templates['text'] = implode("\n", $template);
632 }
633
634 // To check for an html part strip tags
635 if (trim(strip_tags($this->body_html, '<img>'))) {
636
637 $template = [];
638 if ($this->header) {
639 $template[] = $this->header->body_html;
640 }
641
642 $template[] = $this->body_html;
643
644 if ($this->footer) {
645 $template[] = $this->footer->body_html;
646 }
647
648 $this->templates['html'] = implode("\n", $template);
649
650 // this is where we create a text template from the html template if the text template did not exist
651 // this way we ensure that every recipient will receive an email even if the pref is set to text and the
652 // user uploads an html email only
653 if (empty($this->templates['text'])) {
654 $this->templates['text'] = CRM_Utils_String::htmlToText($this->templates['html']);
655 }
656 }
657
658 if ($this->subject) {
659 $template = [];
660 $template[] = $this->subject;
661 $this->templates['subject'] = implode("\n", $template);
662 }
663
664 $this->templates['mailingID'] = $this->id;
665 $this->templates['campaign_id'] = $this->campaign_id;
666 $this->templates['template_type'] = $this->template_type;
667 CRM_Utils_Hook::alterMailContent($this->templates);
668 }
669 return $this->templates;
670 }
671
672 /**
673 *
674 * Retrieve a ref to an array that holds all of the tokens in the email body
675 * where the keys are the type of token and the values are ordinal arrays
676 * that hold the token names (even repeated tokens) in the order in which
677 * they appear in the body of the email.
678 *
679 * note: the real work is done in the _getTokens() function
680 *
681 * this function needs to have some sort of a body assigned
682 * either text or html for this to have any meaningful impact
683 *
684 * @return array
685 * reference to an assoc array
686 */
687 public function &getTokens() {
688 if (!$this->tokens) {
689
690 $this->tokens = ['html' => [], 'text' => [], 'subject' => []];
691
692 if ($this->body_html) {
693 $this->_getTokens('html');
694 if (!$this->body_text) {
695 // Since the text template was created from html, use the html tokens.
696 // @see CRM_Mailing_BAO_Mailing::getTemplates()
697 $this->tokens['text'] = $this->tokens['html'];
698 }
699 }
700
701 if ($this->body_text) {
702 $this->_getTokens('text');
703 }
704
705 if ($this->subject) {
706 $this->_getTokens('subject');
707 }
708 }
709
710 return $this->tokens;
711 }
712
713 /**
714 * Returns the token set for all 3 parts as one set. This allows it to be sent to the
715 * hook in one call and standardizes it across other token workflows
716 *
717 * @return array
718 * reference to an assoc array
719 */
720 public function &getFlattenedTokens() {
721 if (!$this->flattenedTokens) {
722 $tokens = $this->getTokens();
723
724 $this->flattenedTokens = CRM_Utils_Token::flattenTokens($tokens);
725 }
726
727 return $this->flattenedTokens;
728 }
729
730 /**
731 *
732 * _getTokens parses out all of the tokens that have been
733 * included in the html and text bodies of the email
734 * we get the tokens and then separate them into an
735 * internal structure named tokens that has the same
736 * form as the static tokens property(?) of the CRM_Utils_Token class.
737 * The difference is that there might be repeated token names as we want the
738 * structures to represent the order in which tokens were found from left to right, top to bottom.
739 *
740 *
741 * @param string $prop name of the property that holds the text that we want to scan for tokens (html, text).
742 * Name of the property that holds the text that we want to scan for tokens (html, text).
743 *
744 * @return void
745 */
746 private function _getTokens($prop) {
747 $templates = $this->getTemplates();
748
749 $newTokens = CRM_Utils_Token::getTokens($templates[$prop]);
750
751 foreach ($newTokens as $type => $names) {
752 if (!isset($this->tokens[$prop][$type])) {
753 $this->tokens[$prop][$type] = [];
754 }
755 foreach ($names as $key => $name) {
756 $this->tokens[$prop][$type][] = $name;
757 }
758 }
759 }
760
761 /**
762 * Generate an event queue for a test job.
763 *
764 * @param array $testParams
765 * Contains form values.
766 *
767 * @return void
768 */
769 public function getTestRecipients($testParams) {
770 if (!empty($testParams['test_group']) && array_key_exists($testParams['test_group'], CRM_Core_PseudoConstant::group())) {
771 $contacts = civicrm_api('contact', 'get', [
772 'version' => 3,
773 'group' => $testParams['test_group'],
774 'return' => 'id',
775 'options' => [
776 'limit' => 100000000000,
777 ],
778 ]);
779
780 foreach (array_keys($contacts['values']) as $groupContact) {
781 $query = "
782 SELECT civicrm_email.id AS email_id,
783 civicrm_email.is_primary as is_primary,
784 civicrm_email.is_bulkmail as is_bulkmail
785 FROM civicrm_email
786 INNER JOIN civicrm_contact ON civicrm_email.contact_id = civicrm_contact.id
787 WHERE (civicrm_email.is_bulkmail = 1 OR civicrm_email.is_primary = 1)
788 AND civicrm_contact.id = {$groupContact}
789 AND civicrm_contact.do_not_email = 0
790 AND civicrm_contact.is_deceased <> 1
791 AND civicrm_email.on_hold = 0
792 AND civicrm_contact.is_opt_out = 0
793 GROUP BY civicrm_email.id
794 ORDER BY civicrm_email.is_bulkmail DESC
795 ";
796 $dao = CRM_Core_DAO::executeQuery($query);
797 if ($dao->fetch()) {
798 $params = [
799 'job_id' => $testParams['job_id'],
800 'email_id' => $dao->email_id,
801 'contact_id' => $groupContact,
802 ];
803 CRM_Mailing_Event_BAO_Queue::create($params);
804 }
805 }
806 }
807 }
808
809 /**
810 * Load this->header and this->footer.
811 */
812 private function getHeaderFooter() {
813 if (!$this->header and $this->header_id) {
814 $this->header = new CRM_Mailing_BAO_MailingComponent();
815 $this->header->id = $this->header_id;
816 $this->header->find(TRUE);
817 }
818
819 if (!$this->footer and $this->footer_id) {
820 $this->footer = new CRM_Mailing_BAO_MailingComponent();
821 $this->footer->id = $this->footer_id;
822 $this->footer->find(TRUE);
823 }
824 }
825
826 /**
827 * Given and array of headers and a prefix, job ID, event queue ID, and hash,
828 * add a Message-ID header if needed.
829 *
830 * i.e. if the global includeMessageId is set and there isn't already a
831 * Message-ID in the array.
832 * The message ID is structured the same way as a verp. However no interpretation
833 * is placed on the values received, so they do not need to follow the verp
834 * convention.
835 *
836 * @param array $headers
837 * Array of message headers to update, in-out.
838 * @param string $prefix
839 * Prefix for the message ID, use same prefixes as verp.
840 * wherever possible
841 * @param string $job_id
842 * Job ID component of the generated message ID.
843 * @param string $event_queue_id
844 * Event Queue ID component of the generated message ID.
845 * @param string $hash
846 * Hash component of the generated message ID.
847 *
848 * @return void
849 */
850 public static function addMessageIdHeader(&$headers, $prefix, $job_id, $event_queue_id, $hash) {
851 $config = CRM_Core_Config::singleton();
852 $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart();
853 $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
854 $includeMessageId = CRM_Core_BAO_MailSettings::includeMessageId();
855 $fields = [];
856 $fields[] = 'Message-ID';
857 // CRM-17754 check if Resent-Message-id is set also if not add it in when re-laying reply email
858 if ($prefix == 'r') {
859 $fields[] = 'Resent-Message-ID';
860 }
861 foreach ($fields as $field) {
862 if ($includeMessageId && (!array_key_exists($field, $headers))) {
863 $headers[$field] = '<' . implode($config->verpSeparator,
864 [
865 $localpart . $prefix,
866 $job_id,
867 $event_queue_id,
868 $hash,
869 ]
870 ) . "@{$emailDomain}>";
871 }
872 }
873
874 }
875
876 /**
877 * Static wrapper for getting verp and urls.
878 *
879 * @param int $job_id
880 * ID of the Job associated with this message.
881 * @param int $event_queue_id
882 * ID of the EventQueue.
883 * @param string $hash
884 * Hash of the EventQueue.
885 * @param string $email
886 * Destination address.
887 *
888 * @return array
889 * (reference) array array ref that hold array refs to the verp info and urls
890 */
891 public static function getVerpAndUrls($job_id, $event_queue_id, $hash, $email) {
892 // create a skeleton object and set its properties that are required by getVerpAndUrlsAndHeaders()
893 $config = CRM_Core_Config::singleton();
894 $bao = new CRM_Mailing_BAO_Mailing();
895 $bao->_domain = CRM_Core_BAO_Domain::getDomain();
896 $bao->from_name = $bao->from_email = $bao->subject = '';
897
898 // use $bao's instance method to get verp and urls
899 list($verp, $urls, $_) = $bao->getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email);
900 return [$verp, $urls];
901 }
902
903 /**
904 * Get verp, urls and headers
905 *
906 * @param int $job_id
907 * ID of the Job associated with this message.
908 * @param int $event_queue_id
909 * ID of the EventQueue.
910 * @param string $hash
911 * Hash of the EventQueue.
912 * @param string $email
913 * Destination address.
914 *
915 * @param bool $isForward
916 *
917 * @return array
918 * array ref that hold array refs to the verp info, urls, and headers
919 */
920 public function getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email, $isForward = FALSE) {
921 $config = CRM_Core_Config::singleton();
922
923 /**
924 * Inbound VERP keys:
925 * reply: user replied to mailing
926 * bounce: email address bounced
927 * unsubscribe: contact opts out of all target lists for the mailing
928 * resubscribe: contact opts back into all target lists for the mailing
929 * optOut: contact unsubscribes from the domain
930 */
931 $verp = [];
932 $verpTokens = [
933 'reply' => 'r',
934 'bounce' => 'b',
935 'unsubscribe' => 'u',
936 'resubscribe' => 'e',
937 'optOut' => 'o',
938 ];
939
940 $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart();
941 $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
942 // Make sure the user configured the site correctly, otherwise you just get "Could not identify any recipients. Perhaps the group is empty?" from the mailing UI
943 if (empty($emailDomain)) {
944 CRM_Core_Error::debug_log_message('Error setting verp parameters, defaultDomain is NULL. Did you configure the bounce processing account for this domain?');
945 }
946
947 foreach ($verpTokens as $key => $value) {
948 $verp[$key] = implode($config->verpSeparator,
949 [
950 $localpart . $value,
951 $job_id,
952 $event_queue_id,
953 $hash,
954 ]
955 ) . "@$emailDomain";
956 }
957
958 //handle should override VERP address.
959 $skipEncode = FALSE;
960
961 if ($job_id &&
962 self::overrideVerp($job_id)
963 ) {
964 $verp['reply'] = "\"{$this->from_name}\" <{$this->from_email}>";
965 }
966
967 $urls = [
968 'forward' => CRM_Utils_System::url('civicrm/mailing/forward', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
969 'unsubscribeUrl' => CRM_Utils_System::url('civicrm/mailing/unsubscribe', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
970 'resubscribeUrl' => CRM_Utils_System::url('civicrm/mailing/resubscribe', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
971 'optOutUrl' => CRM_Utils_System::url('civicrm/mailing/optout', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", TRUE, NULL, TRUE, TRUE),
972 'subscribeUrl' => CRM_Utils_System::url('civicrm/mailing/subscribe', 'reset=1', TRUE, NULL, TRUE, TRUE),
973 ];
974
975 $headers = [
976 'Reply-To' => $verp['reply'],
977 'Return-Path' => $verp['bounce'],
978 'From' => "\"{$this->from_name}\" <{$this->from_email}>",
979 'Subject' => $this->subject,
980 'List-Unsubscribe' => "<mailto:{$verp['unsubscribe']}>",
981 ];
982 self::addMessageIdHeader($headers, 'm', $job_id, $event_queue_id, $hash);
983 if ($isForward) {
984 $headers['Subject'] = "[Fwd:{$this->subject}]";
985 }
986 return [&$verp, &$urls, &$headers];
987 }
988
989 /**
990 * Compose a message.
991 *
992 * @deprecated
993 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
994 * @param int $job_id
995 * ID of the Job associated with this message.
996 * @param int $event_queue_id
997 * ID of the EventQueue.
998 * @param string $hash
999 * Hash of the EventQueue.
1000 * @param string $contactId
1001 * ID of the Contact.
1002 * @param string $email
1003 * Destination address.
1004 * @param string $recipient
1005 * To: of the recipient.
1006 * @param bool $test
1007 * Is this mailing a test?.
1008 * @param $contactDetails
1009 * @param $attachments
1010 * @param bool $isForward
1011 * Is this mailing compose for forward?.
1012 * @param string $fromEmail
1013 * Email address of who is forwardinf it.
1014 *
1015 * @param null $replyToEmail
1016 *
1017 * @return Mail_mime The mail object
1018 */
1019 public function compose(
1020 $job_id, $event_queue_id, $hash, $contactId,
1021 $email, &$recipient, $test,
1022 $contactDetails, &$attachments, $isForward = FALSE,
1023 $fromEmail = NULL, $replyToEmail = NULL
1024 ) {
1025 $config = CRM_Core_Config::singleton();
1026 $this->getTokens();
1027
1028 if ($this->_domain == NULL) {
1029 $this->_domain = CRM_Core_BAO_Domain::getDomain();
1030 }
1031
1032 list($verp, $urls, $headers) = $this->getVerpAndUrlsAndHeaders(
1033 $job_id,
1034 $event_queue_id,
1035 $hash,
1036 $email,
1037 $isForward
1038 );
1039
1040 //set from email who is forwarding it and not original one.
1041 if ($fromEmail) {
1042 unset($headers['From']);
1043 $headers['From'] = "<{$fromEmail}>";
1044 }
1045
1046 if ($replyToEmail && ($fromEmail != $replyToEmail)) {
1047 $headers['Reply-To'] = "{$replyToEmail}";
1048 }
1049
1050 if ($contactDetails) {
1051 $contact = $contactDetails;
1052 }
1053 elseif ($contactId === 0) {
1054 //anonymous user
1055 $contact = [];
1056 CRM_Utils_Hook::tokenValues($contact, [$contactId], $job_id);
1057 }
1058 else {
1059 $params = [['contact_id', '=', $contactId, 0, 0]];
1060 list($contact) = CRM_Contact_BAO_Query::apiQuery($params);
1061 // $contact is an array of [ contactID => contactDetails ]
1062
1063 // also call the hook to get contact details
1064 CRM_Utils_Hook::tokenValues($contact, [$contactId], $job_id);
1065
1066 // Don't send if contact doesn't exist
1067 $contact = reset($contact);
1068 if (!$contact || is_a($contact, 'CRM_Core_Error')) {
1069 CRM_Core_Error::debug_log_message(ts('CiviMail will not send email to a non-existent contact: %1',
1070 [1 => $contactId]
1071 ));
1072 // setting this because function is called by reference
1073 //@todo test not calling function by reference
1074 $res = NULL;
1075 return $res;
1076 }
1077 }
1078
1079 $pTemplates = $this->getPreparedTemplates();
1080 $pEmails = [];
1081
1082 foreach ($pTemplates as $type => $pTemplate) {
1083 $html = $type == 'html';
1084 $pEmails[$type] = [];
1085 $pEmail = &$pEmails[$type];
1086 $template = &$pTemplates[$type]['template'];
1087 $tokens = &$pTemplates[$type]['tokens'];
1088 $idx = 0;
1089 if (!empty($tokens)) {
1090 foreach ($tokens as $idx => $token) {
1091 $token_data = $this->getTokenData($token, $html, $contact, $verp, $urls, $event_queue_id);
1092 array_push($pEmail, $template[$idx]);
1093 array_push($pEmail, $token_data);
1094 }
1095 }
1096 else {
1097 array_push($pEmail, $template[$idx]);
1098 }
1099
1100 if (isset($template[($idx + 1)])) {
1101 array_push($pEmail, $template[($idx + 1)]);
1102 }
1103 }
1104
1105 $html = NULL;
1106 if (isset($pEmails['html']) && is_array($pEmails['html']) && count($pEmails['html'])) {
1107 $html = &$pEmails['html'];
1108 }
1109
1110 $text = NULL;
1111 if (isset($pEmails['text']) && is_array($pEmails['text']) && count($pEmails['text'])) {
1112 $text = &$pEmails['text'];
1113 }
1114
1115 // push the tracking url on to the html email if necessary
1116 if ($this->open_tracking && $html) {
1117 array_push($html, "\n" . '<img src="' . CRM_Utils_System::externUrl('extern/open', "q=$event_queue_id")
1118 . '" width="1" height="1" alt="" border="0">'
1119 );
1120 }
1121
1122 $message = new Mail_mime("\n");
1123
1124 $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY;
1125 if ($useSmarty) {
1126 $smarty = CRM_Core_Smarty::singleton();
1127 // also add the contact tokens to the template
1128 $smarty->assign_by_ref('contact', $contact);
1129 }
1130
1131 $mailParams = $headers;
1132 if ($text && ($test || $contact['preferred_mail_format'] == 'Text' ||
1133 $contact['preferred_mail_format'] == 'Both' ||
1134 ($contact['preferred_mail_format'] == 'HTML' && !array_key_exists('html', $pEmails))
1135 )
1136 ) {
1137 $textBody = implode('', $text);
1138 if ($useSmarty) {
1139 $textBody = $smarty->fetch("string:$textBody");
1140 }
1141 $mailParams['text'] = $textBody;
1142 }
1143
1144 if ($html && ($test || ($contact['preferred_mail_format'] == 'HTML' ||
1145 $contact['preferred_mail_format'] == 'Both'
1146 ))
1147 ) {
1148 $htmlBody = implode('', $html);
1149 if ($useSmarty) {
1150 $htmlBody = $smarty->fetch("string:$htmlBody");
1151 }
1152 $mailParams['html'] = $htmlBody;
1153 }
1154
1155 if (empty($mailParams['text']) && empty($mailParams['html'])) {
1156 // CRM-9833
1157 // something went wrong, lets log it and return null (by reference)
1158 CRM_Core_Error::debug_log_message(ts('CiviMail will not send an empty mail body, Skipping: %1',
1159 [1 => $email]
1160 ));
1161 $res = NULL;
1162 return $res;
1163 }
1164
1165 $mailParams['attachments'] = $attachments;
1166
1167 $mailParams['Subject'] = $pEmails['subject'] ?? NULL;
1168 if (is_array($mailParams['Subject'])) {
1169 $mailParams['Subject'] = implode('', $mailParams['Subject']);
1170 }
1171
1172 $mailParams['toName'] = CRM_Utils_Array::value('display_name',
1173 $contact
1174 );
1175 $mailParams['toEmail'] = $email;
1176
1177 // Add job ID to mailParams for external email delivery service to utilise
1178 $mailParams['job_id'] = $job_id;
1179
1180 CRM_Utils_Hook::alterMailParams($mailParams, 'civimail');
1181
1182 // CRM-10699 support custom email headers
1183 if (!empty($mailParams['headers'])) {
1184 $headers = array_merge($headers, $mailParams['headers']);
1185 }
1186 //cycle through mailParams and set headers array
1187 foreach ($mailParams as $paramKey => $paramValue) {
1188 //exclude values not intended for the header
1189 if (!in_array($paramKey, [
1190 'text',
1191 'html',
1192 'attachments',
1193 'toName',
1194 'toEmail',
1195 ])
1196 ) {
1197 $headers[$paramKey] = $paramValue;
1198 }
1199 }
1200
1201 if (!empty($mailParams['text'])) {
1202 $message->setTxtBody($mailParams['text']);
1203 }
1204
1205 if (!empty($mailParams['html'])) {
1206 $message->setHTMLBody($mailParams['html']);
1207 }
1208
1209 if (!empty($mailParams['attachments'])) {
1210 foreach ($mailParams['attachments'] as $fileID => $attach) {
1211 $message->addAttachment($attach['fullPath'],
1212 $attach['mime_type'],
1213 $attach['cleanName']
1214 );
1215 }
1216 }
1217
1218 //pickup both params from mail params.
1219 $toName = trim($mailParams['toName']);
1220 $toEmail = trim($mailParams['toEmail']);
1221 if ($toName == $toEmail ||
1222 strpos($toName, '@') !== FALSE
1223 ) {
1224 $toName = NULL;
1225 }
1226 else {
1227 $toName = CRM_Utils_Mail::formatRFC2822Name($toName);
1228 }
1229
1230 $headers['To'] = "$toName <$toEmail>";
1231
1232 $headers['Precedence'] = 'bulk';
1233 // Will test in the mail processor if the X-VERP is set in the bounced email.
1234 // (As an option to replace real VERP for those that can't set it up)
1235 $headers['X-CiviMail-Bounce'] = $verp['bounce'];
1236
1237 //CRM-5058
1238 //token replacement of subject
1239 $headers['Subject'] = $mailParams['Subject'];
1240
1241 CRM_Utils_Mail::setMimeParams($message);
1242 $headers = $message->headers($headers);
1243
1244 //get formatted recipient
1245 $recipient = $headers['To'];
1246
1247 // make sure we unset a lot of stuff
1248 unset($verp);
1249 unset($urls);
1250 unset($params);
1251 unset($contact);
1252 unset($ids);
1253
1254 return $message;
1255 }
1256
1257 /**
1258 * Replace tokens.
1259 *
1260 * Get mailing object and replaces subscribeInvite, domain and mailing tokens.
1261 *
1262 * @deprecated
1263 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
1264 * @param CRM_Mailing_BAO_Mailing $mailing
1265 */
1266 public static function tokenReplace(&$mailing) {
1267 $domain = CRM_Core_BAO_Domain::getDomain();
1268
1269 foreach (['text', 'html'] as $type) {
1270 $tokens = $mailing->getTokens();
1271 if (isset($mailing->templates[$type])) {
1272 $mailing->templates[$type] = CRM_Utils_Token::replaceSubscribeInviteTokens($mailing->templates[$type]);
1273 $mailing->templates[$type] = CRM_Utils_Token::replaceDomainTokens(
1274 $mailing->templates[$type],
1275 $domain,
1276 $type == 'html',
1277 $tokens[$type]
1278 );
1279 $mailing->templates[$type] = CRM_Utils_Token::replaceMailingTokens($mailing->templates[$type], $mailing, NULL, $tokens[$type]);
1280 }
1281 }
1282 }
1283
1284 /**
1285 * Get data to resolve tokens.
1286 *
1287 * @deprecated
1288 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
1289 *
1290 * @param array $token_a
1291 * @param bool $html
1292 * Whether to encode the token result for use in HTML email
1293 * @param array $contact
1294 * @param string $verp
1295 * @param array $urls
1296 * @param int $event_queue_id
1297 *
1298 * @return bool|mixed|null|string
1299 */
1300 private function getTokenData(&$token_a, $html, &$contact, &$verp, &$urls, $event_queue_id) {
1301 $type = $token_a['type'];
1302 $token = $token_a['token'];
1303 $data = $token;
1304
1305 $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY;
1306
1307 if ($type == 'embedded_url') {
1308 $embed_data = [];
1309 foreach ($token as $t) {
1310 $embed_data[] = $this->getTokenData($t, $html, $contact, $verp, $urls, $event_queue_id);
1311 }
1312 $numSlices = count($embed_data);
1313 $url = '';
1314 for ($i = 0; $i < $numSlices; $i++) {
1315 $embed_url_data = parse_url($embed_data[$i]);
1316 if (!empty($embed_url_data['scheme'])) {
1317 $token_a['embed_parts'][$i] = preg_replace("/href=\"(https*:\/\/)/", "href=\"", $token_a['embed_parts'][$i]);
1318 }
1319 $url .= "{$token_a['embed_parts'][$i]}{$embed_data[$i]}";
1320 }
1321 if (isset($token_a['embed_parts'][$numSlices])) {
1322 $url .= $token_a['embed_parts'][$numSlices];
1323 }
1324 // add trailing quote since we've gobbled it up in a previous regex
1325 // function getPatterns, line 431
1326 if (preg_match("/^href[ ]*=[ ]*'.*[^']$/", $url)) {
1327 $url .= "'";
1328 }
1329 elseif (preg_match('/^href[ ]*=[ ]*".*[^"]$/', $url)) {
1330 $url .= '"';
1331 }
1332 $data = $url;
1333 // CRM-20206 Fix ampersand encoding in plain text emails
1334 if (empty($html)) {
1335 $data = CRM_Utils_String::unstupifyUrl($data);
1336 }
1337 }
1338 elseif ($type == 'url') {
1339 if ($this->url_tracking && !empty($this->id)) {
1340 // ensure that Google CSS and any .css files are not tracked.
1341 if (!(strpos($token, 'css?family') || strpos($token, '.css'))) {
1342 $data = CRM_Mailing_BAO_TrackableURL::getTrackerURL($token, $this->id, $event_queue_id);
1343 if (!empty($html)) {
1344 $data = htmlentities($data, ENT_NOQUOTES);
1345 }
1346 }
1347 }
1348 else {
1349 $data = $token;
1350 }
1351 }
1352 elseif ($type == 'contact') {
1353 $data = CRM_Utils_Token::getContactTokenReplacement($token, $contact, FALSE, FALSE, $useSmarty);
1354 }
1355 elseif ($type == 'action') {
1356 $data = CRM_Utils_Token::getActionTokenReplacement($token, $verp, $urls, $html);
1357 }
1358 elseif ($type == 'domain') {
1359 $domain = CRM_Core_BAO_Domain::getDomain();
1360 $data = CRM_Utils_Token::getDomainTokenReplacement($token, $domain, $html);
1361 }
1362 elseif ($type == 'mailing') {
1363 if ($token == 'name') {
1364 $data = $this->name;
1365 }
1366 elseif ($token == 'group') {
1367 $groups = $this->getGroupNames();
1368 $data = implode(', ', $groups);
1369 }
1370 }
1371 else {
1372 $data = $contact["{$type}.{$token}"] ?? NULL;
1373 }
1374 return $data;
1375 }
1376
1377 /**
1378 * Return a list of group names for this mailing. Does not work with
1379 * prior-mailing targets.
1380 *
1381 * @return array
1382 * Names of groups receiving this mailing
1383 */
1384 public function &getGroupNames() {
1385 if (!isset($this->id)) {
1386 return [];
1387 }
1388 $mg = new CRM_Mailing_DAO_MailingGroup();
1389 $mgtable = CRM_Mailing_DAO_MailingGroup::getTableName();
1390 $group = CRM_Contact_BAO_Group::getTableName();
1391
1392 $mg->query("SELECT $group.title as name FROM $mgtable
1393 INNER JOIN $group ON $mgtable.entity_id = $group.id
1394 WHERE $mgtable.mailing_id = {$this->id}
1395 AND $mgtable.entity_table = '$group'
1396 AND $mgtable.group_type = 'Include'
1397 ORDER BY $group.name");
1398
1399 $groups = [];
1400 while ($mg->fetch()) {
1401 $groups[] = $mg->name;
1402 }
1403 return $groups;
1404 }
1405
1406 /**
1407 * Add the mailings.
1408 *
1409 * @param array $params
1410 * Reference array contains the values submitted by the form.
1411 * @param array $ids
1412 * Reference array contains the id.
1413 *
1414 *
1415 * @return CRM_Mailing_DAO_Mailing
1416 */
1417 public static function add(&$params, $ids = []) {
1418 $id = $params['id'] ?? $ids['mailing_id'] ?? NULL;
1419
1420 if (empty($params['id']) && !empty($ids)) {
1421 CRM_Core_Error::deprecatedWarning('Parameter $ids is no longer used by Mailing::add. Use the api or just pass $params');
1422 }
1423
1424 if ($id) {
1425 CRM_Utils_Hook::pre('edit', 'Mailing', $id, $params);
1426 }
1427 else {
1428 CRM_Utils_Hook::pre('create', 'Mailing', NULL, $params);
1429 }
1430
1431 $mailing = new static();
1432 if ($id) {
1433 $mailing->id = $id;
1434 $mailing->find(TRUE);
1435 }
1436 $mailing->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
1437
1438 if (((!$id && empty($params['replyto_email'])) || !isset($params['replyto_email'])) &&
1439 isset($params['from_email'])
1440 ) {
1441 $params['replyto_email'] = $params['from_email'];
1442 }
1443 $mailing->copyValues($params);
1444
1445 // CRM-20892 Unset Modifed Date here so that MySQL can correctly set an updated modfied date.
1446 unset($mailing->modified_date);
1447 $result = $mailing->save();
1448
1449 // CRM-20892 Re find record after saing so we can set the updated modified date in the result.
1450 $mailing->find(TRUE);
1451
1452 if (isset($mailing->modified_date)) {
1453 $result->modified_date = $mailing->modified_date;
1454 }
1455
1456 if ($id) {
1457 CRM_Utils_Hook::post('edit', 'Mailing', $mailing->id, $mailing);
1458 }
1459 else {
1460 CRM_Utils_Hook::post('create', 'Mailing', $mailing->id, $mailing);
1461 }
1462
1463 return $result;
1464 }
1465
1466 /**
1467 * Construct a new mailing object, along with job and mailing_group
1468 * objects, from the form values of the create mailing wizard.
1469 *
1470 * This function is a bit evil. It not only merges $params and saves
1471 * the mailing -- it also schedules the mailing and chooses the recipients.
1472 * Since it merges $params, it's also the only place to correctly trigger
1473 * multi-field validation. It should be broken up.
1474 *
1475 * In the mean time, use-cases which break under the weight of this
1476 * evil may find reprieve in these extra evil params:
1477 *
1478 * - _skip_evil_bao_auto_recipients_: bool
1479 * - _skip_evil_bao_auto_schedule_: bool
1480 * - _evil_bao_validator_: string|callable
1481 *
1482 * </twowrongsmakesaright>
1483 *
1484 * @params array $params
1485 * Form values.
1486 *
1487 * @param array $params
1488 *
1489 * @return object
1490 * $mailing The new mailing object
1491 *
1492 * @throws \CRM_Core_Exception
1493 * @throws \CiviCRM_API3_Exception
1494 */
1495 public static function create(&$params) {
1496
1497 // CRM-#1843
1498 // If it is a mass sms, set url_tracking to false
1499 if (!empty($params['sms_provider_id'])) {
1500 $params['url_tracking'] = 0;
1501 }
1502
1503 // CRM-12430
1504 // Do the below only for an insert
1505 // for an update, we should not set the defaults
1506 if (!isset($params['id'])) {
1507 // Retrieve domain email and name for default sender
1508 $domain = civicrm_api(
1509 'Domain',
1510 'getsingle',
1511 [
1512 'version' => 3,
1513 'current_domain' => 1,
1514 'sequential' => 1,
1515 ]
1516 );
1517 if (isset($domain['from_email'])) {
1518 $domain_email = $domain['from_email'];
1519 $domain_name = $domain['from_name'];
1520 }
1521 else {
1522 $domain_email = 'info@EXAMPLE.ORG';
1523 $domain_name = 'EXAMPLE.ORG';
1524 }
1525 if (!isset($params['created_id'])) {
1526 $params['created_id'] = CRM_Core_Session::getLoggedInContactID();
1527 }
1528 $defaults = [
1529 // load the default config settings for each
1530 // eg reply_id, unsubscribe_id need to use
1531 // correct template IDs here
1532 'override_verp' => TRUE,
1533 'forward_replies' => FALSE,
1534 'open_tracking' => Civi::settings()->get('open_tracking_default'),
1535 'url_tracking' => Civi::settings()->get('url_tracking_default'),
1536 'visibility' => 'Public Pages',
1537 'replyto_email' => $domain_email,
1538 'header_id' => CRM_Mailing_PseudoConstant::defaultComponent('header_id', ''),
1539 'footer_id' => CRM_Mailing_PseudoConstant::defaultComponent('footer_id', ''),
1540 'from_email' => $domain_email,
1541 'from_name' => $domain_name,
1542 'msg_template_id' => NULL,
1543 'created_id' => $params['created_id'],
1544 'approver_id' => NULL,
1545 'auto_responder' => 0,
1546 'created_date' => date('YmdHis'),
1547 'scheduled_date' => NULL,
1548 'approval_date' => NULL,
1549 ];
1550
1551 // Get the default from email address, if not provided.
1552 if (empty($defaults['from_email'])) {
1553 $defaultAddress = CRM_Core_BAO_Domain::getNameAndEmail(TRUE, TRUE);
1554 foreach ($defaultAddress as $id => $value) {
1555 if (preg_match('/"(.*)" <(.*)>/', $value, $match)) {
1556 $defaults['from_email'] = $match[2];
1557 $defaults['from_name'] = $match[1];
1558 }
1559 }
1560 }
1561
1562 $params = array_merge($defaults, $params);
1563 }
1564
1565 /**
1566 * Could check and warn for the following cases:
1567 *
1568 * - groups OR mailings should be populated.
1569 * - body html OR body text should be populated.
1570 */
1571
1572 $transaction = new CRM_Core_Transaction();
1573
1574 $mailing = self::add($params);
1575
1576 if (is_a($mailing, 'CRM_Core_Error')) {
1577 $transaction->rollback();
1578 return $mailing;
1579 }
1580 // update mailings with hash values
1581 CRM_Contact_BAO_Contact_Utils::generateChecksum($mailing->id, NULL, NULL, NULL, 'mailing', 16);
1582
1583 $groupTableName = CRM_Contact_BAO_Group::getTableName();
1584
1585 /* Create the mailing group record */
1586 $mg = new CRM_Mailing_DAO_MailingGroup();
1587 $groupTypes = [
1588 'include' => 'Include',
1589 'exclude' => 'Exclude',
1590 'base' => 'Base',
1591 ];
1592 foreach (['groups', 'mailings'] as $entity) {
1593 foreach (['include', 'exclude', 'base'] as $type) {
1594 if (isset($params[$entity][$type])) {
1595 self::replaceGroups($mailing->id, $groupTypes[$type], $entity, $params[$entity][$type]);
1596 }
1597 }
1598 }
1599
1600 if (!empty($params['search_id']) && !empty($params['group_id'])) {
1601 $mg->reset();
1602 $mg->mailing_id = $mailing->id;
1603 $mg->entity_table = $groupTableName;
1604 $mg->entity_id = $params['group_id'];
1605 $mg->search_id = $params['search_id'];
1606 $mg->search_args = $params['search_args'];
1607 $mg->group_type = 'Include';
1608 $mg->save();
1609 }
1610
1611 // check and attach and files as needed
1612 CRM_Core_BAO_File::processAttachment($params, 'civicrm_mailing', $mailing->id);
1613
1614 // If we're going to autosend, then check validity before saving.
1615 if (empty($params['is_completed']) && !empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && !empty($params['_evil_bao_validator_'])) {
1616 $cb = Civi\Core\Resolver::singleton()
1617 ->get($params['_evil_bao_validator_']);
1618 $errors = call_user_func($cb, $mailing);
1619 if (!empty($errors)) {
1620 $fields = implode(',', array_keys($errors));
1621 throw new CRM_Core_Exception("Mailing cannot be sent. There are missing or invalid fields ($fields).", 'cannot-send', $errors);
1622 }
1623 }
1624
1625 $transaction->commit();
1626
1627 // Create parent job if not yet created.
1628 // Condition on the existence of a scheduled date.
1629 if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && empty($params['_skip_evil_bao_auto_schedule_'])) {
1630 $job = new CRM_Mailing_BAO_MailingJob();
1631 $job->mailing_id = $mailing->id;
1632 // If we are creating a new Completed mailing (e.g. import from another system) set the job to completed.
1633 // Keeping former behaviour when an id is present is precautionary and may warrant reconsideration later.
1634 $job->status = ((empty($params['is_completed']) || !empty($params['id'])) ? 'Scheduled' : 'Complete');
1635 $job->is_test = 0;
1636
1637 if (!$job->find(TRUE)) {
1638 // Don't schedule job until we populate the recipients.
1639 $job->scheduled_date = NULL;
1640 $job->save();
1641 }
1642 // Schedule the job now that it has recipients.
1643 $job->scheduled_date = $params['scheduled_date'];
1644 $job->save();
1645 }
1646
1647 // Populate the recipients.
1648 if (empty($params['_skip_evil_bao_auto_recipients_'])) {
1649 self::getRecipients($mailing->id);
1650 }
1651
1652 return $mailing;
1653 }
1654
1655 /**
1656 * @deprecated
1657 * This is used by CiviMail but will be made redundant by FlexMailer.
1658 * @param CRM_Mailing_DAO_Mailing $mailing
1659 * The mailing which may or may not be sendable.
1660 * @return array
1661 * List of error messages.
1662 */
1663 public static function checkSendable($mailing) {
1664 $errors = [];
1665 foreach (['subject', 'name', 'from_name', 'from_email'] as $field) {
1666 if (empty($mailing->{$field})) {
1667 $errors[$field] = ts('Field "%1" is required.', [
1668 1 => $field,
1669 ]);
1670 }
1671 }
1672 if (empty($mailing->body_html) && empty($mailing->body_text)) {
1673 $errors['body'] = ts('Field "body_html" or "body_text" is required.');
1674 }
1675
1676 if (!Civi::settings()->get('disable_mandatory_tokens_check')) {
1677 $header = $mailing->header_id && $mailing->header_id != 'null' ? CRM_Mailing_BAO_MailingComponent::findById($mailing->header_id) : NULL;
1678 $footer = $mailing->footer_id && $mailing->footer_id != 'null' ? CRM_Mailing_BAO_MailingComponent::findById($mailing->footer_id) : NULL;
1679 foreach (['body_html', 'body_text'] as $field) {
1680 if (empty($mailing->{$field})) {
1681 continue;
1682 }
1683 $str = ($header ? $header->{$field} : '') . $mailing->{$field} . ($footer ? $footer->{$field} : '');
1684 $err = CRM_Utils_Token::requiredTokens($str);
1685 if ($err !== TRUE) {
1686 foreach ($err as $token => $desc) {
1687 $errors["{$field}:{$token}"] = ts('This message is missing a required token - {%1}: %2',
1688 [1 => $token, 2 => $desc]
1689 );
1690 }
1691 }
1692 }
1693 }
1694
1695 return $errors;
1696 }
1697
1698 /**
1699 * Replace the list of recipients on a given mailing.
1700 *
1701 * @param int $mailingId
1702 * @param string $type
1703 * 'include' or 'exclude'.
1704 * @param string $entity
1705 * 'groups' or 'mailings'.
1706 * @param array $entityIds
1707 * @throws CiviCRM_API3_Exception
1708 */
1709 public static function replaceGroups($mailingId, $type, $entity, $entityIds) {
1710 $values = [];
1711 foreach ($entityIds as $entityId) {
1712 $values[] = ['entity_id' => $entityId];
1713 }
1714 civicrm_api3('mailing_group', 'replace', [
1715 'mailing_id' => $mailingId,
1716 'group_type' => $type,
1717 'entity_table' => ($entity == 'groups') ? CRM_Contact_BAO_Group::getTableName() : CRM_Mailing_BAO_Mailing::getTableName(),
1718 'values' => $values,
1719 ]);
1720 }
1721
1722 /**
1723 * Get hash value of the mailing.
1724 *
1725 * @param $id
1726 *
1727 * @return null|string
1728 */
1729 public static function getMailingHash($id) {
1730 $hash = NULL;
1731 if (Civi::settings()->get('hash_mailing_url') && !empty($id)) {
1732 $hash = CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $id, 'hash', 'id');
1733 }
1734 return $hash;
1735 }
1736
1737 /**
1738 * Generate a report. Fetch event count information, mailing data, and job
1739 * status.
1740 *
1741 * @param int $id
1742 * The mailing id to report.
1743 * @param bool $skipDetails
1744 * Whether return all detailed report.
1745 *
1746 * @param bool $isSMS
1747 *
1748 * @return array
1749 * Associative array of reporting data
1750 */
1751 public static function &report($id, $skipDetails = FALSE, $isSMS = FALSE) {
1752 $mailing_id = CRM_Utils_Type::escape($id, 'Integer');
1753
1754 $mailing = new CRM_Mailing_BAO_Mailing();
1755
1756 $t = [
1757 'mailing' => self::getTableName(),
1758 'mailing_group' => CRM_Mailing_DAO_MailingGroup::getTableName(),
1759 'group' => CRM_Contact_BAO_Group::getTableName(),
1760 'job' => CRM_Mailing_BAO_MailingJob::getTableName(),
1761 'queue' => CRM_Mailing_Event_BAO_Queue::getTableName(),
1762 'delivered' => CRM_Mailing_Event_BAO_Delivered::getTableName(),
1763 'opened' => CRM_Mailing_Event_BAO_Opened::getTableName(),
1764 'reply' => CRM_Mailing_Event_BAO_Reply::getTableName(),
1765 'unsubscribe' => CRM_Mailing_Event_BAO_Unsubscribe::getTableName(),
1766 'bounce' => CRM_Mailing_Event_BAO_Bounce::getTableName(),
1767 'forward' => CRM_Mailing_Event_BAO_Forward::getTableName(),
1768 'url' => CRM_Mailing_BAO_TrackableURL::getTableName(),
1769 'urlopen' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTableName(),
1770 'component' => CRM_Mailing_BAO_MailingComponent::getTableName(),
1771 'spool' => CRM_Mailing_BAO_Spool::getTableName(),
1772 ];
1773
1774 $report = [];
1775 $additionalWhereClause = " AND ";
1776 if (!$isSMS) {
1777 $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NULL ";
1778 }
1779 else {
1780 $additionalWhereClause .= " {$t['mailing']}.sms_provider_id IS NOT NULL ";
1781 }
1782
1783 /* Get the mailing info */
1784
1785 $mailing->query("
1786 SELECT {$t['mailing']}.*
1787 FROM {$t['mailing']}
1788 WHERE {$t['mailing']}.id = $mailing_id {$additionalWhereClause}");
1789
1790 $mailing->fetch();
1791
1792 $report['mailing'] = [];
1793 foreach (array_keys(self::fields()) as $field) {
1794 $field = self::fields()[$field]['name'];
1795 $report['mailing'][$field] = $mailing->$field;
1796 }
1797
1798 //get the campaign
1799 if ($campaignId = CRM_Utils_Array::value('campaign_id', $report['mailing'])) {
1800 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId);
1801 $report['mailing']['campaign'] = $campaigns[$campaignId];
1802 }
1803
1804 //mailing report is called by activity
1805 //we dont need all detail report
1806 if ($skipDetails) {
1807 return $report;
1808 }
1809
1810 /* Get the component info */
1811
1812 $query = [];
1813
1814 $components = [
1815 'header' => ts('Header'),
1816 'footer' => ts('Footer'),
1817 'reply' => ts('Reply'),
1818 'optout' => ts('Opt-Out'),
1819 'resubscribe' => ts('Resubscribe'),
1820 'unsubscribe' => ts('Unsubscribe'),
1821 ];
1822 foreach (array_keys($components) as $type) {
1823 $query[] = "SELECT {$t['component']}.name as name,
1824 '$type' as type,
1825 {$t['component']}.id as id
1826 FROM {$t['component']}
1827 INNER JOIN {$t['mailing']}
1828 ON {$t['mailing']}.{$type}_id =
1829 {$t['component']}.id
1830 WHERE {$t['mailing']}.id = $mailing_id";
1831 }
1832 $q = '(' . implode(') UNION (', $query) . ')';
1833 $mailing->query($q);
1834
1835 $report['component'] = [];
1836 while ($mailing->fetch()) {
1837 $report['component'][] = [
1838 'type' => $components[$mailing->type],
1839 'name' => $mailing->name,
1840 'link' => CRM_Utils_System::url('civicrm/mailing/component', "reset=1&action=update&id={$mailing->id}"),
1841 ];
1842 }
1843
1844 /* Get the recipient group info */
1845
1846 $mailing->query("
1847 SELECT {$t['mailing_group']}.group_type as group_type,
1848 {$t['group']}.id as group_id,
1849 {$t['group']}.title as group_title,
1850 {$t['group']}.is_hidden as group_hidden,
1851 {$t['mailing']}.id as mailing_id,
1852 {$t['mailing']}.name as mailing_name
1853 FROM {$t['mailing_group']}
1854 LEFT JOIN {$t['group']}
1855 ON {$t['mailing_group']}.entity_id = {$t['group']}.id
1856 AND {$t['mailing_group']}.entity_table =
1857 '{$t['group']}'
1858 LEFT JOIN {$t['mailing']}
1859 ON {$t['mailing_group']}.entity_id =
1860 {$t['mailing']}.id
1861 AND {$t['mailing_group']}.entity_table =
1862 '{$t['mailing']}'
1863
1864 WHERE {$t['mailing_group']}.mailing_id = $mailing_id
1865 ");
1866
1867 $report['group'] = ['include' => [], 'exclude' => [], 'base' => []];
1868 while ($mailing->fetch()) {
1869 $row = [];
1870 if (isset($mailing->group_id)) {
1871 $row['id'] = $mailing->group_id;
1872 $row['name'] = $mailing->group_title;
1873 $row['link'] = CRM_Utils_System::url('civicrm/group/search',
1874 "reset=1&force=1&context=smog&gid={$row['id']}"
1875 );
1876 }
1877 else {
1878 $row['id'] = $mailing->mailing_id;
1879 $row['name'] = $mailing->mailing_name;
1880 $row['mailing'] = TRUE;
1881 $row['link'] = CRM_Utils_System::url('civicrm/mailing/report',
1882 "mid={$row['id']}"
1883 );
1884 }
1885
1886 /* Rename hidden groups */
1887
1888 if ($mailing->group_hidden == 1) {
1889 $row['name'] = "Search Results";
1890 }
1891
1892 if ($mailing->group_type == 'Include') {
1893 $report['group']['include'][] = $row;
1894 }
1895 elseif ($mailing->group_type == 'Base') {
1896 $report['group']['base'][] = $row;
1897 }
1898 else {
1899 $report['group']['exclude'][] = $row;
1900 }
1901 }
1902
1903 /* Get the event totals, grouped by job (retries) */
1904
1905 $mailing->query("
1906 SELECT {$t['job']}.*,
1907 COUNT(DISTINCT {$t['queue']}.id) as queue,
1908 COUNT(DISTINCT {$t['delivered']}.id) as delivered,
1909 COUNT(DISTINCT {$t['reply']}.id) as reply,
1910 COUNT(DISTINCT {$t['forward']}.id) as forward,
1911 COUNT(DISTINCT {$t['bounce']}.id) as bounce,
1912 COUNT(DISTINCT {$t['urlopen']}.id) as url,
1913 COUNT(DISTINCT {$t['spool']}.id) as spool
1914 FROM {$t['job']}
1915 LEFT JOIN {$t['queue']}
1916 ON {$t['queue']}.job_id = {$t['job']}.id
1917 LEFT JOIN {$t['reply']}
1918 ON {$t['reply']}.event_queue_id = {$t['queue']}.id
1919 LEFT JOIN {$t['forward']}
1920 ON {$t['forward']}.event_queue_id = {$t['queue']}.id
1921 LEFT JOIN {$t['bounce']}
1922 ON {$t['bounce']}.event_queue_id = {$t['queue']}.id
1923 LEFT JOIN {$t['delivered']}
1924 ON {$t['delivered']}.event_queue_id = {$t['queue']}.id
1925 AND {$t['bounce']}.id IS null
1926 LEFT JOIN {$t['urlopen']}
1927 ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id
1928 LEFT JOIN {$t['spool']}
1929 ON {$t['spool']}.job_id = {$t['job']}.id
1930 WHERE {$t['job']}.mailing_id = $mailing_id
1931 AND {$t['job']}.is_test = 0
1932 GROUP BY {$t['job']}.id");
1933
1934 $report['jobs'] = [];
1935 $report['event_totals'] = [];
1936 $path = 'civicrm/mailing/report/event';
1937 $elements = [
1938 'queue',
1939 'delivered',
1940 'url',
1941 'forward',
1942 'reply',
1943 'unsubscribe',
1944 'optout',
1945 'opened',
1946 'total_opened',
1947 'bounce',
1948 'spool',
1949 ];
1950
1951 // initialize various counters
1952 foreach ($elements as $field) {
1953 $report['event_totals'][$field] = 0;
1954 }
1955
1956 while ($mailing->fetch()) {
1957 $row = [];
1958 foreach ($elements as $field) {
1959 if (isset($mailing->$field)) {
1960 $row[$field] = $mailing->$field;
1961 $report['event_totals'][$field] += $mailing->$field;
1962 }
1963 }
1964
1965 // compute open total separately to discount duplicates
1966 // CRM-1258
1967 $row['opened'] = CRM_Mailing_Event_BAO_Opened::getTotalCount($mailing_id, $mailing->id, TRUE);
1968 $report['event_totals']['opened'] += $row['opened'];
1969 $row['total_opened'] = CRM_Mailing_Event_BAO_Opened::getTotalCount($mailing_id, $mailing->id);
1970 $report['event_totals']['total_opened'] += $row['total_opened'];
1971
1972 // compute unsub total separately to discount duplicates
1973 // CRM-1783
1974 $row['unsubscribe'] = CRM_Mailing_Event_BAO_Unsubscribe::getTotalCount($mailing_id, $mailing->id, TRUE, TRUE);
1975 $report['event_totals']['unsubscribe'] += $row['unsubscribe'];
1976
1977 $row['optout'] = CRM_Mailing_Event_BAO_Unsubscribe::getTotalCount($mailing_id, $mailing->id, TRUE, FALSE);
1978 $report['event_totals']['optout'] += $row['optout'];
1979
1980 foreach (array_keys(CRM_Mailing_BAO_MailingJob::fields()) as $field) {
1981 // Get the field name from the MailingJob fields as that will not have any prefixing.
1982 // dev/mailing#56
1983 $field = CRM_Mailing_BAO_MailingJob::fields()[$field]['name'];
1984 $row[$field] = $mailing->$field;
1985 }
1986
1987 if ($mailing->queue) {
1988 $row['delivered_rate'] = (100.0 * $mailing->delivered) / $mailing->queue;
1989 $row['bounce_rate'] = (100.0 * $mailing->bounce) / $mailing->queue;
1990 $row['unsubscribe_rate'] = (100.0 * $row['unsubscribe']) / $mailing->queue;
1991 $row['optout_rate'] = (100.0 * $row['optout']) / $mailing->queue;
1992 $row['opened_rate'] = $mailing->delivered ? (($row['opened'] / $mailing->delivered) * 100.0) : 0;
1993 $row['clickthrough_rate'] = $mailing->delivered ? (($mailing->url / $mailing->delivered) * 100.0) : 0;
1994 }
1995 else {
1996 $row['delivered_rate'] = 0;
1997 $row['bounce_rate'] = 0;
1998 $row['unsubscribe_rate'] = 0;
1999 $row['optout_rate'] = 0;
2000 $row['opened_rate'] = 0;
2001 $row['clickthrough_rate'] = 0;
2002 }
2003
2004 $arg = "reset=1&mid=$mailing_id&jid={$mailing->id}";
2005 $row['links'] = [
2006 'clicks' => CRM_Utils_System::url($path, "$arg&event=click"),
2007 'queue' => CRM_Utils_System::url($path, "$arg&event=queue"),
2008 'delivered' => CRM_Utils_System::url($path, "$arg&event=delivered"),
2009 'bounce' => CRM_Utils_System::url($path, "$arg&event=bounce"),
2010 'unsubscribe' => CRM_Utils_System::url($path, "$arg&event=unsubscribe"),
2011 'forward' => CRM_Utils_System::url($path, "$arg&event=forward"),
2012 'reply' => CRM_Utils_System::url($path, "$arg&event=reply"),
2013 'opened' => CRM_Utils_System::url($path, "$arg&event=opened"),
2014 ];
2015
2016 foreach (['scheduled_date', 'start_date', 'end_date'] as $key) {
2017 $row[$key] = CRM_Utils_Date::customFormat($row[$key]);
2018 }
2019 $report['jobs'][] = $row;
2020 }
2021
2022 $newTableSize = CRM_Mailing_BAO_Recipients::mailingSize($mailing_id);
2023
2024 // we need to do this for backward compatibility, since old mailings did not
2025 // use the mailing_recipients table
2026 if ($newTableSize > 0) {
2027 $report['event_totals']['queue'] = $newTableSize;
2028 }
2029 else {
2030 $report['event_totals']['queue'] = self::getRecipientsCount($mailing_id);
2031 }
2032
2033 if (!empty($report['event_totals']['queue'])) {
2034 $report['event_totals']['delivered_rate'] = (100.0 * $report['event_totals']['delivered']) / $report['event_totals']['queue'];
2035 $report['event_totals']['bounce_rate'] = (100.0 * $report['event_totals']['bounce']) / $report['event_totals']['queue'];
2036 $report['event_totals']['unsubscribe_rate'] = (100.0 * $report['event_totals']['unsubscribe']) / $report['event_totals']['queue'];
2037 $report['event_totals']['optout_rate'] = (100.0 * $report['event_totals']['optout']) / $report['event_totals']['queue'];
2038 $report['event_totals']['opened_rate'] = !empty($report['event_totals']['delivered']) ? (($report['event_totals']['opened'] / $report['event_totals']['delivered']) * 100.0) : 0;
2039 $report['event_totals']['clickthrough_rate'] = !empty($report['event_totals']['delivered']) ? (($report['event_totals']['url'] / $report['event_totals']['delivered']) * 100.0) : 0;
2040 }
2041 else {
2042 $report['event_totals']['delivered_rate'] = 0;
2043 $report['event_totals']['bounce_rate'] = 0;
2044 $report['event_totals']['unsubscribe_rate'] = 0;
2045 $report['event_totals']['optout_rate'] = 0;
2046 $report['event_totals']['opened_rate'] = 0;
2047 $report['event_totals']['clickthrough_rate'] = 0;
2048 }
2049
2050 /* Get the click-through totals, grouped by URL */
2051
2052 $mailing->query("
2053 SELECT {$t['url']}.url,
2054 {$t['url']}.id,
2055 COUNT({$t['urlopen']}.id) as clicks,
2056 COUNT(DISTINCT {$t['queue']}.id) as unique_clicks
2057 FROM {$t['url']}
2058 LEFT JOIN {$t['urlopen']}
2059 ON {$t['urlopen']}.trackable_url_id = {$t['url']}.id
2060 LEFT JOIN {$t['queue']}
2061 ON {$t['urlopen']}.event_queue_id = {$t['queue']}.id
2062 LEFT JOIN {$t['job']}
2063 ON {$t['queue']}.job_id = {$t['job']}.id
2064 WHERE {$t['url']}.mailing_id = $mailing_id
2065 AND {$t['job']}.is_test = 0
2066 GROUP BY {$t['url']}.id
2067 ORDER BY unique_clicks DESC");
2068
2069 $report['click_through'] = [];
2070
2071 while ($mailing->fetch()) {
2072 $report['click_through'][] = [
2073 'url' => $mailing->url,
2074 'link' => CRM_Utils_System::url($path, "reset=1&event=click&mid=$mailing_id&uid={$mailing->id}"),
2075 'link_unique' => CRM_Utils_System::url($path, "reset=1&event=click&mid=$mailing_id&uid={$mailing->id}&distinct=1"),
2076 'clicks' => $mailing->clicks,
2077 'unique' => $mailing->unique_clicks,
2078 'rate' => !empty($report['event_totals']['delivered']) ? (100.0 * $mailing->unique_clicks) / $report['event_totals']['delivered'] : 0,
2079 'report' => CRM_Report_Utils_Report::getNextUrl('mailing/clicks', "reset=1&mailing_id_value={$mailing_id}&url_value={$mailing->url}", FALSE, TRUE),
2080 ];
2081 }
2082
2083 $arg = "reset=1&mid=$mailing_id";
2084 $report['event_totals']['links'] = [
2085 'clicks' => CRM_Utils_System::url($path, "$arg&event=click"),
2086 'clicks_unique' => CRM_Utils_System::url($path, "$arg&event=click&distinct=1"),
2087 'queue' => CRM_Utils_System::url($path, "$arg&event=queue"),
2088 'delivered' => CRM_Utils_System::url($path, "$arg&event=delivered"),
2089 'bounce' => CRM_Utils_System::url($path, "$arg&event=bounce"),
2090 'unsubscribe' => CRM_Utils_System::url($path, "$arg&event=unsubscribe"),
2091 'optout' => CRM_Utils_System::url($path, "$arg&event=optout"),
2092 'forward' => CRM_Utils_System::url($path, "$arg&event=forward"),
2093 'reply' => CRM_Utils_System::url($path, "$arg&event=reply"),
2094 'opened' => CRM_Utils_System::url($path, "$arg&event=opened"),
2095 ];
2096
2097 $actionLinks = [CRM_Core_Action::VIEW => ['name' => ts('Report')]];
2098 $actionLinks[CRM_Core_Action::ADVANCED] = [
2099 'name' => ts('Advanced Search'),
2100 'url' => 'civicrm/contact/search/advanced',
2101 ];
2102 $action = array_sum(array_keys($actionLinks));
2103
2104 $report['event_totals']['actionlinks'] = [];
2105 foreach (['clicks', 'clicks_unique', 'queue', 'delivered', 'bounce', 'unsubscribe', 'forward', 'reply', 'opened', 'opened_unique', 'optout'] as $key) {
2106 $url = 'mailing/detail';
2107 $reportFilter = "reset=1&mailing_id_value={$mailing_id}";
2108 $searchFilter = "force=1&mailing_id=%%mid%%";
2109 switch ($key) {
2110 case 'delivered':
2111 $reportFilter .= "&delivery_status_value=successful";
2112 $searchFilter .= "&mailing_delivery_status=Y";
2113 break;
2114
2115 case 'bounce':
2116 $url = "mailing/bounce";
2117 $searchFilter .= "&mailing_delivery_status=N";
2118 break;
2119
2120 case 'forward':
2121 $reportFilter .= "&is_forwarded_value=1";
2122 $searchFilter .= "&mailing_forward=1";
2123 break;
2124
2125 case 'reply':
2126 $reportFilter .= "&is_replied_value=1";
2127 $searchFilter .= "&mailing_reply_status=Y";
2128 break;
2129
2130 case 'unsubscribe':
2131 $reportFilter .= "&is_unsubscribed_value=1";
2132 $searchFilter .= "&mailing_unsubscribe=1";
2133 break;
2134
2135 case 'optout':
2136 $reportFilter .= "&is_optout_value=1";
2137 $searchFilter .= "&mailing_optout=1";
2138 break;
2139
2140 case 'opened':
2141 // do not use group by clause in report, because same report used for total and unique open
2142 $reportFilter .= "&distinct=0";
2143 case 'opened_unique':
2144 $url = "mailing/opened";
2145 $searchFilter .= "&mailing_open_status=Y";
2146 break;
2147
2148 case 'clicks':
2149 case 'clicks_unique':
2150 $url = "mailing/clicks";
2151 $searchFilter .= "&mailing_click_status=Y";
2152 break;
2153 }
2154 $actionLinks[CRM_Core_Action::VIEW]['url'] = CRM_Report_Utils_Report::getNextUrl($url, $reportFilter, FALSE, TRUE);
2155 if (array_key_exists(CRM_Core_Action::ADVANCED, $actionLinks)) {
2156 $actionLinks[CRM_Core_Action::ADVANCED]['qs'] = $searchFilter;
2157 }
2158 $report['event_totals']['actionlinks'][$key] = CRM_Core_Action::formLink(
2159 $actionLinks,
2160 $action,
2161 ['mid' => $mailing_id],
2162 ts('more'),
2163 FALSE,
2164 'mailing.report.action',
2165 'Mailing',
2166 $mailing_id
2167 );
2168 }
2169
2170 return $report;
2171 }
2172
2173 /**
2174 * Get the count of mailings.
2175 *
2176 * @param
2177 *
2178 * @return int
2179 * Count
2180 */
2181 public function getCount() {
2182 $this->selectAdd();
2183 $this->selectAdd('COUNT(id) as count');
2184
2185 $session = CRM_Core_Session::singleton();
2186 $this->find(TRUE);
2187
2188 return $this->count;
2189 }
2190
2191 /**
2192 * @param int $id
2193 *
2194 * @throws Exception
2195 */
2196 public static function checkPermission($id) {
2197 if (!$id) {
2198 return;
2199 }
2200
2201 $mailingIDs = self::mailingACLIDs();
2202 if ($mailingIDs === TRUE) {
2203 return;
2204 }
2205
2206 if (!in_array($id, $mailingIDs)) {
2207 throw new CRM_Core_Exception(ts('You do not have permission to access this mailing report'));
2208 }
2209 }
2210
2211 /**
2212 * @param null $alias
2213 *
2214 * @return string
2215 */
2216 public static function mailingACL($alias = NULL) {
2217 $mailingACL = " ( 0 ) ";
2218
2219 $mailingIDs = self::mailingACLIDs();
2220 if ($mailingIDs === TRUE) {
2221 return " ( 1 ) ";
2222 }
2223
2224 if (!empty($mailingIDs)) {
2225 $mailingIDs = implode(',', $mailingIDs);
2226 $tableName = !$alias ? self::getTableName() : $alias;
2227 $mailingACL = " $tableName.id IN ( $mailingIDs ) ";
2228 }
2229 return $mailingACL;
2230 }
2231
2232 /**
2233 * Returns all the mailings that this user can access. This is dependent on
2234 * all the groups that the user has access to.
2235 * However since most civi installs dont use ACL's we special case the condition
2236 * where the user has access to ALL groups, and hence ALL mailings and return a
2237 * value of TRUE (to avoid the downstream where clause with a list of mailing list IDs
2238 *
2239 * @return bool|array
2240 * TRUE if the user has access to all mailings, else array of mailing IDs (possibly empty).
2241 */
2242 public static function mailingACLIDs() {
2243 // CRM-11633
2244 // optimize common case where admin has access
2245 // to all mailings
2246 if (
2247 CRM_Core_Permission::check('view all contacts') ||
2248 CRM_Core_Permission::check('edit all contacts')
2249 ) {
2250 return TRUE;
2251 }
2252
2253 $mailingIDs = [];
2254
2255 // get all the groups that this user can access
2256 // if they dont have universal access
2257 $groupNames = civicrm_api3('Group', 'get', [
2258 'check_permissions' => TRUE,
2259 'return' => ['title', 'id'],
2260 'options' => ['limit' => 0],
2261 ]);
2262 foreach ($groupNames['values'] as $group) {
2263 $groups[$group['id']] = $group['title'];
2264 }
2265 if (!empty($groups)) {
2266 $groupIDs = implode(',', array_keys($groups));
2267 $domain_id = CRM_Core_Config::domainID();
2268
2269 // get all the mailings that are in this subset of groups
2270 $query = "
2271 SELECT DISTINCT( m.id ) as id
2272 FROM civicrm_mailing m
2273 LEFT JOIN civicrm_mailing_group g ON g.mailing_id = m.id
2274 WHERE ( ( g.entity_table like 'civicrm_group%' AND g.entity_id IN ( $groupIDs ) )
2275 OR ( g.entity_table IS NULL AND g.entity_id IS NULL AND m.domain_id = $domain_id ) )
2276 ";
2277 $dao = CRM_Core_DAO::executeQuery($query);
2278
2279 $mailingIDs = [];
2280 while ($dao->fetch()) {
2281 $mailingIDs[] = $dao->id;
2282 }
2283 //CRM-18181 Get all mailings that use the mailings found earlier as receipients
2284 if (!empty($mailingIDs)) {
2285 $mailings = implode(',', $mailingIDs);
2286 $mailingQuery = "
2287 SELECT DISTINCT ( m.id ) as id
2288 FROM civicrm_mailing m
2289 LEFT JOIN civicrm_mailing_group g ON g.mailing_id = m.id
2290 WHERE g.entity_table like 'civicrm_mailing%' AND g.entity_id IN ($mailings)";
2291 $mailingDao = CRM_Core_DAO::executeQuery($mailingQuery);
2292 while ($mailingDao->fetch()) {
2293 $mailingIDs[] = $mailingDao->id;
2294 }
2295 }
2296 }
2297
2298 return $mailingIDs;
2299 }
2300
2301 /**
2302 * Get the rows for a browse operation.
2303 *
2304 * @param int $offset
2305 * The row number to start from.
2306 * @param int $rowCount
2307 * The nmber of rows to return.
2308 * @param string $sort
2309 * The sql string that describes the sort order.
2310 *
2311 * @param null $additionalClause
2312 * @param array $additionalParams
2313 *
2314 * @return array
2315 * The rows
2316 */
2317 public function &getRows($offset, $rowCount, $sort, $additionalClause = NULL, $additionalParams = NULL) {
2318 $mailing = self::getTableName();
2319 $job = CRM_Mailing_BAO_MailingJob::getTableName();
2320 $group = CRM_Mailing_DAO_MailingGroup::getTableName();
2321 $session = CRM_Core_Session::singleton();
2322
2323 $mailingACL = self::mailingACL();
2324
2325 //get all campaigns.
2326 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
2327 $select = [
2328 "$mailing.id",
2329 "$mailing.name",
2330 "$job.status",
2331 "$mailing.approval_status_id",
2332 "createdContact.sort_name as created_by",
2333 "scheduledContact.sort_name as scheduled_by",
2334 "$mailing.created_id as created_id",
2335 "$mailing.scheduled_id as scheduled_id",
2336 "$mailing.is_archived as archived",
2337 "$mailing.created_date as created_date",
2338 "campaign_id",
2339 "$mailing.sms_provider_id as sms_provider_id",
2340 "$mailing.language",
2341 ];
2342
2343 // we only care about parent jobs, since that holds all the info on
2344 // the mailing
2345 $selectClause = implode(', ', $select);
2346 $groupFromSelect = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($select, "$mailing.id");
2347 $query = "
2348 SELECT {$selectClause},
2349 MIN($job.scheduled_date) as scheduled_date,
2350 MIN($job.start_date) as start_date,
2351 MAX($job.end_date) as end_date
2352 FROM $mailing
2353 LEFT JOIN $job ON ( $job.mailing_id = $mailing.id AND $job.is_test = 0 AND $job.parent_id IS NULL )
2354 LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
2355 LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
2356 WHERE $mailingACL $additionalClause";
2357
2358 if (!empty($groupFromSelect)) {
2359 $query .= $groupFromSelect;
2360 }
2361
2362 if ($sort) {
2363 $orderBy = trim($sort->orderBy());
2364 if (!empty($orderBy)) {
2365 $query .= " ORDER BY $orderBy";
2366 }
2367 }
2368
2369 if ($rowCount) {
2370 $offset = CRM_Utils_Type::escape($offset, 'Int');
2371 $rowCount = CRM_Utils_Type::escape($rowCount, 'Int');
2372
2373 $query .= " LIMIT $offset, $rowCount ";
2374 }
2375
2376 if (!$additionalParams) {
2377 $additionalParams = [];
2378 }
2379
2380 $dao = CRM_Core_DAO::executeQuery($query, $additionalParams);
2381
2382 $rows = [];
2383 while ($dao->fetch()) {
2384 $rows[] = [
2385 'id' => $dao->id,
2386 'name' => $dao->name,
2387 'status' => $dao->status ? $dao->status : 'Not scheduled',
2388 'created_date' => CRM_Utils_Date::customFormat($dao->created_date),
2389 'scheduled' => CRM_Utils_Date::customFormat($dao->scheduled_date),
2390 'scheduled_iso' => $dao->scheduled_date,
2391 'start' => CRM_Utils_Date::customFormat($dao->start_date),
2392 'end' => CRM_Utils_Date::customFormat($dao->end_date),
2393 'created_by' => $dao->created_by,
2394 'scheduled_by' => $dao->scheduled_by,
2395 'created_id' => $dao->created_id,
2396 'scheduled_id' => $dao->scheduled_id,
2397 'archived' => $dao->archived,
2398 'approval_status_id' => $dao->approval_status_id,
2399 'campaign_id' => $dao->campaign_id,
2400 'campaign' => empty($dao->campaign_id) ? NULL : $allCampaigns[$dao->campaign_id],
2401 'sms_provider_id' => $dao->sms_provider_id,
2402 'language' => $dao->language,
2403 ];
2404 }
2405 return $rows;
2406 }
2407
2408 /**
2409 * Show detail Mailing report.
2410 *
2411 * @param int $id
2412 *
2413 * @return string
2414 */
2415 public static function showEmailDetails($id) {
2416 return CRM_Utils_System::url('civicrm/mailing/report', "mid=$id");
2417 }
2418
2419 /**
2420 * Delete Mails and all its associated records.
2421 *
2422 * @param int $id
2423 * Id of the mail to delete.
2424 *
2425 * @return void
2426 */
2427 public static function del($id) {
2428 if (empty($id)) {
2429 throw new CRM_Core_Exception(ts('No id passed to mailing del function'));
2430 }
2431
2432 CRM_Utils_Hook::pre('delete', 'Mailing', $id);
2433
2434 // delete all file attachments
2435 CRM_Core_BAO_File::deleteEntityFile('civicrm_mailing',
2436 $id
2437 );
2438
2439 $dao = new CRM_Mailing_DAO_Mailing();
2440 $dao->id = $id;
2441 $dao->delete();
2442
2443 CRM_Core_Session::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
2444
2445 CRM_Utils_Hook::post('delete', 'Mailing', $id, $dao);
2446 }
2447
2448 /**
2449 * @deprecated
2450 * Use CRM_Mailing_BAO_MailingJob::del($id)
2451 *
2452 * @param int $id
2453 * Id of the Job to delete.
2454 *
2455 * @return void
2456 */
2457 public static function delJob($id) {
2458 if (empty($id)) {
2459 throw new CRM_Core_Exception(ts('No id passed to mailing delJob function'));
2460 }
2461
2462 CRM_Core_Error::deprecatedWarning('This function is deprecated, use CRM_Mailing_BAO_MailingJob::del instead');
2463
2464 CRM_Mailing_BAO_MailingJob::del($id);
2465 }
2466
2467 /**
2468 * @deprecated
2469 * This is used by CiviMail but will be made redundant by FlexMailer/TokenProcessor.
2470 * @return array
2471 */
2472 public function getReturnProperties() {
2473 $tokens = &$this->getTokens();
2474
2475 $properties = [];
2476 if (isset($tokens['html']) &&
2477 isset($tokens['html']['contact'])
2478 ) {
2479 $properties = array_merge($properties, $tokens['html']['contact']);
2480 }
2481
2482 if (isset($tokens['text']) &&
2483 isset($tokens['text']['contact'])
2484 ) {
2485 $properties = array_merge($properties, $tokens['text']['contact']);
2486 }
2487
2488 if (isset($tokens['subject']) &&
2489 isset($tokens['subject']['contact'])
2490 ) {
2491 $properties = array_merge($properties, $tokens['subject']['contact']);
2492 }
2493
2494 $returnProperties = [];
2495 $returnProperties['display_name'] = $returnProperties['contact_id'] = $returnProperties['preferred_mail_format'] = $returnProperties['hash'] = 1;
2496
2497 foreach ($properties as $p) {
2498 $returnProperties[$p] = 1;
2499 }
2500
2501 return $returnProperties;
2502 }
2503
2504 /**
2505 * Build the compose mail form.
2506 *
2507 * @param CRM_Core_Form $form
2508 *
2509 * @return void
2510 */
2511 public static function commonCompose(&$form) {
2512 //get the tokens.
2513 $tokens = [];
2514
2515 if (method_exists($form, 'listTokens')) {
2516 $tokens = array_merge($form->listTokens(), $tokens);
2517 }
2518
2519 //sorted in ascending order tokens by ignoring word case
2520 $form->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
2521
2522 $templates = [];
2523
2524 $textFields = [
2525 'text_message' => ts('HTML Format'),
2526 'sms_text_message' => ts('SMS Message'),
2527 ];
2528 $modePrefixes = ['Mail' => NULL, 'SMS' => 'SMS'];
2529
2530 $className = CRM_Utils_System::getClassName($form);
2531
2532 if ($className != 'CRM_SMS_Form_Upload' && $className != 'CRM_Contact_Form_Task_SMS' &&
2533 $className != 'CRM_Contact_Form_Task_SMS'
2534 ) {
2535 $form->add('wysiwyg', 'html_message',
2536 strstr($className, 'PDF') ? ts('Document Body') : ts('HTML Format'),
2537 [
2538 'cols' => '80',
2539 'rows' => '8',
2540 'onkeyup' => "return verify(this)",
2541 ]
2542 );
2543
2544 if ($className != 'CRM_Admin_Form_ScheduleReminders') {
2545 unset($modePrefixes['SMS']);
2546 }
2547 }
2548 else {
2549 unset($textFields['text_message']);
2550 unset($modePrefixes['Mail']);
2551 }
2552
2553 //insert message Text by selecting "Select Template option"
2554 foreach ($textFields as $id => $label) {
2555 $prefix = NULL;
2556 if ($id == 'sms_text_message') {
2557 $prefix = "SMS";
2558 $form->assign('max_sms_length', CRM_SMS_Provider::MAX_SMS_CHAR);
2559 }
2560 $form->add('textarea', $id, $label,
2561 [
2562 'cols' => '80',
2563 'rows' => '8',
2564 'onkeyup' => "return verify(this, '{$prefix}')",
2565 ]
2566 );
2567 }
2568
2569 foreach ($modePrefixes as $prefix) {
2570 if ($prefix == 'SMS') {
2571 $templates[$prefix] = CRM_Core_BAO_MessageTemplate::getMessageTemplates(FALSE, TRUE);
2572 }
2573 else {
2574 $templates[$prefix] = CRM_Core_BAO_MessageTemplate::getMessageTemplates(FALSE);
2575 }
2576 if (!empty($templates[$prefix])) {
2577 $form->assign('templates', TRUE);
2578
2579 $form->add('select', "{$prefix}template", ts('Use Template'),
2580 ['' => ts('- select -')] + $templates[$prefix], FALSE,
2581 ['onChange' => "selectValue( this.value, '{$prefix}');"]
2582 );
2583 }
2584 $form->add('checkbox', "{$prefix}updateTemplate", ts('Update Template'), NULL);
2585
2586 $form->add('checkbox', "{$prefix}saveTemplate", ts('Save As New Template'), NULL, FALSE,
2587 ['onclick' => "showSaveDetails(this, '{$prefix}');"]
2588 );
2589 $form->add('text', "{$prefix}saveTemplateName", ts('Template Title'));
2590 }
2591
2592 // I'm not sure this is ever called.
2593 $action = CRM_Utils_Request::retrieve('action', 'String', $form, FALSE);
2594 if ((CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Task_PDF') &&
2595 $action == CRM_Core_Action::VIEW
2596 ) {
2597 $form->freeze('html_message');
2598 }
2599 }
2600
2601 /**
2602 * Get the search based mailing Ids.
2603 *
2604 * @return array
2605 * , searched base mailing ids.
2606 */
2607 public function searchMailingIDs() {
2608 $group = CRM_Mailing_DAO_MailingGroup::getTableName();
2609 $mailing = self::getTableName();
2610
2611 $query = "
2612 SELECT $mailing.id as mailing_id
2613 FROM $mailing, $group
2614 WHERE $group.mailing_id = $mailing.id
2615 AND $group.group_type = 'Base'";
2616
2617 $searchDAO = CRM_Core_DAO::executeQuery($query);
2618 $mailingIDs = [];
2619 while ($searchDAO->fetch()) {
2620 $mailingIDs[] = $searchDAO->mailing_id;
2621 }
2622
2623 return $mailingIDs;
2624 }
2625
2626 /**
2627 * Get the content/components of mailing based on mailing Id
2628 *
2629 * @param array $report
2630 * of mailing report.
2631 *
2632 * @param $form
2633 * Reference of this.
2634 *
2635 * @param bool $isSMS
2636 *
2637 * @return array
2638 * array content/component.
2639 */
2640 public static function getMailingContent(&$report, &$form, $isSMS = FALSE) {
2641 $htmlHeader = $textHeader = NULL;
2642 $htmlFooter = $textFooter = NULL;
2643
2644 if (!$isSMS) {
2645 if ($report['mailing']['header_id']) {
2646 $header = new CRM_Mailing_BAO_MailingComponent();
2647 $header->id = $report['mailing']['header_id'];
2648 $header->find(TRUE);
2649 $htmlHeader = $header->body_html;
2650 $textHeader = $header->body_text;
2651 }
2652
2653 if ($report['mailing']['footer_id']) {
2654 $footer = new CRM_Mailing_BAO_MailingComponent();
2655 $footer->id = $report['mailing']['footer_id'];
2656 $footer->find(TRUE);
2657 $htmlFooter = $footer->body_html;
2658 $textFooter = $footer->body_text;
2659 }
2660 }
2661
2662 $mailingKey = $form->_mailing_id;
2663 if (!$isSMS) {
2664 if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) {
2665 $mailingKey = $hash;
2666 }
2667 }
2668
2669 if (!empty($report['mailing']['body_text'])) {
2670 $url = CRM_Utils_System::url('civicrm/mailing/view', 'reset=1&text=1&id=' . $mailingKey);
2671 $form->assign('textViewURL', $url);
2672 }
2673
2674 if (!$isSMS) {
2675 if (!empty($report['mailing']['body_html'])) {
2676 $url = CRM_Utils_System::url('civicrm/mailing/view', 'reset=1&id=' . $mailingKey);
2677 $form->assign('htmlViewURL', $url);
2678 }
2679 }
2680
2681 if (!$isSMS) {
2682 $report['mailing']['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $form->_mailing_id);
2683 }
2684 return $report;
2685 }
2686
2687 /**
2688 * @param int $jobID
2689 *
2690 * @return mixed
2691 */
2692 public static function overrideVerp($jobID) {
2693 static $_cache = [];
2694
2695 if (!isset($_cache[$jobID])) {
2696 $query = "
2697 SELECT override_verp
2698 FROM civicrm_mailing
2699 INNER JOIN civicrm_mailing_job ON civicrm_mailing.id = civicrm_mailing_job.mailing_id
2700 WHERE civicrm_mailing_job.id = %1
2701 ";
2702 $params = [1 => [$jobID, 'Integer']];
2703 $_cache[$jobID] = CRM_Core_DAO::singleValueQuery($query, $params);
2704 }
2705 return $_cache[$jobID];
2706 }
2707
2708 /**
2709 * @param null $mode
2710 *
2711 * @return bool
2712 * @throws Exception
2713 */
2714 public static function processQueue($mode = NULL) {
2715 $config = CRM_Core_Config::singleton();
2716
2717 if ($mode == NULL && CRM_Core_BAO_MailSettings::defaultDomain() == "EXAMPLE.ORG") {
2718 // Using forceBackend=TRUE because WordPress sometimes fails to detect cron
2719 throw new CRM_Core_Exception(ts('The <a href="%1">default mailbox</a> has not been configured. You will find <a href="%2">more info in the online system administrator guide</a>', [
2720 1 => CRM_Utils_System::url('civicrm/admin/mailSettings', 'reset=1', FALSE, NULL, TRUE, FALSE, TRUE),
2721 2 => "https://docs.civicrm.org/sysadmin/en/latest/setup/civimail/",
2722 ]));
2723 }
2724
2725 // check if we are enforcing number of parallel cron jobs
2726 // CRM-8460
2727 $gotCronLock = FALSE;
2728
2729 $mailerJobsMax = Civi::settings()->get('mailerJobsMax');
2730 if (is_numeric($mailerJobsMax) && $mailerJobsMax > 0) {
2731 $lockArray = range(1, $mailerJobsMax);
2732
2733 // Shuffle the array to improve chances of quickly finding an open thread
2734 shuffle($lockArray);
2735
2736 // Check if we are using global locks
2737 foreach ($lockArray as $lockID) {
2738 $cronLock = Civi::lockManager()
2739 ->acquire("worker.mailing.send.{$lockID}");
2740 if ($cronLock->isAcquired()) {
2741 $gotCronLock = TRUE;
2742 break;
2743 }
2744 }
2745
2746 // Exit here since we have enough mailing processes running
2747 if (!$gotCronLock) {
2748 CRM_Core_Error::debug_log_message('Returning early, since the maximum number of mailing processes are running');
2749 return TRUE;
2750 }
2751
2752 if (getenv('CIVICRM_CRON_HOLD')) {
2753 // In testing, we may need to simulate some slow activities.
2754 sleep(getenv('CIVICRM_CRON_HOLD'));
2755 }
2756 }
2757
2758 // Split up the parent jobs into multiple child jobs
2759 $mailerJobSize = Civi::settings()->get('mailerJobSize');
2760 CRM_Mailing_BAO_MailingJob::runJobs_pre($mailerJobSize, $mode);
2761 CRM_Mailing_BAO_MailingJob::runJobs(NULL, $mode);
2762 CRM_Mailing_BAO_MailingJob::runJobs_post($mode);
2763
2764 // Release the global lock if we do have one
2765 if ($gotCronLock) {
2766 $cronLock->release();
2767 }
2768
2769 return TRUE;
2770 }
2771
2772 /**
2773 * @param int $mailingID
2774 */
2775 private static function addMultipleEmails($mailingID) {
2776 $sql = "
2777 INSERT INTO civicrm_mailing_recipients
2778 (mailing_id, email_id, contact_id)
2779 SELECT %1, e.id, e.contact_id FROM civicrm_email e
2780 WHERE e.on_hold = 0
2781 AND e.is_bulkmail = 1
2782 AND e.contact_id IN
2783 ( SELECT contact_id FROM civicrm_mailing_recipients mr WHERE mailing_id = %1 )
2784 AND e.id NOT IN ( SELECT email_id FROM civicrm_mailing_recipients mr WHERE mailing_id = %1 )
2785 ";
2786 $params = [1 => [$mailingID, 'Integer']];
2787
2788 $dao = CRM_Core_DAO::executeQuery($sql, $params);
2789 }
2790
2791 /**
2792 * @param bool $isSMS
2793 *
2794 * @return mixed
2795 */
2796 public static function getMailingsList($isSMS = FALSE) {
2797 static $list = [];
2798 $where = " WHERE ";
2799 if (!$isSMS) {
2800 $where .= " civicrm_mailing.sms_provider_id IS NULL ";
2801 }
2802 else {
2803 $where .= " civicrm_mailing.sms_provider_id IS NOT NULL ";
2804 }
2805
2806 if (empty($list)) {
2807 $query = "
2808 SELECT civicrm_mailing.id, civicrm_mailing.name, civicrm_mailing_job.end_date
2809 FROM civicrm_mailing
2810 INNER JOIN civicrm_mailing_job ON civicrm_mailing.id = civicrm_mailing_job.mailing_id {$where}
2811 ORDER BY civicrm_mailing.name";
2812 $mailing = CRM_Core_DAO::executeQuery($query);
2813
2814 while ($mailing->fetch()) {
2815 $list[$mailing->id] = "{$mailing->name} :: {$mailing->end_date}";
2816 }
2817 }
2818
2819 return $list;
2820 }
2821
2822 /**
2823 * wrapper for ajax activity selector.
2824 *
2825 * @param array $params
2826 * Associated array for params record id.
2827 *
2828 * @return array
2829 * associated array of contact activities
2830 */
2831 public static function getContactMailingSelector(&$params) {
2832 // format the params
2833 $params['offset'] = ($params['page'] - 1) * $params['rp'];
2834 $params['rowCount'] = $params['rp'];
2835 $params['sort'] = $params['sortBy'] ?? NULL;
2836 $params['caseId'] = NULL;
2837
2838 // get contact mailings
2839 $mailings = CRM_Mailing_BAO_Mailing::getContactMailings($params);
2840
2841 // add total
2842 $params['total'] = CRM_Mailing_BAO_Mailing::getContactMailingsCount($params);
2843
2844 //CRM-12814
2845 if (!empty($mailings)) {
2846 $openCounts = CRM_Mailing_Event_BAO_Opened::getMailingContactCount(array_keys($mailings), $params['contact_id']);
2847 $clickCounts = CRM_Mailing_Event_BAO_TrackableURLOpen::getMailingContactCount(array_keys($mailings), $params['contact_id']);
2848 }
2849
2850 // format params and add links
2851 $contactMailings = [];
2852 foreach ($mailings as $mailingId => $values) {
2853 $mailing = [];
2854 $mailing['subject'] = $values['subject'];
2855 $mailing['creator_name'] = CRM_Utils_System::href(
2856 $values['creator_name'],
2857 'civicrm/contact/view',
2858 "reset=1&cid={$values['creator_id']}");
2859 $mailing['recipients'] = CRM_Utils_System::href(ts('(recipients)'), 'civicrm/mailing/report/event',
2860 "mid={$values['mailing_id']}&reset=1&cid={$params['contact_id']}&event=queue&context=mailing");
2861 $mailing['start_date'] = CRM_Utils_Date::customFormat($values['start_date']);
2862 //CRM-12814
2863 $mailing['openstats'] = "Opens: " .
2864 CRM_Utils_Array::value($values['mailing_id'], $openCounts, 0) .
2865 "<br />Clicks: " .
2866 CRM_Utils_Array::value($values['mailing_id'], $clickCounts, 0);
2867
2868 $actionLinks = [
2869 CRM_Core_Action::VIEW => [
2870 'name' => ts('View'),
2871 'url' => 'civicrm/mailing/view',
2872 'qs' => "reset=1&id=%%mkey%%",
2873 'title' => ts('View Mailing'),
2874 'class' => 'crm-popup',
2875 ],
2876 CRM_Core_Action::BROWSE => [
2877 'name' => ts('Mailing Report'),
2878 'url' => 'civicrm/mailing/report',
2879 'qs' => "mid=%%mid%%&reset=1&cid=%%cid%%&context=mailing",
2880 'title' => ts('View Mailing Report'),
2881 ],
2882 ];
2883
2884 $mailingKey = $values['mailing_id'];
2885 if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) {
2886 $mailingKey = $hash;
2887 }
2888
2889 $mailing['links'] = CRM_Core_Action::formLink(
2890 $actionLinks,
2891 NULL,
2892 [
2893 'mid' => $values['mailing_id'],
2894 'cid' => $params['contact_id'],
2895 'mkey' => $mailingKey,
2896 ],
2897 ts('more'),
2898 FALSE,
2899 'mailing.contact.action',
2900 'Mailing',
2901 $values['mailing_id']
2902 );
2903
2904 array_push($contactMailings, $mailing);
2905 }
2906
2907 $contactMailingsDT = [];
2908 $contactMailingsDT['data'] = $contactMailings;
2909 $contactMailingsDT['recordsTotal'] = $params['total'];
2910 $contactMailingsDT['recordsFiltered'] = $params['total'];
2911
2912 return $contactMailingsDT;
2913 }
2914
2915 /**
2916 * Retrieve contact mailing.
2917 *
2918 * @param array $params
2919 *
2920 * @return array
2921 * Array of mailings for a contact
2922 *
2923 */
2924 public static function getContactMailings(&$params) {
2925 $params['version'] = 3;
2926 $params['offset'] = ($params['page'] - 1) * $params['rp'];
2927 $params['limit'] = $params['rp'];
2928 $params['sort'] = $params['sortBy'] ?? NULL;
2929
2930 $result = civicrm_api('MailingContact', 'get', $params);
2931 return $result['values'];
2932 }
2933
2934 /**
2935 * Retrieve contact mailing count.
2936 *
2937 * @param array $params
2938 *
2939 * @return int
2940 * count of mailings for a contact
2941 *
2942 */
2943 public static function getContactMailingsCount(&$params) {
2944 $params['version'] = 3;
2945 return civicrm_api('MailingContact', 'getcount', $params);
2946 }
2947
2948 /**
2949 * Get a list of permissions required for CRUD'ing each field
2950 * (when workflow is enabled).
2951 *
2952 * @return array
2953 * Array (string $fieldName => string $permName)
2954 */
2955 public static function getWorkflowFieldPerms() {
2956 $fieldNames = array_keys(CRM_Mailing_DAO_Mailing::fields());
2957 $fieldPerms = [];
2958 foreach ($fieldNames as $fieldName) {
2959 if ($fieldName == 'id') {
2960 $fieldPerms[$fieldName] = [
2961 // OR
2962 [
2963 'access CiviMail',
2964 'schedule mailings',
2965 'approve mailings',
2966 ],
2967 ];
2968 }
2969 elseif (in_array($fieldName, ['scheduled_date', 'scheduled_id'])) {
2970 $fieldPerms[$fieldName] = [
2971 // OR
2972 ['access CiviMail', 'schedule mailings'],
2973 ];
2974 }
2975 elseif (in_array($fieldName, [
2976 'approval_date',
2977 'approver_id',
2978 'approval_status_id',
2979 'approval_note',
2980 ])) {
2981 $fieldPerms[$fieldName] = [
2982 // OR
2983 ['access CiviMail', 'approve mailings'],
2984 ];
2985 }
2986 else {
2987 $fieldPerms[$fieldName] = [
2988 // OR
2989 ['access CiviMail', 'create mailings'],
2990 ];
2991 }
2992 }
2993 return $fieldPerms;
2994 }
2995
2996 /**
2997 * White-list of possible values for the entity_table field.
2998 *
2999 * @return array
3000 */
3001 public static function mailingGroupEntityTables() {
3002 return [
3003 CRM_Contact_BAO_Group::getTableName() => 'Group',
3004 CRM_Mailing_BAO_Mailing::getTableName() => 'Mailing',
3005 ];
3006 }
3007
3008 /**
3009 * Get the public view url.
3010 *
3011 * @param int $id
3012 * @param bool $absolute
3013 *
3014 * @return string
3015 */
3016 public static function getPublicViewUrl($id, $absolute = TRUE) {
3017 if ((civicrm_api3('Mailing', 'getvalue', [
3018 'id' => $id,
3019 'return' => 'visibility',
3020 ])) === 'Public Pages') {
3021
3022 // if hash setting is on then we change the public url into a hash
3023 $hash = CRM_Mailing_BAO_Mailing::getMailingHash($id);
3024 if (!empty($hash)) {
3025 $id = $hash;
3026 }
3027
3028 return CRM_Utils_System::url('civicrm/mailing/view', ['id' => $id], $absolute, NULL, TRUE, TRUE);
3029 }
3030 }
3031
3032 /**
3033 * Get a list of template types which can be used as `civicrm_mailing.template_type`.
3034 *
3035 * @return array
3036 * A list of template-types, keyed numerically. Each defines:
3037 * - name: string, a short symbolic name
3038 * - editorUrl: string, Angular template name
3039 *
3040 * Ex: $templateTypes[0] === array('name' => 'mosaico', 'editorUrl' => '~/crmMosaico/editor.html').
3041 */
3042 public static function getTemplateTypes() {
3043 if (!isset(Civi::$statics[__CLASS__]['templateTypes'])) {
3044 $types = [];
3045 $types[] = [
3046 'name' => 'traditional',
3047 'editorUrl' => CRM_Mailing_Info::workflowEnabled() ? '~/crmMailing/EditMailingCtrl/workflow.html' : '~/crmMailing/EditMailingCtrl/2step.html',
3048 'weight' => 0,
3049 ];
3050
3051 CRM_Utils_Hook::mailingTemplateTypes($types);
3052
3053 $defaults = ['weight' => 0];
3054 foreach (array_keys($types) as $typeName) {
3055 $types[$typeName] = array_merge($defaults, $types[$typeName]);
3056 }
3057 usort($types, function ($a, $b) {
3058 if ($a['weight'] === $b['weight']) {
3059 return 0;
3060 }
3061 return $a['weight'] < $b['weight'] ? -1 : 1;
3062 });
3063
3064 Civi::$statics[__CLASS__]['templateTypes'] = $types;
3065 }
3066
3067 return Civi::$statics[__CLASS__]['templateTypes'];
3068 }
3069
3070 /**
3071 * Get a list of template types.
3072 *
3073 * @return array
3074 * Array(string $name => string $label).
3075 */
3076 public static function getTemplateTypeNames() {
3077 $r = [];
3078 foreach (self::getTemplateTypes() as $type) {
3079 $r[$type['name']] = $type['name'];
3080 }
3081 return $r;
3082 }
3083
3084 }