Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Mailing_Event_BAO_Forward extends CRM_Mailing_Event_DAO_Forward { | |
36 | ||
37 | /** | |
fe482240 | 38 | * Class constructor. |
6a488035 | 39 | */ |
00be9182 | 40 | public function __construct() { |
6a488035 TO |
41 | parent::__construct(); |
42 | } | |
43 | ||
44 | /** | |
45 | * Create a new forward event, create a new contact if necessary | |
ab432335 EM |
46 | * |
47 | * @param $job_id | |
48 | * @param $queue_id | |
49 | * @param $hash | |
50 | * @param $forward_email | |
51 | * @param null $fromEmail | |
52 | * @param null $comment | |
53 | * | |
54 | * @return bool | |
6a488035 | 55 | */ |
00be9182 | 56 | public static function &forward($job_id, $queue_id, $hash, $forward_email, $fromEmail = NULL, $comment = NULL) { |
6a488035 TO |
57 | $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash); |
58 | ||
59 | $successfulForward = FALSE; | |
60 | $contact_id = NULL; | |
61 | if (!$q) { | |
62 | return $successfulForward; | |
63 | } | |
64 | ||
65 | /* Find the email address/contact, if it exists */ | |
66 | ||
67 | $contact = CRM_Contact_BAO_Contact::getTableName(); | |
68 | $location = CRM_Core_BAO_Location::getTableName(); | |
69 | $email = CRM_Core_BAO_Email::getTableName(); | |
70 | $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
9da8dc8c | 71 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
72 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); |
73 | $forward = self::getTableName(); | |
74 | ||
75 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
76 | ||
77 | $dao = new CRM_Core_Dao(); | |
78 | $dao->query(" | |
79 | SELECT $contact.id as contact_id, | |
80 | $email.id as email_id, | |
81 | $contact.do_not_email as do_not_email, | |
82 | $queueTable.id as queue_id | |
83 | FROM ($email, $job as temp_job) | |
84 | INNER JOIN $contact | |
85 | ON $email.contact_id = $contact.id | |
86 | LEFT JOIN $queueTable | |
87 | ON $email.id = $queueTable.email_id | |
88 | LEFT JOIN $job | |
89 | ON $queueTable.job_id = $job.id | |
90 | AND temp_job.mailing_id = $job.mailing_id | |
91 | WHERE $queueTable.job_id = $job_id | |
92 | AND $email.email = '" . | |
93 | CRM_Utils_Type::escape($forward_email, 'String') . "'" | |
94 | ); | |
95 | ||
96 | $dao->fetch(); | |
97 | ||
98 | $transaction = new CRM_Core_Transaction(); | |
99 | ||
100 | if (isset($dao->queue_id) || | |
101 | (isset($dao->do_not_email) && $dao->do_not_email == 1) | |
102 | ) { | |
103 | /* We already sent this mailing to $forward_email, or we should | |
006389de | 104 | * never email this contact. Give up. */ |
6a488035 TO |
105 | |
106 | return $successfulForward; | |
107 | } | |
108 | ||
109 | require_once 'api/api.php'; | |
110 | $contactParams = array( | |
111 | 'email' => $forward_email, | |
112 | 'version' => 3, | |
113 | ); | |
114 | $contactValues = civicrm_api('contact', 'get', $contactParams); | |
115 | $count = $contactValues['count']; | |
116 | ||
117 | if ($count == 0) { | |
118 | /* If the contact does not exist, create one. */ | |
119 | ||
120 | $formatted = array( | |
121 | 'contact_type' => 'Individual', | |
122 | 'version' => 3, | |
123 | ); | |
124 | $locationType = CRM_Core_BAO_LocationType::getDefault(); | |
125 | $value = array( | |
126 | 'email' => $forward_email, | |
127 | 'location_type_id' => $locationType->id, | |
128 | ); | |
129 | require_once 'CRM/Utils/DeprecatedUtils.php'; | |
130 | _civicrm_api3_deprecated_add_formatted_param($value, $formatted); | |
a05662ef | 131 | $formatted['onDuplicate'] = CRM_Import_Parser::DUPLICATE_SKIP; |
6a488035 TO |
132 | $formatted['fixAddress'] = TRUE; |
133 | $contact = civicrm_api('contact', 'create', $formatted); | |
134 | if (civicrm_error($contact)) { | |
135 | return $successfulForward; | |
136 | } | |
137 | $contact_id = $contact['id']; | |
138 | } | |
139 | $email = new CRM_Core_DAO_Email(); | |
140 | $email->email = $forward_email; | |
141 | $email->find(TRUE); | |
142 | $email_id = $email->id; | |
143 | if (!$contact_id) { | |
144 | $contact_id = $email->contact_id; | |
145 | } | |
146 | ||
147 | /* Create a new queue event */ | |
148 | ||
149 | $queue_params = array( | |
150 | 'email_id' => $email_id, | |
151 | 'contact_id' => $contact_id, | |
152 | 'job_id' => $job_id, | |
153 | ); | |
154 | ||
155 | $queue = CRM_Mailing_Event_BAO_Queue::create($queue_params); | |
156 | ||
157 | $forward = new CRM_Mailing_Event_BAO_Forward(); | |
158 | $forward->time_stamp = date('YmdHis'); | |
159 | $forward->event_queue_id = $queue_id; | |
160 | $forward->dest_queue_id = $queue->id; | |
161 | $forward->save(); | |
162 | ||
163 | $dao->reset(); | |
164 | $dao->query(" SELECT $job.mailing_id as mailing_id | |
165 | FROM $job | |
166 | WHERE $job.id = " . | |
167 | CRM_Utils_Type::escape($job_id, 'Integer') | |
168 | ); | |
169 | $dao->fetch(); | |
170 | $mailing_obj = new CRM_Mailing_BAO_Mailing(); | |
171 | $mailing_obj->id = $dao->mailing_id; | |
172 | $mailing_obj->find(TRUE); | |
173 | ||
174 | $config = CRM_Core_Config::singleton(); | |
247eb841 | 175 | $mailer = \Civi\Core\Container::singleton()->get('pear_mail'); |
6a488035 TO |
176 | |
177 | $recipient = NULL; | |
178 | $attachments = NULL; | |
179 | $message = $mailing_obj->compose($job_id, $queue->id, $queue->hash, | |
180 | $queue->contact_id, $forward_email, $recipient, FALSE, NULL, $attachments, TRUE, $fromEmail | |
181 | ); | |
182 | //append comment if added while forwarding. | |
183 | if (count($comment)) { | |
184 | $message->_txtbody = CRM_Utils_Array::value('body_text', $comment) . $message->_txtbody; | |
a7488080 | 185 | if (!empty($comment['body_html'])) { |
6a488035 TO |
186 | $message->_htmlbody = $comment['body_html'] . '<br />---------------Original message---------------------<br />' . $message->_htmlbody; |
187 | } | |
188 | } | |
189 | ||
190 | $body = $message->get(); | |
191 | $headers = $message->headers(); | |
192 | ||
6a488035 TO |
193 | $result = NULL; |
194 | if (is_object($mailer)) { | |
6a4257d4 | 195 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
6a488035 | 196 | $result = $mailer->send($recipient, $headers, $body); |
6a4257d4 | 197 | unset($errorScope); |
6a488035 TO |
198 | } |
199 | ||
200 | $params = array( | |
201 | 'event_queue_id' => $queue->id, | |
202 | 'job_id' => $job_id, | |
203 | 'hash' => $queue->hash, | |
204 | ); | |
205 | if (is_a($result, 'PEAR_Error')) { | |
206 | /* Register the bounce event */ | |
207 | ||
208 | $params = array_merge($params, | |
209 | CRM_Mailing_BAO_BouncePattern::match($result->getMessage()) | |
210 | ); | |
211 | CRM_Mailing_Event_BAO_Bounce::create($params); | |
212 | } | |
213 | else { | |
214 | $successfulForward = TRUE; | |
215 | /* Register the delivery event */ | |
216 | ||
217 | CRM_Mailing_Event_BAO_Delivered::create($params); | |
218 | } | |
219 | ||
220 | $transaction->commit(); | |
221 | ||
222 | return $successfulForward; | |
223 | } | |
224 | ||
225 | /** | |
fe482240 | 226 | * Get row count for the event selector. |
6a488035 | 227 | * |
90c8230e TO |
228 | * @param int $mailing_id |
229 | * ID of the mailing. | |
230 | * @param int $job_id | |
231 | * Optional ID of a job to filter on. | |
232 | * @param bool $is_distinct | |
233 | * Group by queue ID?. | |
6a488035 | 234 | * |
a6c01b45 CW |
235 | * @return int |
236 | * Number of rows in result set | |
6a488035 | 237 | */ |
a3d7e8ee TO |
238 | public static function getTotalCount( |
239 | $mailing_id, $job_id = NULL, | |
6a488035 TO |
240 | $is_distinct = FALSE |
241 | ) { | |
242 | $dao = new CRM_Core_DAO(); | |
243 | ||
244 | $forward = self::getTableName(); | |
245 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
246 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 247 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
248 | |
249 | $query = " | |
250 | SELECT COUNT($forward.id) as forward | |
251 | FROM $forward | |
252 | INNER JOIN $queue | |
253 | ON $forward.event_queue_id = $queue.id | |
254 | INNER JOIN $job | |
255 | ON $queue.job_id = $job.id | |
256 | INNER JOIN $mailing | |
257 | ON $job.mailing_id = $mailing.id | |
258 | AND $job.is_test = 0 | |
259 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
260 | ||
261 | if (!empty($job_id)) { | |
262 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
263 | } | |
264 | ||
265 | if ($is_distinct) { | |
266 | $query .= " GROUP BY $queue.id "; | |
267 | } | |
268 | ||
269 | // query was missing | |
270 | $dao->query($query); | |
271 | ||
272 | if ($dao->fetch()) { | |
273 | return $dao->forward; | |
274 | } | |
275 | ||
276 | return NULL; | |
277 | } | |
278 | ||
279 | /** | |
fe482240 | 280 | * Get rows for the event browser. |
6a488035 | 281 | * |
90c8230e TO |
282 | * @param int $mailing_id |
283 | * ID of the mailing. | |
284 | * @param int $job_id | |
285 | * Optional ID of the job. | |
286 | * @param bool $is_distinct | |
287 | * Group by queue id?. | |
288 | * @param int $offset | |
289 | * Offset. | |
290 | * @param int $rowCount | |
291 | * Number of rows. | |
292 | * @param array $sort | |
293 | * Sort array. | |
6a488035 | 294 | * |
a6c01b45 CW |
295 | * @return array |
296 | * Result set | |
6a488035 | 297 | */ |
a3d7e8ee TO |
298 | public static function &getRows( |
299 | $mailing_id, $job_id = NULL, | |
6a488035 TO |
300 | $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL |
301 | ) { | |
302 | ||
303 | $dao = new CRM_Core_Dao(); | |
304 | ||
305 | $forward = self::getTableName(); | |
306 | $queue = CRM_Mailing_Event_BAO_Queue::getTableName(); | |
307 | $mailing = CRM_Mailing_BAO_Mailing::getTableName(); | |
9da8dc8c | 308 | $job = CRM_Mailing_BAO_MailingJob::getTableName(); |
6a488035 TO |
309 | $contact = CRM_Contact_BAO_Contact::getTableName(); |
310 | $email = CRM_Core_BAO_Email::getTableName(); | |
311 | ||
312 | $query = " | |
313 | SELECT $contact.display_name as from_name, | |
314 | $contact.id as from_id, | |
315 | $email.email as from_email, | |
316 | dest_contact.id as dest_id, | |
317 | dest_email.email as dest_email, | |
318 | $forward.time_stamp as date | |
319 | FROM $contact | |
320 | INNER JOIN $queue | |
321 | ON $queue.contact_id = $contact.id | |
322 | INNER JOIN $email | |
323 | ON $queue.email_id = $email.id | |
324 | INNER JOIN $forward | |
325 | ON $forward.event_queue_id = $queue.id | |
326 | INNER JOIN $queue as dest_queue | |
327 | ON $forward.dest_queue_id = dest_queue.id | |
328 | INNER JOIN $contact as dest_contact | |
329 | ON dest_queue.contact_id = dest_contact.id | |
330 | INNER JOIN $email as dest_email | |
331 | ON dest_queue.email_id = dest_email.id | |
332 | INNER JOIN $job | |
333 | ON $queue.job_id = $job.id | |
334 | INNER JOIN $mailing | |
335 | ON $job.mailing_id = $mailing.id | |
336 | AND $job.is_test = 0 | |
337 | WHERE $mailing.id = " . CRM_Utils_Type::escape($mailing_id, 'Integer'); | |
338 | ||
339 | if (!empty($job_id)) { | |
340 | $query .= " AND $job.id = " . CRM_Utils_Type::escape($job_id, 'Integer'); | |
341 | } | |
342 | ||
343 | if ($is_distinct) { | |
344 | $query .= " GROUP BY $queue.id "; | |
345 | } | |
346 | ||
347 | $orderBy = "sort_name ASC, {$forward}.time_stamp DESC"; | |
348 | if ($sort) { | |
349 | if (is_string($sort)) { | |
21d32567 | 350 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
351 | $orderBy = $sort; |
352 | } | |
353 | else { | |
354 | $orderBy = trim($sort->orderBy()); | |
355 | } | |
356 | } | |
357 | ||
358 | $query .= " ORDER BY {$orderBy} "; | |
359 | ||
360 | if ($offset || $rowCount) { | |
361 | //Added "||$rowCount" to avoid displaying all records on first page | |
362 | $query .= ' LIMIT ' . CRM_Utils_Type::escape($offset, 'Integer') . ', ' . CRM_Utils_Type::escape($rowCount, 'Integer'); | |
363 | } | |
364 | ||
365 | $dao->query($query); | |
366 | ||
367 | $results = array(); | |
368 | ||
369 | while ($dao->fetch()) { | |
370 | $from_url = CRM_Utils_System::url('civicrm/contact/view', | |
371 | "reset=1&cid={$dao->from_id}" | |
372 | ); | |
373 | $dest_url = CRM_Utils_System::url('civicrm/contact/view', | |
374 | "reset=1&cid={$dao->dest_id}" | |
375 | ); | |
376 | $results[] = array( | |
377 | 'from_name' => "<a href=\"$from_url\">{$dao->from_name}</a>", | |
378 | 'from_email' => $dao->from_email, | |
379 | 'dest_email' => "<a href=\"$dest_url\">{$dao->dest_email}</a>", | |
380 | 'date' => CRM_Utils_Date::customFormat($dao->date), | |
381 | ); | |
382 | } | |
383 | return $results; | |
384 | } | |
96025800 | 385 | |
6a488035 | 386 | } |