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
{
22 public function __construct() {
23 parent
::__construct();
27 * Create a new forward event, create a new contact if necessary
32 * @param $forward_email
33 * @param string|null $fromEmail
34 * @param array|null $comment
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);
41 $successfulForward = FALSE;
44 return $successfulForward;
47 // Find the email address/contact, if it exists.
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();
54 $dao = new CRM_Core_DAO();
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)
62 ON $email.contact_id = $contact.id
64 ON $email.id = $queueTable.email_id
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') . "'"
75 $transaction = new CRM_Core_Transaction();
77 if (isset($dao->queue_id
) ||
78 (isset($dao->do_not_email
) && $dao->do_not_email
== 1)
80 // We already sent this mailing to $forward_email, or we should
81 // never email this contact. Give up.
83 return $successfulForward;
86 require_once 'api/api.php';
88 'email' => $forward_email,
91 $contactValues = civicrm_api('contact', 'get', $contactParams);
92 $count = $contactValues['count'];
95 // If the contact does not exist, create one.
98 'contact_type' => 'Individual',
101 $locationType = CRM_Core_BAO_LocationType
::getDefault();
103 'email' => $forward_email,
104 'location_type_id' => $locationType->id
,
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;
114 $contact_id = $contact['id'];
116 $email = new CRM_Core_DAO_Email();
117 $email->email
= $forward_email;
119 $email_id = $email->id
;
121 $contact_id = $email->contact_id
;
124 // Create a new queue event.
127 'email_id' => $email_id,
128 'contact_id' => $contact_id,
132 $queue = CRM_Mailing_Event_BAO_Queue
::create($queue_params);
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
;
141 $dao->query(" SELECT $job.mailing_id as mailing_id
144 CRM_Utils_Type
::escape($job_id, 'Integer')
147 $mailing_obj = new CRM_Mailing_BAO_Mailing();
148 $mailing_obj->id
= $dao->mailing_id
;
149 $mailing_obj->find(TRUE);
151 $config = CRM_Core_Config
::singleton();
152 $mailer = \Civi
::service('pear_mail');
156 $message = $mailing_obj->compose($job_id, $queue->id
, $queue->hash
,
157 $queue->contact_id
, $forward_email, $recipient, FALSE, NULL, $attachments, TRUE, $fromEmail
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
;
167 $body = $message->get();
168 $headers = $message->headers();
171 if (is_object($mailer)) {
172 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
173 $result = $mailer->send($recipient, $headers, $body);
178 'event_queue_id' => $queue->id
,
180 'hash' => $queue->hash
,
182 if (is_a($result, 'PEAR_Error')) {
183 // Register the bounce event.
185 $params = array_merge($params,
186 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
188 CRM_Mailing_Event_BAO_Bounce
::create($params);
191 $successfulForward = TRUE;
192 // Register the delivery event.
194 CRM_Mailing_Event_BAO_Delivered
::create($params);
197 $transaction->commit();
199 return $successfulForward;
203 * Get row count for the event selector.
205 * @param int $mailing_id
208 * Optional ID of a job to filter on.
209 * @param bool $is_distinct
210 * Group by queue ID?.
213 * Number of rows in result set
215 public static function getTotalCount(
216 $mailing_id, $job_id = NULL,
219 $dao = new CRM_Core_DAO();
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();
227 SELECT COUNT($forward.id) as forward
230 ON $forward.event_queue_id = $queue.id
232 ON $queue.job_id = $job.id
234 ON $job.mailing_id = $mailing.id
236 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
238 if (!empty($job_id)) {
239 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
243 $query .= " GROUP BY $queue.id ";
250 return $dao->forward
;
257 * Get rows for the event browser.
259 * @param int $mailing_id
262 * Optional ID of the job.
263 * @param bool $is_distinct
264 * Group by queue id?.
267 * @param int $rowCount
275 public static function &getRows(
276 $mailing_id, $job_id = NULL,
277 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
280 $dao = new CRM_Core_DAO();
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();
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
298 ON $queue.contact_id = $contact.id
300 ON $queue.email_id = $email.id
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
310 ON $queue.job_id = $job.id
312 ON $job.mailing_id = $mailing.id
314 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
316 if (!empty($job_id)) {
317 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
321 $query .= " GROUP BY $queue.id, dest_contact.id, dest_email.email, $forward.time_stamp ";
324 $orderBy = "$contact.sort_name ASC, {$forward}.time_stamp DESC";
326 if (is_string($sort)) {
327 $sort = CRM_Utils_Type
::escape($sort, 'String');
331 $orderBy = trim($sort->orderBy());
335 $query .= " ORDER BY {$orderBy} ";
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');
346 while ($dao->fetch()) {
347 $from_url = CRM_Utils_System
::url('civicrm/contact/view',
348 "reset=1&cid={$dao->from_id}"
350 $dest_url = CRM_Utils_System
::url('civicrm/contact/view',
351 "reset=1&cid={$dao->dest_id}"
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
),