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