3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
36 require_once 'Mail/mime.php';
39 * Class CRM_Mailing_Event_BAO_Resubscribe
41 class CRM_Mailing_Event_BAO_Resubscribe
{
44 * Resubscribe a contact to the groups, he/she was unsubscribed from.
48 * @param int $queue_id
49 * The Queue Event ID of the recipient.
54 * $groups Array of all groups to which the contact was added, or null if the queue event could not be found.
56 public static function &resub_to_mailing($job_id, $queue_id, $hash) {
57 /* First make sure there's a matching queue event */
59 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
65 // check if this queue_id was actually unsubscribed
66 $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
67 $ue->event_queue_id
= $queue_id;
68 $ue->org_unsubscribe
= 0;
69 if (!$ue->find(TRUE)) {
73 $contact_id = $q->contact_id
;
75 $transaction = new CRM_Core_Transaction();
77 $do = new CRM_Core_DAO();
78 $mg = CRM_Mailing_DAO_MailingGroup
::getTableName();
79 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
80 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
81 $group = CRM_Contact_BAO_Group
::getTableName();
82 $gc = CRM_Contact_BAO_GroupContact
::getTableName();
84 //We Need the mailing Id for the hook...
85 $do->query("SELECT $job.mailing_id as mailing_id
87 WHERE $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer'));
89 $mailing_id = $do->mailing_id
;
92 SELECT $mg.entity_table as entity_table,
93 $mg.entity_id as entity_id
96 ON $job.mailing_id = $mg.mailing_id
98 ON $mg.entity_id = $group.id
99 WHERE $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer') . "
100 AND $mg.group_type IN ( 'Include', 'Base' )
101 AND $group.is_hidden = 0"
104 /* Make a list of groups and a list of prior mailings that received
110 while ($do->fetch()) {
111 if ($do->entity_table
== $group) {
112 $groups[$do->entity_id
] = NULL;
114 elseif ($do->entity_table
== $mailing) {
115 $mailings[] = $do->entity_id
;
119 /* As long as we have prior mailings, find their groups and add to the
122 while (!empty($mailings)) {
124 SELECT $mg.entity_table as entity_table,
125 $mg.entity_id as entity_id
127 WHERE $mg.mailing_id IN (" . implode(', ', $mailings) . ")
128 AND $mg.group_type = 'Include'");
132 while ($do->fetch()) {
133 if ($do->entity_table
== $group) {
134 $groups[$do->entity_id
] = TRUE;
136 elseif ($do->entity_table
== $mailing) {
137 $mailings[] = $do->entity_id
;
142 $group_ids = array_keys($groups);
144 CRM_Utils_Hook
::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups);
146 /* Now we have a complete list of recipient groups. Filter out all
147 * those except smart groups and those that the contact belongs to */
150 SELECT $group.id as group_id,
151 $group.title as title
154 ON $gc.group_id = $group.id
155 WHERE $group.id IN (" . implode(', ', $group_ids) . ")
156 AND ($group.saved_search_id is not null
157 OR ($gc.contact_id = $contact_id
158 AND $gc.status = 'Removed')
161 while ($do->fetch()) {
162 $groups[$do->group_id
] = $do->title
;
165 $contacts = array($contact_id);
166 foreach ($groups as $group_id => $group_name) {
169 list($total, $added, $notadded) = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contacts, $group_id, 'Email');
172 unset($groups[$group_id]);
176 // remove entry from Unsubscribe table.
177 $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
178 $ue->event_queue_id
= $queue_id;
179 $ue->org_resubscribe
= 0;
180 if ($ue->find(TRUE)) {
184 $transaction->commit();
189 * Send a response email informing the contact of the groups to which he/she
190 * has been resubscribed.
192 * @param string $queue_id
193 * The queue event ID.
194 * @param array $groups
196 * @param bool $is_domain
197 * Is this domain-level?.
203 public static function send_resub_response($queue_id, $groups, $is_domain = FALSE, $job) {
204 // param is_domain is not supported as of now.
206 $config = CRM_Core_Config
::singleton();
207 $domain = CRM_Core_BAO_Domain
::getDomain();
209 $jobTable = CRM_Mailing_BAO_MailingJob
::getTableName();
210 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
211 $contacts = CRM_Contact_DAO_Contact
::getTableName();
212 $email = CRM_Core_DAO_Email
::getTableName();
213 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
215 //get the default domain email address.
216 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain
::getNameAndEmail();
218 $dao = new CRM_Mailing_BAO_Mailing();
219 $dao->query(" SELECT * FROM $mailingTable
220 INNER JOIN $jobTable ON
221 $jobTable.mailing_id = $mailingTable.id
222 WHERE $jobTable.id = $job");
225 $component = new CRM_Mailing_BAO_Component();
226 $component->id
= $dao->resubscribe_id
;
227 $component->find(TRUE);
229 $html = $component->body_html
;
230 if ($component->body_text
) {
231 $text = $component->body_text
;
234 $text = CRM_Utils_String
::htmlToText($component->body_html
);
237 $eq = new CRM_Core_DAO();
239 "SELECT $contacts.preferred_mail_format as format,
240 $contacts.id as contact_id,
241 $email.email as email,
244 INNER JOIN $queue ON $queue.contact_id = $contacts.id
245 INNER JOIN $email ON $queue.email_id = $email.id
246 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
249 foreach ($groups as $key => $value) {
251 unset($groups[$key]);
255 $message = new Mail_mime("\n");
257 list($addresses, $urls) = CRM_Mailing_BAO_Mailing
::getVerpAndUrls($job, $queue_id, $eq->hash
, $eq->email
);
258 $bao = new CRM_Mailing_BAO_Mailing();
259 $bao->body_text
= $text;
260 $bao->body_html
= $html;
261 $tokens = $bao->getTokens();
262 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
263 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
264 $html = CRM_Utils_Token
::replaceResubscribeTokens($html, $domain, $groups, TRUE, $eq->contact_id
, $eq->hash
);
265 $html = CRM_Utils_Token
::replaceActionTokens($html, $addresses, $urls, TRUE, $tokens['html']);
266 $html = CRM_Utils_Token
::replaceMailingTokens($html, $dao, NULL, $tokens['html']);
267 $message->setHTMLBody($html);
269 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
270 $text = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['text']);
271 $text = CRM_Utils_Token
::replaceResubscribeTokens($text, $domain, $groups, FALSE, $eq->contact_id
, $eq->hash
);
272 $text = CRM_Utils_Token
::replaceActionTokens($text, $addresses, $urls, FALSE, $tokens['text']);
273 $text = CRM_Utils_Token
::replaceMailingTokens($text, $dao, NULL, $tokens['text']);
274 $message->setTxtBody($text);
277 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
280 'Subject' => $component->subject
,
281 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>",
283 'Reply-To' => "do-not-reply@$emailDomain",
284 'Return-Path' => "do-not-reply@$emailDomain",
286 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($headers, 'e', $job, $queue_id, $eq->hash
);
287 $b = CRM_Utils_Mail
::setMimeParams($message);
288 $h = $message->headers($headers);
290 $mailer = \Civi\Core\Container
::singleton()->get('pear_mail');
292 if (is_object($mailer)) {
293 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
294 $mailer->send($eq->email
, $h, $b);