3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 class CRM_Mailing_Event_BAO_Forward
extends CRM_Mailing_Event_DAO_Forward
{
40 public function __construct() {
41 parent
::__construct();
45 * Create a new forward event, create a new contact if necessary
50 * @param $forward_email
51 * @param null $fromEmail
52 * @param null $comment
56 public static function &forward($job_id, $queue_id, $hash, $forward_email, $fromEmail = NULL, $comment = NULL) {
57 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
59 $successfulForward = FALSE;
62 return $successfulForward;
65 /* Find the email address/contact, if it exists */
67 $contact = CRM_Contact_BAO_Contact
::getTableName();
68 $location = CRM_Core_BAO_Location
::getTableName();
69 $email = CRM_Core_BAO_Email
::getTableName();
70 $queueTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
71 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
72 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
73 $forward = self
::getTableName();
75 $domain = CRM_Core_BAO_Domain
::getDomain();
77 $dao = new CRM_Core_Dao();
79 SELECT $contact.id as contact_id,
80 $email.id as email_id,
81 $contact.do_not_email as do_not_email,
82 $queueTable.id as queue_id
83 FROM ($email, $job as temp_job)
85 ON $email.contact_id = $contact.id
87 ON $email.id = $queueTable.email_id
89 ON $queueTable.job_id = $job.id
90 AND temp_job.mailing_id = $job.mailing_id
91 WHERE $queueTable.job_id = $job_id
92 AND $email.email = '" .
93 CRM_Utils_Type
::escape($forward_email, 'String') . "'"
98 $transaction = new CRM_Core_Transaction();
100 if (isset($dao->queue_id
) ||
101 (isset($dao->do_not_email
) && $dao->do_not_email
== 1)
103 /* We already sent this mailing to $forward_email, or we should
104 * never email this contact. Give up. */
106 return $successfulForward;
109 require_once 'api/api.php';
110 $contactParams = array(
111 'email' => $forward_email,
114 $contactValues = civicrm_api('contact', 'get', $contactParams);
115 $count = $contactValues['count'];
118 /* If the contact does not exist, create one. */
121 'contact_type' => 'Individual',
124 $locationType = CRM_Core_BAO_LocationType
::getDefault();
126 'email' => $forward_email,
127 'location_type_id' => $locationType->id
,
129 require_once 'CRM/Utils/DeprecatedUtils.php';
130 _civicrm_api3_deprecated_add_formatted_param($value, $formatted);
131 $formatted['onDuplicate'] = CRM_Import_Parser
::DUPLICATE_SKIP
;
132 $formatted['fixAddress'] = TRUE;
133 $contact = civicrm_api('contact', 'create', $formatted);
134 if (civicrm_error($contact)) {
135 return $successfulForward;
137 $contact_id = $contact['id'];
139 $email = new CRM_Core_DAO_Email();
140 $email->email
= $forward_email;
142 $email_id = $email->id
;
144 $contact_id = $email->contact_id
;
147 /* Create a new queue event */
149 $queue_params = array(
150 'email_id' => $email_id,
151 'contact_id' => $contact_id,
155 $queue = CRM_Mailing_Event_BAO_Queue
::create($queue_params);
157 $forward = new CRM_Mailing_Event_BAO_Forward();
158 $forward->time_stamp
= date('YmdHis');
159 $forward->event_queue_id
= $queue_id;
160 $forward->dest_queue_id
= $queue->id
;
164 $dao->query(" SELECT $job.mailing_id as mailing_id
167 CRM_Utils_Type
::escape($job_id, 'Integer')
170 $mailing_obj = new CRM_Mailing_BAO_Mailing();
171 $mailing_obj->id
= $dao->mailing_id
;
172 $mailing_obj->find(TRUE);
174 $config = CRM_Core_Config
::singleton();
175 $mailer = \Civi\Core\Container
::singleton()->get('pear_mail');
179 $message = $mailing_obj->compose($job_id, $queue->id
, $queue->hash
,
180 $queue->contact_id
, $forward_email, $recipient, FALSE, NULL, $attachments, TRUE, $fromEmail
182 //append comment if added while forwarding.
183 if (count($comment)) {
184 $message->_txtbody
= CRM_Utils_Array
::value('body_text', $comment) . $message->_txtbody
;
185 if (!empty($comment['body_html'])) {
186 $message->_htmlbody
= $comment['body_html'] . '<br />---------------Original message---------------------<br />' . $message->_htmlbody
;
190 $body = $message->get();
191 $headers = $message->headers();
194 if (is_object($mailer)) {
195 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
196 $result = $mailer->send($recipient, $headers, $body);
201 'event_queue_id' => $queue->id
,
203 'hash' => $queue->hash
,
205 if (is_a($result, 'PEAR_Error')) {
206 /* Register the bounce event */
208 $params = array_merge($params,
209 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
211 CRM_Mailing_Event_BAO_Bounce
::create($params);
214 $successfulForward = TRUE;
215 /* Register the delivery event */
217 CRM_Mailing_Event_BAO_Delivered
::create($params);
220 $transaction->commit();
222 return $successfulForward;
226 * Get row count for the event selector.
228 * @param int $mailing_id
231 * Optional ID of a job to filter on.
232 * @param bool $is_distinct
233 * Group by queue ID?.
236 * Number of rows in result set
238 public static function getTotalCount(
239 $mailing_id, $job_id = NULL,
242 $dao = new CRM_Core_DAO();
244 $forward = self
::getTableName();
245 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
246 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
247 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
250 SELECT COUNT($forward.id) as forward
253 ON $forward.event_queue_id = $queue.id
255 ON $queue.job_id = $job.id
257 ON $job.mailing_id = $mailing.id
259 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
261 if (!empty($job_id)) {
262 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
266 $query .= " GROUP BY $queue.id ";
273 return $dao->forward
;
280 * Get rows for the event browser.
282 * @param int $mailing_id
285 * Optional ID of the job.
286 * @param bool $is_distinct
287 * Group by queue id?.
290 * @param int $rowCount
298 public static function &getRows(
299 $mailing_id, $job_id = NULL,
300 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
303 $dao = new CRM_Core_Dao();
305 $forward = self
::getTableName();
306 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
307 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
308 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
309 $contact = CRM_Contact_BAO_Contact
::getTableName();
310 $email = CRM_Core_BAO_Email
::getTableName();
313 SELECT $contact.display_name as from_name,
314 $contact.id as from_id,
315 $email.email as from_email,
316 dest_contact.id as dest_id,
317 dest_email.email as dest_email,
318 $forward.time_stamp as date
321 ON $queue.contact_id = $contact.id
323 ON $queue.email_id = $email.id
325 ON $forward.event_queue_id = $queue.id
326 INNER JOIN $queue as dest_queue
327 ON $forward.dest_queue_id = dest_queue.id
328 INNER JOIN $contact as dest_contact
329 ON dest_queue.contact_id = dest_contact.id
330 INNER JOIN $email as dest_email
331 ON dest_queue.email_id = dest_email.id
333 ON $queue.job_id = $job.id
335 ON $job.mailing_id = $mailing.id
337 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
339 if (!empty($job_id)) {
340 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
344 $query .= " GROUP BY $queue.id ";
347 $orderBy = "sort_name ASC, {$forward}.time_stamp DESC";
349 if (is_string($sort)) {
350 $sort = CRM_Utils_Type
::escape($sort, 'String');
354 $orderBy = trim($sort->orderBy());
358 $query .= " ORDER BY {$orderBy} ";
360 if ($offset ||
$rowCount) {
361 //Added "||$rowCount" to avoid displaying all records on first page
362 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
369 while ($dao->fetch()) {
370 $from_url = CRM_Utils_System
::url('civicrm/contact/view',
371 "reset=1&cid={$dao->from_id}"
373 $dest_url = CRM_Utils_System
::url('civicrm/contact/view',
374 "reset=1&cid={$dao->dest_id}"
377 'from_name' => "<a href=\"$from_url\">{$dao->from_name}</a>",
378 'from_email' => $dao->from_email
,
379 'dest_email' => "<a href=\"$dest_url\">{$dao->dest_email}</a>",
380 'date' => CRM_Utils_Date
::customFormat($dao->date
),