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