3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC https://civicrm.org/licensing
34 require_once 'Mail/mime.php';
37 * Class CRM_Mailing_Event_BAO_Unsubscribe
39 class CRM_Mailing_Event_BAO_Unsubscribe
extends CRM_Mailing_Event_DAO_Unsubscribe
{
44 public function __construct() {
45 parent
::__construct();
49 * Unsubscribe a contact from the domain.
53 * @param int $queue_id
54 * The Queue Event ID of the recipient.
59 * Was the contact successfully unsubscribed?
61 public static function unsub_from_domain($job_id, $queue_id, $hash) {
62 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
67 $transaction = new CRM_Core_Transaction();
69 $now = date('YmdHis');
70 if (CRM_Core_BAO_Email
::isMultipleBulkMail()) {
71 $email = new CRM_Core_BAO_Email();
72 $email->id
= $q->email_id
;
73 if ($email->find(TRUE)) {
81 1 => [$now, 'Timestamp'],
82 2 => [$email->email
, 'String'],
84 CRM_Core_DAO
::executeQuery($sql, $sqlParams);
88 $contact = new CRM_Contact_BAO_Contact();
89 $contact->id
= $q->contact_id
;
90 $contact->is_opt_out
= TRUE;
94 $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
95 $ue->event_queue_id
= $queue_id;
96 $ue->org_unsubscribe
= 1;
97 $ue->time_stamp
= $now;
101 'contact_id' => $q->contact_id
,
103 'status' => 'Removed',
105 'tracking' => $ue->id
,
107 CRM_Contact_BAO_SubscriptionHistory
::create($shParams);
109 $transaction->commit();
115 * Unsubscribe a contact from all groups that received this mailing.
119 * @param int $queue_id
120 * The Queue Event ID of the recipient.
121 * @param string $hash
123 * @param bool $return
124 * If true return the list of groups.
127 * $groups Array of all groups from which the contact was removed, or null if the queue event could not be found.
129 * @throws \CiviCRM_API3_Exception
131 public static function &unsub_from_mailing($job_id, $queue_id, $hash, $return = FALSE) {
132 // First make sure there's a matching queue event.
134 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
140 $contact_id = $q->contact_id
;
141 $transaction = new CRM_Core_Transaction();
143 $do = new CRM_Core_DAO();
144 $mgObject = new CRM_Mailing_DAO_MailingGroup();
145 $mg = $mgObject->getTableName();
146 $jobObject = new CRM_Mailing_BAO_MailingJob();
147 $job = $jobObject->getTableName();
148 $mailingObject = new CRM_Mailing_BAO_Mailing();
149 $mailing = $mailingObject->getTableName();
150 $groupObject = new CRM_Contact_BAO_Group();
151 $group = $groupObject->getTableName();
152 $gcObject = new CRM_Contact_BAO_GroupContact();
153 $gc = $gcObject->getTableName();
154 $abObject = new CRM_Mailing_DAO_MailingAB();
155 $ab = $abObject->getTableName();
157 $mailing_id = civicrm_api3('MailingJob', 'getvalue', ['id' => $job_id, 'return' => 'mailing_id']);
158 $mailing_type = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing', $mailing_id, 'mailing_type', 'id');
159 $entity = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_MailingGroup', $mailing_id, 'entity_table', 'mailing_id');
161 // If $entity is null and $mailing_Type is either winner or experiment then we are deailing with an AB test
162 $abtest_types = ['experiment', 'winner'];
163 if (empty($entity) && in_array($mailing_type, $abtest_types)) {
164 $mailing_id_a = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_MailingAB', $mailing_id, 'mailing_id_a', 'mailing_id_b');
165 $field = 'mailing_id_b';
166 if (empty($mailing_id_a)) {
167 $mailing_id_a = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_MailingAB', $mailing_id, 'mailing_id_a', 'mailing_id_c');
168 $field = 'mailing_id_c';
170 $jobJoin = "INNER JOIN $ab ON $ab.mailing_id_a = $mg.mailing_id
171 INNER JOIN $job ON $job.mailing_id = $ab.$field";
172 $entity = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_MailingGroup', $mailing_id_a, 'entity_table', 'mailing_id');
175 $jobJoin = "INNER JOIN $job ON $job.mailing_id = $mg.mailing_id";
179 if ($entity == $group) {
180 $groupClause = "AND $group.is_hidden = 0";
184 SELECT $mg.entity_table as entity_table,
185 $mg.entity_id as entity_id,
186 $mg.group_type as group_type
190 ON $mg.entity_id = $entity.id
191 WHERE $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer') . "
192 AND $mg.group_type IN ('Include', 'Base') $groupClause"
195 // Make a list of groups and a list of prior mailings that received
202 while ($do->fetch()) {
203 if ($do->entity_table
== $group) {
204 if ($do->group_type
== 'Base') {
205 $base_groups[$do->entity_id
] = NULL;
208 $groups[$do->entity_id
] = NULL;
211 elseif ($do->entity_table
== $mailing) {
212 $mailings[] = $do->entity_id
;
216 // As long as we have prior mailings, find their groups and add to the
219 while (!empty($mailings)) {
220 $do = CRM_Core_DAO
::executeQuery("
221 SELECT $mg.entity_table as entity_table,
222 $mg.entity_id as entity_id
223 FROM civicrm_mailing_group $mg
224 WHERE $mg.mailing_id IN (" . implode(', ', $mailings) . ")
225 AND $mg.group_type = 'Include'");
229 while ($do->fetch()) {
230 if ($do->entity_table
== $group) {
231 $groups[$do->entity_id
] = TRUE;
233 elseif ($do->entity_table
== $mailing) {
234 $mailings[] = $do->entity_id
;
239 //Pass the groups to be unsubscribed from through a hook.
240 $groupIds = array_keys($groups);
241 //include child groups if any
242 $groupIds = array_merge($groupIds, CRM_Contact_BAO_Group
::getChildGroupIds($groupIds));
244 $baseGroupIds = array_keys($base_groups);
245 CRM_Utils_Hook
::unsubscribeGroups('unsubscribe', $mailing_id, $contact_id, $groupIds, $baseGroupIds);
247 // Now we have a complete list of recipient groups. Filter out all
248 // those except smart groups, those that the contact belongs to and
249 // base groups from search based mailings.
250 $baseGroupClause = '';
251 if (!empty($baseGroupIds)) {
252 $baseGroupClause = "OR $group.id IN(" . implode(', ', $baseGroupIds) . ")";
255 if ($groupIds ||
$baseGroupIds) {
256 $groupIdClause = "AND $group.id IN (" . implode(', ', array_merge($groupIds, $baseGroupIds)) . ")";
258 $do = CRM_Core_DAO
::executeQuery("
259 SELECT $group.id as group_id,
260 $group.title as title,
261 $group.description as description
262 FROM civicrm_group $group
263 LEFT JOIN civicrm_group_contact $gc
264 ON $gc.group_id = $group.id
265 WHERE $group.is_hidden = 0
267 AND ($group.saved_search_id is not null
268 OR ($gc.contact_id = $contact_id
269 AND $gc.status = 'Added')
275 while ($do->fetch()) {
276 $returnGroups[$do->group_id
] = [
277 'title' => $do->title
,
278 'description' => $do->description
,
281 return $returnGroups;
284 while ($do->fetch()) {
285 $groups[$do->group_id
] = $do->title
;
289 $contacts = [$contact_id];
290 foreach ($groups as $group_id => $group_name) {
293 if (in_array($group_id, $baseGroupIds)) {
294 list($total, $removed, $notremoved) = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contacts, $group_id, 'Email', 'Removed');
297 list($total, $removed, $notremoved) = CRM_Contact_BAO_GroupContact
::removeContactsFromGroup($contacts, $group_id, 'Email');
301 unset($groups[$group_id]);
305 $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
306 $ue->event_queue_id
= $queue_id;
307 $ue->org_unsubscribe
= 0;
308 $ue->time_stamp
= date('YmdHis');
311 $transaction->commit();
316 * Send a response email informing the contact of the groups from which he.
317 * has been unsubscribed.
319 * @param string $queue_id
320 * The queue event ID.
321 * @param array $groups
323 * @param bool $is_domain
324 * Is this domain-level?.
328 public static function send_unsub_response($queue_id, $groups, $is_domain = FALSE, $job) {
329 $config = CRM_Core_Config
::singleton();
330 $domain = CRM_Core_BAO_Domain
::getDomain();
332 $jobObject = new CRM_Mailing_BAO_MailingJob();
333 $jobTable = $jobObject->getTableName();
334 $mailingObject = new CRM_Mailing_DAO_Mailing();
335 $mailingTable = $mailingObject->getTableName();
336 $contactsObject = new CRM_Contact_DAO_Contact();
337 $contacts = $contactsObject->getTableName();
338 $emailObject = new CRM_Core_DAO_Email();
339 $email = $emailObject->getTableName();
340 $queueObject = new CRM_Mailing_Event_BAO_Queue();
341 $queue = $queueObject->getTableName();
343 //get the default domain email address.
344 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain
::getNameAndEmail();
346 $dao = new CRM_Mailing_BAO_Mailing();
347 $dao->query(" SELECT * FROM $mailingTable
348 INNER JOIN $jobTable ON
349 $jobTable.mailing_id = $mailingTable.id
350 WHERE $jobTable.id = $job");
353 $component = new CRM_Mailing_BAO_MailingComponent();
356 $component->id
= $dao->optout_id
;
359 $component->id
= $dao->unsubscribe_id
;
361 $component->find(TRUE);
363 $html = $component->body_html
;
364 if ($component->body_text
) {
365 $text = $component->body_text
;
368 $text = CRM_Utils_String
::htmlToText($component->body_html
);
371 $eq = new CRM_Core_DAO();
373 "SELECT $contacts.preferred_mail_format as format,
374 $contacts.id as contact_id,
375 $email.email as email,
378 INNER JOIN $queue ON $queue.contact_id = $contacts.id
379 INNER JOIN $email ON $queue.email_id = $email.id
380 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
385 foreach ($groups as $key => $value) {
387 unset($groups[$key]);
392 $message = new Mail_mime("\n");
394 list($addresses, $urls) = CRM_Mailing_BAO_Mailing
::getVerpAndUrls($job, $queue_id, $eq->hash
, $eq->email
);
395 $bao = new CRM_Mailing_BAO_Mailing();
396 $bao->body_text
= $text;
397 $bao->body_html
= $html;
398 $tokens = $bao->getTokens();
399 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
400 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
401 $html = CRM_Utils_Token
::replaceUnsubscribeTokens($html, $domain, $groups, TRUE, $eq->contact_id
, $eq->hash
);
402 $html = CRM_Utils_Token
::replaceActionTokens($html, $addresses, $urls, TRUE, $tokens['html']);
403 $html = CRM_Utils_Token
::replaceMailingTokens($html, $dao, NULL, $tokens['html']);
404 $message->setHTMLBody($html);
406 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
407 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
408 $text = CRM_Utils_Token
::replaceUnsubscribeTokens($text, $domain, $groups, FALSE, $eq->contact_id
, $eq->hash
);
409 $text = CRM_Utils_Token
::replaceActionTokens($text, $addresses, $urls, FALSE, $tokens['text']);
410 $text = CRM_Utils_Token
::replaceMailingTokens($text, $dao, NULL, $tokens['text']);
411 $message->setTxtBody($text);
414 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
417 'Subject' => $component->subject
,
418 'From' => "\"$domainEmailName\" <" . CRM_Core_BAO_Domain
::getNoReplyEmailAddress() . '>',
420 'Reply-To' => CRM_Core_BAO_Domain
::getNoReplyEmailAddress(),
421 'Return-Path' => CRM_Core_BAO_Domain
::getNoReplyEmailAddress(),
423 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($headers, 'u', $job, $queue_id, $eq->hash
);
425 $b = CRM_Utils_Mail
::setMimeParams($message);
426 $h = $message->headers($headers);
428 $mailer = \Civi
::service('pear_mail');
430 if (is_object($mailer)) {
431 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
432 $mailer->send($eq->email
, $h, $b);
438 * Get row count for the event selector.
440 * @param int $mailing_id
443 * Optional ID of a job to filter on.
444 * @param bool $is_distinct
445 * Group by queue ID?.
447 * @param string $org_unsubscribe
449 * @param string $toDate
452 * Number of rows in result set
454 public static function getTotalCount(
455 $mailing_id, $job_id = NULL,
456 $is_distinct = FALSE, $org_unsubscribe = NULL, $toDate = NULL
458 $dao = new CRM_Core_DAO();
460 $unsub = self
::$_tableName;
461 $queueObject = new CRM_Mailing_Event_BAO_Queue();
462 $queue = $queueObject->getTableName();
463 $mailingObject = new CRM_Mailing_BAO_Mailing();
464 $mailing = $mailingObject->getTableName();
465 $jobObject = new CRM_Mailing_BAO_MailingJob();
466 $job = $jobObject->getTableName();
469 SELECT COUNT($unsub.id) as unsubs
472 ON $unsub.event_queue_id = $queue.id
474 ON $queue.job_id = $job.id
476 ON $job.mailing_id = $mailing.id
478 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
480 if (!empty($toDate)) {
481 $query .= " AND $unsub.time_stamp <= $toDate";
484 if (!empty($job_id)) {
485 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
488 if ($org_unsubscribe !== NULL) {
489 $query .= " AND $unsub.org_unsubscribe = " . ($org_unsubscribe ?
0 : 1);
493 $query .= " GROUP BY $queue.id ";
502 return $dao->unsubs ?
$dao->unsubs
: 0;
507 * Get rows for the event browser.
509 * @param int $mailing_id
512 * Optional ID of the job.
513 * @param bool $is_distinct
514 * Group by queue id?.
517 * @param int $rowCount
522 * @param null $org_unsubscribe
526 public static function &getRows(
527 $mailing_id, $job_id = NULL,
528 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL,
529 $org_unsubscribe = NULL
532 $dao = new CRM_Core_DAO();
534 $unsub = self
::$_tableName;
535 $queueObject = new CRM_Mailing_Event_BAO_Queue();
536 $queue = $queueObject->getTableName();
537 $mailingObject = new CRM_Mailing_BAO_Mailing();
538 $mailing = $mailingObject->getTableName();
539 $jobObject = new CRM_Mailing_BAO_MailingJob();
540 $job = $jobObject->getTableName();
541 $contactObject = new CRM_Contact_BAO_Contact();
542 $contact = $contactObject->getTableName();
543 $emailObject = new CRM_Core_BAO_Email();
544 $email = $emailObject->getTableName();
547 SELECT $contact.display_name as display_name,
548 $contact.id as contact_id,
549 $email.email as email,
550 $unsub.time_stamp as date,
551 $unsub.org_unsubscribe as org_unsubscribe
554 ON $queue.contact_id = $contact.id
556 ON $queue.email_id = $email.id
558 ON $unsub.event_queue_id = $queue.id
560 ON $queue.job_id = $job.id
562 ON $job.mailing_id = $mailing.id
564 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
566 if (!empty($job_id)) {
567 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
570 if ($org_unsubscribe !== NULL) {
571 $query .= " AND $unsub.org_unsubscribe = " . ($org_unsubscribe ?
0 : 1);
575 $query .= " GROUP BY $queue.id, $unsub.time_stamp, $unsub.org_unsubscribe";
578 $orderBy = "sort_name ASC, {$unsub}.time_stamp DESC";
580 if (is_string($sort)) {
581 $sort = CRM_Utils_Type
::escape($sort, 'String');
585 $orderBy = trim($sort->orderBy());
589 $query .= " ORDER BY {$orderBy} ";
591 if ($offset ||
$rowCount) {
592 //Added "||$rowCount" to avoid displaying all records on first page
593 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
600 while ($dao->fetch()) {
601 $url = CRM_Utils_System
::url('civicrm/contact/view',
602 "reset=1&cid={$dao->contact_id}"
605 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
606 'email' => $dao->email
,
607 // Next value displays in selector under either Unsubscribe OR Optout column header, so always s/b Yes.
608 'unsubOrOptout' => ts('Yes'),
609 'date' => CRM_Utils_Date
::customFormat($dao->date
),
616 * @param int $queueID
620 public static function getContactInfo($queueID) {
622 SELECT DISTINCT(civicrm_mailing_event_queue.contact_id) as contact_id,
623 civicrm_contact.display_name as display_name
624 civicrm_email.email as email
625 FROM civicrm_mailing_event_queue,
628 WHERE civicrm_mailing_event_queue.contact_id = civicrm_contact.id
629 AND civicrm_mailing_event_queue.email_id = civicrm_email.id
630 AND civicrm_mailing_event_queue.id = " . CRM_Utils_Type
::escape($queueID, 'Integer');
632 $dao = CRM_Core_DAO
::executeQuery($query);
634 $displayName = 'Unknown';
637 $displayName = $dao->display_name
;
638 $email = $dao->email
;
641 return [$displayName, $email];