Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | require_once 'Mail/mime.php'; | |
4c6ce474 EM |
19 | |
20 | /** | |
21 | * Class CRM_Mailing_Event_BAO_Resubscribe | |
22 | */ | |
6a488035 TO |
23 | class CRM_Mailing_Event_BAO_Resubscribe { |
24 | ||
25 | /** | |
26 | * Resubscribe a contact to the groups, he/she was unsubscribed from. | |
27 | * | |
90c8230e TO |
28 | * @param int $job_id |
29 | * The job ID. | |
30 | * @param int $queue_id | |
31 | * The Queue Event ID of the recipient. | |
32 | * @param string $hash | |
33 | * The hash. | |
6a488035 | 34 | * |
72b3a70c CW |
35 | * @return array|null |
36 | * $groups Array of all groups to which the contact was added, or null if the queue event could not be found. | |
6a488035 TO |
37 | */ |
38 | public static function &resub_to_mailing($job_id, $queue_id, $hash) { | |
25606795 | 39 | // First make sure there's a matching queue event. |
6a488035 TO |
40 | |
41 | $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); | |
42 | $success = NULL; | |
43 | if (!$q) { | |
44 | return $success; | |
45 | } | |
46 | ||
47 | // check if this queue_id was actually unsubscribed | |
48 | $ue = new CRM_Mailing_Event_BAO_Unsubscribe(); | |
49 | $ue->event_queue_id = $queue_id; | |
50 | $ue->org_unsubscribe = 0; | |
51 | if (!$ue->find(TRUE)) { | |
52 | return $success; | |
53 | } | |
54 | ||
55 | $contact_id = $q->contact_id; | |
56 | ||
57 | $transaction = new CRM_Core_Transaction(); | |
58 | ||
59 | $do = new CRM_Core_DAO(); | |
04124b30 | 60 | $mg = CRM_Mailing_DAO_MailingGroup::getTableName(); |
9da8dc8c | 61 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
62 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
63 | $group = CRM_Contact_BAO_Group::getTableName(); | |
64 | $gc = CRM_Contact_BAO_GroupContact::getTableName(); | |
65 | ||
25606795 | 66 | // We Need the mailing Id for the hook... |
03e04002 | 67 | $do->query("SELECT $job.mailing_id as mailing_id |
68 | FROM $job | |
6a488035 TO |
69 | WHERE $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer')); |
70 | $do->fetch(); | |
71 | $mailing_id = $do->mailing_id; | |
72 | ||
73 | $do->query(" | |
74 | SELECT $mg.entity_table as entity_table, | |
75 | $mg.entity_id as entity_id | |
76 | FROM $mg | |
77 | INNER JOIN $job | |
78 | ON $job.mailing_id = $mg.mailing_id | |
79 | INNER JOIN $group | |
80 | ON $mg.entity_id = $group.id | |
81 | WHERE $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . " | |
82 | AND $mg.group_type IN ( 'Include', 'Base' ) | |
83 | AND $group.is_hidden = 0" | |
84 | ); | |
85 | ||
25606795 SB |
86 | // Make a list of groups and a list of prior mailings that received |
87 | // this mailing. | |
be2fb01f CW |
88 | $groups = []; |
89 | $mailings = []; | |
6a488035 TO |
90 | |
91 | while ($do->fetch()) { | |
92 | if ($do->entity_table == $group) { | |
93 | $groups[$do->entity_id] = NULL; | |
94 | } | |
95 | elseif ($do->entity_table == $mailing) { | |
96 | $mailings[] = $do->entity_id; | |
97 | } | |
98 | } | |
99 | ||
25606795 SB |
100 | // As long as we have prior mailings, find their groups and add to the |
101 | // list. | |
6a488035 TO |
102 | while (!empty($mailings)) { |
103 | $do->query(" | |
104 | SELECT $mg.entity_table as entity_table, | |
105 | $mg.entity_id as entity_id | |
106 | FROM $mg | |
107 | WHERE $mg.mailing_id IN (" . implode(', ', $mailings) . ") | |
108 | AND $mg.group_type = 'Include'"); | |
109 | ||
be2fb01f | 110 | $mailings = []; |
6a488035 TO |
111 | |
112 | while ($do->fetch()) { | |
113 | if ($do->entity_table == $group) { | |
114 | $groups[$do->entity_id] = TRUE; | |
115 | } | |
116 | elseif ($do->entity_table == $mailing) { | |
117 | $mailings[] = $do->entity_id; | |
118 | } | |
119 | } | |
120 | } | |
121 | ||
122 | $group_ids = array_keys($groups); | |
123 | $base_groups = NULL; | |
124 | CRM_Utils_Hook::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups); | |
125 | ||
25606795 SB |
126 | // Now we have a complete list of recipient groups. Filter out all |
127 | // those except smart groups and those that the contact belongs to. | |
6a488035 TO |
128 | $do->query(" |
129 | SELECT $group.id as group_id, | |
130 | $group.title as title | |
131 | FROM $group | |
132 | LEFT JOIN $gc | |
133 | ON $gc.group_id = $group.id | |
134 | WHERE $group.id IN (" . implode(', ', $group_ids) . ") | |
135 | AND ($group.saved_search_id is not null | |
136 | OR ($gc.contact_id = $contact_id | |
137 | AND $gc.status = 'Removed') | |
138 | )"); | |
139 | ||
140 | while ($do->fetch()) { | |
141 | $groups[$do->group_id] = $do->title; | |
142 | } | |
143 | ||
be2fb01f | 144 | $contacts = [$contact_id]; |
6a488035 TO |
145 | foreach ($groups as $group_id => $group_name) { |
146 | $notadded = 0; | |
147 | if ($group_name) { | |
148 | list($total, $added, $notadded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email'); | |
149 | } | |
150 | if ($notadded) { | |
151 | unset($groups[$group_id]); | |
152 | } | |
153 | } | |
154 | ||
155 | // remove entry from Unsubscribe table. | |
156 | $ue = new CRM_Mailing_Event_BAO_Unsubscribe(); | |
157 | $ue->event_queue_id = $queue_id; | |
158 | $ue->org_resubscribe = 0; | |
159 | if ($ue->find(TRUE)) { | |
160 | $ue->delete(); | |
161 | } | |
162 | ||
163 | $transaction->commit(); | |
164 | return $groups; | |
165 | } | |
166 | ||
167 | /** | |
b44e3f84 | 168 | * Send a response email informing the contact of the groups to which he/she |
6a488035 TO |
169 | * has been resubscribed. |
170 | * | |
90c8230e TO |
171 | * @param string $queue_id |
172 | * The queue event ID. | |
173 | * @param array $groups | |
174 | * List of group IDs. | |
90c8230e TO |
175 | * @param int $job |
176 | * The job ID. | |
6a488035 | 177 | */ |
5c2d1ad1 | 178 | public static function send_resub_response($queue_id, $groups, $job) { |
6a488035 TO |
179 | // param is_domain is not supported as of now. |
180 | ||
181 | $config = CRM_Core_Config::singleton(); | |
182 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
183 | ||
9da8dc8c | 184 | $jobTable = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
185 | $mailingTable = CRM_Mailing_DAO_Mailing::getTableName(); |
186 | $contacts = CRM_Contact_DAO_Contact::getTableName(); | |
187 | $email = CRM_Core_DAO_Email::getTableName(); | |
188 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
189 | ||
190 | //get the default domain email address. | |
191 | list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(); | |
192 | ||
193 | $dao = new CRM_Mailing_BAO_Mailing(); | |
03e04002 | 194 | $dao->query(" SELECT * FROM $mailingTable |
6a488035 | 195 | INNER JOIN $jobTable ON |
03e04002 | 196 | $jobTable.mailing_id = $mailingTable.id |
6a488035 TO |
197 | WHERE $jobTable.id = $job"); |
198 | $dao->fetch(); | |
199 | ||
4825de4a | 200 | $component = new CRM_Mailing_BAO_MailingComponent(); |
6a488035 TO |
201 | $component->id = $dao->resubscribe_id; |
202 | $component->find(TRUE); | |
203 | ||
204 | $html = $component->body_html; | |
205 | if ($component->body_text) { | |
206 | $text = $component->body_text; | |
207 | } | |
208 | else { | |
209 | $text = CRM_Utils_String::htmlToText($component->body_html); | |
210 | } | |
211 | ||
212 | $eq = new CRM_Core_DAO(); | |
213 | $eq->query( | |
214 | "SELECT $contacts.preferred_mail_format as format, | |
215 | $contacts.id as contact_id, | |
216 | $email.email as email, | |
217 | $queue.hash as hash | |
218 | FROM $contacts | |
219 | INNER JOIN $queue ON $queue.contact_id = $contacts.id | |
220 | INNER JOIN $email ON $queue.email_id = $email.id | |
221 | WHERE $queue.id = " . CRM_Utils_Type::escape($queue_id, 'Integer') | |
222 | ); | |
223 | $eq->fetch(); | |
224 | foreach ($groups as $key => $value) { | |
225 | if (!$value) { | |
226 | unset($groups[$key]); | |
227 | } | |
228 | } | |
229 | ||
6a488035 TO |
230 | list($addresses, $urls) = CRM_Mailing_BAO_Mailing::getVerpAndUrls($job, $queue_id, $eq->hash, $eq->email); |
231 | $bao = new CRM_Mailing_BAO_Mailing(); | |
232 | $bao->body_text = $text; | |
233 | $bao->body_html = $html; | |
234 | $tokens = $bao->getTokens(); | |
235 | if ($eq->format == 'HTML' || $eq->format == 'Both') { | |
236 | $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']); | |
237 | $html = CRM_Utils_Token::replaceResubscribeTokens($html, $domain, $groups, TRUE, $eq->contact_id, $eq->hash); | |
238 | $html = CRM_Utils_Token::replaceActionTokens($html, $addresses, $urls, TRUE, $tokens['html']); | |
239 | $html = CRM_Utils_Token::replaceMailingTokens($html, $dao, NULL, $tokens['html']); | |
6a488035 TO |
240 | } |
241 | if (!$html || $eq->format == 'Text' || $eq->format == 'Both') { | |
7e50a87e | 242 | $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, TRUE, $tokens['text']); |
6a488035 TO |
243 | $text = CRM_Utils_Token::replaceResubscribeTokens($text, $domain, $groups, FALSE, $eq->contact_id, $eq->hash); |
244 | $text = CRM_Utils_Token::replaceActionTokens($text, $addresses, $urls, FALSE, $tokens['text']); | |
245 | $text = CRM_Utils_Token::replaceMailingTokens($text, $dao, NULL, $tokens['text']); | |
6a488035 TO |
246 | } |
247 | ||
52f4ecb2 SL |
248 | $params = [ |
249 | 'subject' => $component->subject, | |
a5c03029 | 250 | 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>", |
52f4ecb2 SL |
251 | 'toEmail' => $eq->email, |
252 | 'replyTo' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), | |
253 | 'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(), | |
254 | 'html' => $html, | |
255 | 'text' => $text, | |
be2fb01f | 256 | ]; |
52f4ecb2 SL |
257 | CRM_Mailing_BAO_Mailing::addMessageIdHeader($params, 'e', $job, $queue_id, $eq->hash); |
258 | if (CRM_Core_BAO_MailSettings::includeMessageId()) { | |
259 | $params['messageId'] = $params['Message-ID']; | |
6a488035 | 260 | } |
52f4ecb2 | 261 | CRM_Utils_Mail::send($params); |
6a488035 | 262 | } |
96025800 | 263 | |
6a488035 | 264 | } |