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