split sql select into two selects for performance reasons
[civicrm-core.git] / CRM / Mailing / Event / BAO / Resubscribe.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 require_once 'Mail/mime.php';
19
20 /**
21 * Class CRM_Mailing_Event_BAO_Resubscribe
22 */
23 class CRM_Mailing_Event_BAO_Resubscribe {
24
25 /**
26 * Resubscribe a contact to the groups, he/she was unsubscribed from.
27 *
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.
34 *
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.
37 */
38 public static function &resub_to_mailing($job_id, $queue_id, $hash) {
39 // First make sure there's a matching queue event.
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();
60 $mg = CRM_Mailing_DAO_MailingGroup::getTableName();
61 $job = CRM_Mailing_BAO_MailingJob::getTableName();
62 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
63 $group = CRM_Contact_BAO_Group::getTableName();
64 $gc = CRM_Contact_BAO_GroupContact::getTableName();
65
66 // We Need the mailing Id for the hook...
67 $do->query("SELECT $job.mailing_id as mailing_id
68 FROM $job
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
86 // Make a list of groups and a list of prior mailings that received
87 // this mailing.
88 $groups = [];
89 $mailings = [];
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
100 // As long as we have prior mailings, find their groups and add to the
101 // list.
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
110 $mailings = [];
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
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.
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
144 $contacts = [$contact_id];
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 /**
168 * Send a response email informing the contact of the groups to which he/she
169 * has been resubscribed.
170 *
171 * @param string $queue_id
172 * The queue event ID.
173 * @param array $groups
174 * List of group IDs.
175 * @param int $job
176 * The job ID.
177 */
178 public static function send_resub_response($queue_id, $groups, $job) {
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
184 $jobTable = CRM_Mailing_BAO_MailingJob::getTableName();
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();
194 $dao->query(" SELECT * FROM $mailingTable
195 INNER JOIN $jobTable ON
196 $jobTable.mailing_id = $mailingTable.id
197 WHERE $jobTable.id = $job");
198 $dao->fetch();
199
200 $component = new CRM_Mailing_BAO_MailingComponent();
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
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']);
240 }
241 if (!$html || $eq->format == 'Text' || $eq->format == 'Both') {
242 $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, TRUE, $tokens['text']);
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']);
246 }
247
248 $params = [
249 'subject' => $component->subject,
250 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
251 'toEmail' => $eq->email,
252 'replyTo' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
253 'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
254 'html' => $html,
255 'text' => $text,
256 ];
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'];
260 }
261 CRM_Utils_Mail::send($params);
262 }
263
264 }