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