3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
33 class CRM_Mailing_Event_BAO_Forward
extends CRM_Mailing_Event_DAO_Forward
{
38 public function __construct() {
39 parent
::__construct();
43 * Create a new forward event, create a new contact if necessary
48 * @param $forward_email
49 * @param null $fromEmail
50 * @param null $comment
54 public static function &forward($job_id, $queue_id, $hash, $forward_email, $fromEmail = NULL, $comment = NULL) {
55 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
57 $successfulForward = FALSE;
60 return $successfulForward;
63 // Find the email address/contact, if it exists.
65 $contact = CRM_Contact_BAO_Contact
::getTableName();
66 $location = CRM_Core_BAO_Location
::getTableName();
67 $email = CRM_Core_BAO_Email
::getTableName();
68 $queueTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
69 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
70 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
71 $forward = self
::getTableName();
73 $domain = CRM_Core_BAO_Domain
::getDomain();
75 $dao = new CRM_Core_Dao();
77 SELECT $contact.id as contact_id,
78 $email.id as email_id,
79 $contact.do_not_email as do_not_email,
80 $queueTable.id as queue_id
81 FROM ($email, $job as temp_job)
83 ON $email.contact_id = $contact.id
85 ON $email.id = $queueTable.email_id
87 ON $queueTable.job_id = $job.id
88 AND temp_job.mailing_id = $job.mailing_id
89 WHERE $queueTable.job_id = $job_id
90 AND $email.email = '" .
91 CRM_Utils_Type
::escape($forward_email, 'String') . "'"
96 $transaction = new CRM_Core_Transaction();
98 if (isset($dao->queue_id
) ||
99 (isset($dao->do_not_email
) && $dao->do_not_email
== 1)
101 // We already sent this mailing to $forward_email, or we should
102 // never email this contact. Give up.
104 return $successfulForward;
107 require_once 'api/api.php';
108 $contactParams = array(
109 'email' => $forward_email,
112 $contactValues = civicrm_api('contact', 'get', $contactParams);
113 $count = $contactValues['count'];
116 // If the contact does not exist, create one.
119 'contact_type' => 'Individual',
122 $locationType = CRM_Core_BAO_LocationType
::getDefault();
124 'email' => $forward_email,
125 'location_type_id' => $locationType->id
,
127 require_once 'CRM/Utils/DeprecatedUtils.php';
128 _civicrm_api3_deprecated_add_formatted_param($value, $formatted);
129 $formatted['onDuplicate'] = CRM_Import_Parser
::DUPLICATE_SKIP
;
130 $formatted['fixAddress'] = TRUE;
131 $contact = civicrm_api('contact', 'create', $formatted);
132 if (civicrm_error($contact)) {
133 return $successfulForward;
135 $contact_id = $contact['id'];
137 $email = new CRM_Core_DAO_Email();
138 $email->email
= $forward_email;
140 $email_id = $email->id
;
142 $contact_id = $email->contact_id
;
145 // Create a new queue event.
147 $queue_params = array(
148 'email_id' => $email_id,
149 'contact_id' => $contact_id,
153 $queue = CRM_Mailing_Event_BAO_Queue
::create($queue_params);
155 $forward = new CRM_Mailing_Event_BAO_Forward();
156 $forward->time_stamp
= date('YmdHis');
157 $forward->event_queue_id
= $queue_id;
158 $forward->dest_queue_id
= $queue->id
;
162 $dao->query(" SELECT $job.mailing_id as mailing_id
165 CRM_Utils_Type
::escape($job_id, 'Integer')
168 $mailing_obj = new CRM_Mailing_BAO_Mailing();
169 $mailing_obj->id
= $dao->mailing_id
;
170 $mailing_obj->find(TRUE);
172 $config = CRM_Core_Config
::singleton();
173 $mailer = \Civi
::service('pear_mail');
177 $message = $mailing_obj->compose($job_id, $queue->id
, $queue->hash
,
178 $queue->contact_id
, $forward_email, $recipient, FALSE, NULL, $attachments, TRUE, $fromEmail
180 //append comment if added while forwarding.
181 if (count($comment)) {
182 $message->_txtbody
= CRM_Utils_Array
::value('body_text', $comment) . $message->_txtbody
;
183 if (!empty($comment['body_html'])) {
184 $message->_htmlbody
= $comment['body_html'] . '<br />---------------Original message---------------------<br />' . $message->_htmlbody
;
188 $body = $message->get();
189 $headers = $message->headers();
192 if (is_object($mailer)) {
193 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
194 $result = $mailer->send($recipient, $headers, $body);
199 'event_queue_id' => $queue->id
,
201 'hash' => $queue->hash
,
203 if (is_a($result, 'PEAR_Error')) {
204 // Register the bounce event.
206 $params = array_merge($params,
207 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
209 CRM_Mailing_Event_BAO_Bounce
::create($params);
212 $successfulForward = TRUE;
213 // Register the delivery event.
215 CRM_Mailing_Event_BAO_Delivered
::create($params);
218 $transaction->commit();
220 return $successfulForward;
224 * Get row count for the event selector.
226 * @param int $mailing_id
229 * Optional ID of a job to filter on.
230 * @param bool $is_distinct
231 * Group by queue ID?.
234 * Number of rows in result set
236 public static function getTotalCount(
237 $mailing_id, $job_id = NULL,
240 $dao = new CRM_Core_DAO();
242 $forward = self
::getTableName();
243 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
244 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
245 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
248 SELECT COUNT($forward.id) as forward
251 ON $forward.event_queue_id = $queue.id
253 ON $queue.job_id = $job.id
255 ON $job.mailing_id = $mailing.id
257 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
259 if (!empty($job_id)) {
260 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
264 $query .= " GROUP BY $queue.id ";
271 return $dao->forward
;
278 * Get rows for the event browser.
280 * @param int $mailing_id
283 * Optional ID of the job.
284 * @param bool $is_distinct
285 * Group by queue id?.
288 * @param int $rowCount
296 public static function &getRows(
297 $mailing_id, $job_id = NULL,
298 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
301 $dao = new CRM_Core_Dao();
303 $forward = self
::getTableName();
304 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
305 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
306 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
307 $contact = CRM_Contact_BAO_Contact
::getTableName();
308 $email = CRM_Core_BAO_Email
::getTableName();
311 SELECT $contact.display_name as from_name,
312 $contact.id as from_id,
313 $email.email as from_email,
314 dest_contact.id as dest_id,
315 dest_email.email as dest_email,
316 $forward.time_stamp as date
319 ON $queue.contact_id = $contact.id
321 ON $queue.email_id = $email.id
323 ON $forward.event_queue_id = $queue.id
324 INNER JOIN $queue as dest_queue
325 ON $forward.dest_queue_id = dest_queue.id
326 INNER JOIN $contact as dest_contact
327 ON dest_queue.contact_id = dest_contact.id
328 INNER JOIN $email as dest_email
329 ON dest_queue.email_id = dest_email.id
331 ON $queue.job_id = $job.id
333 ON $job.mailing_id = $mailing.id
335 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
337 if (!empty($job_id)) {
338 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
342 $query .= " GROUP BY $queue.id, dest_contact.id, dest_email.email, $forward.time_stamp ";
345 $orderBy = "$contact.sort_name ASC, {$forward}.time_stamp DESC";
347 if (is_string($sort)) {
348 $sort = CRM_Utils_Type
::escape($sort, 'String');
352 $orderBy = trim($sort->orderBy());
356 $query .= " ORDER BY {$orderBy} ";
358 if ($offset ||
$rowCount) {
359 //Added "||$rowCount" to avoid displaying all records on first page
360 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
367 while ($dao->fetch()) {
368 $from_url = CRM_Utils_System
::url('civicrm/contact/view',
369 "reset=1&cid={$dao->from_id}"
371 $dest_url = CRM_Utils_System
::url('civicrm/contact/view',
372 "reset=1&cid={$dao->dest_id}"
375 'from_name' => "<a href=\"$from_url\">{$dao->from_name}</a>",
376 'from_email' => $dao->from_email
,
377 'dest_email' => "<a href=\"$dest_url\">{$dao->dest_email}</a>",
378 'date' => CRM_Utils_Date
::customFormat($dao->date
),