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';
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.
46 * @param int $job_id The job ID
47 * @param int $queue_id The Queue Event ID of the recipient
48 * @param string $hash The hash
50 * @return array|null $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
109 while ($do->fetch()) {
110 if ($do->entity_table
== $group) {
111 $groups[$do->entity_id
] = NULL;
113 elseif ($do->entity_table
== $mailing) {
114 $mailings[] = $do->entity_id
;
118 /* As long as we have prior mailings, find their groups and add to the
121 while (!empty($mailings)) {
123 SELECT $mg.entity_table as entity_table,
124 $mg.entity_id as entity_id
126 WHERE $mg.mailing_id IN (" . implode(', ', $mailings) . ")
127 AND $mg.group_type = 'Include'");
131 while ($do->fetch()) {
132 if ($do->entity_table
== $group) {
133 $groups[$do->entity_id
] = TRUE;
135 elseif ($do->entity_table
== $mailing) {
136 $mailings[] = $do->entity_id
;
141 $group_ids = array_keys($groups);
143 CRM_Utils_Hook
::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups);
145 /* Now we have a complete list of recipient groups. Filter out all
146 * those except smart groups and those that the contact belongs to */
149 SELECT $group.id as group_id,
150 $group.title as title
153 ON $gc.group_id = $group.id
154 WHERE $group.id IN (" . implode(', ', $group_ids) . ")
155 AND ($group.saved_search_id is not null
156 OR ($gc.contact_id = $contact_id
157 AND $gc.status = 'Removed')
160 while ($do->fetch()) {
161 $groups[$do->group_id
] = $do->title
;
164 $contacts = array($contact_id);
165 foreach ($groups as $group_id => $group_name) {
168 list($total, $added, $notadded) = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contacts, $group_id, 'Email');
171 unset($groups[$group_id]);
175 // remove entry from Unsubscribe table.
176 $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
177 $ue->event_queue_id
= $queue_id;
178 $ue->org_resubscribe
= 0;
179 if ($ue->find(TRUE)) {
183 $transaction->commit();
188 * Send a reponse email informing the contact of the groups to which he/she
189 * has been resubscribed.
191 * @param string $queue_id The queue event ID
192 * @param array $groups List of group IDs
193 * @param bool $is_domain Is this domain-level?
194 * @param int $job The job ID
200 public static function send_resub_response($queue_id, $groups, $is_domain = FALSE, $job) {
201 // param is_domain is not supported as of now.
203 $config = CRM_Core_Config
::singleton();
204 $domain = CRM_Core_BAO_Domain
::getDomain();
206 $jobTable = CRM_Mailing_BAO_MailingJob
::getTableName();
207 $mailingTable = CRM_Mailing_DAO_Mailing
::getTableName();
208 $contacts = CRM_Contact_DAO_Contact
::getTableName();
209 $email = CRM_Core_DAO_Email
::getTableName();
210 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
212 //get the default domain email address.
213 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain
::getNameAndEmail();
215 $dao = new CRM_Mailing_BAO_Mailing();
216 $dao->query(" SELECT * FROM $mailingTable
217 INNER JOIN $jobTable ON
218 $jobTable.mailing_id = $mailingTable.id
219 WHERE $jobTable.id = $job");
222 $component = new CRM_Mailing_BAO_Component();
223 $component->id
= $dao->resubscribe_id
;
224 $component->find(TRUE);
226 $html = $component->body_html
;
227 if ($component->body_text
) {
228 $text = $component->body_text
;
231 $text = CRM_Utils_String
::htmlToText($component->body_html
);
234 $eq = new CRM_Core_DAO();
236 "SELECT $contacts.preferred_mail_format as format,
237 $contacts.id as contact_id,
238 $email.email as email,
241 INNER JOIN $queue ON $queue.contact_id = $contacts.id
242 INNER JOIN $email ON $queue.email_id = $email.id
243 WHERE $queue.id = " . CRM_Utils_Type
::escape($queue_id, 'Integer')
246 foreach ($groups as $key => $value) {
248 unset($groups[$key]);
252 $message = new Mail_mime("\n");
254 list($addresses, $urls) = CRM_Mailing_BAO_Mailing
::getVerpAndUrls($job, $queue_id, $eq->hash
, $eq->email
);
255 $bao = new CRM_Mailing_BAO_Mailing();
256 $bao->body_text
= $text;
257 $bao->body_html
= $html;
258 $tokens = $bao->getTokens();
259 if ($eq->format
== 'HTML' ||
$eq->format
== 'Both') {
260 $html = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
261 $html = CRM_Utils_Token
::replaceResubscribeTokens($html, $domain, $groups, TRUE, $eq->contact_id
, $eq->hash
);
262 $html = CRM_Utils_Token
::replaceActionTokens($html, $addresses, $urls, TRUE, $tokens['html']);
263 $html = CRM_Utils_Token
::replaceMailingTokens($html, $dao, NULL, $tokens['html']);
264 $message->setHTMLBody($html);
266 if (!$html ||
$eq->format
== 'Text' ||
$eq->format
== 'Both') {
267 $text = CRM_Utils_Token
::replaceDomainTokens($html, $domain, TRUE, $tokens['text']);
268 $text = CRM_Utils_Token
::replaceResubscribeTokens($text, $domain, $groups, FALSE, $eq->contact_id
, $eq->hash
);
269 $text = CRM_Utils_Token
::replaceActionTokens($text, $addresses, $urls, FALSE, $tokens['text']);
270 $text = CRM_Utils_Token
::replaceMailingTokens($text, $dao, NULL, $tokens['text']);
271 $message->setTxtBody($text);
274 $emailDomain = CRM_Core_BAO_MailSettings
::defaultDomain();
277 'Subject' => $component->subject
,
278 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>",
280 'Reply-To' => "do-not-reply@$emailDomain",
281 'Return-Path' => "do-not-reply@$emailDomain",
283 CRM_Mailing_BAO_Mailing
::addMessageIdHeader($headers, 'e', $job, $queue_id, $eq->hash
);
284 $b = CRM_Utils_Mail
::setMimeParams($message);
285 $h = $message->headers($headers);
287 $mailer = $config->getMailer();
289 if (is_object($mailer)) {
290 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
291 $mailer->send($eq->email
, $h, $b);