3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Mailing_Event_BAO_Forward
extends CRM_Mailing_Event_DAO_Forward
{
20 * Create a new forward event, create a new contact if necessary
25 * @param $forward_email
26 * @param string|null $fromEmail
27 * @param array|null $comment
31 public static function &forward($job_id, $queue_id, $hash, $forward_email, $fromEmail = NULL, $comment = NULL) {
32 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
34 $successfulForward = FALSE;
37 return $successfulForward;
40 // Find the email address/contact, if it exists.
42 $contact = CRM_Contact_BAO_Contact
::getTableName();
43 $email = CRM_Core_BAO_Email
::getTableName();
44 $queueTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
45 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
47 $dao = new CRM_Core_DAO();
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)
55 ON $email.contact_id = $contact.id
57 ON $email.id = $queueTable.email_id
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') . "'"
68 $transaction = new CRM_Core_Transaction();
70 if (isset($dao->queue_id
) ||
71 (isset($dao->do_not_email
) && $dao->do_not_email
== 1)
73 // We already sent this mailing to $forward_email, or we should
74 // never email this contact. Give up.
76 return $successfulForward;
79 require_once 'api/api.php';
81 'email' => $forward_email,
84 $contactValues = civicrm_api('contact', 'get', $contactParams);
85 $count = $contactValues['count'];
88 // If the contact does not exist, create one.
91 'contact_type' => 'Individual',
94 $locationType = CRM_Core_BAO_LocationType
::getDefault();
96 'email' => $forward_email,
97 'location_type_id' => $locationType->id
,
99 require_once 'CRM/Utils/DeprecatedUtils.php';
100 _civicrm_api3_deprecated_add_formatted_param($value, $formatted);
101 $formatted['onDuplicate'] = CRM_Import_Parser
::DUPLICATE_SKIP
;
102 $formatted['fixAddress'] = TRUE;
103 $contact = civicrm_api('contact', 'create', $formatted);
104 if (civicrm_error($contact)) {
105 return $successfulForward;
107 $contact_id = $contact['id'];
109 $email = new CRM_Core_DAO_Email();
110 $email->email
= $forward_email;
112 $email_id = $email->id
;
114 $contact_id = $email->contact_id
;
117 // Create a new queue event.
120 'email_id' => $email_id,
121 'contact_id' => $contact_id,
125 $queue = CRM_Mailing_Event_BAO_Queue
::create($queue_params);
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
;
134 $dao->query(" SELECT $job.mailing_id as mailing_id
137 CRM_Utils_Type
::escape($job_id, 'Integer')
140 $mailing_obj = new CRM_Mailing_BAO_Mailing();
141 $mailing_obj->id
= $dao->mailing_id
;
142 $mailing_obj->find(TRUE);
144 $config = CRM_Core_Config
::singleton();
145 $mailer = \Civi
::service('pear_mail');
149 $message = $mailing_obj->compose($job_id, $queue->id
, $queue->hash
,
150 $queue->contact_id
, $forward_email, $recipient, FALSE, NULL, $attachments, TRUE, $fromEmail
152 //append comment if added while forwarding.
153 if (count($comment)) {
154 $message->_txtbody
= CRM_Utils_Array
::value('body_text', $comment) . $message->_txtbody
;
155 if (!empty($comment['body_html'])) {
156 $message->_htmlbody
= $comment['body_html'] . '<br />---------------Original message---------------------<br />' . $message->_htmlbody
;
160 $body = $message->get();
161 $headers = $message->headers();
164 if (is_object($mailer)) {
165 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
166 $result = $mailer->send($recipient, $headers, $body);
171 'event_queue_id' => $queue->id
,
173 'hash' => $queue->hash
,
175 if (is_a($result, 'PEAR_Error')) {
176 // Register the bounce event.
178 $params = array_merge($params,
179 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
181 CRM_Mailing_Event_BAO_Bounce
::create($params);
184 $successfulForward = TRUE;
185 // Register the delivery event.
187 CRM_Mailing_Event_BAO_Delivered
::create($params);
190 $transaction->commit();
192 return $successfulForward;
196 * Get row count for the event selector.
198 * @param int $mailing_id
201 * Optional ID of a job to filter on.
202 * @param bool $is_distinct
203 * Group by queue ID?.
206 * Number of rows in result set
208 public static function getTotalCount(
209 $mailing_id, $job_id = NULL,
212 $dao = new CRM_Core_DAO();
214 $forward = self
::getTableName();
215 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
216 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
217 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
220 SELECT COUNT($forward.id) as forward
223 ON $forward.event_queue_id = $queue.id
225 ON $queue.job_id = $job.id
227 ON $job.mailing_id = $mailing.id
229 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
231 if (!empty($job_id)) {
232 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
236 $query .= " GROUP BY $queue.id ";
243 return $dao->forward
;
250 * Get rows for the event browser.
252 * @param int $mailing_id
255 * Optional ID of the job.
256 * @param bool $is_distinct
257 * Group by queue id?.
260 * @param int $rowCount
268 public static function &getRows(
269 $mailing_id, $job_id = NULL,
270 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
273 $dao = new CRM_Core_DAO();
275 $forward = self
::getTableName();
276 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
277 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
278 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
279 $contact = CRM_Contact_BAO_Contact
::getTableName();
280 $email = CRM_Core_BAO_Email
::getTableName();
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
291 ON $queue.contact_id = $contact.id
293 ON $queue.email_id = $email.id
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
303 ON $queue.job_id = $job.id
305 ON $job.mailing_id = $mailing.id
307 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
309 if (!empty($job_id)) {
310 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
314 $query .= " GROUP BY $queue.id, dest_contact.id, dest_email.email, $forward.time_stamp ";
317 $orderBy = "$contact.sort_name ASC, {$forward}.time_stamp DESC";
319 if (is_string($sort)) {
320 $sort = CRM_Utils_Type
::escape($sort, 'String');
324 $orderBy = trim($sort->orderBy());
328 $query .= " ORDER BY {$orderBy} ";
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');
339 while ($dao->fetch()) {
340 $from_url = CRM_Utils_System
::url('civicrm/contact/view',
341 "reset=1&cid={$dao->from_id}"
343 $dest_url = CRM_Utils_System
::url('civicrm/contact/view',
344 "reset=1&cid={$dao->dest_id}"
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
),