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