3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 require_once 'Mail/mime.php';
21 * Class CRM_Mailing_Event_BAO_Resubscribe
23 class CRM_Mailing_Event_BAO_Resubscribe
{
26 * Resubscribe a contact to the groups, he/she was unsubscribed from.
30 * @param int $queue_id
31 * The Queue Event ID of the recipient.
36 * $groups Array of all groups to which the contact was added, or null if the queue event could not be found.
38 public static function &resub_to_mailing($job_id, $queue_id, $hash) {
39 // First make sure there's a matching queue event.
41 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
47 // check if this queue_id was actually unsubscribed
48 $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
49 $ue->event_queue_id
= $queue_id;
50 $ue->org_unsubscribe
= 0;
51 if (!$ue->find(TRUE)) {
55 $contact_id = $q->contact_id
;
57 $transaction = new CRM_Core_Transaction();
59 $do = new CRM_Core_DAO();
60 $mg = CRM_Mailing_DAO_MailingGroup
::getTableName();
61 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
62 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
63 $group = CRM_Contact_BAO_Group
::getTableName();
64 $gc = CRM_Contact_BAO_GroupContact
::getTableName();
66 // We Need the mailing Id for the hook...
67 $do->query("SELECT $job.mailing_id as mailing_id
69 WHERE $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer'));
71 $mailing_id = $do->mailing_id
;
74 SELECT $mg.entity_table as entity_table,
75 $mg.entity_id as entity_id
78 ON $job.mailing_id = $mg.mailing_id
80 ON $mg.entity_id = $group.id
81 WHERE $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer') . "
82 AND $mg.group_type IN ( 'Include', 'Base' )
83 AND $group.is_hidden = 0"
86 // Make a list of groups and a list of prior mailings that received
91 while ($do->fetch()) {
92 if ($do->entity_table
== $group) {
93 $groups[$do->entity_id
] = NULL;
95 elseif ($do->entity_table
== $mailing) {
96 $mailings[] = $do->entity_id
;
100 // As long as we have prior mailings, find their groups and add to the
102 while (!empty($mailings)) {
104 SELECT $mg.entity_table as entity_table,
105 $mg.entity_id as entity_id
107 WHERE $mg.mailing_id IN (" . implode(', ', $mailings) . ")
108 AND $mg.group_type = 'Include'");
112 while ($do->fetch()) {
113 if ($do->entity_table
== $group) {
114 $groups[$do->entity_id
] = TRUE;
116 elseif ($do->entity_table
== $mailing) {
117 $mailings[] = $do->entity_id
;
122 $group_ids = array_keys($groups);
124 CRM_Utils_Hook
::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups);
126 // Now we have a complete list of recipient groups. Filter out all
127 // those except smart groups and those that the contact belongs to.
129 SELECT $group.id as group_id,
130 $group.title as title
133 ON $gc.group_id = $group.id
134 WHERE $group.id IN (" . implode(', ', $group_ids) . ")
135 AND ($group.saved_search_id is not null
136 OR ($gc.contact_id = $contact_id
137 AND $gc.status = 'Removed')
140 while ($do->fetch()) {
141 $groups[$do->group_id
] = $do->title
;
144 $contacts = [$contact_id];
145 foreach ($groups as $group_id => $group_name) {
148 list($total, $added, $notadded) = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contacts, $group_id, 'Email');
151 unset($groups[$group_id]);
155 // remove entry from Unsubscribe table.
156 $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
157 $ue->event_queue_id
= $queue_id;
158 $ue->org_resubscribe
= 0;
159 if ($ue->find(TRUE)) {
163 $transaction->commit();
168 * Send a response email informing the contact of the groups to which he/she
169 * has been resubscribed.
171 * @param string $queue_id
172 * The queue event ID.
173 * @param array $groups
178 public static function send_resub_response($queue_id, $groups, $job) {
179 // param is_domain is not supported as of now.
181 $config = CRM_Core_Config
::singleton();
182 $domain = CRM_Core_BAO_Domain
::getDomain();
184 $jobTable = CRM_Mailing_BAO_MailingJob
::getTableName();
185 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
186 $contacts = CRM_Contact_DAO_Contact
::getTableName();
187 $email = CRM_Core_DAO_Email
::getTableName();
188 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
190 //get the default domain email address.
191 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain
::getNameAndEmail();
193 $dao = new CRM_Mailing_BAO_Mailing();
194 $dao->query(" SELECT * FROM $mailingTable
195 INNER JOIN $jobTable ON
196 $jobTable.mailing_id = $mailingTable.id
197 WHERE $jobTable.id = $job");
200 $component = new CRM_Mailing_BAO_MailingComponent();
201 $component->id
= $dao->resubscribe_id
;
202 $component->find(TRUE);
204 $html = $component->body_html
;
205 if ($component->body_text
) {
206 $text = $component->body_text
;
209 $text = CRM_Utils_String
::htmlToText($component->body_html
);
212 $eq = new CRM_Core_DAO();
214 "SELECT $contacts.preferred_mail_format as format,
215 $contacts.id as contact_id,
216 $email.email as email,
219 INNER JOIN $queue ON $queue.contact_id = $contacts.id
220 INNER JOIN $email ON $queue.email_id = $email.id
221 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
224 foreach ($groups as $key => $value) {
226 unset($groups[$key]);
230 list($addresses, $urls) = CRM_Mailing_BAO_Mailing
::getVerpAndUrls($job, $queue_id, $eq->hash
, $eq->email
);
231 $bao = new CRM_Mailing_BAO_Mailing();
232 $bao->body_text
= $text;
233 $bao->body_html
= $html;
234 $tokens = $bao->getTokens();
235 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
236 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
237 $html = CRM_Utils_Token
::replaceResubscribeTokens($html, $domain, $groups, TRUE, $eq->contact_id
, $eq->hash
);
238 $html = CRM_Utils_Token
::replaceActionTokens($html, $addresses, $urls, TRUE, $tokens['html']);
239 $html = CRM_Utils_Token
::replaceMailingTokens($html, $dao, NULL, $tokens['html']);
241 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
242 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, TRUE, $tokens['text']);
243 $text = CRM_Utils_Token
::replaceResubscribeTokens($text, $domain, $groups, FALSE, $eq->contact_id
, $eq->hash
);
244 $text = CRM_Utils_Token
::replaceActionTokens($text, $addresses, $urls, FALSE, $tokens['text']);
245 $text = CRM_Utils_Token
::replaceMailingTokens($text, $dao, NULL, $tokens['text']);
249 'subject' => $component->subject
,
250 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
251 'toEmail' => $eq->email
,
252 'replyTo' => CRM_Core_BAO_Domain
::getNoReplyEmailAddress(),
253 'returnPath' => CRM_Core_BAO_Domain
::getNoReplyEmailAddress(),
257 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($params, 'e', $job, $queue_id, $eq->hash
);
258 if (CRM_Core_BAO_MailSettings
::includeMessageId()) {
259 $params['messageId'] = $params['Message-ID'];
261 CRM_Utils_Mail
::send($params);