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