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