3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
36 require_once 'Mail/mime.php';
37 class CRM_Mailing_Event_BAO_Resubscribe
{
40 * Resubscribe a contact to the groups, he/she was unsubscribed from.
42 * @param int $job_id The job ID
43 * @param int $queue_id The Queue Event ID of the recipient
44 * @param string $hash The hash
46 * @return array|null $groups Array of all groups to which the contact was added, or null if the queue event could not be found.
50 public static function &resub_to_mailing($job_id, $queue_id, $hash) {
51 /* First make sure there's a matching queue event */
53 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
59 // check if this queue_id was actually unsubscribed
60 $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
61 $ue->event_queue_id
= $queue_id;
62 $ue->org_unsubscribe
= 0;
63 if (!$ue->find(TRUE)) {
67 $contact_id = $q->contact_id
;
69 $transaction = new CRM_Core_Transaction();
71 $do = new CRM_Core_DAO();
72 $mg = CRM_Mailing_DAO_MailingGroup
::getTableName();
73 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
74 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
75 $group = CRM_Contact_BAO_Group
::getTableName();
76 $gc = CRM_Contact_BAO_GroupContact
::getTableName();
78 //We Need the mailing Id for the hook...
79 $do->query("SELECT $job.mailing_id as mailing_id
81 WHERE $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer'));
83 $mailing_id = $do->mailing_id
;
86 SELECT $mg.entity_table as entity_table,
87 $mg.entity_id as entity_id
90 ON $job.mailing_id = $mg.mailing_id
92 ON $mg.entity_id = $group.id
93 WHERE $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer') . "
94 AND $mg.group_type IN ( 'Include', 'Base' )
95 AND $group.is_hidden = 0"
98 /* Make a list of groups and a list of prior mailings that received
105 while ($do->fetch()) {
106 if ($do->entity_table
== $group) {
107 $groups[$do->entity_id
] = NULL;
109 elseif ($do->entity_table
== $mailing) {
110 $mailings[] = $do->entity_id
;
114 /* As long as we have prior mailings, find their groups and add to the
117 while (!empty($mailings)) {
119 SELECT $mg.entity_table as entity_table,
120 $mg.entity_id as entity_id
122 WHERE $mg.mailing_id IN (" . implode(', ', $mailings) . ")
123 AND $mg.group_type = 'Include'");
127 while ($do->fetch()) {
128 if ($do->entity_table
== $group) {
129 $groups[$do->entity_id
] = TRUE;
131 elseif ($do->entity_table
== $mailing) {
132 $mailings[] = $do->entity_id
;
137 $group_ids = array_keys($groups);
139 CRM_Utils_Hook
::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups);
141 /* Now we have a complete list of recipient groups. Filter out all
142 * 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 reponse email informing the contact of the groups to which he/she
185 * has been resubscribed.
187 * @param string $queue_id The queue event ID
188 * @param array $groups List of group IDs
189 * @param bool $is_domain Is this domain-level?
190 * @param int $job The job ID
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($html, $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 = $config->getMailer();
285 if (is_object($mailer)) {
286 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
287 $mailer->send($eq->email
, $h, $b);