3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 (c) 2004-2018
34 require_once 'Mail/mime.php';
37 * Class CRM_Mailing_Event_BAO_Resubscribe
39 class CRM_Mailing_Event_BAO_Resubscribe
{
42 * Resubscribe a contact to the groups, he/she was unsubscribed from.
46 * @param int $queue_id
47 * The Queue Event ID of the recipient.
52 * $groups Array of all groups to which the contact was added, or null if the queue event could not be found.
54 public static function &resub_to_mailing($job_id, $queue_id, $hash) {
55 // First make sure there's a matching queue event.
57 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
63 // check if this queue_id was actually unsubscribed
64 $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
65 $ue->event_queue_id
= $queue_id;
66 $ue->org_unsubscribe
= 0;
67 if (!$ue->find(TRUE)) {
71 $contact_id = $q->contact_id
;
73 $transaction = new CRM_Core_Transaction();
75 $do = new CRM_Core_DAO();
76 $mg = CRM_Mailing_DAO_MailingGroup
::getTableName();
77 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
78 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
79 $group = CRM_Contact_BAO_Group
::getTableName();
80 $gc = CRM_Contact_BAO_GroupContact
::getTableName();
82 // We Need the mailing Id for the hook...
83 $do->query("SELECT $job.mailing_id as mailing_id
85 WHERE $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer'));
87 $mailing_id = $do->mailing_id
;
90 SELECT $mg.entity_table as entity_table,
91 $mg.entity_id as entity_id
94 ON $job.mailing_id = $mg.mailing_id
96 ON $mg.entity_id = $group.id
97 WHERE $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer') . "
98 AND $mg.group_type IN ( 'Include', 'Base' )
99 AND $group.is_hidden = 0"
102 // Make a list of groups and a list of prior mailings that received
107 while ($do->fetch()) {
108 if ($do->entity_table
== $group) {
109 $groups[$do->entity_id
] = NULL;
111 elseif ($do->entity_table
== $mailing) {
112 $mailings[] = $do->entity_id
;
116 // As long as we have prior mailings, find their groups and add to the
118 while (!empty($mailings)) {
120 SELECT $mg.entity_table as entity_table,
121 $mg.entity_id as entity_id
123 WHERE $mg.mailing_id IN (" . implode(', ', $mailings) . ")
124 AND $mg.group_type = 'Include'");
128 while ($do->fetch()) {
129 if ($do->entity_table
== $group) {
130 $groups[$do->entity_id
] = TRUE;
132 elseif ($do->entity_table
== $mailing) {
133 $mailings[] = $do->entity_id
;
138 $group_ids = array_keys($groups);
140 CRM_Utils_Hook
::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups);
142 // Now we have a complete list of recipient groups. Filter out all
143 // those except smart groups and those that the contact belongs to.
145 SELECT $group.id as group_id,
146 $group.title as title
149 ON $gc.group_id = $group.id
150 WHERE $group.id IN (" . implode(', ', $group_ids) . ")
151 AND ($group.saved_search_id is not null
152 OR ($gc.contact_id = $contact_id
153 AND $gc.status = 'Removed')
156 while ($do->fetch()) {
157 $groups[$do->group_id
] = $do->title
;
160 $contacts = array($contact_id);
161 foreach ($groups as $group_id => $group_name) {
164 list($total, $added, $notadded) = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contacts, $group_id, 'Email');
167 unset($groups[$group_id]);
171 // remove entry from Unsubscribe table.
172 $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
173 $ue->event_queue_id
= $queue_id;
174 $ue->org_resubscribe
= 0;
175 if ($ue->find(TRUE)) {
179 $transaction->commit();
184 * Send a response email informing the contact of the groups to which he/she
185 * has been resubscribed.
187 * @param string $queue_id
188 * The queue event ID.
189 * @param array $groups
191 * @param bool $is_domain
192 * Is this domain-level?.
196 public static function send_resub_response($queue_id, $groups, $is_domain = FALSE, $job) {
197 // param is_domain is not supported as of now.
199 $config = CRM_Core_Config
::singleton();
200 $domain = CRM_Core_BAO_Domain
::getDomain();
202 $jobTable = CRM_Mailing_BAO_MailingJob
::getTableName();
203 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
204 $contacts = CRM_Contact_DAO_Contact
::getTableName();
205 $email = CRM_Core_DAO_Email
::getTableName();
206 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
208 //get the default domain email address.
209 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain
::getNameAndEmail();
211 $dao = new CRM_Mailing_BAO_Mailing();
212 $dao->query(" SELECT * FROM $mailingTable
213 INNER JOIN $jobTable ON
214 $jobTable.mailing_id = $mailingTable.id
215 WHERE $jobTable.id = $job");
218 $component = new CRM_Mailing_BAO_Component();
219 $component->id
= $dao->resubscribe_id
;
220 $component->find(TRUE);
222 $html = $component->body_html
;
223 if ($component->body_text
) {
224 $text = $component->body_text
;
227 $text = CRM_Utils_String
::htmlToText($component->body_html
);
230 $eq = new CRM_Core_DAO();
232 "SELECT $contacts.preferred_mail_format as format,
233 $contacts.id as contact_id,
234 $email.email as email,
237 INNER JOIN $queue ON $queue.contact_id = $contacts.id
238 INNER JOIN $email ON $queue.email_id = $email.id
239 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
242 foreach ($groups as $key => $value) {
244 unset($groups[$key]);
248 $message = new Mail_mime("\n");
250 list($addresses, $urls) = CRM_Mailing_BAO_Mailing
::getVerpAndUrls($job, $queue_id, $eq->hash
, $eq->email
);
251 $bao = new CRM_Mailing_BAO_Mailing();
252 $bao->body_text
= $text;
253 $bao->body_html
= $html;
254 $tokens = $bao->getTokens();
255 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
256 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
257 $html = CRM_Utils_Token
::replaceResubscribeTokens($html, $domain, $groups, TRUE, $eq->contact_id
, $eq->hash
);
258 $html = CRM_Utils_Token
::replaceActionTokens($html, $addresses, $urls, TRUE, $tokens['html']);
259 $html = CRM_Utils_Token
::replaceMailingTokens($html, $dao, NULL, $tokens['html']);
260 $message->setHTMLBody($html);
262 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
263 $text = CRM_Utils_Token
::replaceDomainTokens($text, $domain, TRUE, $tokens['text']);
264 $text = CRM_Utils_Token
::replaceResubscribeTokens($text, $domain, $groups, FALSE, $eq->contact_id
, $eq->hash
);
265 $text = CRM_Utils_Token
::replaceActionTokens($text, $addresses, $urls, FALSE, $tokens['text']);
266 $text = CRM_Utils_Token
::replaceMailingTokens($text, $dao, NULL, $tokens['text']);
267 $message->setTxtBody($text);
270 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
273 'Subject' => $component->subject
,
274 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>",
276 'Reply-To' => "do-not-reply@$emailDomain",
277 'Return-Path' => "do-not-reply@$emailDomain",
279 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($headers, 'e', $job, $queue_id, $eq->hash
);
280 $b = CRM_Utils_Mail
::setMimeParams($message);
281 $h = $message->headers($headers);
283 $mailer = \Civi
::service('pear_mail');
285 if (is_object($mailer)) {
286 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
287 $mailer->send($eq->email
, $h, $b);