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