Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | * | |
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 | |
49 | * | |
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. | |
51 | * @access public | |
52 | * @static | |
53 | */ | |
54 | public static function &resub_to_mailing($job_id, $queue_id, $hash) { | |
55 | /* First make sure there's a matching queue event */ | |
56 | ||
57 | $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); | |
58 | $success = NULL; | |
59 | if (!$q) { | |
60 | return $success; | |
61 | } | |
62 | ||
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)) { | |
68 | return $success; | |
69 | } | |
70 | ||
71 | $contact_id = $q->contact_id; | |
72 | ||
73 | $transaction = new CRM_Core_Transaction(); | |
74 | ||
75 | $do = new CRM_Core_DAO(); | |
04124b30 | 76 | $mg = CRM_Mailing_DAO_MailingGroup::getTableName(); |
9da8dc8c | 77 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
78 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
79 | $group = CRM_Contact_BAO_Group::getTableName(); | |
80 | $gc = CRM_Contact_BAO_GroupContact::getTableName(); | |
81 | ||
82 | //We Need the mailing Id for the hook... | |
03e04002 | 83 | $do->query("SELECT $job.mailing_id as mailing_id |
84 | FROM $job | |
6a488035 TO |
85 | WHERE $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer')); |
86 | $do->fetch(); | |
87 | $mailing_id = $do->mailing_id; | |
88 | ||
89 | $do->query(" | |
90 | SELECT $mg.entity_table as entity_table, | |
91 | $mg.entity_id as entity_id | |
92 | FROM $mg | |
93 | INNER JOIN $job | |
94 | ON $job.mailing_id = $mg.mailing_id | |
95 | INNER JOIN $group | |
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" | |
100 | ); | |
101 | ||
03e04002 | 102 | /* Make a list of groups and a list of prior mailings that received |
6a488035 TO |
103 | * this mailing */ |
104 | ||
105 | ||
106 | $groups = array(); | |
107 | $mailings = array(); | |
108 | ||
109 | while ($do->fetch()) { | |
110 | if ($do->entity_table == $group) { | |
111 | $groups[$do->entity_id] = NULL; | |
112 | } | |
113 | elseif ($do->entity_table == $mailing) { | |
114 | $mailings[] = $do->entity_id; | |
115 | } | |
116 | } | |
117 | ||
118 | /* As long as we have prior mailings, find their groups and add to the | |
119 | * list */ | |
120 | ||
121 | while (!empty($mailings)) { | |
122 | $do->query(" | |
123 | SELECT $mg.entity_table as entity_table, | |
124 | $mg.entity_id as entity_id | |
125 | FROM $mg | |
126 | WHERE $mg.mailing_id IN (" . implode(', ', $mailings) . ") | |
127 | AND $mg.group_type = 'Include'"); | |
128 | ||
129 | $mailings = array(); | |
130 | ||
131 | while ($do->fetch()) { | |
132 | if ($do->entity_table == $group) { | |
133 | $groups[$do->entity_id] = TRUE; | |
134 | } | |
135 | elseif ($do->entity_table == $mailing) { | |
136 | $mailings[] = $do->entity_id; | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | $group_ids = array_keys($groups); | |
142 | $base_groups = NULL; | |
143 | CRM_Utils_Hook::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups); | |
144 | ||
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 */ | |
147 | ||
148 | $do->query(" | |
149 | SELECT $group.id as group_id, | |
150 | $group.title as title | |
151 | FROM $group | |
152 | LEFT JOIN $gc | |
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') | |
158 | )"); | |
159 | ||
160 | while ($do->fetch()) { | |
161 | $groups[$do->group_id] = $do->title; | |
162 | } | |
163 | ||
164 | $contacts = array($contact_id); | |
165 | foreach ($groups as $group_id => $group_name) { | |
166 | $notadded = 0; | |
167 | if ($group_name) { | |
168 | list($total, $added, $notadded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email'); | |
169 | } | |
170 | if ($notadded) { | |
171 | unset($groups[$group_id]); | |
172 | } | |
173 | } | |
174 | ||
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)) { | |
180 | $ue->delete(); | |
181 | } | |
182 | ||
183 | $transaction->commit(); | |
184 | return $groups; | |
185 | } | |
186 | ||
187 | /** | |
188 | * Send a reponse email informing the contact of the groups to which he/she | |
189 | * has been resubscribed. | |
190 | * | |
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 | |
195 | * | |
196 | * @return void | |
197 | * @access public | |
198 | * @static | |
199 | */ | |
200 | public static function send_resub_response($queue_id, $groups, $is_domain = FALSE, $job) { | |
201 | // param is_domain is not supported as of now. | |
202 | ||
203 | $config = CRM_Core_Config::singleton(); | |
204 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
205 | ||
9da8dc8c | 206 | $jobTable = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
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(); | |
211 | ||
212 | //get the default domain email address. | |
213 | list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(); | |
214 | ||
215 | $dao = new CRM_Mailing_BAO_Mailing(); | |
03e04002 | 216 | $dao->query(" SELECT * FROM $mailingTable |
6a488035 | 217 | INNER JOIN $jobTable ON |
03e04002 | 218 | $jobTable.mailing_id = $mailingTable.id |
6a488035 TO |
219 | WHERE $jobTable.id = $job"); |
220 | $dao->fetch(); | |
221 | ||
222 | $component = new CRM_Mailing_BAO_Component(); | |
223 | $component->id = $dao->resubscribe_id; | |
224 | $component->find(TRUE); | |
225 | ||
226 | $html = $component->body_html; | |
227 | if ($component->body_text) { | |
228 | $text = $component->body_text; | |
229 | } | |
230 | else { | |
231 | $text = CRM_Utils_String::htmlToText($component->body_html); | |
232 | } | |
233 | ||
234 | $eq = new CRM_Core_DAO(); | |
235 | $eq->query( | |
236 | "SELECT $contacts.preferred_mail_format as format, | |
237 | $contacts.id as contact_id, | |
238 | $email.email as email, | |
239 | $queue.hash as hash | |
240 | FROM $contacts | |
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') | |
244 | ); | |
245 | $eq->fetch(); | |
246 | foreach ($groups as $key => $value) { | |
247 | if (!$value) { | |
248 | unset($groups[$key]); | |
249 | } | |
250 | } | |
251 | ||
252 | $message = new Mail_mime("\n"); | |
253 | ||
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); | |
265 | } | |
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); | |
272 | } | |
273 | ||
274 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
275 | ||
276 | $headers = array( | |
277 | 'Subject' => $component->subject, | |
278 | 'From' => "\"$domainEmailName\" <do-not-reply@$emailDomain>", | |
279 | 'To' => $eq->email, | |
280 | 'Reply-To' => "do-not-reply@$emailDomain", | |
281 | 'Return-Path' => "do-not-reply@$emailDomain", | |
282 | ); | |
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); | |
286 | ||
287 | $mailer = $config->getMailer(); | |
288 | ||
6a488035 | 289 | if (is_object($mailer)) { |
6a4257d4 | 290 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 291 | $mailer->send($eq->email, $h, $b); |
6a4257d4 | 292 | unset($errorScope); |
6a488035 TO |
293 | } |
294 | } | |
295 | } | |
296 |