3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Mailing_Event_BAO_Forward
extends CRM_Mailing_Event_DAO_Forward
{
40 function __construct() {
41 parent
::__construct();
45 * Create a new forward event, create a new contact if necessary
47 static function &forward($job_id, $queue_id, $hash, $forward_email, $fromEmail = NULL, $comment = NULL) {
48 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
50 $successfulForward = FALSE;
53 return $successfulForward;
56 /* Find the email address/contact, if it exists */
58 $contact = CRM_Contact_BAO_Contact
::getTableName();
59 $location = CRM_Core_BAO_Location
::getTableName();
60 $email = CRM_Core_BAO_Email
::getTableName();
61 $queueTable = CRM_Mailing_Event_BAO_Queue
::getTableName();
62 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
63 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
64 $forward = self
::getTableName();
66 $domain = CRM_Core_BAO_Domain
::getDomain();
68 $dao = new CRM_Core_Dao();
70 SELECT $contact.id as contact_id,
71 $email.id as email_id,
72 $contact.do_not_email as do_not_email,
73 $queueTable.id as queue_id
74 FROM ($email, $job as temp_job)
76 ON $email.contact_id = $contact.id
78 ON $email.id = $queueTable.email_id
80 ON $queueTable.job_id = $job.id
81 AND temp_job.mailing_id = $job.mailing_id
82 WHERE $queueTable.job_id = $job_id
83 AND $email.email = '" .
84 CRM_Utils_Type
::escape($forward_email, 'String') . "'"
89 $transaction = new CRM_Core_Transaction();
91 if (isset($dao->queue_id
) ||
92 (isset($dao->do_not_email
) && $dao->do_not_email
== 1)
94 /* We already sent this mailing to $forward_email, or we should
95 * never email this contact. Give up. */
97 return $successfulForward;
100 require_once 'api/api.php';
101 $contactParams = array(
102 'email' => $forward_email,
105 $contactValues = civicrm_api('contact', 'get', $contactParams);
106 $count = $contactValues['count'];
109 /* If the contact does not exist, create one. */
112 'contact_type' => 'Individual',
115 $locationType = CRM_Core_BAO_LocationType
::getDefault();
117 'email' => $forward_email,
118 'location_type_id' => $locationType->id
,
120 require_once 'CRM/Utils/DeprecatedUtils.php';
121 _civicrm_api3_deprecated_add_formatted_param($value, $formatted);
122 $formatted['onDuplicate'] = CRM_Import_Parser
::DUPLICATE_SKIP
;
123 $formatted['fixAddress'] = TRUE;
124 $contact = civicrm_api('contact', 'create', $formatted);
125 if (civicrm_error($contact)) {
126 return $successfulForward;
128 $contact_id = $contact['id'];
130 $email = new CRM_Core_DAO_Email();
131 $email->email
= $forward_email;
133 $email_id = $email->id
;
135 $contact_id = $email->contact_id
;
138 /* Create a new queue event */
140 $queue_params = array(
141 'email_id' => $email_id,
142 'contact_id' => $contact_id,
146 $queue = CRM_Mailing_Event_BAO_Queue
::create($queue_params);
148 $forward = new CRM_Mailing_Event_BAO_Forward();
149 $forward->time_stamp
= date('YmdHis');
150 $forward->event_queue_id
= $queue_id;
151 $forward->dest_queue_id
= $queue->id
;
155 $dao->query(" SELECT $job.mailing_id as mailing_id
158 CRM_Utils_Type
::escape($job_id, 'Integer')
161 $mailing_obj = new CRM_Mailing_BAO_Mailing();
162 $mailing_obj->id
= $dao->mailing_id
;
163 $mailing_obj->find(TRUE);
165 $config = CRM_Core_Config
::singleton();
166 $mailer = $config->getMailer();
170 $message = $mailing_obj->compose($job_id, $queue->id
, $queue->hash
,
171 $queue->contact_id
, $forward_email, $recipient, FALSE, NULL, $attachments, TRUE, $fromEmail
173 //append comment if added while forwarding.
174 if (count($comment)) {
175 $message->_txtbody
= CRM_Utils_Array
::value('body_text', $comment) . $message->_txtbody
;
176 if (!empty($comment['body_html'])) {
177 $message->_htmlbody
= $comment['body_html'] . '<br />---------------Original message---------------------<br />' . $message->_htmlbody
;
181 $body = $message->get();
182 $headers = $message->headers();
185 if (is_object($mailer)) {
186 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
187 $result = $mailer->send($recipient, $headers, $body);
192 'event_queue_id' => $queue->id
,
194 'hash' => $queue->hash
,
196 if (is_a($result, 'PEAR_Error')) {
197 /* Register the bounce event */
199 $params = array_merge($params,
200 CRM_Mailing_BAO_BouncePattern
::match($result->getMessage())
202 CRM_Mailing_Event_BAO_Bounce
::create($params);
205 $successfulForward = TRUE;
206 /* Register the delivery event */
208 CRM_Mailing_Event_BAO_Delivered
::create($params);
211 $transaction->commit();
213 return $successfulForward;
217 * Get row count for the event selector
219 * @param int $mailing_id ID of the mailing
220 * @param int $job_id Optional ID of a job to filter on
221 * @param boolean $is_distinct Group by queue ID?
223 * @return int Number of rows in result set
227 public static function getTotalCount($mailing_id, $job_id = NULL,
230 $dao = new CRM_Core_DAO();
232 $forward = self
::getTableName();
233 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
234 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
235 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
238 SELECT COUNT($forward.id) as forward
241 ON $forward.event_queue_id = $queue.id
243 ON $queue.job_id = $job.id
245 ON $job.mailing_id = $mailing.id
247 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
249 if (!empty($job_id)) {
250 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
254 $query .= " GROUP BY $queue.id ";
261 return $dao->forward
;
268 * Get rows for the event browser
270 * @param int $mailing_id ID of the mailing
271 * @param int $job_id optional ID of the job
272 * @param boolean $is_distinct Group by queue id?
273 * @param int $offset Offset
274 * @param int $rowCount Number of rows
275 * @param array $sort sort array
277 * @return array Result set
281 public static function &getRows($mailing_id, $job_id = NULL,
282 $is_distinct = FALSE, $offset = NULL, $rowCount = NULL, $sort = NULL
285 $dao = new CRM_Core_Dao();
287 $forward = self
::getTableName();
288 $queue = CRM_Mailing_Event_BAO_Queue
::getTableName();
289 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
290 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
291 $contact = CRM_Contact_BAO_Contact
::getTableName();
292 $email = CRM_Core_BAO_Email
::getTableName();
295 SELECT $contact.display_name as from_name,
296 $contact.id as from_id,
297 $email.email as from_email,
298 dest_contact.id as dest_id,
299 dest_email.email as dest_email,
300 $forward.time_stamp as date
303 ON $queue.contact_id = $contact.id
305 ON $queue.email_id = $email.id
307 ON $forward.event_queue_id = $queue.id
308 INNER JOIN $queue as dest_queue
309 ON $forward.dest_queue_id = dest_queue.id
310 INNER JOIN $contact as dest_contact
311 ON dest_queue.contact_id = dest_contact.id
312 INNER JOIN $email as dest_email
313 ON dest_queue.email_id = dest_email.id
315 ON $queue.job_id = $job.id
317 ON $job.mailing_id = $mailing.id
319 WHERE $mailing.id = " . CRM_Utils_Type
::escape($mailing_id, 'Integer');
321 if (!empty($job_id)) {
322 $query .= " AND $job.id = " . CRM_Utils_Type
::escape($job_id, 'Integer');
326 $query .= " GROUP BY $queue.id ";
329 $orderBy = "sort_name ASC, {$forward}.time_stamp DESC";
331 if (is_string($sort)) {
332 $sort = CRM_Utils_Type
::escape($sort, 'String');
336 $orderBy = trim($sort->orderBy());
340 $query .= " ORDER BY {$orderBy} ";
342 if ($offset ||
$rowCount) {
343 //Added "||$rowCount" to avoid displaying all records on first page
344 $query .= ' LIMIT ' . CRM_Utils_Type
::escape($offset, 'Integer') . ', ' . CRM_Utils_Type
::escape($rowCount, 'Integer');
351 while ($dao->fetch()) {
352 $from_url = CRM_Utils_System
::url('civicrm/contact/view',
353 "reset=1&cid={$dao->from_id}"
355 $dest_url = CRM_Utils_System
::url('civicrm/contact/view',
356 "reset=1&cid={$dao->dest_id}"
359 'from_name' => "<a href=\"$from_url\">{$dao->from_name}</a>",
360 'from_email' => $dao->from_email
,
361 'dest_email' => "<a href=\"$dest_url\">{$dao->dest_email}</a>",
362 'date' => CRM_Utils_Date
::customFormat($dao->date
),