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