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