mass update of comment blocks
[civicrm-core.git] / CRM / Mailing / Event / BAO / Resubscribe.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 require_once 'Mail/mime.php';
37 class CRM_Mailing_Event_BAO_Resubscribe {
38
39 /**
40 * Resubscribe a contact to the groups, he/she was unsubscribed from.
41 *
42 * @param int $job_id The job ID
43 * @param int $queue_id The Queue Event ID of the recipient
44 * @param string $hash The hash
45 *
46 * @return array|null $groups Array of all groups to which the contact was added, or null if the queue event could not be found.
47 * @access public
48 * @static
49 */
50 public static function &resub_to_mailing($job_id, $queue_id, $hash) {
51 /* First make sure there's a matching queue event */
52
53 $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
54 $success = NULL;
55 if (!$q) {
56 return $success;
57 }
58
59 // check if this queue_id was actually unsubscribed
60 $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
61 $ue->event_queue_id = $queue_id;
62 $ue->org_unsubscribe = 0;
63 if (!$ue->find(TRUE)) {
64 return $success;
65 }
66
67 $contact_id = $q->contact_id;
68
69 $transaction = new CRM_Core_Transaction();
70
71 $do = new CRM_Core_DAO();
72 $mg = CRM_Mailing_DAO_MailingGroup::getTableName();
73 $job = CRM_Mailing_BAO_MailingJob::getTableName();
74 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
75 $group = CRM_Contact_BAO_Group::getTableName();
76 $gc = CRM_Contact_BAO_GroupContact::getTableName();
77
78 //We Need the mailing Id for the hook...
79 $do->query("SELECT $job.mailing_id as mailing_id
80 FROM $job
81 WHERE $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'));
82 $do->fetch();
83 $mailing_id = $do->mailing_id;
84
85 $do->query("
86 SELECT $mg.entity_table as entity_table,
87 $mg.entity_id as entity_id
88 FROM $mg
89 INNER JOIN $job
90 ON $job.mailing_id = $mg.mailing_id
91 INNER JOIN $group
92 ON $mg.entity_id = $group.id
93 WHERE $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . "
94 AND $mg.group_type IN ( 'Include', 'Base' )
95 AND $group.is_hidden = 0"
96 );
97
98 /* Make a list of groups and a list of prior mailings that received
99 * this mailing */
100
101
102 $groups = array();
103 $mailings = array();
104
105 while ($do->fetch()) {
106 if ($do->entity_table == $group) {
107 $groups[$do->entity_id] = NULL;
108 }
109 elseif ($do->entity_table == $mailing) {
110 $mailings[] = $do->entity_id;
111 }
112 }
113
114 /* As long as we have prior mailings, find their groups and add to the
115 * list */
116
117 while (!empty($mailings)) {
118 $do->query("
119 SELECT $mg.entity_table as entity_table,
120 $mg.entity_id as entity_id
121 FROM $mg
122 WHERE $mg.mailing_id IN (" . implode(', ', $mailings) . ")
123 AND $mg.group_type = 'Include'");
124
125 $mailings = array();
126
127 while ($do->fetch()) {
128 if ($do->entity_table == $group) {
129 $groups[$do->entity_id] = TRUE;
130 }
131 elseif ($do->entity_table == $mailing) {
132 $mailings[] = $do->entity_id;
133 }
134 }
135 }
136
137 $group_ids = array_keys($groups);
138 $base_groups = NULL;
139 CRM_Utils_Hook::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups);
140
141 /* Now we have a complete list of recipient groups. Filter out all
142 * those except smart groups and those that the contact belongs to */
143
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 reponse email informing the contact of the groups to which he/she
185 * has been resubscribed.
186 *
187 * @param string $queue_id The queue event ID
188 * @param array $groups List of group IDs
189 * @param bool $is_domain Is this domain-level?
190 * @param int $job The job ID
191 *
192 * @return void
193 * @access public
194 * @static
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($html, $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 = $config->getMailer();
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