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