Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | require_once 'Mail/mime.php'; | |
4c6ce474 EM |
37 | |
38 | /** | |
39 | * Class CRM_Mailing_Event_BAO_Resubscribe | |
40 | */ | |
6a488035 TO |
41 | class CRM_Mailing_Event_BAO_Resubscribe { |
42 | ||
43 | /** | |
44 | * Resubscribe a contact to the groups, he/she was unsubscribed from. | |
45 | * | |
90c8230e TO |
46 | * @param int $job_id |
47 | * The job ID. | |
48 | * @param int $queue_id | |
49 | * The Queue Event ID of the recipient. | |
50 | * @param string $hash | |
51 | * The hash. | |
6a488035 TO |
52 | * |
53 | * @return array|null $groups Array of all groups to which the contact was added, or null if the queue event could not be found. | |
6a488035 TO |
54 | * @static |
55 | */ | |
56 | public static function &resub_to_mailing($job_id, $queue_id, $hash) { | |
57 | /* First make sure there's a matching queue event */ | |
58 | ||
59 | $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); | |
60 | $success = NULL; | |
61 | if (!$q) { | |
62 | return $success; | |
63 | } | |
64 | ||
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)) { | |
70 | return $success; | |
71 | } | |
72 | ||
73 | $contact_id = $q->contact_id; | |
74 | ||
75 | $transaction = new CRM_Core_Transaction(); | |
76 | ||
77 | $do = new CRM_Core_DAO(); | |
04124b30 | 78 | $mg = CRM_Mailing_DAO_MailingGroup::getTableName(); |
9da8dc8c | 79 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
80 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
81 | $group = CRM_Contact_BAO_Group::getTableName(); | |
82 | $gc = CRM_Contact_BAO_GroupContact::getTableName(); | |
83 | ||
84 | //We Need the mailing Id for the hook... | |
03e04002 | 85 | $do->query("SELECT $job.mailing_id as mailing_id |
86 | FROM $job | |
6a488035 TO |
87 | WHERE $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer')); |
88 | $do->fetch(); | |
89 | $mailing_id = $do->mailing_id; | |
90 | ||
91 | $do->query(" | |
92 | SELECT $mg.entity_table as entity_table, | |
93 | $mg.entity_id as entity_id | |
94 | FROM $mg | |
95 | INNER JOIN $job | |
96 | ON $job.mailing_id = $mg.mailing_id | |
97 | INNER JOIN $group | |
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" | |
102 | ); | |
103 | ||
03e04002 | 104 | /* Make a list of groups and a list of prior mailings that received |
6a488035 TO |
105 | * this mailing */ |
106 | ||
107 | ||
108 | $groups = array(); | |
109 | $mailings = array(); | |
110 | ||
111 | while ($do->fetch()) { | |
112 | if ($do->entity_table == $group) { | |
113 | $groups[$do->entity_id] = NULL; | |
114 | } | |
115 | elseif ($do->entity_table == $mailing) { | |
116 | $mailings[] = $do->entity_id; | |
117 | } | |
118 | } | |
119 | ||
120 | /* As long as we have prior mailings, find their groups and add to the | |
121 | * list */ | |
122 | ||
123 | while (!empty($mailings)) { | |
124 | $do->query(" | |
125 | SELECT $mg.entity_table as entity_table, | |
126 | $mg.entity_id as entity_id | |
127 | FROM $mg | |
128 | WHERE $mg.mailing_id IN (" . implode(', ', $mailings) . ") | |
129 | AND $mg.group_type = 'Include'"); | |
130 | ||
131 | $mailings = array(); | |
132 | ||
133 | while ($do->fetch()) { | |
134 | if ($do->entity_table == $group) { | |
135 | $groups[$do->entity_id] = TRUE; | |
136 | } | |
137 | elseif ($do->entity_table == $mailing) { | |
138 | $mailings[] = $do->entity_id; | |
139 | } | |
140 | } | |
141 | } | |
142 | ||
143 | $group_ids = array_keys($groups); | |
144 | $base_groups = NULL; | |
145 | CRM_Utils_Hook::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups); | |
146 | ||
147 | /* Now we have a complete list of recipient groups. Filter out all | |
148 | * those except smart groups and those that the contact belongs to */ | |
149 | ||
150 | $do->query(" | |
151 | SELECT $group.id as group_id, | |
152 | $group.title as title | |
153 | FROM $group | |
154 | LEFT JOIN $gc | |
155 | ON $gc.group_id = $group.id | |
156 | WHERE $group.id IN (" . implode(', ', $group_ids) . ") | |
157 | AND ($group.saved_search_id is not null | |
158 | OR ($gc.contact_id = $contact_id | |
159 | AND $gc.status = 'Removed') | |
160 | )"); | |
161 | ||
162 | while ($do->fetch()) { | |
163 | $groups[$do->group_id] = $do->title; | |
164 | } | |
165 | ||
166 | $contacts = array($contact_id); | |
167 | foreach ($groups as $group_id => $group_name) { | |
168 | $notadded = 0; | |
169 | if ($group_name) { | |
170 | list($total, $added, $notadded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email'); | |
171 | } | |
172 | if ($notadded) { | |
173 | unset($groups[$group_id]); | |
174 | } | |
175 | } | |
176 | ||
177 | // remove entry from Unsubscribe table. | |
178 | $ue = new CRM_Mailing_Event_BAO_Unsubscribe(); | |
179 | $ue->event_queue_id = $queue_id; | |
180 | $ue->org_resubscribe = 0; | |
181 | if ($ue->find(TRUE)) { | |
182 | $ue->delete(); | |
183 | } | |
184 | ||
185 | $transaction->commit(); | |
186 | return $groups; | |
187 | } | |
188 | ||
189 | /** | |
190 | * Send a reponse email informing the contact of the groups to which he/she | |
191 | * has been resubscribed. | |
192 | * | |
90c8230e TO |
193 | * @param string $queue_id |
194 | * The queue event ID. | |
195 | * @param array $groups | |
196 | * List of group IDs. | |
197 | * @param bool $is_domain | |
198 | * Is this domain-level?. | |
199 | * @param int $job | |
200 | * The job ID. | |
6a488035 TO |
201 | * |
202 | * @return void | |
6a488035 TO |
203 | * @static |
204 | */ | |
205 | public static function send_resub_response($queue_id, $groups, $is_domain = FALSE, $job) { | |
206 | // param is_domain is not supported as of now. | |
207 | ||
208 | $config = CRM_Core_Config::singleton(); | |
209 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
210 | ||
9da8dc8c | 211 | $jobTable = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
212 | $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); |
213 | $contacts = CRM_Contact_DAO_Contact::getTableName(); | |
214 | $email = CRM_Core_DAO_Email::getTableName(); | |
215 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
216 | ||
217 | //get the default domain email address. | |
218 | list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(); | |
219 | ||
220 | $dao = new CRM_Mailing_BAO_Mailing(); | |
03e04002 | 221 | $dao->query(" SELECT * FROM $mailingTable |
6a488035 | 222 | INNER JOIN $jobTable ON |
03e04002 | 223 | $jobTable.mailing_id = $mailingTable.id |
6a488035 TO |
224 | WHERE $jobTable.id = $job"); |
225 | $dao->fetch(); | |
226 | ||
227 | $component = new CRM_Mailing_BAO_Component(); | |
228 | $component->id = $dao->resubscribe_id; | |
229 | $component->find(TRUE); | |
230 | ||
231 | $html = $component->body_html; | |
232 | if ($component->body_text) { | |
233 | $text = $component->body_text; | |
234 | } | |
235 | else { | |
236 | $text = CRM_Utils_String::htmlToText($component->body_html); | |
237 | } | |
238 | ||
239 | $eq = new CRM_Core_DAO(); | |
240 | $eq->query( | |
241 | "SELECT $contacts.preferred_mail_format as format, | |
242 | $contacts.id as contact_id, | |
243 | $email.email as email, | |
244 | $queue.hash as hash | |
245 | FROM $contacts | |
246 | INNER JOIN $queue ON $queue.contact_id = $contacts.id | |
247 | INNER JOIN $email ON $queue.email_id = $email.id | |
248 | WHERE $queue.id = " . CRM_Utils_Type::escape($queue_id, 'Integer') | |
249 | ); | |
250 | $eq->fetch(); | |
251 | foreach ($groups as $key => $value) { | |
252 | if (!$value) { | |
253 | unset($groups[$key]); | |
254 | } | |
255 | } | |
256 | ||
257 | $message = new Mail_mime("\n"); | |
258 | ||
259 | list($addresses, $urls) = CRM_Mailing_BAO_Mailing::getVerpAndUrls($job, $queue_id, $eq->hash, $eq->email); | |
260 | $bao = new CRM_Mailing_BAO_Mailing(); | |
261 | $bao->body_text = $text; | |
262 | $bao->body_html = $html; | |
263 | $tokens = $bao->getTokens(); | |
264 | if ($eq->format == 'HTML' || $eq->format == 'Both') { | |
265 | $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']); | |
266 | $html = CRM_Utils_Token::replaceResubscribeTokens($html, $domain, $groups, TRUE, $eq->contact_id, $eq->hash); | |
267 | $html = CRM_Utils_Token::replaceActionTokens($html, $addresses, $urls, TRUE, $tokens['html']); | |
268 | $html = CRM_Utils_Token::replaceMailingTokens($html, $dao, NULL, $tokens['html']); | |
269 | $message->setHTMLBody($html); | |
270 | } | |
271 | if (!$html || $eq->format == 'Text' || $eq->format == 'Both') { | |
272 | $text = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['text']); | |
273 | $text = CRM_Utils_Token::replaceResubscribeTokens($text, $domain, $groups, FALSE, $eq->contact_id, $eq->hash); | |
274 | $text = CRM_Utils_Token::replaceActionTokens($text, $addresses, $urls, FALSE, $tokens['text']); | |
275 | $text = CRM_Utils_Token::replaceMailingTokens($text, $dao, NULL, $tokens['text']); | |
276 | $message->setTxtBody($text); | |
277 | } | |
278 | ||
279 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
280 | ||
281 | $headers = array( | |
282 | 'Subject' => $component->subject, | |
283 | 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>", | |
284 | 'To' => $eq->email, | |
285 | 'Reply-To' => "do-not-reply@$emailDomain", | |
286 | 'Return-Path' => "do-not-reply@$emailDomain", | |
287 | ); | |
288 | CRM_Mailing_BAO_Mailing::addMessageIdHeader($headers, 'e', $job, $queue_id, $eq->hash); | |
289 | $b = CRM_Utils_Mail::setMimeParams($message); | |
290 | $h = $message->headers($headers); | |
291 | ||
292 | $mailer = $config->getMailer(); | |
293 | ||
6a488035 | 294 | if (is_object($mailer)) { |
6a4257d4 | 295 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 296 | $mailer->send($eq->email, $h, $b); |
6a4257d4 | 297 | unset($errorScope); |
6a488035 TO |
298 | } |
299 | } | |
300 | } |